PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.10.1
Fluent Support – Helpdesk & Customer Support Ticket System v1.10.1
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 / Models / Traits / CustomerTrait.php

CustomerTrait.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.10.1, at app/Models/Traits/CustomerTrait.php

431 lines 13.8 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\Models\Traits;
4
5 use Exception;
6 use FluentSupport\App\Models\Ticket;
7 use FluentSupport\App\Services\Helper;
8 use FluentSupport\Framework\Support\Arr;
9 use FluentSupport\App\Services\ProfileInfoService;
10
11 trait CustomerTrait
12 {
13
14 /**
15 * This getCustomers method will return all customers
16 * @param string $search
17 * @param string $status
18 * @return object
19 */
20 public function getCustomers($search, $status=false)
21 {
22 $customersQuery = static::latest();
23
24 if ( $search ) {
25 $customersQuery = $customersQuery->searchBy($search);
26 }
27
28 if ($status && $status != 'all') {
29 $customersQuery->filterByStatues([$status]);
30 }
31
32 $customers = $customersQuery->paginate();
33
34 if ( defined( 'FLUENTCRM' ) && $customers->isEmpty() && !$status ) {
35 $customers = $this->findInCrmAndMakeCustomer ( $search );
36 ! is_null( $customers ) ? $customers : $customers = $customersQuery->paginate();
37 }
38
39 if ( defined('FLUENT_SUPPORT_PRO_DIR_FILE') && $customers->isEmpty() && !$status ) {
40 $customers = $this->findUserInWPAndMakeCustomer( $search );
41 ! is_null( $customers ) ? $customers : $customers = $customersQuery->paginate();
42 }
43
44 return $this->attachCustomersMetaData( $customers );
45 }
46
47 public function getCustomerField($customer_id,$userID = null)
48 {
49 $basicFields = [
50 'first_name' => [
51 'label' => __('First Name', 'fluent-support'),
52 'data_type' => 'text',
53 'placeholder' => __('First Name', 'fluent-support'),
54 'type' => 'input-text',
55 'wrapper_class' => 'fs_half_field',
56 ],
57 'last_name' => [
58 'label' => __('Last Name', 'fluent-support'),
59 'data_type' => 'text',
60 'placeholder' => __('Last Name', 'fluent-support'),
61 'type' => 'input-text',
62 'wrapper_class' => 'fs_half_field',
63 ],
64 'email' => [
65 'label' => __('Email', 'fluent-support'),
66 'data_type' => 'email',
67 'placeholder' => __('Email', 'fluent-support'),
68 'type' => 'input-text',
69 'wrapper_class' => 'fs_half_field',
70 'disabled' => !empty($customer_id),
71 ],
72 'title' => [
73 'label' => __('Job Title', 'fluent-support'),
74 'data_type' => 'text',
75 'placeholder' => __('Job Title', 'fluent-support'),
76 'type' => 'input-text',
77 'wrapper_class' => 'fs_half_field',
78 ],
79 'note' => [
80 'label' => __('Note', 'fluent-support'),
81 'data_type' => 'textarea',
82 'placeholder' => __('Note', 'fluent-support'),
83 'type' => 'input-text',
84 'wrapper_class' => 'fs_half_field',
85 ],
86 'status' => [
87 'label' => __('Status', 'fluent-support'),
88 'data_type' => 'text',
89 'type' => 'input-radio',
90 'options' => [
91 'active' => ['id' => 'active', 'value' => 'active', 'label' => __('Active', 'fluent-support')],
92 'inactive' => ['id' => 'inactive', 'value' => 'inactive', 'label' => __('Blocked', 'fluent-support')],
93 ],
94 'wrapper_class' => 'fs_half_field',
95 ],
96 'status_html' => [
97 'dependency' => [
98 'depends_on' => 'status',
99 'operator' => '=',
100 'value' => 'inactive',
101 ],
102 'type' => 'html-viewer',
103 'wrapper_class' => 'fs_warn',
104 'html' => __('block_instruction', 'fluent-support'),
105 ],
106 ];
107
108 $addressFields = [
109 'address_line_1' => [
110 'label' => __('Address Line 1', 'fluent-support'),
111 'data_type' => 'text',
112 'placeholder' => __('Address Line 1', 'fluent-support'),
113 'type' => 'input-text',
114 'wrapper_class' => 'fs_half_field',
115 ],
116 'address_line_2' => [
117 'label' => __('Address Line 2', 'fluent-support'),
118 'data_type' => 'text',
119 'placeholder' => __('Address Line 2', 'fluent-support'),
120 'type' => 'input-text',
121 'wrapper_class' => 'fs_half_field',
122 ],
123 'city' => [
124 'label' => __('City', 'fluent-support'),
125 'data_type' => 'text',
126 'placeholder' => __('City', 'fluent-support'),
127 'type' => 'input-text',
128 'wrapper_class' => 'fs_half_field',
129 ],
130 'state' => [
131 'label' => __('State', 'fluent-support'),
132 'data_type' => 'text',
133 'placeholder' => __('State', 'fluent-support'),
134 'type' => 'input-text',
135 'wrapper_class' => 'fs_half_field',
136 ],
137 'zip' => [
138 'label' => __('Zip Code', 'fluent-support'),
139 'data_type' => 'text',
140 'placeholder' => __('Zip Code', 'fluent-support'),
141 'type' => 'input-text',
142 'wrapper_class' => 'fs_half_field',
143 ],
144 'country' => [
145 'label' => __('Country', 'fluent-support'),
146 'placeholder' => __('Country', 'fluent-support'),
147 'type' => 'country-selector',
148 'wrapper_class' => 'fs_half_field',
149 ],
150 ];
151
152 if ($userID) {
153 $addressFields = apply_filters('fluent_support/custom_registration_form_fields',$addressFields);
154
155 foreach ($addressFields as $key => $field) {
156 //To create a custom field using _formBuilder.vue, the 'type' should be 'input-text', and the 'data_type' may vary, such as (text, number, date...).
157 $addressFields[$key]['data_type'] = $addressFields[$key]['type'];
158 $addressFields[$key]['type'] = 'input-text';
159 }
160 }
161
162 $loading = false;
163
164 $state = [
165 'basic_fields' => $basicFields,
166 'address_fields' => $addressFields,
167 'loading' => $loading,
168 ];
169
170 return $state;
171
172 }
173
174 /**
175 * This `findUserInWPAndMakeCustomer` method will find customer in WP Users and make it as a customer
176 * @param string $search
177 * @return object
178 */
179 public function findUserInWPAndMakeCustomer ( $search )
180 {
181 if ( is_email( $search ) ){
182 $user = get_user_by( 'email', $search );
183
184 if( $user ) {
185 unset( $user->user_pass );
186 $customerData = [
187 'user_id' => $user->ID,
188 'email' => $user->user_email,
189 'first_name' => $user->first_name,
190 'last_name' => $user->last_name
191 ];
192 $customer = static::maybeCreateCustomer( $customerData );
193 return static::where('email', $user->user_email)->paginate();
194 }
195 }
196 }
197
198 /**
199 * This `findInCrmAndMakeCustomer` method will find customer in Fluent CRM and make it as a customer
200 * @param string $search
201 * @return object
202 */
203 public function findInCrmAndMakeCustomer ( $search )
204 {
205 $customer = \FluentCrm\App\Models\Subscriber::where( 'email', $search )->first();
206 if( $customer ) {
207 $customerData = [
208 'email' => $customer->email,
209 'first_name' => $customer->first_name,
210 'last_name' => $customer->last_name
211 ];
212 $customer = static::maybeCreateCustomer( $customerData );
213 return static::where('email', $customer->email)->paginate();
214 }
215 }
216
217 /**
218 * This getCustomer method will return a single customer
219 * @param int $id
220 * @param array $with
221 * @return object
222 */
223
224 public function getCustomer($id, $with)
225 {
226 $customer = static::find($id);
227
228 $data = [
229 'customer' => $customer
230 ];
231
232 if (!$with) {
233 return $data;
234 }
235
236 return $this->getCustomerAdditionalData($with, $customer, $data);
237 }
238
239 /**
240 * This createCustomer method will create a new customer
241 * @param array $data
242 * @return object
243 */
244
245 public function createCustomer($data)
246 {
247 $data = Arr::only($data, $this->getFillable());
248
249 $user = get_user_by('email', $data['email']);
250
251 if ($user) {
252 $data['user_id'] = $user->ID;
253 if (empty($data['first_name'])) {
254 $data['first_name'] = $user->first_name;
255 }
256 if (empty($data['last_name'])) {
257 $data['last_name'] = $user->last_name;
258 }
259 }
260
261 return static::create($data);
262 }
263
264 /**
265 * This updateCustomer method will update a customer
266 * @since 1.5.7
267 * @param int $customerId
268 * @param array $data
269 * @return object
270 */
271
272 public function updateCustomer($customerId, $data)
273 {
274 $customFieldsKeys = apply_filters('fluent_support/custom_registration_form_fields_key', Helper::getBusinessSettings('custom_registration_form_field'));
275 $customFieldsKeys = is_array($customFieldsKeys) ? $customFieldsKeys : [];
276 $customFormValue = Arr::only($data, $customFieldsKeys);
277
278 $data = $this->takeValidKeysForUpdate($data);
279
280 $customer = static::findOrFail($customerId);
281
282 if($this->customerExists($customerId, $data['email']))
283 {
284 throw new \Exception('Another Customer has same email address');
285 }
286
287 $user = get_user_by('email', $data['email']);
288
289 if ($user && !empty($customFieldsKeys)) {
290 $data['user_id'] = $user->ID;
291
292 //Update Custom field data for user
293 foreach ($customFieldsKeys as $key) {
294 if (isset($customFormValue[$key])) {
295 $fieldValue = $customFormValue[$key];
296 update_user_meta($user->ID, $key, $fieldValue);
297 }
298 }
299 }
300
301 static::where('id', $customer->id)->update($data);
302
303 return static::findOrFail($customerId);
304 }
305
306 /**
307 * deleteCustomer method will delete a customer and all tickets by that customer
308 * @since 1.5.7
309 * @param int $customerId
310 * @return array
311 */
312
313 public function deleteCustomer($customerId)
314 {
315 $customer = static::findOrFail($customerId);
316
317 $tickets = Ticket::where('customer_id', $customer->id)->get();
318
319 foreach ($tickets as $ticket) {
320 $ticket->deleteTicket();
321 }
322
323 $customer->delete();
324
325 return [
326 'message' => __('Customer Deleted Successfully', 'fluent-support')
327 ];
328 }
329
330 /**
331 * This attachCustomersMetaData method will attach meta data to customers
332 * @since 1.5.7
333 * @param object $customers
334 * @return object
335 */
336 private function attachCustomersMetaData($customers)
337 {
338 foreach ($customers as $customer) {
339 $customer->total_tickets = $customer->getTicketCounts();
340 $customer->total_responses = $customer->getResponseCounts();
341 if ($customer->user_id) {
342 //Get profile link, if they are WP user
343 $customer->user_profile = admin_url('user-edit.php?user_id=' . $customer->user_id);
344 }
345 }
346
347 return $customers;
348 }
349
350 /**
351 * This getCustomerAdditionalData method will return additional data for a customer
352 * @since 1.5.7
353 * @param array $with
354 * @param object $customer
355 * @param array $data
356 * @return array
357 */
358
359 private function getCustomerAdditionalData($with, $customer, $data)
360 {
361 $customFieldKeys = apply_filters('fluent_support/custom_registration_form_fields_key', []);
362
363 $userMetaData = get_user_meta($customer['user_id']);
364
365 //Custom field data from wp user_meta
366 foreach ($customFieldKeys as $fieldKey) {
367 if (isset($userMetaData[$fieldKey][0])) {
368 $customer[$fieldKey] = $userMetaData[$fieldKey][0];
369 }
370 }
371
372 if (in_array('widgets', $with)) {
373 $data['widgets'] = ProfileInfoService::getProfileExtraWidgets($customer);
374 }
375
376 if (in_array('tickets', $with)) {
377 /*
378 * Filter ticket limit to show ticket in customer page sidebar
379 * @since 1.5.6
380 * @param int $limit
381 */
382 $limit = apply_filters('fluent_support/customer_page_ticket_widgets_limit', 20);
383
384 $data['tickets'] = Ticket::select(['id', 'title', 'status', 'customer_id', 'created_at'])
385 ->where('customer_id', $customer->id)
386 ->latest('id')
387 ->limit($limit)
388 ->get();
389 }
390
391 if(in_array('fluentcrm_profile', $with)) {
392 $data['fluentcrm_profile'] = Helper::getFluentCrmContactData($customer);
393 }
394
395 return $data;
396 }
397
398 /**
399 * This customerExists method will check if customer exists
400 * @since 1.5.7
401 * @param int $customerId
402 * @param string $email
403 * @return mixed
404 */
405
406 private function customerExists($customerId, $email)
407 {
408 $customer = static::where('id','!=', $customerId)->where('email', $email)->first();
409 if ($customer) {
410 return $customer;
411 }
412 return false;
413 }
414
415
416 /**
417 * This takeValidKeysForUpdate method will take valid keys data for update
418 * @since 1.5.7
419 * @param array $data
420 * @return array
421 */
422 private function takeValidKeysForUpdate($data)
423 {
424 $validKeys = $this->getFillable();
425 unset($validKeys['hash']);
426 unset($validKeys['user_id']);
427
428 return Arr::only($data, $validKeys);
429 }
430 }
431