1 | <?php |
||
11 | class DropdownElement extends InputElement { |
||
12 | |||
13 | /** @var OptGroup */ |
||
14 | protected $options = null; |
||
15 | |||
16 | /** @var array OptGroup[] */ |
||
17 | protected $optGroups = array(); |
||
18 | |||
19 | protected $value = ''; |
||
20 | |||
21 | /** |
||
22 | * @param string $name The name of this form element |
||
23 | * @param string $options The available options |
||
24 | * @param string $label The label text for this element (will be autoescaped) |
||
25 | */ |
||
26 | public function __construct($name, $options, $label = '') { |
||
27 | parent::__construct('dropdown', $name, $label); |
||
28 | $this->rmattr('type'); |
||
29 | $this->options = new OptGroup(null, $options); |
||
30 | $this->val(''); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param $label |
||
35 | * @param $options |
||
36 | * @return OptGroup |
||
37 | * @throws \Exception |
||
38 | */ |
||
39 | public function addOptGroup($label, $options) { |
||
46 | |||
47 | /** |
||
48 | * Set or get the optgroups of an Dropdown-Element. |
||
49 | * |
||
50 | * optgroups have to be given as associative array |
||
51 | * * the key being the label of the group |
||
52 | * * the value being an array of options as defined in @see OptGroup::options() |
||
53 | * |
||
54 | * @param null|array $optGroups |
||
55 | * @return array |
||
56 | */ |
||
57 | public function optGroups($optGroups = null) { |
||
69 | |||
70 | /** |
||
71 | * Get or set the options of the Dropdown |
||
72 | * |
||
73 | * Options can be given as associative array (value => label) or as an |
||
74 | * indexd array (label = value) or as an array of arrays. In the latter |
||
75 | * case an element has to look as follows: |
||
76 | * option-value => array ( |
||
77 | * 'label' => option-label, |
||
78 | * 'attrs' => array ( |
||
79 | * attr-key => attr-value, ... |
||
80 | * ) |
||
81 | * ) |
||
82 | * |
||
83 | * @param null|array $options |
||
84 | * @return $this|array |
||
85 | */ |
||
86 | public function options($options = null) { |
||
93 | |||
94 | /** |
||
95 | * Gets or sets an attribute |
||
96 | * |
||
97 | * When no $value is given, the current content of the attribute is returned. |
||
98 | * An empty string is returned for unset attributes. |
||
99 | * |
||
100 | * When a $value is given, the content is set to that value and the Element |
||
101 | * itself is returned for easy chaining |
||
102 | * |
||
103 | * @param string $name Name of the attribute to access |
||
104 | * @param null|string $value New value to set |
||
105 | * @return string|$this |
||
106 | */ |
||
107 | public function attr($name, $value = null) { |
||
113 | |||
114 | /** |
||
115 | * Get or set the current value |
||
116 | * |
||
117 | * When setting a value that is not defined in the options, the value is ignored |
||
118 | * and the first option's value is selected instead |
||
119 | * |
||
120 | * @param null|string $value The value to set |
||
121 | * @return $this|string |
||
122 | */ |
||
123 | public function val($value = null) { |
||
138 | |||
139 | /** |
||
140 | * Returns the first options as it will be rendered in HTML |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | protected function getFirstOption() { |
||
156 | |||
157 | /** |
||
158 | * Set the value in the OptGroups, including the optgroup for the options without optgroup. |
||
159 | * |
||
160 | * @param string $value |
||
161 | * @return bool |
||
162 | */ |
||
163 | protected function setValueInOptGroups($value) { |
||
175 | |||
176 | /** |
||
177 | * Create the HTML for the select it self |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function mainElementHTML() { |
||
191 | |||
192 | } |
||
193 |