overrides
3 days ago
core.php
3 days ago
dispatcher.php
3 days ago
formbuilder.php
3 days ago
formfield.php
3 days ago
formfieldconstraints.php
3 days ago
index.html
3 days ago
job.php
3 days ago
response.php
3 days ago
formfield.php
605 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 represent an HTML standard input field. |
| 16 | * |
| 17 | * @since 1.5 |
| 18 | * @since 1.7 Renamed from CronFormField. |
| 19 | * |
| 20 | * @see VAPCronFormFieldConstraints |
| 21 | */ |
| 22 | class VAPCronFormField |
| 23 | { |
| 24 | /** |
| 25 | * The name of the field, used to retrieve the value of the setting. |
| 26 | * This value must be unique. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | private $name; |
| 31 | |
| 32 | /** |
| 33 | * The type of the field. |
| 34 | * |
| 35 | * @var int |
| 36 | */ |
| 37 | private $type; |
| 38 | |
| 39 | /** |
| 40 | * The label of the field to display. |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | private $label; |
| 45 | |
| 46 | /** |
| 47 | * An additional label to place next to the input. |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | private $label_2; |
| 52 | |
| 53 | /** |
| 54 | * The object containing all the attributes of the field. |
| 55 | * |
| 56 | * @var CronFormFieldConstraints |
| 57 | */ |
| 58 | private $constraints = null; |
| 59 | |
| 60 | /** |
| 61 | * Indicates whether the field is required or not. |
| 62 | * |
| 63 | * @var boolean |
| 64 | */ |
| 65 | private $required = 1; |
| 66 | |
| 67 | /** |
| 68 | * The associative array containing all the accepted values. |
| 69 | * This attribute is considered only for Dropdown fields. |
| 70 | * |
| 71 | * @var array |
| 72 | */ |
| 73 | private $values = array(); |
| 74 | |
| 75 | /** |
| 76 | * The default value of the field. |
| 77 | * |
| 78 | * @var string |
| 79 | */ |
| 80 | private $default = ''; |
| 81 | |
| 82 | /** |
| 83 | * The construct of the cron form field to initialize the required parameters of this object. |
| 84 | * |
| 85 | * @param string $name The field name. |
| 86 | * @param string $label The field label. |
| 87 | * @param integer $type The field type (default 0: HTML). |
| 88 | * |
| 89 | * @uses setName() |
| 90 | * @uses setLabel() |
| 91 | * @uses setType() |
| 92 | */ |
| 93 | public function __construct($name, $label, $type = 0) |
| 94 | { |
| 95 | $this->setName($name) |
| 96 | ->setLabel($label) |
| 97 | ->setType($type); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Set the name of the field. |
| 102 | * |
| 103 | * @param string $name The name of the field. |
| 104 | * |
| 105 | * @return self Returns this object to support chaining. |
| 106 | */ |
| 107 | public function setName($name) |
| 108 | { |
| 109 | $this->name = $name; |
| 110 | |
| 111 | return $this; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the name of the field. If the name is empty, returns a random name. |
| 116 | * |
| 117 | * @return string The name of the field. |
| 118 | */ |
| 119 | public function getName() |
| 120 | { |
| 121 | return !empty($this->name) ? $this->name : 'input-' . rand(1, 100); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sets the label of the field. |
| 126 | * |
| 127 | * @param string $label The label of the field. |
| 128 | * |
| 129 | * @return self Returns this object to support chaining. |
| 130 | */ |
| 131 | public function setLabel($label) |
| 132 | { |
| 133 | $this->label = $label; |
| 134 | |
| 135 | return $this; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get the label of the field. |
| 140 | * |
| 141 | * @return string The label of the field. |
| 142 | */ |
| 143 | public function getLabel() |
| 144 | { |
| 145 | return $this->label; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets the additional label of the field. |
| 150 | * |
| 151 | * @param string $label The additional label of the field. |
| 152 | * |
| 153 | * @return self Returns this object to support chaining. |
| 154 | */ |
| 155 | public function setSecondaryLabel($label) |
| 156 | { |
| 157 | $this->label_2 = $label; |
| 158 | |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the additional label of the field. |
| 164 | * |
| 165 | * @return string The additional label of the field. |
| 166 | */ |
| 167 | public function getSecondaryLabel() |
| 168 | { |
| 169 | return $this->label_2; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Sets the type of the field. |
| 174 | * |
| 175 | * @param integer $type The type of the field. Values must be between 0 and 5, |
| 176 | * any other value will be revert to 0 (html). |
| 177 | * |
| 178 | * @return self Returns this object to support chaining. |
| 179 | */ |
| 180 | public function setType($type) |
| 181 | { |
| 182 | $this->type = (int) $type; |
| 183 | |
| 184 | return $this; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get the type of the field. |
| 189 | * |
| 190 | * @param boolean $str True to return the type string. |
| 191 | * |
| 192 | * @return mixed The type of the field. |
| 193 | */ |
| 194 | public function getType($str = false) |
| 195 | { |
| 196 | if (!$str) |
| 197 | { |
| 198 | return $this->type; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Fetch HTML tag type. |
| 203 | * |
| 204 | * @since 1.7 |
| 205 | */ |
| 206 | switch ($this->type) |
| 207 | { |
| 208 | case static::INPUT_TEXT: |
| 209 | return 'text'; |
| 210 | |
| 211 | case static::INPUT_NUMBER: |
| 212 | return 'number'; |
| 213 | |
| 214 | case static::INPUT_PASSWORD: |
| 215 | return 'password'; |
| 216 | |
| 217 | case static::TEXTAREA: |
| 218 | return 'textarea'; |
| 219 | |
| 220 | case static::DROPDOWN: |
| 221 | return 'select'; |
| 222 | |
| 223 | case static::SEPARATOR: |
| 224 | return 'separator'; |
| 225 | |
| 226 | case static::EDITOR: |
| 227 | return 'editor'; |
| 228 | |
| 229 | case static::CHECKBOX: |
| 230 | return 'checkbox'; |
| 231 | |
| 232 | default: |
| 233 | return 'html'; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Sets the constraints of the field. |
| 239 | * |
| 240 | * @param mixed The attributes of the field. The object must be a |
| 241 | * CronFormFieldConstraints or NULL |
| 242 | * |
| 243 | * @return self Returns this object to support chaining. |
| 244 | */ |
| 245 | public function setConstraints($constraints) |
| 246 | { |
| 247 | $this->constraints = $constraints; |
| 248 | |
| 249 | return $this; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Inserts a new attribute in the constraints list of this field. |
| 254 | * |
| 255 | * @param string $key The standard name of the attribute. |
| 256 | * @param string $val An acceptable value of the attribute. |
| 257 | * |
| 258 | * @return self Returns this object to support chaining. |
| 259 | */ |
| 260 | public function addConstraint($key, $val) |
| 261 | { |
| 262 | // if the constaints object is null, instantiate it |
| 263 | if ($this->constraints === null) |
| 264 | { |
| 265 | $this->constraints = new CronFormFieldConstraints(); |
| 266 | } |
| 267 | |
| 268 | if ($key != 'class') |
| 269 | { |
| 270 | $this->constraints->add($key, $val); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | // For class attribute we may concat the value to the existing one. |
| 275 | // Use an empty space for the concatenation. |
| 276 | $this->constraints->add($key, $val, ' '); |
| 277 | } |
| 278 | |
| 279 | return $this; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Get the constraints of the field. |
| 284 | * |
| 285 | * @return CronFormFieldConstraints The constraints object or null. |
| 286 | */ |
| 287 | public function getConstraints() |
| 288 | { |
| 289 | return $this->constraints; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Sets whether the field is required or not. |
| 294 | * |
| 295 | * @param boolean $required The status of the field. |
| 296 | * |
| 297 | * @return self Returns this object to support chaining. |
| 298 | */ |
| 299 | public function setRequired($required) |
| 300 | { |
| 301 | $this->required = $required; |
| 302 | |
| 303 | return $this; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Returns true if the field is required, otherwise false. |
| 308 | * |
| 309 | * @return boolean If the field is required. |
| 310 | */ |
| 311 | public function isRequired() |
| 312 | { |
| 313 | return $this->required; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Sets the default value of the field. |
| 318 | * |
| 319 | * @param string $default The initial value of the field. |
| 320 | * |
| 321 | * @return self Returns this object to support chaining. |
| 322 | * |
| 323 | * @uses isRequired() |
| 324 | */ |
| 325 | public function setDefaultValue($default) |
| 326 | { |
| 327 | // if the default is empty and the field is optional |
| 328 | // the default (value) of the field can be empty |
| 329 | // a required field cannot have empty values. |
| 330 | if (strlen($default) || !$this->isRequired()) |
| 331 | { |
| 332 | $this->default = $default; |
| 333 | } |
| 334 | |
| 335 | return $this; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Get the default value of the field. |
| 340 | * |
| 341 | * @return string The default value of the field. |
| 342 | */ |
| 343 | public function getDefaultValue() |
| 344 | { |
| 345 | return $this->default; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Sets all the possible values of a dropdown field. |
| 350 | * |
| 351 | * @param array $values The values to insert in a dropdown. |
| 352 | * The key of a row will be used as value |
| 353 | * and the content will be used as label. |
| 354 | * |
| 355 | * @return self Returns this object to support chaining. |
| 356 | */ |
| 357 | public function setListValues($values) |
| 358 | { |
| 359 | $this->values = $values; |
| 360 | |
| 361 | return $this; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Returns the list with all the values of the dropdown field. |
| 366 | * |
| 367 | * @return array The list of the values of the field. |
| 368 | */ |
| 369 | public function getListValues() |
| 370 | { |
| 371 | return $this->values; |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * Sets the type of the field as HTML: 0. |
| 376 | * |
| 377 | * @return self Returns this object to support chaining. |
| 378 | * |
| 379 | * @uses setType() |
| 380 | */ |
| 381 | public function isHtml() |
| 382 | { |
| 383 | $this->setType(self::HTML); |
| 384 | |
| 385 | return $this; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Sets the type of the field as Input Text: 1. |
| 390 | * |
| 391 | * @return self Returns this object to support chaining. |
| 392 | * |
| 393 | * @uses setType() |
| 394 | */ |
| 395 | public function isInputText() |
| 396 | { |
| 397 | $this->setType(self::INPUT_TEXT); |
| 398 | |
| 399 | return $this; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Sets the type of the field as Input Number: 2. |
| 404 | * |
| 405 | * @return self Returns this object to support chaining. |
| 406 | * |
| 407 | * @uses setType() |
| 408 | */ |
| 409 | public function isInputNumber() |
| 410 | { |
| 411 | $this->setType(self::INPUT_NUMBER); |
| 412 | |
| 413 | return $this; |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Sets the type of the field as Input Password: 3. |
| 418 | * |
| 419 | * @return self Returns this object to support chaining. |
| 420 | * |
| 421 | * @uses setType() |
| 422 | */ |
| 423 | public function isInputPassword() |
| 424 | { |
| 425 | $this->setType(self::INPUT_PASSWORD); |
| 426 | |
| 427 | return $this; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Sets the type of the field as Textarea: 4. |
| 432 | * |
| 433 | * @return self Returns this object to support chaining. |
| 434 | * |
| 435 | * @uses setType() |
| 436 | */ |
| 437 | public function isTextarea() |
| 438 | { |
| 439 | $this->setType(self::TEXTAREA); |
| 440 | |
| 441 | return $this; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Sets the type of the field as Select: 5. |
| 446 | * |
| 447 | * @return self Returns this object to support chaining. |
| 448 | * |
| 449 | * @uses setType() |
| 450 | */ |
| 451 | public function isDropdown() |
| 452 | { |
| 453 | $this->setType(self::DROPDOWN); |
| 454 | |
| 455 | return $this; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Sets the type of the field as Separator: 6. |
| 460 | * |
| 461 | * @return self Returns this object to support chaining. |
| 462 | * |
| 463 | * @uses setType() |
| 464 | */ |
| 465 | public function isSeparator() |
| 466 | { |
| 467 | $this->setType(self::SEPARATOR); |
| 468 | |
| 469 | return $this; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Sets the type of the field as advanced Editor: 7. |
| 474 | * |
| 475 | * @return self Returns this object to support chaining. |
| 476 | * |
| 477 | * @uses setType() |
| 478 | */ |
| 479 | public function isEditor() |
| 480 | { |
| 481 | $this->setType(self::EDITOR); |
| 482 | |
| 483 | return $this; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Sets the type of the field as checkbox: 8. |
| 488 | * |
| 489 | * @return self Returns this object to support chaining. |
| 490 | * |
| 491 | * @uses setType() |
| 492 | * |
| 493 | * @since 1.7.5 |
| 494 | */ |
| 495 | public function isCheckbox() |
| 496 | { |
| 497 | $this->setType(self::CHECKBOX); |
| 498 | |
| 499 | return $this; |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Returns the fields attributes as array. |
| 504 | * |
| 505 | * @return array |
| 506 | * |
| 507 | * @since 1.7 |
| 508 | */ |
| 509 | public function toArray() |
| 510 | { |
| 511 | // create base attributes |
| 512 | $data = array( |
| 513 | 'type' => $this->getType(true), |
| 514 | 'name' => $this->getName(), |
| 515 | 'label' => $this->getLabel(), |
| 516 | 'description' => $this->getSecondaryLabel(), |
| 517 | 'required' => $this->isRequired(), |
| 518 | 'value' => $this->getDefaultValue(), |
| 519 | 'options' => $this->getListValues(), |
| 520 | ); |
| 521 | |
| 522 | // include constraints, if any |
| 523 | if ($this->getConstraints()) |
| 524 | { |
| 525 | foreach ($this->getConstraints() as $k => $v) |
| 526 | { |
| 527 | $data[$k] = $v; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | return $data; |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * The identifier for html fields type. |
| 536 | * |
| 537 | * @var integer |
| 538 | */ |
| 539 | const HTML = 0; |
| 540 | |
| 541 | /** |
| 542 | * The identifier for text fields type. |
| 543 | * |
| 544 | * @var integer |
| 545 | */ |
| 546 | const INPUT_TEXT = 1; |
| 547 | |
| 548 | /** |
| 549 | * The identifier for number fields type. |
| 550 | * |
| 551 | * @var integer |
| 552 | */ |
| 553 | const INPUT_NUMBER = 2; |
| 554 | |
| 555 | /** |
| 556 | * The identifier for password fields type. |
| 557 | * |
| 558 | * @var integer |
| 559 | */ |
| 560 | const INPUT_PASSWORD = 3; |
| 561 | |
| 562 | /** |
| 563 | * The identifier for textarea fields type. |
| 564 | * |
| 565 | * @var integer |
| 566 | */ |
| 567 | const TEXTAREA = 4; |
| 568 | |
| 569 | /** |
| 570 | * The identifier for dropdown fields type. |
| 571 | * |
| 572 | * @var integer |
| 573 | */ |
| 574 | const DROPDOWN = 5; |
| 575 | |
| 576 | /** |
| 577 | * The identifier for separator fields type. |
| 578 | * |
| 579 | * @var integer |
| 580 | */ |
| 581 | const SEPARATOR = 6; |
| 582 | |
| 583 | /** |
| 584 | * The identifier for editor fields type. |
| 585 | * |
| 586 | * @var integer |
| 587 | */ |
| 588 | const EDITOR = 7; |
| 589 | |
| 590 | /** |
| 591 | * The identifier for checkbox fields type. |
| 592 | * |
| 593 | * @var integer |
| 594 | * @since 1.7.5 |
| 595 | */ |
| 596 | const CHECKBOX = 8; |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Register a class alias for backward compatibility. |
| 601 | * |
| 602 | * @deprecated 1.8 |
| 603 | */ |
| 604 | class_alias('VAPCronFormField', 'CronFormField'); |
| 605 |