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 / customfields / rule.php
vikappointments / site / helpers / libraries / customfields Last commit date
rules 2 days ago types 2 days ago control.php 2 days ago emptyloader.php 2 days ago factory.php 2 days ago field.php 2 days ago index.html 2 days ago loader.php 2 days ago renderer.php 2 days ago requestor.php 2 days ago rule.php 2 days ago
rule.php
120 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 * VikAppointments custom field rules dispatcher.
16 *
17 * @since 1.7
18 */
19 abstract class VAPCustomFieldRule
20 {
21 /**
22 * Creates a new instance for the specified field rule.
23 *
24 * @param string $rule The requested rule.
25 *
26 * @return self A new instance.
27 */
28 final public static function getInstance($rule)
29 {
30 // attempt to load a default rule
31 if (!VAPLoader::import('libraries.customfields.rules.' . $rule))
32 {
33 // unable to find a file for the specified rule
34 throw new Exception(sprintf('Custom field [%s] rule not found', $rule), 404);
35 }
36
37 // create class name
38 $classname = 'VAPCustomFieldRule' . ucfirst($rule);
39
40 if (!class_exists($classname))
41 {
42 // unable to find a class for the specified type
43 throw new Exception(sprintf('Custom field [%s] rule class not found', $classname), 404);
44 }
45
46 // create instance
47 $handler = new $classname();
48
49 if (!$handler instanceof VAPCustomFieldRule)
50 {
51 // the class handler must inherit this class
52 throw new Exception(sprintf('Custom field [%s] rule is not a valid instance', $classname), 404);
53 }
54
55 return $handler;
56 }
57
58 /**
59 * Returns a unique ID for this rule.
60 *
61 * @return string
62 */
63 public function getID()
64 {
65 // create ID from class name
66 return strtolower(preg_replace("/^VAPCustomFieldRule/i", '', get_class($this)));
67 }
68
69 /**
70 * Returns the name of the rule.
71 *
72 * @return string
73 */
74 public function getName()
75 {
76 $id = $this->getID();
77 $key = 'VAPCUSTFIELDRULE' . strtoupper($id);
78
79 // try to translate the given language definition
80 $name = JText::translate($key);
81
82 if ($name === $key)
83 {
84 // translation not found, return plain ID
85 return $id;
86 }
87
88 return $name;
89 }
90
91 /**
92 * Dispatches the field rule.
93 *
94 * @param mixed $value The value of the field set in request.
95 * @param array &$args The array data to fill-in in case of
96 * specific rules (name, e-mail, etc...).
97 * @param mixed $field The custom field object.
98 *
99 * @return void
100 */
101 abstract public function dispatch($value, &$args, $field);
102
103 /**
104 * Renders the field rule.
105 *
106 * @param array &$data An array of display data.
107 * @param mixed $field The custom field object.
108 *
109 * @return string The HTML that will be used in place of the layout
110 * defined by the field. Omit this value to keep using
111 * the default HTML of the field.
112 */
113 public function render(&$data, $field)
114 {
115 // always define this method because the rules
116 // might not have to display anything
117 return '';
118 }
119 }
120