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




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

def create
  @comment_body = params[:comment_body]
  respond_with(@opportunity) do |format|
    if @opportunity.(params)
      @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.order('name')
      unless params[:account][:id].blank?
        @account = Account.find(params[:account][:id])
      else
        if request.referer =~ /\/accounts\/(.+)$/
          @account = Account.find($1) # related account
        else
          @account = Account.new(:user => current_user)
        end
      end
      @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




121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/controllers/entities/opportunities_controller.rb', line 121

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




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

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

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

  respond_with(@opportunity)
end

#filterObject

POST /opportunities/filter AJAX




160
161
162
163
164
165
# File 'app/controllers/entities/opportunities_controller.rb', line 160

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

#indexObject

GET /opportunities




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

def index
  @opportunities = get_opportunities(:page => params[:page], :per_page => params[:per_page])

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

#newObject

GET /opportunities/new




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

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.order('name')

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

  respond_with(@opportunity)
end

#redrawObject

POST /opportunities/redraw AJAX




149
150
151
152
153
154
155
156
# File 'app/controllers/entities/opportunities_controller.rb', line 149

def redraw
  @opportunities = get_opportunities(:page => 1, :per_page => params[:per_page])
  set_options # Refresh options
  
  respond_with(@opportunities) do |format|
    format.js { render :index }
  end
end

#showObject

GET /opportunities/1 AJAX /opportunities/1




25
26
27
28
29
# File 'app/controllers/entities/opportunities_controller.rb', line 25

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

#updateObject

PUT /opportunities/1




98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/entities/opportunities_controller.rb', line 98

def update
  respond_with(@opportunity) do |format|
    if @opportunity.(params)
      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.order('name')
      if @opportunity.
        @account = Account.find(@opportunity..id)
      else
        @account = Account.new(:user => current_user)
      end
    end
  end
end