PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / customer.php
vikappointments / admin / controllers Last commit date
analytics.php 4 years ago apiban.php 4 years ago apilog.php 4 years ago apiplugin.php 4 years ago apiuser.php 4 years ago backup.php 4 years ago calendar.php 4 years ago city.php 4 years ago closure.php 1 month ago configapp.php 4 years ago configcldays.php 2 years ago configcron.php 4 years ago configemp.php 4 years ago configsmsapi.php 4 years ago configuration.php 1 month ago conversion.php 2 years ago country.php 4 years ago coupon.php 4 years ago coupongroup.php 4 years ago cronjob.php 2 years ago cronjoblog.php 4 years ago customer.php 5 months ago customf.php 1 year ago dashboard.php 4 years ago emplocwdays.php 4 years ago employee.php 1 year ago emprates.php 4 years ago export.php 4 years ago exportres.php 4 years ago file.php 5 months ago findreservation.php 1 month ago group.php 4 years ago import.php 4 years ago index.html 4 years ago invoice.php 1 month ago langcustomf.php 4 years ago langemployee.php 4 years ago langgroup.php 4 years ago langmedia.php 4 years ago langoption.php 4 years ago langoptiongroup.php 4 years ago langpackage.php 4 years ago langpackgroup.php 4 years ago langpayment.php 4 years ago langservice.php 4 years ago langstatuscode.php 4 years ago langsubscr.php 4 years ago langtax.php 4 years ago location.php 4 years ago mailtext.php 2 years ago makerecurrence.php 1 month ago media.php 4 years ago multiorder.php 4 years ago option.php 5 months ago optiongroup.php 4 years ago package.php 2 years ago packgroup.php 4 years ago packorder.php 1 year ago payment.php 4 years ago rate.php 4 years ago reportsemp.php 4 years ago reportsser.php 4 years ago reservation.php 1 month ago restriction.php 4 years ago review.php 4 years ago service.php 1 year ago serworkday.php 5 months ago state.php 4 years ago statuscode.php 4 years ago subscription.php 4 years ago subscrorder.php 4 years ago tag.php 4 years ago tax.php 4 years ago usernote.php 4 years ago waitinglist.php 4 years ago webhook.php 4 years ago wizard.php 1 year ago
customer.php
563 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.mvc.controllers.admin');
15
16 /**
17 * VikAppointments customer controller.
18 *
19 * @since 1.7
20 */
21 class VikAppointmentsControllerCustomer extends VAPControllerAdmin
22 {
23 /**
24 * Task used to access the creation page of a new record.
25 *
26 * @return boolean
27 */
28 public function add()
29 {
30 $app = JFactory::getApplication();
31 $user = JFactory::getUser();
32
33 // unset user state for being recovered again
34 $app->setUserState('vap.customer.data', array());
35
36 // check if we should use a blank template
37 $blank = $app->input->get('tmpl') === 'component';
38
39 // check user permissions
40 if (!$user->authorise('core.create', 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
41 {
42 if ($blank)
43 {
44 // throw exception in order to avoid unexpected behaviors
45 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), '403');
46 }
47
48 // back to main list, not authorised to create records
49 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
50 $this->cancel();
51
52 return false;
53 }
54
55 $url = 'index.php?option=com_vikappointments&view=managecustomer';
56
57 if ($blank)
58 {
59 $url .= '&tmpl=component';
60 }
61
62 $this->setRedirect($url);
63
64 return true;
65 }
66
67 /**
68 * Task used to access the management page of an existing record.
69 *
70 * @return boolean
71 */
72 public function edit()
73 {
74 $app = JFactory::getApplication();
75 $user = JFactory::getUser();
76
77 // unset user state for being recovered again
78 $app->setUserState('vap.customer.data', array());
79
80 // check if we should use a blank template
81 $blank = $app->input->get('tmpl') === 'component';
82
83 // check user permissions
84 if (!$user->authorise('core.edit', 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
85 {
86 if ($blank)
87 {
88 // throw exception in order to avoid unexpected behaviors
89 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), '403');
90 }
91
92 // back to main list, not authorised to edit records
93 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
94 $this->cancel();
95
96 return false;
97 }
98
99 $cid = $app->input->getUint('cid', array(0));
100
101 $url = 'index.php?option=com_vikappointments&view=managecustomer&cid[]=' . $cid[0];
102
103 if ($blank)
104 {
105 $url .= '&tmpl=component';
106 }
107
108 $this->setRedirect($url);
109
110 return true;
111 }
112
113 /**
114 * Task used to save the record data set in the request.
115 * After saving, the user is redirected to the main list.
116 *
117 * @return void
118 */
119 public function saveclose()
120 {
121 if ($this->save())
122 {
123 $this->cancel();
124 }
125 }
126
127 /**
128 * Task used to save the record data set in the request.
129 * After saving, the user is redirected to the creation
130 * page of a new record.
131 *
132 * @return void
133 */
134 public function savenew()
135 {
136 if ($this->save())
137 {
138 $this->setRedirect('index.php?option=com_vikappointments&task=customer.add');
139 }
140 }
141
142 /**
143 * Task used to save the record data set in the request.
144 * After saving, the user is redirected to the management
145 * page of the record that has been saved.
146 *
147 * @param boolean $copy True to save the record as a copy.
148 *
149 * @return boolean
150 */
151 public function save($copy = false)
152 {
153 $app = JFactory::getApplication();
154 $input = $app->input;
155 $user = JFactory::getUser();
156
157 /**
158 * Added token validation.
159 *
160 * @since 1.7
161 */
162 if (!JSession::checkToken())
163 {
164 // back to main list, missing CSRF-proof token
165 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
166 $this->cancel();
167
168 return false;
169 }
170
171 $args = array();
172 $args['jid'] = $input->get('jid', 0, 'int');
173 $args['billing_name'] = $input->get('billing_name', '', 'string');
174 $args['billing_mail'] = $input->get('billing_mail', '', 'string');
175 $args['billing_phone'] = $input->get('billing_phone', '', 'string');
176 $args['country_code'] = $input->get('country_code', '', 'string');
177 $args['billing_state'] = $input->get('billing_state', '', 'string');
178 $args['billing_city'] = $input->get('billing_city', '', 'string');
179 $args['billing_address'] = $input->get('billing_address', '', 'string');
180 $args['billing_address_2'] = $input->get('billing_address_2', '', 'string');
181 $args['billing_zip'] = $input->get('billing_zip', '', 'string');
182 $args['company'] = $input->get('company', '', 'string');
183 $args['vatnum'] = $input->get('vatnum', '', 'string');
184 $args['ssn'] = $input->get('ssn', '', 'string');
185 $args['pec'] = $input->get('pec', '', 'string');
186 $args['credit'] = $input->get('credit', null, 'float');
187 $args['active_to_date'] = $input->get('active_to_date', '', 'string');
188 $args['image'] = $input->get('image', '', 'string');
189 $args['id'] = $input->get('id', 0, 'int');
190
191 // fill user fields only if we need to create them
192 if ($input->getBool('create_new_user'))
193 {
194 // user fields
195 $args['user'] = array();
196 $args['user']['username'] = $input->get('username', '', 'string');
197 $args['user']['usermail'] = $input->get('usermail', '', 'string');
198 $args['user']['password'] = $input->get('password', '', 'string');
199 $args['user']['confirm'] = $input->get('confpassword', '', 'string');
200 }
201
202 // convert expiratiom date from local timezone to UTC
203 $args['active_to_date'] = VAPDateHelper::getSqlDateLocale($args['active_to_date']);
204
205 // import custom fields requestor and loader (as dependency)
206 VAPLoader::import('libraries.customfields.requestor');
207
208 // get relevant custom fields only
209 $_cf = VAPCustomFieldsLoader::getInstance()
210 ->noRequiredCheckbox()
211 ->fetch();
212
213 // load custom fields from request
214 $args['fields'] = VAPCustomFieldsRequestor::loadForm($_cf, $tmp, $strict = false);
215
216 // inject uploads in custom fields array
217 foreach ($tmp['uploads'] as $k => $v)
218 {
219 $args['fields'][$k] = $v;
220 }
221
222 // register data fetched by the custom fields so that the customer
223 // model is able to use them for saving purposes
224 $args['fields_data'] = $tmp;
225
226 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
227
228 // check if we should use a blank template
229 $blank = $app->input->get('tmpl') === 'component';
230
231 // check user permissions
232 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
233 {
234 if ($blank)
235 {
236 // throw exception in order to avoid unexpected behaviors
237 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), '403');
238 }
239
240 // back to main list, not authorised to create/edit records
241 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
242 $this->cancel();
243
244 return false;
245 }
246
247 // get customer model
248 $customer = $this->getModel();
249
250 // try to save arguments
251 $id = $customer->save($args);
252
253 if (!$id)
254 {
255 if (!empty($args['user']))
256 {
257 // update user state data by injecting the user groups and username
258 $data = $app->getUserState('vap.customer.data', array());
259 $data['username'] = $args['user']['username'];
260 $data['usermail'] = $args['user']['usermail'];
261 $app->setUserState('vap.customer.data', $data);
262 }
263
264 // get string error
265 $error = $customer->getError(null, true);
266
267 // display error message
268 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
269
270 $url = 'index.php?option=com_vikappointments&view=managecustomer';
271
272 if ($args['id'])
273 {
274 $url .= '&cid[]=' . $args['id'];
275 }
276
277 if ($blank)
278 {
279 $url .= '&tmpl=component';
280 }
281
282 // redirect to new/edit page
283 $this->setRedirect($url);
284
285 return false;
286 }
287
288 $notes = $input->get('notes', '', 'string');
289
290 if ($notes)
291 {
292 // save the notes by using the apposite model
293 $this->getModel('usernote')->saveDraft(array(
294 'id_user' => $id,
295 'content' => $notes,
296 ));
297 }
298
299 // display generic successful message
300 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
301
302 $url = 'index.php?option=com_vikappointments&task=customer.edit&cid[]=' . $id;
303
304 if ($blank)
305 {
306 // keep blank template when returning to edit page
307 $url .= '&tmpl=component';
308 }
309
310 // redirect to edit page
311 $this->setRedirect($url);
312
313 return true;
314 }
315
316 /**
317 * Deletes a list of records set in the request.
318 *
319 * @return boolean
320 */
321 public function delete()
322 {
323 $app = JFactory::getApplication();
324 $user = JFactory::getUser();
325
326 /**
327 * Added token validation.
328 * Both GET and POST are supported.
329 *
330 * @since 1.7
331 */
332 if (!JSession::checkToken() && !JSession::checkToken('get'))
333 {
334 // back to main list, missing CSRF-proof token
335 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
336 $this->cancel();
337
338 return false;
339 }
340
341 $cid = $app->input->get('cid', array(), 'uint');
342
343 // check user permissions
344 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
345 {
346 // back to main list, not authorised to delete records
347 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
348 $this->cancel();
349
350 return false;
351 }
352
353 // delete selected records
354 $this->getModel()->delete($cid);
355
356 // back to main list
357 $this->cancel();
358
359 return true;
360 }
361
362 /**
363 * Redirects the users to the main records list.
364 *
365 * @return void
366 */
367 public function cancel()
368 {
369 $this->setRedirect('index.php?option=com_vikappointments&view=customers');
370 }
371
372 /**
373 * Sends a custom SMS to the specified customer.
374 *
375 * @return void
376 */
377 public function sendsms()
378 {
379 $app = JFactory::getApplication();
380 $input = $app->input;
381 $user = JFactory::getUser();
382
383 /**
384 * Added token validation.
385 * Both GET and POST are supported.
386 *
387 * @since 1.7
388 */
389 if (!JSession::checkToken() && !JSession::checkToken('get'))
390 {
391 // back to main list, missing CSRF-proof token
392 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
393 $this->cancel();
394
395 return false;
396 }
397
398 $cid = $input->get('cid', array(), 'uint');
399
400 // check user permissions
401 if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
402 {
403 // back to main list, not authorised to send SMS notifications
404 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
405 $this->cancel();
406
407 return false;
408 }
409
410 try
411 {
412 // get current SMS instance
413 $smsapi = VAPApplication::getInstance()->getSmsInstance();
414 }
415 catch (Exception $e)
416 {
417 // back to main list, SMS API not configured
418 $app->enqueueMessage(JText::translate('VAPSMSESTIMATEERR1'), 'error');
419 $this->cancel();
420
421 return false;
422 }
423
424 // load message from request
425 $message = $input->get('sms_message', '', 'string');
426
427 // make sure the message is not empty
428 if (!$message)
429 {
430 // missing contents, back to main list
431 $this->cancel();
432
433 return false;
434 }
435
436 $notified = 0;
437 $errors = array();
438
439 foreach ($cid as $id)
440 {
441 // get customer details
442 $customer = VikAppointments::getCustomer($id);
443
444 if ($customer && $customer->billing_phone)
445 {
446 // send message
447 $response = $smsapi->sendMessage($customer->billing_phone, $message);
448
449 // validate response
450 if ($smsapi->validateResponse($response))
451 {
452 // successful notification
453 $notified++;
454 }
455 else
456 {
457 // unable to send the notification, register error message
458 $errors[] = $smsapi->getLog();
459 }
460 }
461 }
462
463 // update default message if needed
464 if ($input->getBool('sms_keep_def'))
465 {
466 // alter configuration
467 VAPFactory::getConfig()->set('smstextcust', $message);
468 }
469
470 if ($notified)
471 {
472 // successful message
473 $app->enqueueMessage(JText::plural('VAPCUSTOMERSMSSENT', $notified));
474 }
475 else
476 {
477 // no notifications sent
478 $app->enqueueMessage(JText::plural('VAPCUSTOMERSMSSENT', $notified), 'warning');
479 }
480
481 // display any returned errors
482 if ($errors)
483 {
484 // do not display duplicate or empty errors
485 $errors = array_unique(array_filter($errors));
486
487 foreach ($errors as $err)
488 {
489 $app->enqueueMessage($err, 'error');
490 }
491 }
492
493 // back to main list
494 $this->cancel();
495 }
496
497 /**
498 * AJAX end-point to obtain a list of users belonging
499 * to the current platform (CMS).
500 *
501 * @return void
502 */
503 public function jusers()
504 {
505 $input = JFactory::getApplication()->input;
506
507 /**
508 * Added token validation.
509 *
510 * @since 1.7
511 */
512 if (!JSession::checkToken())
513 {
514 // missing CSRF-proof token
515 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
516 }
517
518 $search = $input->getString('term');
519 $id = $input->getUint('id', null);
520
521 // get customers model
522 $model = $this->getModel();
523
524 // search CMS users
525 $users = $model->searchUsers($search, $id);
526
527 // send users to caller
528 $this->sendJSON($users);
529 }
530
531 /**
532 * AJAX end-point to obtain a list of customers.
533 *
534 * @return void
535 */
536 public function users()
537 {
538 $input = JFactory::getApplication()->input;
539
540 /**
541 * Added token validation.
542 *
543 * @since 1.7
544 */
545 if (!JSession::checkToken())
546 {
547 // missing CSRF-proof token
548 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
549 }
550
551 $search = $input->getString('term', '');
552
553 // get customers model
554 $model = $this->getModel();
555
556 // search customers
557 $customers = $model->search($search);
558
559 // send users to caller
560 $this->sendJSON($customers);
561 }
562 }
563