ListboxField.php
7.76 KB
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
<?php
/**
* 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
*
* @package forms
* @subpackage fields-basic
*/
class ListboxField extends DropdownField {
/**
* The size of the field in rows.
* @var int
*/
protected $size;
/**
* Should the user be able to select multiple
* items on this dropdown field?
*
* @var boolean
*/
protected $multiple = false;
/**
* @var Array
*/
protected $disabledItems = array();
/**
* @var Array
*/
protected $defaultItems = array();
/**
* 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 $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
* @param form The parent form
*/
public function __construct($name, $title = '', $source = array(), $value = '', $size = null, $multiple = false) {
if($size) $this->size = $size;
if($multiple) $this->multiple = $multiple;
parent::__construct($name, $title, $source, $value);
}
/**
* Returns a <select> tag containing all the appropriate <option> tags
*/
public function Field($properties = array()) {
if($this->multiple) $this->name .= '[]';
$options = array();
// We have an array of values
if(is_array($this->value)){
// Loop through and figure out which values were selected.
foreach($this->getSource() as $value => $title) {
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
'Selected' => (in_array($value, $this->value) || in_array($value, $this->defaultItems)),
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
));
}
} else {
// Listbox was based a singlular value, so treat it like a dropdown.
foreach($this->getSource() as $value => $title) {
$options[] = new ArrayData(array(
'Title' => $title,
'Value' => $value,
'Selected' => ($value == $this->value || in_array($value, $this->defaultItems)),
'Disabled' => $this->disabled || in_array($value, $this->disabledItems),
));
}
}
$properties = array_merge($properties, array(
'Options' => new ArrayList($options)
));
return $this->customise($properties)->renderWith($this->getTemplates());
}
public function getAttributes() {
return array_merge(
parent::getAttributes(),
array(
'multiple' => $this->multiple,
'size' => $this->size
)
);
}
/**
* Sets the size of this dropdown in rows.
* @param int $size The height in rows (e.g. 3)
*/
public function setSize($size) {
$this->size = $size;
return $this;
}
/**
* Sets this field to have a muliple select attribute
* @param boolean $bool
*/
public function setMultiple($bool) {
$this->multiple = $bool;
return $this;
}
public function setSource($source) {
if($source) {
$hasCommas = array_filter(array_keys($source),
create_function('$key', 'return strpos($key, ",") !== FALSE;'));
if($hasCommas) {
throw new InvalidArgumentException('No commas allowed in $source keys');
}
}
parent::setSource($source);
return $this;
}
/**
* Return the CheckboxSetField value as a string
* selected item keys.
*
* @return string
*/
public function dataValue() {
if($this->value && is_array($this->value) && $this->multiple) {
$filtered = array();
foreach($this->value as $item) {
if($item) {
$filtered[] = str_replace(",", "{comma}", $item);
}
}
return implode(',', $filtered);
} else {
return parent::dataValue();
}
}
/**
* Save the current value of this field into a DataObject.
* If the field it is saving to is a has_many or many_many relationship,
* it is saved by setByIDList(), otherwise it creates a comma separated
* list for a standard DB text/varchar field.
*
* @param DataObject $record The record to save into
*/
public function saveInto(DataObjectInterface $record) {
if($this->multiple) {
$fieldname = $this->name;
$relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
if($fieldname && $record && $relation &&
($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) {
$idList = (is_array($this->value)) ? array_values($this->value) : array();
if(!$record->ID) {
$record->write(); // record needs to have an ID in order to set relationships
$relation = ($fieldname && $record && $record->hasMethod($fieldname))
? $record->$fieldname()
: null;
}
$relation->setByIDList($idList);
} elseif($fieldname && $record) {
if($this->value) {
$this->value = str_replace(',', '{comma}', $this->value);
$record->$fieldname = implode(",", $this->value);
} else {
$record->$fieldname = null;
}
}
} else {
parent::saveInto($record);
}
}
/**
* Load a value into this CheckboxSetField
*/
public function setValue($val, $obj = null) {
// If we're not passed a value directly,
// we can look for it in a relation method on the object passed as a second arg
if(!$val && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) {
$funcName = $this->name;
$val = array_values($obj->$funcName()->getIDList());
}
if($val) {
if(!$this->multiple && is_array($val)) {
throw new InvalidArgumentException('Array values are not allowed (when multiple=false).');
}
if($this->multiple) {
$parts = (is_array($val)) ? $val : preg_split("/ *, */", trim($val));
if(ArrayLib::is_associative($parts)) {
// This is due to the possibility of accidentally passing an array of values (as keys) and titles (as values) when only the keys were intended to be saved.
throw new InvalidArgumentException('Associative arrays are not allowed as values (when multiple=true), only indexed arrays.');
}
// Doesn't check against unknown values in order to allow for less rigid data handling.
// They're silently ignored and overwritten the next time the field is saved.
parent::setValue($parts);
} else {
if(!in_array($val, array_keys($this->getSource()))) {
throw new InvalidArgumentException(sprintf(
'Invalid value "%s" for multiple=false',
Convert::raw2xml($val)
));
}
parent::setValue($val);
}
} else {
parent::setValue($val);
}
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
*/
public function setDisabledItems($items) {
$this->disabledItems = $items;
return $this;
}
/**
* @return Array
*/
public function getDisabledItems() {
return $this->disabledItems;
}
/**
* Default selections, regardless of the {@link setValue()} settings.
* Note: Items marked as disabled through {@link setDisabledItems()} can still be
* selected by default through this method.
*
* @param Array $items Collection of array keys, as defined in the $source array
*/
public function setDefaultItems($items) {
$this->defaultItems = $items;
return $this;
}
/**
* @return Array
*/
public function getDefaultItems() {
return $this->defaultItems;
}
}