Class: LeadsController

Inherits:
EntitiesController show all
Defined in:
app/controllers/entities/leads_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

#convertObject

GET /leads/1/convert




106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/entities/leads_controller.rb', line 106

def convert
  @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_by_id($1) || $1.to_i
  end

  respond_with(@lead)
end

#createObject

POST /leads




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

def create
  get_campaigns
  @comment_body = params[:comment_body]

  respond_with(@lead) do |format|
    if @lead.save_with_permissions(params)
      @lead.add_comment_by_user(@comment_body, current_user)
      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




95
96
97
98
99
100
101
102
# File 'app/controllers/entities/leads_controller.rb', line 95

def destroy
  @lead.destroy

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

#editObject

GET /leads/1/edit AJAX




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

def edit
  get_campaigns

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

  respond_with(@lead)
end

#filterObject

POST /leads/filter AJAX




187
188
189
190
191
192
193
194
# File 'app/controllers/entities/leads_controller.rb', line 187

def filter
  session[:leads_filter] = params[:status]
  @leads = get_leads(:page => 1, :per_page => params[:per_page]) # Start one the first page.

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

#indexObject

GET /leads




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

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

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

#newObject

GET /leads/new




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

def new
  @lead.attributes = {:user => current_user, :access => Setting.default_access, :assigned_to => nil}
  get_campaigns

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

  respond_with(@lead)
end

#promoteObject

PUT /leads/1/promote




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

def promote
  @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
end

#redrawObject

POST /leads/redraw AJAX




162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/entities/leads_controller.rb', line 162

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

  # 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, :per_page => params[:per_page]) # Start one the first page.
  set_options # Refresh options

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

#rejectObject

PUT /leads/1/reject




138
139
140
141
142
143
144
145
# File 'app/controllers/entities/leads_controller.rb', line 138

def reject
  @lead.reject
  update_sidebar

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

#showObject

GET /leads/1 AJAX /leads/1




24
25
26
27
28
# File 'app/controllers/entities/leads_controller.rb', line 24

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

#updateObject

PUT /leads/1




81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/entities/leads_controller.rb', line 81

def update
  respond_with(@lead) do |format|
    # Must set access before user_ids, because user_ids= method depends on access value.
    @lead.access = params[:lead][:access] if params[:lead][:access]
    if @lead.update_with_lead_counters(params[:lead])
      update_sidebar
    else
      @campaigns = Campaign.my.order('name')
    end
  end
end