PluginProbe
Disable Comments & Delete All Comments / trunk
Disable Comments & Delete All Comments vtrunk
1.3.3 1.3.2 trunk 1.0.0 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.3.0 1.3.1
comments-plus / libs / factory / forms / includes / form-element.class.php

form-element.class.php in Disable Comments & Delete All Comments trunk, at libs/factory/forms/includes/form-element.class.php

424 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file contains the base class for all form element (controls, holders).
4 *
5 * @author Alex Kovalev <alex.kovalevv@gmail.com>
6 * @copyright (c) 2018, Webcraftic Ltd
7 *
8 * @package factory-forms
9 * @since 1.0.0
10 */
11
12 // Exit if accessed directly
13 if( !defined('ABSPATH') ) {
14 exit;
15 }
16
17 if( !class_exists('Wbcr_FactoryForms480_FormElement') ) {
18
19 /**
20 * The base class for all form element (controls, holders).
21 *
22 * Provides several methods to build html markup of an element.
23 *
24 * @since 1.0.0
25 */
26 abstract class Wbcr_FactoryForms480_FormElement {
27
28 /**
29 * A type of an elemnt.
30 *
31 * @since 1.0.0
32 * @var boolean
33 */
34 protected $type = null;
35
36 /**
37 * An html attribute builder.
38 *
39 * @since 1.0.0
40 * @var Wbcr_FactoryForms480_HtmlAttributeBuilder
41 */
42 private $html_builder;
43
44 /**
45 * Element options.
46 *
47 * @since 1.0.0
48 * @var array
49 */
50 public $options = array();
51
52 /**
53 * A parent form.
54 *
55 * @since 1.0.0
56 * @var Wbcr_FactoryForms480_Form
57 */
58 protected $form;
59
60 /**
61 * A form layout.
62 *
63 * @since 1.0.0
64 * @var Wbcr_FactoryForms480_FormLayout
65 */
66 protected $layout;
67
68 /**
69 * Is this element a control?
70 *
71 * @since 1.0.0
72 * @var bool
73 */
74 public $is_control = false;
75
76 /**
77 * Is this element a control holder?
78 *
79 * @since 1.0.0
80 * @var bool
81 */
82 public $is_holder = false;
83
84 /**
85 * Is this element a custom form element?
86 *
87 * @since 1.0.0
88 * @var bool
89 */
90 public $is_custom = false;
91
92 /**
93 * Creates a new instance of a form element.
94 *
95 * @since 1.0.0
96 * @param mixed[] $options A holder options.
97 * @param Wbcr_FactoryForms480_Form $form A parent form.
98 */
99 public function __construct($options, $form)
100 {
101 $this->options = $options;
102 $this->form = $form;
103 $this->layout = $form->layout;
104
105 $this->html_builder = new Wbcr_FactoryForms480_HtmlAttributeBuilder();
106
107 if( isset($this->options['cssClass']) ) {
108 $this->html_builder->addCssClass($this->options['cssClass']);
109 }
110
111 if( isset($this->options['htmlData']) ) {
112 foreach($this->options['htmlData'] as $data_key => $data_value) {
113 $this->html_builder->addHtmlData($data_key, $data_value);
114 }
115 }
116
117 if( isset($this->options['htmlAttrs']) ) {
118 foreach($this->options['htmlAttrs'] as $attr_key => $attr_value) {
119 $this->html_builder->addHtmlAttr($attr_key, $attr_value);
120 }
121 }
122
123 $this->addCssClass('factory-' . $this->type);
124 }
125
126
127 /**
128 * Sets options for the control.
129 *
130 * @since 1.0.0
131 * @param mixed[] $options
132 * @return void
133 */
134 public function setOptions($options)
135 {
136 $this->options = $options;
137 }
138
139 /**
140 * Gets options of the control.
141 *
142 * @since 1.0.0
143 * @return mixed[] $options
144 */
145 public function getOptions()
146 {
147 return $this->options;
148 }
149
150 /**
151 * Sets a new value for a given option.
152 *
153 * @since 1.0.0
154 * @param string $name An option name to set.
155 * @param mixed $value A value to set.
156 * @return void
157 */
158 public function setOption($name, $value)
159 {
160 $this->options[$name] = $value;
161 }
162
163 /**
164 * Gets an option value or default.
165 *
166 * @since 1.0.0
167 * @param string $name An option name to get.
168 * @param mixed $default A default value
169 * @return mixed|null
170 */
171 public function getOption($name, $default = null)
172 {
173 return isset($this->options[$name])
174 ? $this->options[$name]
175 : $default;
176 }
177
178 /**
179 * Prints an option value or default.
180 *
181 * @since 1.0.0
182 * @param string $name An option name to get.
183 * @param mixed $default A default value
184 * @return void
185 */
186 public function option($name, $default = null)
187 {
188 $value = $this->getOption($name, $default);
189 echo $value;
190 }
191
192 /**
193 * Adds a new CSS class for the element.
194 *
195 * @since 1.0.0
196 * @return void
197 */
198 public function addCssClass($class)
199 {
200 $this->html_builder->addCssClass($class);
201 }
202
203 /**
204 * Prints CSS classes of the element.
205 *
206 * @since 1.0.0
207 * @return void
208 */
209 protected function cssClass()
210 {
211 $this->html_builder->printCssClass();
212 }
213
214 /**
215 * Adds a new html attribute.
216 *
217 * @since 1.0.0
218 * @param string $data_key
219 * @param string $data_value
220 */
221 protected function addHtmlData($data_key, $data_value)
222 {
223 $this->html_builder->addHtmlData($data_key, $data_value);
224 }
225
226 /**
227 * Adds a new html attribute.
228 *
229 * @since 1.0.0
230 * @param string $attr_name
231 * @param string $attr_value
232 * @return void
233 */
234 protected function addHtmlAttr($attr_name, $attr_value)
235 {
236 $this->html_builder->addHtmlAttr($attr_name, $attr_value);
237 }
238
239 /**
240 * Prints all html attributes, including css classes and data.
241 *
242 * @since 1.0.0
243 * @return void
244 */
245 protected function attrs()
246 {
247 $this->html_builder->printAttrs();
248 }
249
250 /**
251 * Returns an element title.
252 *
253 * @since 1.0.0
254 * @return string|bool
255 */
256 public function getTitle()
257 {
258 if( isset($this->options['title']) ) {
259 return $this->options['title'];
260 }
261
262 return false;
263 }
264
265 /**
266 * Returns true if an element has title.
267 *
268 * @since 1.0.0
269 * @return bool
270 */
271 public function hasTitle()
272 {
273 $title = $this->getTitle();
274
275 return !empty($title);
276 }
277
278 /**
279 * Prints an element title.
280 *
281 * @since 1.0.0
282 * @return void
283 */
284 public function title()
285 {
286 echo $this->getTitle();
287 }
288
289 /**
290 * Returns an element hint.
291 *
292 * @since 1.0.0
293 * @return string
294 */
295 public function getHint()
296 {
297 if( isset($this->options['hint']) ) {
298 return $this->options['hint'];
299 }
300
301 return false;
302 }
303
304 /**
305 * Returns true if an element has hint.
306 *
307 * @since 1.0.0
308 * @return bool
309 */
310 public function hasHint()
311 {
312 $hint = $this->getHint();
313
314 return !empty($hint);
315 }
316
317 /**
318 * Prints an element hint.
319 *
320 * @since 1.0.0
321 * @return void
322 */
323 public function hint($esc = false)
324 {
325 echo $esc
326 ? esc_html($this->getHint())
327 : $this->getHint();
328 }
329
330 /**
331 * Returns an element name.
332 *
333 * @since 1.0.0
334 * @return string
335 */
336 public function getName()
337 {
338
339 if( empty($this->options['name']) && !empty($this->options['title']) ) {
340 $this->options['name'] = str_replace(' ', '-', $this->options['title']);
341 $this->options['name'] = strtolower($this->options['name']);
342 }
343
344 if( !isset($this->options['name']) ) {
345 $this->options['name'] = $this->type . '-' . rand();
346 }
347
348 return $this->options['name'];
349 }
350
351 /**
352 * Prints an element name.
353 *
354 * @since 1.0.0
355 * @return void
356 */
357 public function name()
358 {
359 echo $this->getName();
360 }
361
362 /**
363 * Returns a form name
364 *
365 * @since 1.0.0
366 * @return string
367 */
368 public function getFormName()
369 {
370 return $this->form->name;
371 }
372
373 /**
374 * Returns an element type.
375 *
376 * @since 1.0.0
377 * @return string
378 */
379 public function getType()
380 {
381 return $this->type;
382 }
383
384 /**
385 * Returns an element icon.
386 *
387 * @since 1.0.0
388 * @return string
389 */
390 public function getIcon()
391 {
392 if( isset($this->options['icon']) ) {
393 return $this->options['icon'];
394 }
395
396 return false;
397 }
398
399 /**
400 * Returns true if an element has a icon.
401 *
402 * @since 1.0.0
403 * @return bool
404 */
405 public function hasIcon()
406 {
407 $icon = $this->getIcon();
408
409 return !empty($icon);
410 }
411
412 /**
413 * Prints an element icon.
414 *
415 * @since 1.0.0
416 * @return void
417 */
418 public function icon()
419 {
420 echo $this->getIcon();
421 }
422 }
423 }
424