Class: LeadsController

Inherits:
BaseController show all
Defined in:
app/controllers/leads_controller.rb

Overview

Fat Free CRM Copyright © 2008-2011 by Michael Dvorkin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.


Instance Method Summary collapse

Methods inherited from BaseController

#attach, #auto_complete, #discard, #field_group, #tagged, #timeline

Methods inherited from ApplicationController

#klass

Instance Method Details

#convertObject

GET /leads/1/convert




130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/leads_controller.rb', line 130

def convert
  @lead = Lead.my.find(params[:id])
  @users = User.except(@current_user)
  @account = Account.new(:user => @current_user, :name => @lead.company, :access => "Lead")
  @accounts = Account.my.order("name")
  @opportunity = Opportunity.new(:user => @current_user, :access => "Lead", :stage => "prospecting", :campaign => @lead.campaign, :source => @lead.source)
  if params[:previous].to_s =~ /(\d+)\z/
    @previous = Lead.my.find($1)
  end
  respond_with(@lead)

rescue ActiveRecord::RecordNotFound
  @previous ||= $1.to_i
  respond_to_not_found(:js, :json, :xml) unless @lead
end

#createObject

POST /leads




78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/leads_controller.rb', line 78

def create
  @lead = Lead.new(params[:lead])
  @users = User.except(@current_user)
  @campaigns = Campaign.my.order("name")

  respond_with(@lead) do |format|
    if @lead.save_with_permissions(params)
      if called_from_index_page?
        @leads = get_leads
        get_data_for_sidebar
      else
        get_data_for_sidebar(:campaign)
      end
    end
  end
end

#destroyObject

DELETE /leads/1




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

def destroy
  @lead = Lead.my.find(params[:id])
  @lead.destroy if @lead

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

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :js, :json, :xml)
end

#editObject

GET /leads/1/edit AJAX




62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/leads_controller.rb', line 62

def edit
  @lead = Lead.my.find(params[:id])
  @users = User.except(@current_user)
  @campaigns = Campaign.my.order("name")
  if params[:previous].to_s =~ /(\d+)\z/
    @previous = Lead.my.find($1)
  end
  respond_with(@lead)

rescue ActiveRecord::RecordNotFound
  @previous ||= $1.to_i
  respond_to_not_found(:js) unless @lead
end

#filterObject

POST /leads/filter AJAX




231
232
233
234
235
# File 'app/controllers/leads_controller.rb', line 231

def filter
  session[:filter_by_lead_status] = params[:status]
  @leads = get_leads(:page => 1) # Start one the first page.
  render :index
end

#indexObject

GET /leads




23
24
25
26
# File 'app/controllers/leads_controller.rb', line 23

def index
  @leads = get_leads(:page => params[:page])
  respond_with(@leads)
end

#newObject

GET /leads/new




46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/leads_controller.rb', line 46

def new
  @lead = Lead.new(:user => @current_user, :access => Setting.default_access)
  @users = User.except(@current_user)
  @campaigns = Campaign.my.order("name")
  if params[:related]
    model, id = params[:related].split("_")
    instance_variable_set("@#{model}", model.classify.constantize.my.find(id))
  end
  respond_with(@lead)

rescue ActiveRecord::RecordNotFound # Kicks in if related asset was not found.
  respond_to_related_not_found(model, :js) if model
end

#optionsObject

GET /leads/options AJAX




198
199
200
201
202
203
204
205
# File 'app/controllers/leads_controller.rb', line 198

def options
  unless params[:cancel].true?
    @per_page = @current_user.pref[:leads_per_page] || Lead.per_page
    @outline  = @current_user.pref[:leads_outline]  || Lead.outline
    @sort_by  = @current_user.pref[:leads_sort_by]  || Lead.sort_by
    @naming   = @current_user.pref[:leads_naming]   || Lead.first_name_position
  end
end

#promoteObject

PUT /leads/1/promote




148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/leads_controller.rb', line 148

def promote
  @lead = Lead.my.find(params[:id])
  @users = User.except(@current_user)
  @account, @opportunity, @contact = @lead.promote(params)
  @accounts = Account.my.order("name")
  @stage = Setting.unroll(:opportunity_stage)

  respond_with(@lead) do |format|
    if @account.errors.empty? && @opportunity.errors.empty? && @contact.errors.empty?
      @lead.convert
      update_sidebar
    else
      format.json { render :json => @account.errors + @opportunity.errors + @contact.errors, :status => :unprocessable_entity }
      format.xml  { render :xml => @account.errors + @opportunity.errors + @contact.errors, :status => :unprocessable_entity }
    end
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:js, :json, :xml)
end

#redrawObject

POST /leads/redraw AJAX




209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/controllers/leads_controller.rb', line 209

def redraw
  @current_user.pref[:leads_per_page] = params[:per_page] if params[:per_page]
  @current_user.pref[:leads_outline]  = params[:outline]  if params[:outline]

  # Sorting and naming only: set the same option for Contacts if the hasn't been set yet.
  if params[:sort_by]
    @current_user.pref[:leads_sort_by] = Lead::sort_by_map[params[:sort_by]]
    if Contact::sort_by_fields.include?(params[:sort_by])
      @current_user.pref[:contacts_sort_by] ||= Contact::sort_by_map[params[:sort_by]]
    end
  end
  if params[:naming]
    @current_user.pref[:leads_naming] = params[:naming]
    @current_user.pref[:contacts_naming] ||= params[:naming]
  end

  @leads = get_leads(:page => 1) # Start one the first page.
  render :index
end

#rejectObject

PUT /leads/1/reject




171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/leads_controller.rb', line 171

def reject
  @lead = Lead.my.find(params[:id])
  @lead.reject if @lead
  update_sidebar

  respond_with(@lead) do |format|
    format.html { flash[:notice] = t(:msg_asset_rejected, @lead.full_name); redirect_to leads_path }
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :js, :json, :xml)
end

#showObject

GET /leads/1




30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/leads_controller.rb', line 30

def show
  @lead = Lead.my.find(params[:id])

  respond_with(@lead) do |format|
    format.html do
      @comment = Comment.new
      @timeline = timeline(@lead)
    end
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :json, :xml)
end

#updateObject

PUT /leads/1




97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/leads_controller.rb', line 97

def update
  @lead = Lead.my.find(params[:id])

  respond_with(@lead) do |format|
    if @lead.update_with_permissions(params[:lead], params[:users])
      update_sidebar
    else
      @users = User.except(@current_user)
      @campaigns = Campaign.my.order("name")
    end
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:js, :json, :xml)
end