Class: LeadsController

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

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

Methods inherited from ApplicationController

#auto_complete

Instance Method Details

#convertObject

GET /leads/1/convert




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

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

  respond_with(@lead)
end

#createObject

POST /leads




73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/entities/leads_controller.rb', line 73

def create
  @users = User.except(@current_user)
  get_campaigns

  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




104
105
106
107
108
109
110
111
# File 'app/controllers/entities/leads_controller.rb', line 104

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




60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/entities/leads_controller.rb', line 60

def edit
  @users = User.except(@current_user)
  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




205
206
207
208
209
# File 'app/controllers/entities/leads_controller.rb', line 205

def filter
  session[:leads_filter] = 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/entities/leads_controller.rb', line 23

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

#newObject

GET /leads/new




41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/entities/leads_controller.rb', line 41

def new
  @lead.attributes = {:user => @current_user, :access => Setting.default_access}
  @users = User.except(@current_user)
  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

#optionsObject

GET /leads/options AJAX




172
173
174
175
176
177
178
179
# File 'app/controllers/entities/leads_controller.rb', line 172

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




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

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

#redrawObject

POST /leads/redraw AJAX




183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/entities/leads_controller.rb', line 183

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




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

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




30
31
32
33
34
35
36
37
# File 'app/controllers/entities/leads_controller.rb', line 30

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

#updateObject

PUT /leads/1




91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/entities/leads_controller.rb', line 91

def update
  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
end