Class: ContactsController

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

#createObject

POST /contacts




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

def create
  @contact = Contact.new(params[:contact])

  respond_with(@contact) do |format|
    if @contact.(params)
      @contacts = get_contacts if called_from_index_page?
    else
      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
      @opportunity = Opportunity.find(params[:opportunity]) unless params[:opportunity].blank?
    end
  end
end

#destroyObject

DELETE /contacts/1




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

def destroy
  @contact = Contact.my.find(params[:id])
  @contact.destroy if @contact

  respond_with(@contact) 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 /contacts/1/edit AJAX




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

def edit
  @contact  = Contact.my.find(params[:id])
  @account  = @contact. || Account.new(:user => current_user)
  if params[:previous].to_s =~ /(\d+)\z/
    @previous = Contact.my.find($1)
  end
  respond_with(@contact)

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

#indexObject

GET /contacts




24
25
26
27
# File 'app/controllers/contacts_controller.rb', line 24

def index
  @contacts = get_contacts(:page => params[:page])
  respond_with(@contacts)
end

#newObject

GET /contacts/new




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

def new
  @contact  = Contact.new(:user => current_user, :access => Setting.default_access)
  @account  = Account.new(:user => current_user)
  if params[:related]
    model, id = params[:related].split("_")
    instance_variable_set("@#{model}", model.classify.constantize.my.find(id))
  end
  respond_with(@contact)

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

#opportunitiesObject

GET /contacts/opportunities AJAX




159
160
161
# File 'app/controllers/contacts_controller.rb', line 159

def opportunities
  @contact = Contact.my.find(params[:id])
end

#optionsObject

GET /contacts/options AJAX




148
149
150
151
152
153
154
155
# File 'app/controllers/contacts_controller.rb', line 148

def options
  unless params[:cancel].true?
    @per_page = current_user.pref[:contacts_per_page] || Contact.per_page
    @outline  = current_user.pref[:contacts_outline]  || Contact.outline
    @sort_by  = current_user.pref[:contacts_sort_by]  || Contact.sort_by
    @naming   = current_user.pref[:contacts_naming]   || Contact.first_name_position
  end
end

#redrawObject

POST /contacts/redraw AJAX




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

def redraw
  current_user.pref[:contacts_per_page] = params[:per_page] if params[:per_page]
  current_user.pref[:contacts_outline]  = params[:outline]  if params[:outline]

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

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

#showObject

GET /contacts/1




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

def show
  @contact = Contact.my.find(params[:id])

  respond_with(@contact) do |format|
    format.html do
      @stage = Setting.unroll(:opportunity_stage)
      @comment = Comment.new
      @timeline = timeline(@contact)
    end
  end

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

#updateObject

PUT /contacts/1




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

def update
  @contact = Contact.my.find(params[:id])

  respond_with(@contact) do |format|
    unless @contact.(params)
      @users = User.except(current_user)
      if @contact.
        @account = Account.find(@contact..id)
      else
        @account = Account.new(:user => current_user)
      end
    end
  end

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