/
MultiCheckbox.php
210 lines (195 loc) · 5.48 KB
/
MultiCheckbox.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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v3.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\View\Widget;
use Cake\View\Helper\IdGeneratorTrait;
use Cake\View\Widget\WidgetInterface;
/**
* Input widget class for generating multiple checkboxes.
*
*/
class MultiCheckbox implements WidgetInterface {
use IdGeneratorTrait;
/**
* Template instance to use.
*
* @var \Cake\View\StringTemplate
*/
protected $_templates;
/**
* Label widget instance.
*
* @var \Cake\View\Widget\Label
*/
protected $_label;
/**
* Render multi-checkbox widget.
*
* This class uses the following templates:
*
* - `checkbox` Renders checkbox input controls. Accepts
* the `name`, `value` and `attrs` variables.
* - `checkboxContainer` Renders the containing div/element for
* a checkbox and its label. Accepts the `input`, and `label`
* variables.
*
* @param \Cake\View\StringTemplate $templates
* @param \Cake\View\Widget\Label $label
*/
public function __construct($templates, $label) {
$this->_templates = $templates;
$this->_label = $label;
}
/**
* Render multi-checkbox widget.
*
* Data supports the following options.
*
* - `name` The name attribute of the inputs to create.
* `[]` will be appended to the name.
* - `options` An array of options to create checkboxes out of.
* - `val` Either a string/integer or array of values that should be
* checked. Can also be a complex options set.
* - `disabled` Either a boolean or an array of checkboxes to disable.
* - `escape` Set to false to disable HTML escaping.
* - `options` An associative array of value=>labels to generate options for.
* - `idPrefix` Prefix for generated ID attributes.
*
* ### 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.
*
* @param array $data
* @return string
*/
public function render(array $data) {
$data += [
'name' => '',
'escape' => true,
'options' => [],
'disabled' => null,
'val' => null,
'idPrefix' => null
];
$out = [];
$this->_idPrefix = $data['idPrefix'];
$this->_clearIds();
foreach ($data['options'] as $key => $val) {
$checkbox = [
'value' => $key,
'text' => $val,
];
if (is_array($val) && isset($val['text'], $val['value'])) {
$checkbox = $val;
}
$checkbox['name'] = $data['name'];
$checkbox['escape'] = $data['escape'];
if ($this->_isSelected($key, $data['val'])) {
$checkbox['checked'] = true;
}
if ($this->_isDisabled($key, $data['disabled'])) {
$checkbox['disabled'] = true;
}
if (empty($checkbox['id'])) {
$checkbox['id'] = $this->_id($checkbox['name'], $checkbox['value']);
}
$out[] = $this->_renderInput($checkbox);
}
return implode('', $out);
}
/**
* Render a single checkbox & wrapper.
*
* @param array $checkbox An array containing checkbox key/value option pairs
* @return string
*/
protected function _renderInput($checkbox) {
$input = $this->_templates->format('checkbox', [
'name' => $checkbox['name'] . '[]',
'value' => $checkbox['escape'] ? h($checkbox['value']) : $checkbox['value'],
'attrs' => $this->_templates->formatAttributes(
$checkbox,
['name', 'value', 'text']
)
]);
$labelAttrs = [
'for' => $checkbox['id'],
'escape' => $checkbox['escape'],
'text' => $checkbox['text'],
'input' => $input,
];
if (!empty($checkbox['checked']) && empty($labelAttrs['class'])) {
$labelAttrs['class'] = 'selected';
}
$label = $this->_label->render($labelAttrs);
return $this->_templates->format('checkboxContainer', [
'label' => $label,
'input' => $input
]);
}
/**
* Helper method for deciding what options are selected.
*
* @param string $key The key to test.
* @param array|string|null The selected values.
* @return boolean
*/
protected function _isSelected($key, $selected) {
if ($selected === null) {
return false;
}
$isArray = is_array($selected);
if (!$isArray) {
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 The disabled values.
* @return boolean
*/
protected function _isDisabled($key, $disabled) {
if ($disabled === null || $disabled === false) {
return false;
}
if ($disabled === true || is_string($disabled)) {
return true;
}
$strict = !is_numeric($key);
return in_array((string)$key, $disabled, $strict);
}
}