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




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

def convert
  @account = Account.new(user: current_user, name: @lead.company, access: "Lead")
  @accounts = Account.my(current_user).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(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i
  end

  respond_with(@lead)
end

#createObject

POST /leads




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

def create
  get_campaigns
  @comment_body = params[:comment_body]

  respond_with(@lead) do |_format|
    if @lead.save_with_permissions(params.permit!)
      @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




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

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




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

def edit
  get_campaigns

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

  respond_with(@lead)
end

#filterObject

POST /leads/filter AJAX




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

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

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

#indexObject

GET /leads




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

def index
  @leads = get_leads(page: page_param)

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

#newObject

GET /leads/new




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

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(current_user).find_by_id(id)
      instance_variable_set("@#{model}", related)
    else
      respond_to_related_not_found(model) && return
    end
  end

  respond_with(@lead)
end

#promoteObject

PUT /leads/1/promote




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

def promote
  @account, @opportunity, @contact = @lead.promote(params.permit!)
  @accounts = Account.my(current_user).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

GET /leads/redraw AJAX




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

def redraw
  current_user.pref[:leads_per_page] = per_page_param if per_page_param

  # 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: per_page_param) # 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




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

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




26
27
28
29
30
# File 'app/controllers/entities/leads_controller.rb', line 26

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

#updateObject

PUT /leads/1




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

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