/
ListboxField.php
275 lines (238 loc) · 7.14 KB
/
ListboxField.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
<?php
namespace SilverStripe\Forms;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
/**
* Multi-line listbox field, created from a select tag.
*
* <b>Usage</b>
*
* <code>
* new ListboxField(
* $name = "pickanumber",
* $title = "Pick a number",
* $source = array(
* "1" => "one",
* "2" => "two",
* "3" => "three"
* ),
* $value = 1
* )
* </code>
*
* @see DropdownField for a simple select field with a single element.
* @see CheckboxSetField for multiple selections through checkboxes.
* @see OptionsetField for single selections via radiobuttons.
* @see TreeDropdownField for a rich and customizeable UI that can visualize a tree of selectable elements
*/
class ListboxField extends MultiSelectField
{
/**
* The size of the field in rows.
*
* @var int
*/
protected $size;
/**
* @var array
*/
protected $disabledItems = [];
protected $schemaComponent = 'ListboxField';
/**
* Creates a new dropdown field.
*
* @param string $name The field name
* @param string $title The field title
* @param array $source An map of the dropdown items
* @param string|array|null $value You can pass an array of values or a single value like a drop down to be selected
* @param int $size Optional size of the select element
*/
public function __construct($name, $title = '', $source = [], $value = null, $size = null)
{
if ($size) {
$this->setSize($size);
}
$this->addExtraClass('ss-listbox-field');
parent::__construct($name, $title, $source, $value);
}
/**
* Returns a select tag containing all the appropriate option tags
*
* @param array $properties
* @return string
*/
public function Field($properties = [])
{
$properties = array_merge($properties, [
'Options' => $this->getOptions(),
]);
return FormField::Field($properties);
}
/**
* Gets the list of options to render in this formfield
*
* @return ArrayList<ArrayData>
*/
public function getOptions()
{
// Loop through and figure out which values were selected.
$options = [];
$selectedValue = $this->getValueArray();
foreach ($this->getSource() as $itemValue => $title) {
$itemSelected = in_array($itemValue, $selectedValue ?? [])
|| in_array($itemValue, $this->getDefaultItems() ?? []);
$itemDisabled = $this->isDisabled()
|| in_array($itemValue, $this->getDisabledItems() ?? []);
$options[] = new ArrayData([
'Title' => $title,
'Value' => $itemValue,
'Selected' => $itemSelected,
'Disabled' => $itemDisabled,
]);
}
$options = new ArrayList($options);
$this->extend('updateGetOptions', $options);
return $options;
}
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
[
'multiple' => 'true',
'size' => $this->getSize(),
'name' => $this->getName() . '[]'
]
);
}
/**
* Get the size of this dropdown in rows.
*
* @return integer
*/
public function getSize()
{
return $this->size;
}
/**
* Sets the size of this dropdown in rows.
*
* @param int $size The height in rows (e.g. 3)
* @return $this Self reference
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Mark certain elements as disabled,
* regardless of the {@link setDisabled()} settings.
*
* @param array $items Collection of array keys, as defined in the $source array
* @return $this Self reference
*/
public function setDisabledItems($items)
{
$this->disabledItems = $items;
return $this;
}
/**
* @return array
*/
public function getDisabledItems()
{
return $this->disabledItems;
}
/**
* Provide ListboxField data to the JSON schema for the frontend component
*
* @return array
*/
public function getSchemaDataDefaults()
{
$options = $this->getOptions();
$selected = $options->filter('Selected', true);
$name = $this->getName();
$schema = array_merge(
parent::getSchemaDataDefaults(),
[
'name' => $name,
'lazyLoad' => false,
'creatable' => false,
'multi' => true,
'value' => $selected->count() ? $selected->toNestedArray() : null,
'disabled' => $this->isDisabled() || $this->isReadonly(),
]
);
$schema['options'] = array_values($options->toNestedArray() ?? []);
return $schema;
}
public function getSchemaStateDefaults()
{
$data = parent::getSchemaStateDefaults();
// Add options to 'data'
$data['lazyLoad'] = false;
$data['multi'] = true;
$data['creatable'] = false;
$options = $this->getOptions()->filter('Selected', true);
$data['value'] = $options->count() ? $options->toNestedArray() : null;
return $data;
}
/**
* Returns array of arrays representing tags.
*
* @param string $term
* @return array
*/
protected function getOptionsArray($term)
{
$source = $this->getSourceList();
if (!$source) {
return [];
}
$titleField = $this->getTitleField();
$query = $source
->filter($titleField . ':PartialMatch:nocase', $term)
->sort($titleField);
// Map into a distinct list
$items = [];
$titleField = $this->getTitleField();
foreach ($query->map('ID', $titleField)->values() as $title) {
$items[$title] = [
'Title' => $title,
'Value' => $title,
];
}
return array_values($items ?? []);
}
public function getValueArray()
{
$value = $this->Value();
$validValues = $this->getValidValues();
if (empty($validValues)) {
return [];
}
$canary = reset($validValues);
$targetType = gettype($canary);
if (is_array($value) && count($value) > 0) {
$first = reset($value);
// sanity check the values - make sure strings get strings, ints get ints etc
if ($targetType !== gettype($first)) {
$replaced = [];
foreach ($value as $item) {
if (!is_array($item)) {
$item = json_decode($item, true);
}
if ($targetType === gettype($item)) {
$replaced[] = $item;
} elseif (isset($item['Value'])) {
$replaced[] = $item['Value'];
}
}
$value = $replaced;
}
}
return $this->getListValues($value);
}
}