/
SelectBoxWidget.php
319 lines (295 loc) · 10.3 KB
/
SelectBoxWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Widget;
use Cake\View\Form\ContextInterface;
use Traversable;
/**
* Input widget class for generating a selectbox.
*
* This class is intended as an internal implementation detail
* of Cake\View\Helper\FormHelper and is not intended for direct use.
*/
class SelectBoxWidget extends BasicWidget
{
/**
* Render a select box form input.
*
* Render a select box input given a set of data. Supported keys
* are:
*
* - `name` - Set the input name.
* - `options` - An array of options.
* - `disabled` - Either true or an array of options to disable.
* When true, the select element will be disabled.
* - `val` - Either a string or an array of options to mark as selected.
* - `empty` - Set to true to add an empty option at the top of the
* option elements. Set to a string to define the display text of the
* empty option. If an array is used the key will set the value of the empty
* option while, the value will set the display text.
* - `escape` - Set to false to disable HTML escaping.
*
* ### Options format
*
* The options option can take a variety of data format depending on
* the complexity of HTML you want generated.
*
* You can generate simple options using a basic associative array:
*
* ```
* 'options' => ['elk' => 'Elk', 'beaver' => 'Beaver']
* ```
*
* If you need to define additional attributes on your option elements
* you can use the complex form for options:
*
* ```
* 'options' => [
* ['value' => 'elk', 'text' => 'Elk', 'data-foo' => 'bar'],
* ]
* ```
*
* This form **requires** that both the `value` and `text` keys be defined.
* If either is not set options will not be generated correctly.
*
* If you need to define option groups you can do those using nested arrays:
*
* ```
* 'options' => [
* 'Mammals' => [
* 'elk' => 'Elk',
* 'beaver' => 'Beaver'
* ]
* ]
* ```
*
* And finally, if you need to put attributes on your optgroup elements you
* can do that with a more complex nested array form:
*
* ```
* 'options' => [
* [
* 'text' => 'Mammals',
* 'data-id' => 1,
* 'options' => [
* 'elk' => 'Elk',
* 'beaver' => 'Beaver'
* ]
* ],
* ]
* ```
*
* You are free to mix each of the forms in the same option set, and
* nest complex types as required.
*
* @param array $data Data to render with.
* @param \Cake\View\Form\ContextInterface $context The current form context.
* @return string A generated select box.
* @throws \RuntimeException when the name attribute is empty.
*/
public function render(array $data, ContextInterface $context)
{
$data += [
'name' => '',
'empty' => false,
'escape' => true,
'options' => [],
'disabled' => null,
'val' => null,
'templateVars' => []
];
$options = $this->_renderContent($data);
$name = $data['name'];
unset($data['name'], $data['options'], $data['empty'], $data['val'], $data['escape']);
if (isset($data['disabled']) && is_array($data['disabled'])) {
unset($data['disabled']);
}
$template = 'select';
if (!empty($data['multiple'])) {
$template = 'selectMultiple';
unset($data['multiple']);
}
$attrs = $this->_templates->formatAttributes($data);
return $this->_templates->format($template, [
'name' => $name,
'templateVars' => $data['templateVars'],
'attrs' => $attrs,
'content' => implode('', $options),
]);
}
/**
* Render the contents of the select element.
*
* @param array $data The context for rendering a select.
* @return array
*/
protected function _renderContent($data)
{
$options = $data['options'];
if ($options instanceof Traversable) {
$options = iterator_to_array($options);
}
if (!empty($data['empty'])) {
$options = $this->_emptyValue($data['empty']) + (array)$options;
}
if (empty($options)) {
return [];
}
$selected = isset($data['val']) ? $data['val'] : null;
$disabled = null;
if (isset($data['disabled']) && is_array($data['disabled'])) {
$disabled = $data['disabled'];
}
$templateVars = $data['templateVars'];
return $this->_renderOptions($options, $disabled, $selected, $templateVars, $data['escape']);
}
/**
* Generate the empty value based on the input.
*
* @param string|bool|array $value The provided empty value.
* @return array The generated option key/value.
*/
protected function _emptyValue($value)
{
if ($value === true) {
return ['' => ''];
}
if (is_scalar($value)) {
return ['' => $value];
}
if (is_array($value)) {
return $value;
}
return [];
}
/**
* Render the contents of an optgroup element.
*
* @param string $label The optgroup label text
* @param array $optgroup The opt group data.
* @param array|null $disabled The options to disable.
* @param array|string|null $selected The options to select.
* @param array $templateVars Additional template variables.
* @param bool $escape Toggle HTML escaping
* @return string Formatted template string
*/
protected function _renderOptgroup($label, $optgroup, $disabled, $selected, $templateVars, $escape)
{
$opts = $optgroup;
$attrs = [];
if (isset($optgroup['options'], $optgroup['text'])) {
$opts = $optgroup['options'];
$label = $optgroup['text'];
$attrs = $optgroup;
}
$groupOptions = $this->_renderOptions($opts, $disabled, $selected, $templateVars, $escape);
return $this->_templates->format('optgroup', [
'label' => $escape ? h($label) : $label,
'content' => implode('', $groupOptions),
'templateVars' => $templateVars,
'attrs' => $this->_templates->formatAttributes($attrs, ['text', 'options']),
]);
}
/**
* Render a set of options.
*
* Will recursively call itself when option groups are in use.
*
* @param array $options The options to render.
* @param array|null $disabled The options to disable.
* @param array|string|null $selected The options to select.
* @param array $templateVars Additional template variables.
* @param bool $escape Toggle HTML escaping.
* @return array Option elements.
*/
protected function _renderOptions($options, $disabled, $selected, $templateVars, $escape)
{
$out = [];
foreach ($options as $key => $val) {
// Option groups
$arrayVal = (is_array($val) || $val instanceof Traversable);
if ((!is_int($key) && $arrayVal) ||
(is_int($key) && $arrayVal && (isset($val['options']) || !isset($val['value'])))
) {
$out[] = $this->_renderOptgroup($key, $val, $disabled, $selected, $templateVars, $escape);
continue;
}
// Basic options
$optAttrs = [
'value' => $key,
'text' => $val,
'templateVars' => [],
];
if (is_array($val) && isset($val['text'], $val['value'])) {
$optAttrs = $val;
$key = $optAttrs['value'];
}
if (!isset($optAttrs['templateVars'])) {
$optAttrs['templateVars'] = [];
}
if ($this->_isSelected($key, $selected)) {
$optAttrs['selected'] = true;
}
if ($this->_isDisabled($key, $disabled)) {
$optAttrs['disabled'] = true;
}
if (!empty($templateVars)) {
$optAttrs['templateVars'] = array_merge($templateVars, $optAttrs['templateVars']);
}
$optAttrs['escape'] = $escape;
$out[] = $this->_templates->format('option', [
'value' => $escape ? h($optAttrs['value']) : $optAttrs['value'],
'text' => $escape ? h($optAttrs['text']) : $optAttrs['text'],
'templateVars' => $optAttrs['templateVars'],
'attrs' => $this->_templates->formatAttributes($optAttrs, ['text', 'value']),
]);
}
return $out;
}
/**
* Helper method for deciding what options are selected.
*
* @param string $key The key to test.
* @param array|string|null $selected The selected values.
* @return bool
*/
protected function _isSelected($key, $selected)
{
if ($selected === null) {
return false;
}
$isArray = is_array($selected);
if (!$isArray) {
$selected = $selected === false ? '0' : $selected;
return (string)$key === (string)$selected;
}
$strict = !is_numeric($key);
return in_array((string)$key, $selected, $strict);
}
/**
* Helper method for deciding what options are disabled.
*
* @param string $key The key to test.
* @param array|null $disabled The disabled values.
* @return bool
*/
protected function _isDisabled($key, $disabled)
{
if ($disabled === null) {
return false;
}
$strict = !is_numeric($key);
return in_array((string)$key, $disabled, $strict);
}
}