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 / cron / formbuilder.php
vikappointments / site / helpers / libraries / cron Last commit date
overrides 2 days ago core.php 2 days ago dispatcher.php 2 days ago formbuilder.php 2 days ago formfield.php 2 days ago formfieldconstraints.php 2 days ago index.html 2 days ago job.php 2 days ago response.php 2 days ago
formbuilder.php
339 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 * Class used to build a form with a list of cron form fields.
16 *
17 * @since 1.5
18 * @since 1.7 Renamed from CronFormBuilder.
19 *
20 * @see VAPCronFormField
21 * @see VAPCronFormFieldConstraints
22 */
23 class VAPCronFormBuilder
24 {
25 /**
26 * The list of all the CronFormField objects, required to build the settings form.
27 *
28 * @var array
29 */
30 private $fields = array();
31
32 /**
33 * A prefix to extend the CSS classes inside the form.
34 * An empty value may cause warnings.
35 *
36 * @var string
37 */
38 protected $classname = 'cronform';
39
40 /**
41 * The construct of the cron form builder to initialize the required parameters of this object.
42 *
43 * @param array $fields The fields to push into the form.
44 *
45 * @uses setFields()
46 */
47 public function __construct(array $fields = array())
48 {
49 $this->setFields($fields);
50 }
51
52 /**
53 * Sets all the fields in the list.
54 *
55 * @param array $fields The list of CronFormField objects.
56 */
57 public function setFields(array $fields)
58 {
59 $this->fields = $fields;
60 }
61
62 /**
63 * Returns the list containing all the CronFormField objects.
64 *
65 * @return array The list of CronFormField objects.
66 */
67 public function getFields()
68 {
69 return $this->fields;
70 }
71
72 /**
73 * Overwrite the CSS class prefix.
74 *
75 * @param string $classname The new CSS class prefix to use.
76 */
77 public function setClassname($classname)
78 {
79 $this->classname = $classname;
80 }
81
82 /**
83 * Builds the settings form based on the specified fields.
84 * The form is returned as string, nothing is echoed inside this function.
85 *
86 * @param array $args The associative array containing the
87 * stored values of the settings.
88 *
89 * @return string The Html structure of the form.
90 *
91 * @uses buildInputText Renders the html of text inputs.
92 * @uses buildInputNumber Renders the html of number inputs.
93 * @uses buildInputPassword Renders the html of password inputs.
94 * @uses buildTextarea Renders the html of textareas.
95 * @uses buildDropdown Renders the html of dropdowns.
96 * @uses buildHtml Renders the html of custom HTML elements.
97 * @uses wrapField Renders the html structure to wrap the a single field.
98 */
99 public function build($args = array())
100 {
101 $html = "";
102
103 foreach ($this->fields as $field)
104 {
105 // if the type of the field is not HTML and the field has a value in the list,
106 // insert the new value into the object as default
107 if ($field->getType() != VAPCronFormField::HTML && array_key_exists($field->getName(), $args))
108 {
109 $field->setDefaultValue($args[$field->getName()]);
110 }
111
112 // proceed only if the bind() method returns true
113 if ($this->bind($field))
114 {
115 $app = '';
116
117 if ($field->getType() == VAPCronFormField::INPUT_TEXT)
118 {
119 $app = $this->buildInputText($field);
120 }
121 else if ($field->getType() == VAPCronFormField::INPUT_NUMBER)
122 {
123 $app = $this->buildInputNumber($field);
124 }
125 else if ($field->getType() == VAPCronFormField::INPUT_PASSWORD)
126 {
127 $app = $this->buildInputPassword($field);
128 }
129 else if ($field->getType() == VAPCronFormField::TEXTAREA)
130 {
131 $app = $this->buildTextarea($field);
132 }
133 else if ($field->getType() == VAPCronFormField::DROPDOWN)
134 {
135 $app = $this->buildDropdown($field);
136 }
137 else if ($field->getType() == VAPCronFormField::EDITOR)
138 {
139 $app = $this->buildEditor($field);
140 }
141 else
142 {
143 $app = $this->buildHtml($field);
144 }
145
146 // wrap the generated html within the structure of the field
147 $html .= $this->wrapField($app, $field);
148 }
149 }
150
151 return $html;
152 }
153
154 /**
155 * Returns the html of field wrapped with the following structure:
156 * [
157 * [LABEL]
158 * [$html]
159 * ]
160 *
161 * @param string $html The html of the field to wrap.
162 * @param CronFormField $f The CronFormField object containing the info of the field.
163 *
164 * @return string The html of the field.
165 */
166 protected function wrapField($html, $f)
167 {
168 return '<div class="'.$this->classname.'-field">'.
169 (strlen($f->getLabel()) > 0 ?
170 '<div class="'.$this->classname.'"-field-label>
171 <label for="'.$this->classname.'-'.$f->getName().'"><strong>'.$f->getLabel().($f->isRequired() ? '*' : '').':</strong></label>
172 </div>' : '').
173 '<div class="'.$this->classname.'"-field-control>'.$html.'</div>'.
174 ( strlen($f->getSecondaryLabel()) > 0 ? '<div class="'.$this->classname.'-field-sndlabel">'.$f->getSecondaryLabel().'</div>' : '').
175 '</div>';
176 }
177
178 /**
179 * Method used to bind data before building the field.
180 *
181 * @param CronFormField $f The CronFormField object containing the info of the field.
182 *
183 * @return boolean True to start building, otherwise false.
184 */
185 protected function bind(&$f)
186 {
187 // do something here
188
189 return true;
190 }
191
192 /**
193 * Builds the html structure of a Input Text field.
194 *
195 * @param CronFormField $f The CronFormField object containing the info of the field.
196 *
197 * @return string The html structure of the field.
198 *
199 * @uses buildConstraints()
200 */
201 protected function buildInputText($f)
202 {
203 return '<input type="text" name="'.$f->getName().'" id="'.$this->classname.'-'.$f->getName().'" value="'.$f->getDefaultValue().'" '.$this->buildConstraints($f->getConstraints()).'/>';
204 }
205
206 /**
207 * Builds the html structure of a Input Number field.
208 *
209 * @param CronFormField $f The CronFormField object containing the info of the field.
210 *
211 * @return string The html structure of the field.
212 *
213 * @uses buildConstraints()
214 */
215 protected function buildInputNumber($f)
216 {
217 return '<input type="number" name="'.$f->getName().'" id="'.$this->classname.'-'.$f->getName().'" value="'.$f->getDefaultValue().'" '.$this->buildConstraints($f->getConstraints()).'/>';
218 }
219
220 /**
221 * Builds the html structure of a Input Password field.
222 *
223 * @param CronFormField $f The CronFormField object containing the info of the field.
224 *
225 * @return string The html structure of the field.
226 *
227 * @uses buildConstraints()
228 */
229 protected function buildInputPassword($f)
230 {
231 return '<input type="password" name="'.$f->getName().'" id="'.$this->classname.'-'.$f->getName().'" value="'.$f->getDefaultValue().'" '.$this->buildConstraints($f->getConstraints()).'/>';
232 }
233
234 /**
235 * Builds the html structure of a Textarea field.
236 *
237 * @param CronFormField $f The CronFormField object containing the info of the field.
238 *
239 * @return string The html structure of the field.
240 *
241 * @uses buildConstraints()
242 */
243 protected function buildTextarea($f)
244 {
245 return '<textarea name="'.$f->getName().'" id="'.$this->classname.'-'.$f->getName().'" '.$this->buildConstraints($f->getConstraints()).'>'.$f->getDefaultValue().'</textarea>';
246 }
247
248 /**
249 * Builds the html structure of a Select field.
250 *
251 * @param CronFormField $f The CronFormField object containing the info of the field.
252 *
253 * @return string The html structure of the field.
254 *
255 * @uses buildConstraints()
256 */
257 protected function buildDropdown($f)
258 {
259 $str = '<select name="'.$f->getName().'" id="'.$this->classname.'-'.$f->getName().'" '.$this->buildConstraints($f->getConstraints()).'>';
260
261 foreach ($f->getListValues() as $key => $val)
262 {
263 $str .= '<option value="'.$key.'" '.($key == $f->getDefaultValue() ? 'selected="selected"' : '').'>'.$val.'</option>';
264 }
265
266 return $str . '</select>';
267 }
268
269 /**
270 * Builds the html structure of a WYSYWYG editor field.
271 *
272 * @param CronFormField $f The CronFormField object containing the info of the field.
273 *
274 * @return string The html structure of the field.
275 *
276 * @uses buildConstraints()
277 */
278 protected function buildEditor($f)
279 {
280 $attributes = $f->getConstraints();
281
282 return VAPApplication::getInstance()->getEditor()->display(
283 $f->getName(),
284 $f->getDefaultValue(),
285 $attributes->get('width', 400),
286 $attributes->get('height', 200),
287 $attributes->get('rows', 30),
288 $attributes->get('cols', 30),
289 $attributes->get('buttons', false)
290 );
291 }
292
293 /**
294 * Builds the html structure of a HTML field.
295 *
296 * @param CronFormField $f The CronFormField object containing the info of the field.
297 *
298 * @return string The html structure of the field.
299 */
300 protected function buildHtml($f)
301 {
302 return $f->getDefaultValue();
303 }
304
305 /**
306 * Returns all the constraints concatenated with a space.
307 *
308 * @param object $constraints The CronFormFieldConstraints object.
309 *
310 * @return string The attributes string.
311 */
312 protected function buildConstraints($constraints)
313 {
314 $str = '';
315
316 $protected = array('name', 'value', 'id', 'type');
317
318 if ($constraints !== null)
319 {
320 foreach ($constraints->getAttributes() as $key => $val)
321 {
322 if (!in_array($key, $protected))
323 {
324 $str .= "$key=\"$val\" ";
325 }
326 }
327 }
328
329 return $str;
330 }
331 }
332
333 /**
334 * Register a class alias for backward compatibility.
335 *
336 * @deprecated 1.8
337 */
338 class_alias('VAPCronFormBuilder', 'CronFormBuilder');
339