Class: OpportunitiesController

Inherits:
EntitiesController show all
Defined in:
app/controllers/entities/opportunities_controller.rb

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php


Instance Method Summary collapse

Methods inherited from EntitiesController

#attach, #contacts, #discard, #field_group, #leads, #opportunities, #subscribe, #unsubscribe, #versions

Methods inherited from ApplicationController

#auto_complete

Instance Method Details

#createObject

POST /opportunities




67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/entities/opportunities_controller.rb', line 67

def create
  @comment_body = params[:comment_body]
  respond_with(@opportunity) do |_format|
    if @opportunity.(params.permit!)
      @opportunity.add_comment_by_user(@comment_body, current_user)
      if called_from_index_page?
        @opportunities = get_opportunities
        get_data_for_sidebar
      elsif called_from_landing_page?(:accounts)
        get_data_for_sidebar(:account)
      elsif called_from_landing_page?(:campaigns)
        get_data_for_sidebar(:campaign)
      end
    else
      @accounts = Account.my(current_user).order('name')
      @account = (params[:account][:id], request.referer, current_user)
      @contact = Contact.find(params[:contact]) unless params[:contact].blank?
      @campaign = Campaign.find(params[:campaign]) unless params[:campaign].blank?
    end
  end
end

#destroyObject

DELETE /opportunities/1




114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/entities/opportunities_controller.rb', line 114

def destroy
  if called_from_landing_page?(:accounts)
    @account = @opportunity.   # Reload related account if any.
  elsif called_from_landing_page?(:campaigns)
    @campaign = @opportunity.campaign # Reload related campaign if any.
  end
  @opportunity.destroy

  respond_with(@opportunity) do |format|
    format.html { respond_to_destroy(:html) }
    format.js   { respond_to_destroy(:ajax) }
  end
end

#editObject

GET /opportunities/1/edit AJAX




56
57
58
59
60
61
62
63
# File 'app/controllers/entities/opportunities_controller.rb', line 56

def edit
  @account  = @opportunity. || Account.new(user: current_user)
  @accounts = Account.my(current_user).order('name')

  @previous = Opportunity.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/

  respond_with(@opportunity)
end

#filterObject

POST /opportunities/filter AJAX




153
154
155
156
157
158
# File 'app/controllers/entities/opportunities_controller.rb', line 153

def filter
  @opportunities = get_opportunities(page: 1, per_page: per_page_param)
  respond_with(@opportunities) do |format|
    format.js { render :index }
  end
end

#indexObject

GET /opportunities




15
16
17
18
19
20
21
22
# File 'app/controllers/entities/opportunities_controller.rb', line 15

def index
  @opportunities = get_opportunities(page: page_param, per_page: per_page_param)

  respond_with @opportunities do |format|
    format.xls { render layout: 'header' }
    format.csv { render csv: @opportunities }
  end
end

#newObject

GET /opportunities/new




35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/entities/opportunities_controller.rb', line 35

def new
  @opportunity.attributes = { user: current_user, stage: Opportunity.default_stage, access: Setting.default_access, assigned_to: nil }
  @account = Account.new(user: current_user, access: Setting.default_access)
  @accounts = Account.my(current_user).order('name')

  if params[:related]
    model, id = params[:related].split('_')
    if related = model.classify.constantize.my(current_user).find_by_id(id)
      instance_variable_set("@#{model}", related)
      @account = related. if related.respond_to?(:account) && !related..nil?
      @campaign = related.campaign if related.respond_to?(:campaign)
    else
      respond_to_related_not_found(model) && return
    end
  end

  respond_with(@opportunity)
end

#redrawObject

GET /opportunities/redraw AJAX




142
143
144
145
146
147
148
149
# File 'app/controllers/entities/opportunities_controller.rb', line 142

def redraw
  @opportunities = get_opportunities(page: 1, per_page: per_page_param)
  set_options # Refresh options

  respond_with(@opportunities) do |format|
    format.js { render :index }
  end
end

#showObject

GET /opportunities/1 AJAX /opportunities/1




27
28
29
30
31
# File 'app/controllers/entities/opportunities_controller.rb', line 27

def show
  @comment = Comment.new
  @timeline = timeline(@opportunity)
  respond_with(@opportunity)
end

#updateObject

PUT /opportunities/1




91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/entities/opportunities_controller.rb', line 91

def update
  respond_with(@opportunity) do |_format|
    if @opportunity.(params.permit!)
      if called_from_index_page?
        get_data_for_sidebar
      elsif called_from_landing_page?(:accounts)
        get_data_for_sidebar(:account)
      elsif called_from_landing_page?(:campaigns)
        get_data_for_sidebar(:campaign)
      end
    else
      @accounts = Account.my(current_user).order('name')
      @account = if @opportunity.
                   Account.find(@opportunity..id)
                 else
                   Account.new(user: current_user)
                 end
    end
  end
end