PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.5
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.5
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Http / Controllers / CustomerController.php

CustomerController.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.5.5, at app/Http/Controllers/CustomerController.php

253 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Http\Controllers;
4
5 use FluentSupport\App\Models\Attachment;
6 use FluentSupport\App\Models\Conversation;
7 use FluentSupport\App\Models\Customer;
8 use FluentSupport\App\Models\Ticket;
9 use FluentSupport\App\Services\Helper;
10 use FluentSupport\App\Services\Includes\FileSystem;
11 use FluentSupport\App\Services\ProfileInfoService;
12 use FluentSupport\Framework\Request\Request;
13 use FluentSupport\Framework\Support\Arr;
14
15 /**
16 * CustomerController class for REST API
17 * This class is responsible for getting data for all request related to customer
18 * @package FluentSupport\App\Http\Controllers
19 *
20 * @version 1.0.0
21 */
22 class CustomerController extends Controller
23 {
24 /**
25 * index method will return the list of customers
26 * @param Request $request
27 * @return array
28 */
29 public function index(Request $request)
30 {
31 //Add order by selected by suer
32 $customersQuery = Customer::orderBy('id', 'DESC')
33 ->orderBy($request->get('order_by', 'id'), $request->get('order_type', 'ASC'));
34
35 //Filter query based on the search item
36 if ($request->get('search')) {
37 $customersQuery->searchBy($request->get('search'));
38 }
39
40 $status = $request->get('status');
41 //Filter customer by selected status
42 if ($status && $status != 'all') {
43 $customersQuery->filterByStatues([$status]);
44 }
45
46 $customers = $customersQuery->paginate();
47
48 //Get total ticket and responses in ticket by individual customer
49 foreach ($customers as $customer) {
50 $customer->total_tickets = $customer->getTicketCounts();
51 $customer->total_responses = $customer->getResponseCounts();
52 if ($customer->user_id) {
53 //Get profile link, if they are WP user
54 $customer->user_profile = admin_url('user-edit.php?user_id=' . $customer->user_id);
55 }
56 }
57
58 return [
59 'customers' => $customers,
60 ];
61 }
62
63
64 /**
65 * getCustomer method will return individual customer information by customer id
66 * This function will also get information about extra widgets, tickets and Fluent CRM
67 * @param Request $request
68 * @param $customerId
69 * @return array
70 */
71 public function getCustomer(Request $request, $customerId)
72 {
73 $customer = Customer::findOrFail($customerId);
74
75 $data = [
76 'customer' => $customer
77 ];
78
79 $with = $request->get('with', []);
80
81 if (in_array('widgets', $with)) {
82 $data['widgets'] = ProfileInfoService::getProfileExtraWidgets($customer);
83 }
84
85 if (in_array('tickets', $with)) {
86 $data['tickets'] = Ticket::select(['id', 'title', 'status', 'customer_id', 'created_at'])
87 ->where('customer_id', $customer->id)
88 ->orderBy('id', 'DESC')
89 ->limit(20)
90 ->get();
91 }
92
93 if(in_array('fluentcrm_profile', $with)) {
94 $data['fluentcrm_profile'] = Helper::getFluentCrmContactData($customer);
95 }
96
97 return $data;
98
99 }
100
101 /**
102 * Create method will create new customer
103 * @param Request $request
104 * @return array
105 * @throws \FluentSupport\Framework\Validator\ValidationException
106 */
107 public function create(Request $request)
108 {
109 $data = $request->all();
110 $this->validate($data, [
111 'email' => 'required|email|unique:fs_persons'
112 ]);
113
114 $email = $data['email'];
115
116 $data = Arr::only($data, (new Customer)->getFillable());
117
118 $user = get_user_by('email', $email);
119
120 if ($user) {
121 $data['user_id'] = $user->ID;
122 if (empty($data['first_name'])) {
123 $data['first_name'] = $user->first_name;
124 }
125 if (empty($data['last_name'])) {
126 $data['last_name'] = $user->last_name;
127 }
128 }
129
130 $customer = Customer::create($data);
131
132 return [
133 'message' => __('Customer has been added', 'fluent-support'),
134 'customer' => $customer
135 ];
136 }
137
138 /**
139 * update method will update existing customer by customer id
140 * @param Request $request
141 * @param $customerId
142 * @return array
143 * @throws \FluentSupport\Framework\Validator\ValidationException
144 */
145 public function update(Request $request, $customerId)
146 {
147 $customer = Customer::findOrFail($customerId);
148 $data = $request->all();
149 $this->validate($data, [
150 'email' => 'required|email',
151 'first_name' => 'required'
152 ]);
153
154 if ($otherCustomer = Customer::where('id', '!=', $customerId)->where('email', $data['email'])->first()) {
155 return $this->sendError([
156 'message' => __('Another Customer has same email address', 'fluent-support'),
157 'errors' => [
158 'email' => [
159 'unique' => __('Email address has been assigned to other customer', 'fluent-support')
160 ]
161 ]
162 ], 423);
163 }
164
165 $validKeys = (new Customer)->getFillable();
166 unset($validKeys['hash']);
167 unset($validKeys['user_id']);
168
169 $updateData = Arr::only($data, $validKeys);
170
171 $user = get_user_by('email', $data['email']);
172
173 if ($user) {
174 $updateData['user_id'] = $user->ID;
175 }
176
177 Customer::where('id', $customer->id)
178 ->update($updateData);
179
180 return [
181 'message' => __('Customer has been updated', 'fluent-support'),
182 'customer' => Customer::findOrFail($customerId)
183 ];
184 }
185
186 /**
187 * delete method will delete a customer and all ticket by that customer
188 * @param Request $request
189 * @param $customerId
190 * @return array
191 */
192 public function delete(Request $request, $customerId)
193 {
194 $customer = Customer::findOrFail($customerId);
195
196 $tickets = Ticket::where('customer_id', $customer->id)->get();
197
198 foreach ($tickets as $ticket) {
199 $ticket->deleteTicket();
200 }
201
202 $customer->delete();
203
204 return [
205 'message' => __('Customer Deleted Successfully', 'fluent-support')
206 ];
207 }
208
209 /**
210 * addOrUpdateProfileImage method will update a customer avatar
211 * @param Request $request
212 * @return array
213 */
214 public function addOrUpdateProfileImage(Request $request)
215 {
216 $allowExtension = [
217 'jpeg', 'jpe', 'jpg', 'png'
218 ];
219
220 $customer_id = $request->get('customer_id');
221 $file = $request->files();
222
223 $ext = $file['file']->getClientOriginalExtension();
224
225 if(!in_array($ext, $allowExtension)){
226 return $this->sendError([
227 'message' => __('Unsupported file submitted, please select an image file', 'fluent-support')
228 ]);
229 }
230
231 $customer = Customer::findOrFail($customer_id);
232
233 $uploadedImage = FileSystem::setSubDir('customer_avatars')->put($file);
234
235 if($avatar = $uploadedImage[0]['url']){
236 $customer->avatar = $avatar;
237 $customer->save();
238
239 return[
240 'message' => __('Profile picture has been updated successfully', 'fluent-support'),
241 'image' => $customer->avatar,
242 'customer' => $customer
243 ];
244 }
245
246 else{
247 return $this->sendError([
248 'message' => __('Something went wrong while updating the profile picture', 'fluent-support')
249 ]);
250 }
251 }
252 }
253