PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.3
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.3
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.10.3, at app/Http/Controllers/CustomerController.php

251 lines 7.7 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 FluentCrm\App\Models\Subscriber;
6 use FluentSupport\App\Models\Customer;
7 use FluentSupport\Framework\Request\Request;
8 use FluentSupport\App\Services\AvatarUploder;
9
10 /**
11 * CustomerController class for REST API
12 * This class is responsible for getting data for all request related to customer
13 * @package FluentSupport\App\Http\Controllers
14 *
15 * @version 1.0.0
16 */
17 class CustomerController extends Controller
18 {
19 /**
20 * index method will return the list of customers
21 * @param Request $request
22 * @param Customer $customer
23 * @return array
24 */
25 public function index(Request $request, Customer $customer)
26 {
27 return [
28 'customers' => $customer->getCustomers($request->getSafe('search', 'sanitize_text_field'), $request->getSafe('status', 'sanitize_text_field')),
29 ];
30 }
31
32 public function customerField (Request $request,Customer $customer, $customer_id) {
33
34 $userID = intval($request->get('user_id'));
35 return[
36 'customerField' => $customer->getCustomerField($customer_id,$userID)
37 ];
38 }
39
40
41 /**
42 * getCustomer method will return individual customer information by customer id
43 * This function will also get information about extra widgets, tickets and Fluent CRM
44 * @param Request $request
45 * @param Customer $customer
46 * @param $customer_id
47 * @return array
48 */
49 public function getCustomer(Request $request, Customer $customer, $customer_id)
50 {
51 return $customer->getCustomer($customer_id, $request->getSafe('with',null,[]));
52 }
53
54 /**
55 * Create method will create new customer
56 * @param Request $request
57 * @param Customer $customer
58 * @return array
59 * @throws \FluentSupport\Framework\Validator\ValidationException
60 */
61 public function create(Request $request, Customer $customer)
62 {
63 $this->validate($request->get(), [
64 'email' => 'required|email|unique:fs_persons'
65 ]);
66
67 return [
68 'message' => __('Customer has been added', 'fluent-support'),
69 'customer' => $customer->createCustomer($request->get())
70 ];
71 }
72
73 /**
74 * update method will update existing customer by customer id
75 * @param Request $request
76 * @param Customer $customer
77 * @param $customerId
78 * @return array
79 * @throws \FluentSupport\Framework\Validator\ValidationException
80 */
81 public function update(Request $request, Customer $customer, $customer_id)
82 {
83 $data = $this->validate($request->get(), [
84 'email' => 'required|email',
85 'first_name' => 'required'
86 ]);
87
88 try {
89 return [
90 'message' => __('Customer has been updated', 'fluent-support'),
91 'customer' => $customer->updateCustomer($customer_id, $data)
92 ];
93 } catch (\Exception $e) {
94 return $this->sendError([
95 'message' => $e->getMessage(),
96 'errors' => [
97 'email' => [
98 'unique' => __('Email address has been assigned to other customer', 'fluent-support'),
99 ]
100 ]
101 ], 423);
102 }
103 }
104
105 /**
106 * delete method will delete a customer and all tickets by that customer
107 * @param Request $request
108 * @param Customer $customer
109 * @param int $customerId
110 * @return array
111 */
112 public function delete(Request $request, Customer $customer, $customer_id)
113 {
114 return $customer->deleteCustomer($customer_id);
115 }
116
117 /**
118 * addOrUpdateProfileImage method will update a customer avatar
119 * For a successful upload it's required to send file object, customer id and the user type(customer)
120 * @param Request $request
121 * @return array
122 */
123 public function addOrUpdateProfileImage(Request $request, AvatarUploder $avatarUploder)
124 {
125 try {
126 return $avatarUploder->addOrUpdateProfileImage($request->files(), $request->getSafe('customer_id', 'intval'), 'customer');
127 } catch (\Exception $e) {
128 return $this->sendError([
129 'message' => $e->getMessage(),
130 ],
131 $e->getCode()
132 );
133 }
134 }
135
136 /**
137 * resetAvatar method will restore a customer avatar
138 * For a successful upload it's required to send file object, customer id and the user type(customer)
139 * @param Request $request
140 * @param $id
141 * @return array
142 */
143 public function resetAvatar(Customer $customer, $customer_id)
144 {
145 try {
146 $customer->restoreAvatar($customer, $customer_id);
147
148 return [
149 'message' => __('Customer avatar reset to gravatar default', 'fluent-support'),
150 ];
151 } catch (\Exception $e) {
152 return [
153 'message' => $e->getMessage()
154 ];
155 }
156 }
157
158 public function searchContact(Request $request)
159 {
160 $search = $request->getSafe('search', 'sanitize_text_field');
161 if (!$search) {
162 return $this->sendError([
163 'message' => 'Please provide search string'
164 ]);
165 }
166
167 $isEmail = is_email($search);
168
169 // search the existing customers first
170 if ($isEmail) {
171 $customers = Customer::select(['first_name', 'last_name', 'email', 'id', 'user_id'])
172 ->where('email', $search)
173 ->get();
174 } else {
175 $customers = Customer::select(['first_name', 'last_name', 'email', 'id', 'user_id'])
176 ->searchBy($search)
177 ->limit(10)
178 ->get();
179 }
180
181 if (!$customers->isEmpty()) {
182 return [
183 'type' => 'search_result',
184 'provider' => 'fluent_support',
185 'data' => $customers,
186 'is_email' => $isEmail,
187 'search' => $search
188 ];
189 }
190
191 // If FluentCRM exist then let's search for
192 if (defined('FLUENTCRM')) {
193
194 if ($isEmail) {
195 $contacts = \FluentCrm\App\Models\Subscriber::where('email', $search)
196 ->select(['first_name', 'last_name', 'email', 'id', 'user_id'])
197 ->get();
198 } else {
199
200 $contacts = \FluentCrm\App\Models\Subscriber::searchBy($search)
201 ->select(['first_name', 'last_name', 'email', 'id', 'user_id'])
202 ->limit(10)
203 ->get();
204 }
205
206 if (!$contacts->isEmpty()) {
207 return [
208 'type' => 'search_result',
209 'provider' => 'fluent_crm',
210 'data' => $contacts,
211 'is_email' => $isEmail
212 ];
213 }
214 }
215
216 // let's search from user's database
217 $user_query = new \WP_User_Query(array('search' => $search, 'number' => 10));
218
219 $users = $user_query->get_results();
220
221 if ($users) {
222 $formattedUsers = [];
223
224 foreach ($users as $user) {
225 $formattedUsers[] = [
226 'id' => $user->ID,
227 'first_name' => $user->first_name,
228 'last_name' => $user->last_name,
229 'user_id' => $user->ID,
230 'email' => $user->user_email
231 ];
232 }
233
234 return [
235 'type' => 'search_result',
236 'provider' => 'wp_users',
237 'data' => $formattedUsers,
238 'is_email' => $isEmail
239 ];
240 }
241
242 return [
243 'type' => 'none',
244 'provider' => 'none',
245 'data' => [],
246 'is_email' => $isEmail
247 ];
248
249 }
250 }
251