-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
contacts_controller.rb
178 lines (154 loc) · 5.93 KB
/
contacts_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class ContactsController < EntitiesController
before_action :get_accounts, only: [:new, :create, :edit, :update]
# GET /contacts
#----------------------------------------------------------------------------
def index
@contacts = get_contacts(page: params[:page], per_page: params[:per_page])
respond_with @contacts do |format|
format.xls { render layout: 'header' }
format.csv { render csv: @contacts }
end
end
# GET /contacts/1
# AJAX /contacts/1
#----------------------------------------------------------------------------
def show
@stage = Setting.unroll(:opportunity_stage)
@comment = Comment.new
@timeline = timeline(@contact)
respond_with(@contact)
end
# GET /contacts/new
#----------------------------------------------------------------------------
def new
@contact.attributes = { user: current_user, access: Setting.default_access, assigned_to: nil }
@account = Account.new(user: current_user)
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) && return
end
end
respond_with(@contact)
end
# GET /contacts/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@account = @contact.account || Account.new(user: current_user)
if params[:previous].to_s =~ /(\d+)\z/
@previous = Contact.my.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i
end
respond_with(@contact)
end
# POST /contacts
#----------------------------------------------------------------------------
def create
@comment_body = params[:comment_body]
respond_with(@contact) do |_format|
if @contact.save_with_account_and_permissions(params.permit!)
@contact.add_comment_by_user(@comment_body, current_user)
@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\/(\d+)\z/
@account = Account.find(Regexp.last_match[1]) # related account
else
@account = Account.new(user: current_user)
end
end
@opportunity = Opportunity.my.find(params[:opportunity]) unless params[:opportunity].blank?
end
end
end
# PUT /contacts/1
#----------------------------------------------------------------------------
def update
respond_with(@contact) do |_format|
unless @contact.update_with_account_and_permissions(params.permit!)
if @contact.account
@account = @contact.account
else
@account = Account.new(user: current_user)
end
end
end
end
# DELETE /contacts/1
#----------------------------------------------------------------------------
def destroy
@contact.destroy
respond_with(@contact) do |format|
format.html { respond_to_destroy(:html) }
format.js { respond_to_destroy(:ajax) }
end
end
# PUT /contacts/1/attach
#----------------------------------------------------------------------------
# Handled by EntitiesController :attach
# POST /contacts/1/discard
#----------------------------------------------------------------------------
# Handled by EntitiesController :discard
# POST /contacts/auto_complete/query AJAX
#----------------------------------------------------------------------------
# Handled by ApplicationController :auto_complete
# GET /contacts/redraw AJAX
#----------------------------------------------------------------------------
def redraw
current_user.pref[:contacts_per_page] = params[:per_page] if params[:per_page]
# 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, per_page: params[:per_page]) # Start on the first page.
set_options # Refresh options
respond_with(@contacts) do |format|
format.js { render :index }
end
end
private
#----------------------------------------------------------------------------
alias_method :get_contacts, :get_list_of_records
#----------------------------------------------------------------------------
def get_accounts
@accounts = Account.my.order('name')
end
def set_options
super
@naming = (current_user.pref[:contacts_naming] || Contact.first_name_position) unless params[:cancel].true?
end
#----------------------------------------------------------------------------
def respond_to_destroy(method)
if method == :ajax
if called_from_index_page?
@contacts = get_contacts
if @contacts.blank?
@contacts = get_contacts(page: current_page - 1) if current_page > 1
render(:index) && return
end
else
self.current_page = 1
end
# At this point render destroy.js
else
self.current_page = 1
flash[:notice] = t(:msg_asset_deleted, @contact.full_name)
redirect_to contacts_path
end
end
end