PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / models / customfields.php
vikappointments / site / helpers / libraries / models Last commit date
conversion.php 2 days ago customer.php 2 days ago customfields.php 2 days ago index.html 2 days ago locations.php 2 days ago orderstatus.php 2 days ago restrictions.php 2 days ago specialrates.php 2 days ago statistics.php 2 days ago subscriptions.php 2 days ago
customfields.php
851 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 /**
15 * @deprecated 1.8 Use VAPCustomFieldsLoader::NO_FILTER instead.
16 */
17 defined('CF_NO_FILTER') or define('CF_NO_FILTER', 0);
18
19 /**
20 * @deprecated 1.8 Use VAPCustomFieldsLoader::EXCLUDE_REQUIRED_CHECKBOX instead.
21 */
22 defined('CF_EXCLUDE_REQUIRED_CHECKBOX') or define('CF_EXCLUDE_REQUIRED_CHECKBOX', 1);
23
24 /**
25 * @deprecated 1.8 Use VAPCustomFieldsLoader::EXCLUDE_SEPARATOR instead.
26 */
27 defined('CF_EXCLUDE_SEPARATOR') or define('CF_EXCLUDE_SEPARATOR', 2);
28
29 /**
30 * @deprecated 1.8 Use VAPCustomFieldsLoader::EXCLUDE_FILE instead.
31 */
32 defined('CF_EXCLUDE_FILE') or define('CF_EXCLUDE_FILE', 4);
33
34 VAPLoader::import('libraries.customfields.loader');
35
36 /**
37 * VikAppointments custom fields class handler.
38 *
39 * @since 1.6
40 * @deprecated 1.8 Rely on the customfields libraries.
41 */
42 abstract class VAPCustomFields
43 {
44 /**
45 * The default country code in case it is not specified.
46 *
47 * @var string
48 *
49 * @deprecated 1.8 Use VAPCustomFieldsLoader::$defaultCountry instead.
50 */
51 public static $defaultCountry = 'US';
52
53 /**
54 * A list containing all the columns that will be used
55 * in the SELECT query.
56 *
57 * @var array
58 *
59 * @deprecated 1.8 Without replacement. The columns to load are automatically
60 * retrieved according to the properties of the fields table.
61 */
62 public static $listColumns = array(
63 'id',
64 'name',
65 'formname',
66 'description',
67 'type',
68 'choose',
69 'multiple',
70 'required',
71 'rule',
72 'ordering',
73 'poplink',
74 'id_employee',
75 'group',
76 );
77
78 /**
79 * Return the list of the custom fields for the specified section.
80 *
81 * @param integer $group The section of the program.
82 * @param integer $employee The employee ID for customers fields.
83 * @param mixed $service The service ID or a list of services (for customers).
84 * @param integer $flag A mask to filter the custom fields.
85 *
86 * @return array The list of custom fields.
87 *
88 * @deprecated 1.8 Use VAPCustomFieldsLoader::fetch() instead.
89 */
90 public static function getList($group = 0, $employee = 0, $service = null, $flag = 0)
91 {
92 $loader = VAPCustomFieldsLoader::getInstance();
93
94 if ($group == 1)
95 {
96 $loader->employees();
97 }
98 else
99 {
100 if ($employee > 0)
101 {
102 $loader->ofEmployee($employee);
103 }
104
105 if ($service)
106 {
107 $loader->forService($service);
108 }
109 }
110
111 if ($flag & CF_EXCLUDE_SEPARATOR)
112 {
113 $loader->noSeparator();
114 }
115
116 if ($flag & CF_EXCLUDE_REQUIRED_CHECKBOX)
117 {
118 $loader->noRequiredCheckbox();
119 }
120
121 if ($flag & CF_EXCLUDE_FILE)
122 {
123 $loader->noInputFile();
124 }
125
126 return $loader->fetch();
127 }
128
129 /**
130 * Return the default country code assigned to the phone number custom field.
131 *
132 * @param string $langtag The langtag to retrieve the proper country
133 * depending on the current language.
134 * @param mixed $default The default return value in case of unsupported
135 *
136 *
137 * @return string The default country code.
138 *
139 * @deprecated 1.8 Use VAPCustomFieldsLoader::getDefaultCountryCode() instead.
140 */
141 public static function getDefaultCountryCode($langtag = null, $default = true)
142 {
143 return VAPCustomFieldsLoader::getDefaultCountryCode($langtag, $default);
144 }
145
146 /**
147 * Translates the specified custom fields.
148 * The translation of the name will be placed in a different column 'langname'.
149 * The original 'name' column won't be altered.
150 *
151 * @param array $fields The records to translate.
152 *
153 * @return void
154 *
155 * @deprecated 1.8 Use VAPCustomFieldsLoader::doTranslate() instead.
156 */
157 public static function translate(array &$fields, $tag = null)
158 {
159 VAPCustomFieldsLoader::doTranslate($fields, $tag);
160 }
161
162 /**
163 * Translates the specified custom fields array data.
164 *
165 * @param array $data The associative array with the CF data.
166 * @param array $fields The custom fields (MUST BE already translated).
167 *
168 * @return array The translated CF data array.
169 *
170 * @deprecated 1.8 Use VAPCustomFieldsLoader::translateObject() instead.
171 */
172 public static function translateObject(array $data, array $fields)
173 {
174 return VAPCustomFieldsLoader::translateObject($data, $fields);
175 }
176
177 /**
178 * Searches a custom field using the specified query string.
179 *
180 * @param mixed $key The query params (the value to search for or an array
181 * containing the column and the value).
182 * @param array $fields The custom fields list.
183 * @param integer $fields The maximum number of records to get (0 to ignore the limit).
184 *
185 * @return mixed The custom fields that match the query.
186 */
187 protected static function findField($key, array $fields, $lim = 0)
188 {
189 $list = array();
190
191 // if the key is a string, search by ID column
192 if (is_string($key))
193 {
194 $key = array('id', $key);
195 }
196
197 foreach ($fields as $cf)
198 {
199 // check if the column value is equals to the key
200 if (self::getColumnValue($cf, $key[0], null) == $key[1])
201 {
202 // push the custom field in the list
203 $list[] = $cf;
204
205 // stop iterating if we reached the limit
206 if (count($list) == $lim)
207 {
208 break;
209 }
210 }
211 }
212
213 // return false if no matches
214 if (!count($list))
215 {
216 return false;
217 }
218 // return the CF if the limit was set to 1
219 else if ($lim == 1)
220 {
221 return reset($list);
222 }
223
224 // return the list of custom fields found (never empty)
225 return $list;
226 }
227
228 /**
229 * Returns the custom fields values specified in the REQUEST.
230 *
231 * @param mixed $fields The custom fields list to check for.
232 * If the list is not an array, the method will load
233 * all the custom fields that belong to the specified group.
234 * @param array &$args The array data to fill-in in case of specific rules (name, e-mail, etc...).
235 * @param boolean $strict True to raise an error when a mandatory field is missing.
236 *
237 * @return array The lookup array containing the values of the custom fields.
238 *
239 * @throws Exception When a mandatory field is empty or when a file hasn't been uploaded.
240 *
241 * @uses getList()
242 * @uses sanitizeFieldValue()
243 * @uses validateField()
244 * @uses dispatchRule()
245 * @uses helper methods to access fields properties
246 *
247 * @deprecated 1.8 Use VAPCustomFieldsRequestor::loadForm() instead.
248 */
249 public static function loadFromRequest($fields = 0, ?array &$args = null, $strict = true)
250 {
251 return VAPCustomFieldsRequestor::loadForm($fields, $args, $strict);
252 }
253
254 /**
255 * Sanitize the field value.
256 *
257 * @param mixed $field The custom field.
258 * @param string $value The value to sanitize.
259 *
260 * @return mixed The sanitized value.
261 */
262 protected static function sanitizeFieldValue($field, $value)
263 {
264 // sanitize a input number
265 if (static::isInputNumber($field))
266 {
267 // decode the settings
268 $settings = json_decode(static::getColumnValue($field, 'choose', '{}'), true);
269
270 // convert the string to float
271 $value = floatval($value);
272
273 // if min setting exists, make sure the value is not lower
274 if (strlen($settings['min']))
275 {
276 $value = max(array($value, (float) $settings['min']));
277 }
278
279 // if max setting exists, make sure the value is not higher
280 if (strlen($settings['max']))
281 {
282 $value = min(array($value, (float) $settings['max']));
283 }
284
285 // if decimals are not supported, round the value
286 if (!$settings['decimals'])
287 {
288 $value = round($value);
289 }
290 }
291
292 return $value;
293 }
294
295 /**
296 * Checks if the value of the field is accepted.
297 *
298 * @param mixed $field The custom field to evaluate.
299 * @param string $value The value of the field.
300 *
301 * @return boolean True if valid, otherwise false.
302 */
303 protected static function validateField($field, $value)
304 {
305 return (!static::isRequired($field)
306 || (!static::isInputFile($field) && strlen($value))
307 || (static::isInputFile($field) && !empty($value['name'])));
308 }
309
310 /**
311 * Dispatched the rule of the field.
312 *
313 * @param mixed $field The custom field to evaluate.
314 * @param string $value The value of the field.
315 * @param array &$args The array data to fill-in in case of specific rules (name, e-mail, etc...).
316 *
317 * @return void
318 *
319 * @uses isNominative()
320 * @uses isEmail()
321 * @uses isPhoneNumber()
322 */
323 protected static function dispatchRule($field, $value, array &$args)
324 {
325 // check if the field is a nominative
326 if (static::isNominative($field))
327 {
328 if (!empty($args['purchaser_nominative']))
329 {
330 $args['purchaser_nominative'] .= ' ';
331 }
332 else
333 {
334 $args['purchaser_nominative'] = '';
335 }
336
337 $args['purchaser_nominative'] .= $value;
338 }
339 // check if the field is an e-mail
340 else if (static::isEmail($field))
341 {
342 $args['purchaser_mail'] = $value;
343 }
344 // check if the field is a phone number
345 else if (static::isPhoneNumber($field))
346 {
347 // get the prefix country (ID_C2CODE)
348 $country_key = JFactory::getApplication()->input->getString('vapcf' . static::getID($field) . '_prfx', '');
349
350 if (!empty($country_key))
351 {
352 // explode the string
353 $country_key = explode('_', $country_key);
354
355 // get the country using the 2 letters code
356 $country = VAPLocations::getCountryFromCode($country_key[1]);
357 if ($country !== false)
358 {
359 $args['purchaser_prefix'] = $country['phone_prefix'];
360 $args['purchaser_country'] = $country['country_2_code'];
361 }
362 }
363
364 // sanitize phone number
365 $args['purchaser_phone'] = str_replace(' ', '', $value);
366 }
367 // check if the field is a state or a province
368 else if (static::isStateProvince($field))
369 {
370 $args['billing_state'] = $value;
371 }
372 // check if the field is a city
373 else if (static::isCity($field))
374 {
375 $args['billing_city'] = $value;
376 }
377 // check if the field is an address
378 else if (static::isAddress($field))
379 {
380 $args['billing_address'] = $value;
381 }
382 // check if the field is a ZIP code
383 else if (static::isZipCode($field))
384 {
385 $args['billing_zip'] = $value;
386 }
387 // check if the field is a company name
388 else if (static::isCompanyName($field))
389 {
390 $args['company'] = $value;
391 }
392 // check if the field is a VAT number
393 else if (static::isVatNumber($field))
394 {
395 $args['vatnum'] = $value;
396 }
397 }
398
399 /**
400 * Get the ID property of the specified custom field object.
401 *
402 * @param mixed $cf The array or the object of the custom field.
403 *
404 * @return integer The ID of the custom field.
405 *
406 * @uses getColumnValue()
407 */
408 public static function getID($cf)
409 {
410 return static::getColumnValue($cf, 'id', 0);
411 }
412
413 /**
414 * Get the NAME property of the specified custom field object.
415 *
416 * @param mixed $cf The array or the object of the custom field.
417 *
418 * @return string The name of the custom field.
419 *
420 * @uses getColumnValue()
421 */
422 public static function getName($cf)
423 {
424 return static::getColumnValue($cf, 'name', '');
425 }
426
427 /**
428 * Checks if the specified custom field is required.
429 *
430 * @param mixed $cf The array or the object of the custom field.
431 *
432 * @return boolean True if required, otherwise false.
433 *
434 * @uses getColumnValue()
435 */
436 public static function isRequired($cf)
437 {
438 return (bool) static::getColumnValue($cf, 'required', 0);
439 }
440
441 /**
442 * Get the TYPE property of the specified custom field object.
443 *
444 * @param mixed $cf The array or the object of the custom field.
445 *
446 * @return integer The type of the custom field.
447 *
448 * @uses getColumnValue()
449 */
450 public static function getType($cf)
451 {
452 return static::getColumnValue($cf, 'type', 'text');
453 }
454
455 /**
456 * Get the RULE property of the specified custom field object.
457 *
458 * @param mixed $cf The array or the object of the custom field.
459 *
460 * @return string The rule of the custom field,
461 * 'text' if it is not possible to establish it.
462 *
463 * @uses getColumnValue()
464 */
465 public static function getRule($cf)
466 {
467 return static::getColumnValue($cf, 'rule', self::NONE);
468 }
469
470 /**
471 * Checks if the custom field is a nominative.
472 *
473 * @param mixed $cf The array or the object of the custom field.
474 *
475 * @param boolean True if nominative, otherwise false.
476 *
477 * @uses getRule()
478 */
479 public static function isNominative($cf)
480 {
481 return static::getRule($cf) == self::NOMINATIVE;
482 }
483
484 /**
485 * Checks if the custom field is an e-mail.
486 *
487 * @param mixed $cf The array or the object of the custom field.
488 *
489 * @param boolean True if e-mail, otherwise false.
490 *
491 * @uses getRule()
492 */
493 public static function isEmail($cf)
494 {
495 return static::getRule($cf) == self::EMAIL;
496 }
497
498 /**
499 * Checks if the custom field is a phone number.
500 *
501 * @param mixed $cf The array or the object of the custom field.
502 *
503 * @param boolean True if phone number, otherwise false.
504 *
505 * @uses getRule()
506 */
507 public static function isPhoneNumber($cf)
508 {
509 return static::getRule($cf) == self::PHONE_NUMBER;
510 }
511
512 /**
513 * Checks if the custom field is a state or a province.
514 *
515 * @param mixed $cf The array or the object of the custom field.
516 *
517 * @param boolean True if a state, otherwise false.
518 *
519 * @uses getRule()
520 */
521 public static function isStateProvince($cf)
522 {
523 return static::getRule($cf) == self::STATE;
524 }
525
526 /**
527 * Checks if the custom field is a city.
528 *
529 * @param mixed $cf The array or the object of the custom field.
530 *
531 * @param boolean True if city, otherwise false.
532 *
533 * @uses getRule()
534 */
535 public static function isCity($cf)
536 {
537 return static::getRule($cf) == self::CITY;
538 }
539
540 /**
541 * Checks if the custom field is an address.
542 *
543 * @param mixed $cf The array or the object of the custom field.
544 *
545 * @param boolean True if address, otherwise false.
546 *
547 * @uses getRule()
548 */
549 public static function isAddress($cf)
550 {
551 return static::getRule($cf) == self::ADDRESS;
552 }
553
554 /**
555 * Checks if the custom field is a ZIP code.
556 *
557 * @param mixed $cf The array or the object of the custom field.
558 *
559 * @param boolean True if ZIP code, otherwise false.
560 *
561 * @uses getRule()
562 */
563 public static function isZipCode($cf)
564 {
565 return static::getRule($cf) == self::ZIP;
566 }
567
568 /**
569 * Checks if the custom field is a company name.
570 *
571 * @param mixed $cf The array or the object of the custom field.
572 *
573 * @param boolean True if company name, otherwise false.
574 *
575 * @uses getRule()
576 */
577 public static function isCompanyName($cf)
578 {
579 return static::getRule($cf) == self::COMPANY;
580 }
581
582 /**
583 * Checks if the custom field is a VAT number.
584 *
585 * @param mixed $cf The array or the object of the custom field.
586 *
587 * @param boolean True if VAT number, otherwise false.
588 *
589 * @uses getRule()
590 */
591 public static function isVatNumber($cf)
592 {
593 return static::getRule($cf) == self::VATNUM;
594 }
595
596 /**
597 * Checks if the custom field is an input text.
598 *
599 * @param mixed $cf The array or the object of the custom field.
600 *
601 * @param boolean True if input text, otherwise false.
602 *
603 * @uses getType()
604 */
605 public static function isInputText($cf)
606 {
607 return static::getType($cf) == 'text';
608 }
609
610 /**
611 * Checks if the custom field is a textarea.
612 *
613 * @param mixed $cf The array or the object of the custom field.
614 *
615 * @param boolean True if textarea, otherwise false.
616 *
617 * @uses getType()
618 */
619 public static function isTextArea($cf)
620 {
621 return static::getType($cf) == 'textarea';
622 }
623
624 /**
625 * Checks if the custom field is an input number.
626 *
627 * @param mixed $cf The array or the object of the custom field.
628 *
629 * @param boolean True if input number, otherwise false.
630 *
631 * @uses getType()
632 */
633 public static function isInputNumber($cf)
634 {
635 return static::getType($cf) == 'number';
636 }
637
638 /**
639 * Checks if the custom field is a select.
640 *
641 * @param mixed $cf The array or the object of the custom field.
642 *
643 * @param boolean True if select, otherwise false.
644 *
645 * @uses getType()
646 */
647 public static function isSelect($cf)
648 {
649 return static::getType($cf) == 'select';
650 }
651
652 /**
653 * Checks if the custom field is a datepicker.
654 *
655 * @param mixed $cf The array or the object of the custom field.
656 *
657 * @param boolean True if datepicker, otherwise false.
658 *
659 * @uses getType()
660 */
661 public static function isCalendar($cf)
662 {
663 return static::getType($cf) == 'date';
664 }
665
666 /**
667 * Checks if the custom field is an input file.
668 *
669 * @param mixed $cf The array or the object of the custom field.
670 *
671 * @param boolean True if input file, otherwise false.
672 *
673 * @uses getType()
674 */
675 public static function isInputFile($cf)
676 {
677 return static::getType($cf) == 'file';
678 }
679
680 /**
681 * Checks if the custom field is a checkbox.
682 *
683 * @param mixed $cf The array or the object of the custom field.
684 *
685 * @param boolean True if checkbox otherwise false.
686 *
687 * @uses getType()
688 */
689 public static function isCheckbox($cf)
690 {
691 return static::getType($cf) == 'checkbox';
692 }
693
694 /**
695 * Checks if the custom field is a separator.
696 *
697 * @param mixed $cf The array or the object of the custom field.
698 *
699 * @param boolean True if separator, otherwise false.
700 *
701 * @uses getType()
702 */
703 public static function isSeparator($cf)
704 {
705 return static::getType($cf) == 'separator';
706 }
707
708 /**
709 * Method used to access the attributes and properties of the given
710 * custom field. Useful if we don't know if we are handling an array or an object.
711 *
712 * @param mixed $cf The custom field array/object.
713 * @param string $column The column to access.
714 * @param mixed $default The default value in case the column does not exist.
715 *
716 * @return mixed The value at the specified column if exists, otherwise the default one.
717 */
718 protected static function getColumnValue($cf, $column, $default = null)
719 {
720 // check if the field is an array
721 if (is_array($cf))
722 {
723 // if the column key exists, return the value
724 if (array_key_exists($column, $cf))
725 {
726 return $cf[$column];
727 }
728 }
729 // check if the field is an object
730 else if (is_object($cf))
731 {
732 // if the property exists, return the value
733 if (property_exists($cf, $column))
734 {
735 return $cf->{$column};
736 }
737 }
738
739 // otherwise return the default one
740 return $default;
741 }
742
743 /**
744 * Customers identifier group.
745 *
746 * @var integer
747 *
748 * @deprecated 1.8 Use VAPCustomFieldsLoader::CUSTOMERS instaed.
749 */
750 const GROUP_CUSTOMERS = 0;
751
752 /**
753 * Employees identifier group.
754 *
755 * @var integer
756 *
757 * @deprecated 1.8 Use VAPCustomFieldsLoader::EMPLOYEES instaed.
758 */
759 const GROUP_EMPLOYEES = 1;
760
761 /**
762 * NONE identifier rule.
763 *
764 * @var integer
765 *
766 * @deprecated 1.8 Without replacement.
767 */
768 const NONE = '';
769
770 /**
771 * NOMINATIVE identifier rule.
772 *
773 * @var integer
774 *
775 * @deprecated 1.8 Without replacement.
776 */
777 const NOMINATIVE = 'nominative';
778
779 /**
780 * EMAIL identifier rule.
781 *
782 * @var integer
783 *
784 * @deprecated 1.8 Without replacement.
785 */
786 const EMAIL = 'email';
787
788 /**
789 * PHONE NUMBER identifier rule.
790 *
791 * @var integer
792 *
793 * @deprecated 1.8 Without replacement.
794 */
795 const PHONE_NUMBER = 'phone';
796
797 /**
798 * STATE/PROVINCE identifier rule.
799 *
800 * @var integer
801 *
802 * @deprecated 1.8 Without replacement.
803 */
804 const STATE = 'state';
805
806 /**
807 * CITY identifier rule.
808 *
809 * @var integer
810 *
811 * @deprecated 1.8 Without replacement.
812 */
813 const CITY = 'city';
814
815 /**
816 * ADDRESS identifier rule.
817 *
818 * @var integer
819 *
820 * @deprecated 1.8 Without replacement.
821 */
822 const ADDRESS = 'address';
823
824 /**
825 * ZIP/CAP identifier rule.
826 *
827 * @var integer
828 *
829 * @deprecated 1.8 Without replacement.
830 */
831 const ZIP = 'zip';
832
833 /**
834 * COMPANY NAME identifier rule.
835 *
836 * @var integer
837 *
838 * @deprecated 1.8 Without replacement.
839 */
840 const COMPANY = 'company';
841
842 /**
843 * VAT NUMBER identifier rule.
844 *
845 * @var integer
846 *
847 * @deprecated 1.8 Without replacement.
848 */
849 const VATNUM = 'vatnum';
850 }
851