PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.17
VikAppointments Services Booking Calendar v1.2.17
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / controllers / customer.php
vikappointments / admin / controllers Last commit date
analytics.php 5 months ago apiban.php 5 months ago apilog.php 5 months ago apiplugin.php 5 months ago apiuser.php 5 months ago backup.php 5 months ago calendar.php 5 months ago city.php 5 months ago closure.php 5 months ago configapp.php 5 months ago configcldays.php 5 months ago configcron.php 5 months ago configemp.php 5 months ago configsmsapi.php 5 months ago configuration.php 5 months ago conversion.php 5 months ago country.php 5 months ago coupon.php 5 months ago coupongroup.php 5 months ago cronjob.php 5 months ago cronjoblog.php 5 months ago customer.php 5 months ago customf.php 5 months ago dashboard.php 5 months ago emplocwdays.php 5 months ago employee.php 5 months ago emprates.php 5 months ago export.php 5 months ago exportres.php 5 months ago file.php 5 months ago findreservation.php 5 months ago group.php 5 months ago import.php 5 months ago index.html 5 months ago invoice.php 5 months ago langcustomf.php 5 months ago langemployee.php 5 months ago langgroup.php 5 months ago langmedia.php 5 months ago langoption.php 5 months ago langoptiongroup.php 5 months ago langpackage.php 5 months ago langpackgroup.php 5 months ago langpayment.php 5 months ago langservice.php 5 months ago langstatuscode.php 5 months ago langsubscr.php 5 months ago langtax.php 5 months ago location.php 5 months ago mailtext.php 5 months ago makerecurrence.php 5 months ago media.php 5 months ago multiorder.php 5 months ago option.php 5 months ago optiongroup.php 5 months ago package.php 5 months ago packgroup.php 5 months ago packorder.php 5 months ago payment.php 5 months ago rate.php 5 months ago reportsemp.php 5 months ago reportsser.php 5 months ago reservation.php 5 months ago restriction.php 5 months ago review.php 5 months ago service.php 5 months ago serworkday.php 5 months ago state.php 5 months ago statuscode.php 5 months ago subscription.php 5 months ago subscrorder.php 5 months ago tag.php 5 months ago tax.php 5 months ago usernote.php 5 months ago waitinglist.php 5 months ago webhook.php 5 months ago wizard.php 5 months ago
customer.php
562 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['credit'] = $input->get('credit', null, 'float');
186 $args['active_to_date'] = $input->get('active_to_date', '', 'string');
187 $args['image'] = $input->get('image', '', 'string');
188 $args['id'] = $input->get('id', 0, 'int');
189
190 // fill user fields only if we need to create them
191 if ($input->getBool('create_new_user'))
192 {
193 // user fields
194 $args['user'] = array();
195 $args['user']['username'] = $input->get('username', '', 'string');
196 $args['user']['usermail'] = $input->get('usermail', '', 'string');
197 $args['user']['password'] = $input->get('password', '', 'string');
198 $args['user']['confirm'] = $input->get('confpassword', '', 'string');
199 }
200
201 // convert expiratiom date from local timezone to UTC
202 $args['active_to_date'] = VAPDateHelper::getSqlDateLocale($args['active_to_date']);
203
204 // import custom fields requestor and loader (as dependency)
205 VAPLoader::import('libraries.customfields.requestor');
206
207 // get relevant custom fields only
208 $_cf = VAPCustomFieldsLoader::getInstance()
209 ->noRequiredCheckbox()
210 ->fetch();
211
212 // load custom fields from request
213 $args['fields'] = VAPCustomFieldsRequestor::loadForm($_cf, $tmp, $strict = false);
214
215 // inject uploads in custom fields array
216 foreach ($tmp['uploads'] as $k => $v)
217 {
218 $args['fields'][$k] = $v;
219 }
220
221 // register data fetched by the custom fields so that the customer
222 // model is able to use them for saving purposes
223 $args['fields_data'] = $tmp;
224
225 $rule = 'core.' . ($args['id'] > 0 ? 'edit' : 'create');
226
227 // check if we should use a blank template
228 $blank = $app->input->get('tmpl') === 'component';
229
230 // check user permissions
231 if (!$user->authorise($rule, 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
232 {
233 if ($blank)
234 {
235 // throw exception in order to avoid unexpected behaviors
236 throw new Exception(JText::translate('JERROR_ALERTNOAUTHOR'), '403');
237 }
238
239 // back to main list, not authorised to create/edit records
240 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
241 $this->cancel();
242
243 return false;
244 }
245
246 // get customer model
247 $customer = $this->getModel();
248
249 // try to save arguments
250 $id = $customer->save($args);
251
252 if (!$id)
253 {
254 if (!empty($args['user']))
255 {
256 // update user state data by injecting the user groups and username
257 $data = $app->getUserState('vap.customer.data', array());
258 $data['username'] = $args['user']['username'];
259 $data['usermail'] = $args['user']['usermail'];
260 $app->setUserState('vap.customer.data', $data);
261 }
262
263 // get string error
264 $error = $customer->getError(null, true);
265
266 // display error message
267 $app->enqueueMessage(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $error), 'error');
268
269 $url = 'index.php?option=com_vikappointments&view=managecustomer';
270
271 if ($args['id'])
272 {
273 $url .= '&cid[]=' . $args['id'];
274 }
275
276 if ($blank)
277 {
278 $url .= '&tmpl=component';
279 }
280
281 // redirect to new/edit page
282 $this->setRedirect($url);
283
284 return false;
285 }
286
287 $notes = $input->get('notes', '', 'string');
288
289 if ($notes)
290 {
291 // save the notes by using the apposite model
292 $this->getModel('usernote')->saveDraft(array(
293 'id_user' => $id,
294 'content' => $notes,
295 ));
296 }
297
298 // display generic successful message
299 $app->enqueueMessage(JText::translate('JLIB_APPLICATION_SAVE_SUCCESS'));
300
301 $url = 'index.php?option=com_vikappointments&task=customer.edit&cid[]=' . $id;
302
303 if ($blank)
304 {
305 // keep blank template when returning to edit page
306 $url .= '&tmpl=component';
307 }
308
309 // redirect to edit page
310 $this->setRedirect($url);
311
312 return true;
313 }
314
315 /**
316 * Deletes a list of records set in the request.
317 *
318 * @return boolean
319 */
320 public function delete()
321 {
322 $app = JFactory::getApplication();
323 $user = JFactory::getUser();
324
325 /**
326 * Added token validation.
327 * Both GET and POST are supported.
328 *
329 * @since 1.7
330 */
331 if (!JSession::checkToken() && !JSession::checkToken('get'))
332 {
333 // back to main list, missing CSRF-proof token
334 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
335 $this->cancel();
336
337 return false;
338 }
339
340 $cid = $app->input->get('cid', array(), 'uint');
341
342 // check user permissions
343 if (!$user->authorise('core.delete', 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
344 {
345 // back to main list, not authorised to delete records
346 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
347 $this->cancel();
348
349 return false;
350 }
351
352 // delete selected records
353 $this->getModel()->delete($cid);
354
355 // back to main list
356 $this->cancel();
357
358 return true;
359 }
360
361 /**
362 * Redirects the users to the main records list.
363 *
364 * @return void
365 */
366 public function cancel()
367 {
368 $this->setRedirect('index.php?option=com_vikappointments&view=customers');
369 }
370
371 /**
372 * Sends a custom SMS to the specified customer.
373 *
374 * @return void
375 */
376 public function sendsms()
377 {
378 $app = JFactory::getApplication();
379 $input = $app->input;
380 $user = JFactory::getUser();
381
382 /**
383 * Added token validation.
384 * Both GET and POST are supported.
385 *
386 * @since 1.7
387 */
388 if (!JSession::checkToken() && !JSession::checkToken('get'))
389 {
390 // back to main list, missing CSRF-proof token
391 $app->enqueueMessage(JText::translate('JINVALID_TOKEN'), 'error');
392 $this->cancel();
393
394 return false;
395 }
396
397 $cid = $input->get('cid', array(), 'uint');
398
399 // check user permissions
400 if (!$user->authorise('core.edit.state', 'com_vikappointments') || !$user->authorise('core.access.customers', 'com_vikappointments'))
401 {
402 // back to main list, not authorised to send SMS notifications
403 $app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
404 $this->cancel();
405
406 return false;
407 }
408
409 try
410 {
411 // get current SMS instance
412 $smsapi = VAPApplication::getInstance()->getSmsInstance();
413 }
414 catch (Exception $e)
415 {
416 // back to main list, SMS API not configured
417 $app->enqueueMessage(JText::translate('VAPSMSESTIMATEERR1'), 'error');
418 $this->cancel();
419
420 return false;
421 }
422
423 // load message from request
424 $message = $input->get('sms_message', '', 'string');
425
426 // make sure the message is not empty
427 if (!$message)
428 {
429 // missing contents, back to main list
430 $this->cancel();
431
432 return false;
433 }
434
435 $notified = 0;
436 $errors = array();
437
438 foreach ($cid as $id)
439 {
440 // get customer details
441 $customer = VikAppointments::getCustomer($id);
442
443 if ($customer && $customer->billing_phone)
444 {
445 // send message
446 $response = $smsapi->sendMessage($customer->billing_phone, $message);
447
448 // validate response
449 if ($smsapi->validateResponse($response))
450 {
451 // successful notification
452 $notified++;
453 }
454 else
455 {
456 // unable to send the notification, register error message
457 $errors[] = $smsapi->getLog();
458 }
459 }
460 }
461
462 // update default message if needed
463 if ($input->getBool('sms_keep_def'))
464 {
465 // alter configuration
466 VAPFactory::getConfig()->set('smstextcust', $message);
467 }
468
469 if ($notified)
470 {
471 // successful message
472 $app->enqueueMessage(JText::plural('VAPCUSTOMERSMSSENT', $notified));
473 }
474 else
475 {
476 // no notifications sent
477 $app->enqueueMessage(JText::plural('VAPCUSTOMERSMSSENT', $notified), 'warning');
478 }
479
480 // display any returned errors
481 if ($errors)
482 {
483 // do not display duplicate or empty errors
484 $errors = array_unique(array_filter($errors));
485
486 foreach ($errors as $err)
487 {
488 $app->enqueueMessage($err, 'error');
489 }
490 }
491
492 // back to main list
493 $this->cancel();
494 }
495
496 /**
497 * AJAX end-point to obtain a list of users belonging
498 * to the current platform (CMS).
499 *
500 * @return void
501 */
502 public function jusers()
503 {
504 $input = JFactory::getApplication()->input;
505
506 /**
507 * Added token validation.
508 *
509 * @since 1.7
510 */
511 if (!JSession::checkToken())
512 {
513 // missing CSRF-proof token
514 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
515 }
516
517 $search = $input->getString('term');
518 $id = $input->getUint('id', null);
519
520 // get customers model
521 $model = $this->getModel();
522
523 // search CMS users
524 $users = $model->searchUsers($search, $id);
525
526 // send users to caller
527 $this->sendJSON($users);
528 }
529
530 /**
531 * AJAX end-point to obtain a list of customers.
532 *
533 * @return void
534 */
535 public function users()
536 {
537 $input = JFactory::getApplication()->input;
538
539 /**
540 * Added token validation.
541 *
542 * @since 1.7
543 */
544 if (!JSession::checkToken())
545 {
546 // missing CSRF-proof token
547 UIErrorFactory::raiseError(403, JText::translate('JINVALID_TOKEN'));
548 }
549
550 $search = $input->getString('term', '');
551
552 // get customers model
553 $model = $this->getModel();
554
555 // search customers
556 $customers = $model->search($search);
557
558 // send users to caller
559 $this->sendJSON($customers);
560 }
561 }
562