PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / customfields / requestor.php
vikappointments / site / helpers / libraries / customfields Last commit date
rules 1 month ago types 1 month ago control.php 1 month ago emptyloader.php 1 month ago factory.php 1 month ago field.php 1 month ago index.html 1 month ago loader.php 1 month ago renderer.php 1 month ago requestor.php 1 month ago rule.php 1 month ago
requestor.php
214 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.customfields.factory');
15 VAPLoader::import('libraries.customfields.loader');
16
17 /**
18 * VikAppointments custom fields requestor class.
19 *
20 * @since 1.7
21 */
22 abstract class VAPCustomFieldsRequestor
23 {
24 /**
25 * Returns the custom fields values specified in the REQUEST.
26 *
27 * @param mixed $fields The custom fields list to check for.
28 * If the list is not an array, the method will load
29 * all the custom fields that belong to the specified group.
30 * @param array &$args The array data to fill-in in case of specific rules (name, e-mail, etc...).
31 * @param boolean $strict True to raise an error when a mandatory field is missing.
32 *
33 * @return array The lookup array containing the values of the custom fields.
34 *
35 * @throws Exception When a custom field is not valid.
36 */
37 public static function loadForm($fields = 0, ?array &$args = null, $strict = true)
38 {
39 $lookup = array();
40
41 // if not an array, get the fields from the DB using the specified section
42 if (!is_array($fields))
43 {
44 // get custom fields loader
45 $loader = VAPCustomFieldsLoader::getInstance();
46
47 if ($fields == 1)
48 {
49 // load custom fields for the employees
50 $loader->employees();
51 }
52 else
53 {
54 // otherwise load default custom fields (for customers)
55 $loader->customers();
56 }
57
58 $fields = $loader->fetch();
59 }
60
61 // return an empty list in case there are no published fields
62 if (!count($fields))
63 {
64 return $lookup;
65 }
66
67 if (is_null($args))
68 {
69 $args = array();
70 }
71
72 // if not exists, declare 'uploads' property to avoid warnings
73 if (!isset($args['uploads']))
74 {
75 $args['uploads'] = array();
76 }
77
78 foreach ($fields as $cf)
79 {
80 // create field instance
81 $field = VAPCustomField::getInstance($cf);
82
83 // get form field name
84 $name = $field->getName();
85
86 try
87 {
88 // extract custom field from request
89 $lookup[$name] = $field->save($args);
90
91 // dispatch rule assigned to the custom field, if any
92 VAPCustomFieldsFactory::dispatchRule($field, $lookup[$name], $args);
93 }
94 catch (Exception $e)
95 {
96 if ($strict)
97 {
98 // propagate exception
99 throw $e;
100 }
101
102 if (!isset($lookup[$name]))
103 {
104 // register field with an empty string in order
105 // to have it filled in within the resulting array
106 $lookup[$name] = '';
107 }
108 }
109 }
110
111 return $lookup;
112 }
113
114 /**
115 * Returns the custom fields values specified in the REQUEST for the current attendee number.
116 *
117 * @param integer $attendee The attendee number (starts from 1).
118 * @param mixed $fields The custom fields list to check for.
119 * If the list is not an array, the method will load
120 * all the custom fields that belong to the specified group.
121 * @param array &$args The array data to fill-in in case of specific rules (name, e-mail, etc...).
122 * @param boolean $strict True to raise an error when a mandatory field is missing.
123 *
124 * @return array The lookup array containing the values of the custom fields.
125 *
126 * @throws Exception When a custom field is not valid.
127 */
128 public static function loadFormAttendee($attendee, $fields = 0, ?array &$args = null, $strict = true)
129 {
130 $lookup = array();
131
132 // if not an array, get the fields from the DB using the specified section
133 if (!is_array($fields))
134 {
135 // get custom fields loader
136 $loader = VAPCustomFieldsLoader::getInstance();
137
138 if ($fields == 1)
139 {
140 // load custom fields for the employees
141 $loader->employees();
142 }
143 else
144 {
145 // otherwise load default custom fields (for customers)
146 $loader->customers();
147 }
148
149 $fields = $loader->fetch();
150 }
151
152 // return an empty list in case there are no published fields
153 if (!count($fields))
154 {
155 return $lookup;
156 }
157
158 if (is_null($args))
159 {
160 $args = array();
161 }
162
163 // if not exists, declare 'uploads' property to avoid warnings
164 if (!isset($args['uploads']))
165 {
166 $args['uploads'] = array();
167 }
168
169 foreach ($fields as $cf)
170 {
171 // create field instance
172 $field = VAPCustomField::getInstance($cf);
173
174 // use the custom field for attendee form
175 $field->set('attendee', (int) $attendee);
176
177 if (!$field->get('repeat'))
178 {
179 // skip field in case it shouldn't be repeated
180 continue;
181 }
182
183 // get form field name
184 $name = $field->getName();
185
186 try
187 {
188 // extract custom field from request
189 $lookup[$name] = $field->save($args);
190
191 // dispatch rule assigned to the custom field, if any
192 VAPCustomFieldsFactory::dispatchRule($field, $lookup[$name], $args);
193 }
194 catch (Exception $e)
195 {
196 if ($strict)
197 {
198 // propagate exception
199 throw $e;
200 }
201
202 if (!isset($lookup[$name]))
203 {
204 // register field with an empty string in order
205 // to have it filled in within the resulting array
206 $lookup[$name] = '';
207 }
208 }
209 }
210
211 return $lookup;
212 }
213 }
214