1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
19 class CheckboxSetField extends OptionsetField {
20
21 protected $disabled = false;
22
23 24 25
26 protected $defaultItems = array();
27
28 29 30 31 32 33
34 function Field() {
35 Requirements::css(SAPPHIRE_DIR . '/css/CheckboxSetField.css');
36
37 $source = $this->source;
38 $values = $this->value;
39
40
41 if(is_object($this->form)) {
42 $record = $this->form->getRecord();
43 if(!$values && $record && $record->hasMethod($this->name)) {
44 $funcName = $this->name;
45 $join = $record->$funcName();
46 if($join) {
47 foreach($join as $joinItem) {
48 $values[] = $joinItem->ID;
49 }
50 }
51 }
52 }
53
54
55 if(!is_array($source) && !is_a($source, 'SQLMap')) {
56 if(is_array($values)) {
57 $items = $values;
58 } else {
59
60 if($values && is_a($values, 'DataObjectSet')) {
61 foreach($values as $object) {
62 if(is_a($object, 'DataObject')) {
63 $items[] = $object->ID;
64 }
65 }
66 } elseif($values && is_string($values)) {
67 $items = explode(',', $values);
68 $items = str_replace('{comma}', ',', $items);
69 }
70 }
71 } else {
72
73 if(is_a($values, 'DataObjectSet') || is_array($values)) {
74 $items = $values;
75 } else {
76 $items = explode(',', $values);
77 $items = str_replace('{comma}', ',', $items);
78 }
79 }
80
81 if(is_array($source)) {
82 unset($source['']);
83 }
84
85 $odd = 0;
86 $options = '';
87
88 if ($source == null) {
89 $source = array();
90 $options = "<li>No options available</li>";
91 }
92
93 if($source) foreach($source as $index => $item) {
94 if(is_a($item, 'DataObject')) {
95 $key = $item->ID;
96 $value = $item->Title;
97 } else {
98 $key = $index;
99 $value = $item;
100 }
101
102 $odd = ($odd + 1) % 2;
103 $key_lat = Convert::rus2lat($key);
104 $extraClass = $odd ? 'odd' : 'even';
105 $extraClass .= ' val' . str_replace(' ', '', $key_lat);
106 $itemID = $this->id() . '_' . ereg_replace('[^a-zA-Z0-9]+', '', $key_lat);
107 $checked = '';
108
109 if(isset($items)) {
110 $checked = (in_array($key, $items) || in_array($key, $this->defaultItems)) ? ' checked="checked"' : '';
111 }
112
113 $disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? $disabled = ' disabled="disabled"' : '';
114 $options .= "<li class=\"$extraClass\"><input id=\"$itemID\" name=\"$this->name[$key_lat]\" type=\"checkbox\" value=\"$key\"$checked $disabled class=\"checkbox\" /> <label for=\"$itemID\">$value</label></li>\n";
115 }
116
117 return "<ul id=\"{$this->id()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n$options</ul>\n";
118 }
119
120 function setDisabled($val) {
121 $this->disabled = $val;
122 }
123
124 125 126 127 128 129 130
131 function setDefaultItems($items) {
132 $this->defaultItems = $items;
133 }
134
135 136 137
138 function getDefaultItems() {
139 return $this->defaultItems;
140 }
141
142 143 144
145 function setValue($value, $obj = null) {
146
147 if(!$value && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) {
148 $funcName = $this->name;
149 $selected = $obj->$funcName();
150 if ($selected && is_object($selected) ) {
151 $value = $selected->toDropdownMap('ID', 'ID');
152 }
153 }
154
155 parent::setValue($value, $obj);
156 }
157
158 159 160 161 162 163 164 165
166 function saveInto(DataObject $record) {
167 $fieldname = $this->name ;
168 if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
169 $idList = array();
170 if($this->value) foreach($this->value as $id => $bool) {
171 if($bool) {
172 $idList[] = $id;
173 }
174 }
175 $record->$fieldname()->setByIDList($idList);
176 } elseif($fieldname && $record) {
177 if($this->value) {
178 $this->value = str_replace(',', '{comma}', $this->value);
179 $record->$fieldname = implode(",", $this->value);
180 } else {
181 $record->$fieldname = '';
182 }
183 }
184 }
185
186 187 188 189 190 191
192 function dataValue() {
193 if($this->value && is_array($this->value)) {
194 $filtered = array();
195 foreach($this->value as $item) {
196 if($item) {
197 $filtered[] = str_replace(",", "{comma}", $item);
198 }
199 }
200
201 return implode(',', $filtered);
202 }
203
204 return '';
205 }
206
207 function performDisabledTransformation() {
208 $clone = clone $this;
209 $clone->setDisabled(true);
210
211 return $clone;
212 }
213
214 215 216 217 218 219
220 function performReadonlyTransformation() {
221 $values = '';
222 $data = array();
223
224 $items = $this->value;
225 if($this->source) {
226 foreach($this->source as $source) {
227 if(is_object($source)) {
228 $sourceTitles[$source->ID] = $source->Title;
229 }
230 }
231 }
232
233 if($items) {
234
235 if(is_a($items, 'DataObjectSet')) {
236 foreach($items as $item) {
237 $data[] = $item->Title;
238 }
239 if($data) $values = implode(', ', $data);
240
241
242 } else {
243 if(!is_array($items)) {
244 $items = preg_split('/ *, */', trim($items));
245 }
246
247 foreach($items as $item) {
248 if(is_array($item)) {
249 $data[] = $item['Title'];
250 } elseif(is_array($this->source) && !empty($this->source[$item])) {
251 $data[] = $this->source[$item];
252 } elseif(is_a($this->source, 'DataObjectSet')) {
253 $data[] = $sourceTitles[$item];
254 } else {
255 $data[] = $item;
256 }
257 }
258
259 $values = implode(', ', $data);
260 }
261 }
262
263 $title = ($this->title) ? $this->title : '';
264
265 $field = new ReadonlyField($this->name, $title, $values);
266 $field->setForm($this->form);
267
268 return $field;
269 }
270
271 function () {
272 return FormField::ExtraOptions();
273 }
274
275 }
276 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-