radio.php
136 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.widget.input'); |
| 15 | |
| 16 | /** |
| 17 | * Base abstract class to implement a radio button. |
| 18 | * |
| 19 | * @since 1.6 |
| 20 | */ |
| 21 | abstract class UIRadio implements UIInput |
| 22 | { |
| 23 | /** |
| 24 | * The name of the input. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | private $name; |
| 29 | |
| 30 | /** |
| 31 | * The possible radio buttons. |
| 32 | * |
| 33 | * @var array (of objects) |
| 34 | */ |
| 35 | private $elements; |
| 36 | |
| 37 | /** |
| 38 | * An array of options. |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | private $options; |
| 43 | |
| 44 | /** |
| 45 | * Class constructor. |
| 46 | * |
| 47 | * @param string $name |
| 48 | * @param array $elements |
| 49 | * @param array $options |
| 50 | */ |
| 51 | public function __construct($name, array $elements = array(), array $options = array()) |
| 52 | { |
| 53 | $this->name = $name; |
| 54 | $this->elements = array(); |
| 55 | $this->options = $options; |
| 56 | |
| 57 | foreach ($elements as $elem) |
| 58 | { |
| 59 | $this->addElement($elem); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the name of the input. |
| 65 | * |
| 66 | * @return string |
| 67 | */ |
| 68 | public function getName() |
| 69 | { |
| 70 | return $this->name; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the possible radio buttons. |
| 75 | * |
| 76 | * @return array |
| 77 | */ |
| 78 | public function getElements() |
| 79 | { |
| 80 | return $this->elements; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get an array of options. |
| 85 | * |
| 86 | * @return array |
| 87 | */ |
| 88 | public function getOptions() |
| 89 | { |
| 90 | return $this->options; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Insert a radio button element. |
| 95 | * |
| 96 | * @param object $element The radio button element. |
| 97 | * |
| 98 | * @return UIRadio This object to support chaining. |
| 99 | * |
| 100 | * @uses UIRadio::bind() Adapt the given element. |
| 101 | */ |
| 102 | public function addElement($element) |
| 103 | { |
| 104 | if (is_object($element)) |
| 105 | { |
| 106 | $this->elements[] = $this->bind($element); |
| 107 | } |
| 108 | |
| 109 | return $this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Adapt the given element. |
| 114 | * |
| 115 | * Children classes can override this method to adapt |
| 116 | * the element depending on their requirements. |
| 117 | * |
| 118 | * @param object $element The radio button element. |
| 119 | * |
| 120 | * @return object The adapted element. |
| 121 | */ |
| 122 | protected function bind($element) |
| 123 | { |
| 124 | // do something here... |
| 125 | |
| 126 | return $element; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Call this method to build and return the HTML of the input. |
| 131 | * |
| 132 | * @return string The input HTML. |
| 133 | */ |
| 134 | public abstract function display(); |
| 135 | } |
| 136 |