|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -class DropdownImageField extends FormField { |
| 3 | +class DropdownImageField extends DropdownField { |
4 | 4 |
|
5 |
| - /** |
6 |
| - * @var boolean $source Associative or numeric array of all dropdown items, |
7 |
| - * with array key as the submitted field value, and the array value as a |
8 |
| - * natural language description shown in the interface element. |
9 |
| - */ |
10 |
| - protected $sourceObject; |
11 |
| - |
12 |
| - /** |
13 |
| - * @ignore |
14 |
| - */ |
15 | 5 | protected $keyField, $labelField, $imageField;
|
16 |
| - |
17 |
| - /** |
18 |
| - * @var boolean $isSelected Determines if the field was selected |
19 |
| - * at the time it was rendered, so if {@link $value} matches on of the array |
20 |
| - * values specified in {@link $source} |
21 |
| - */ |
22 |
| - protected $isSelected; |
23 |
| - |
24 |
| - /** |
25 |
| - * @var boolean $hasEmptyDefault Show the first <option> element as |
26 |
| - * empty (not having a value), with an optional label defined through |
27 |
| - * {@link $emptyString}. By default, the <select> element will be |
28 |
| - * rendered with the first option from {@link $source} selected. |
29 |
| - */ |
30 |
| - protected $hasEmptyDefault = false; |
31 |
| - |
32 |
| - /** |
33 |
| - * @var string $emptyString The title shown for an empty default selection, |
34 |
| - * e.g. "Select...". |
35 |
| - */ |
36 |
| - protected $emptyString = ''; |
37 |
| - |
38 |
| - /** |
39 |
| - * @var array $disabledItems The keys for items that should be disabled (greyed out) in the dropdown |
40 |
| - */ |
41 |
| - protected $disabledItems = array(); |
42 | 6 |
|
43 |
| - public function __construct($name, $title=null, $sourceObject='Group', $keyField = 'ID', $labelField = null, $imageField = 'Image', $value='', $form=null, $emptyString=null) { |
44 |
| - $this->setSourceObject($sourceObject); |
| 7 | + public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value='', $form=null) { |
45 | 8 |
|
46 | 9 | $this->keyField = $keyField;
|
47 | 10 | $this->labelField = $labelField;
|
48 | 11 | $this->imageField = $imageField;
|
49 |
| - |
50 |
| - if($emptyString === true) { |
51 |
| - Deprecation::notice('3.1', |
52 |
| - 'Please use setHasEmptyDefault(true) instead of passing a boolean true $emptyString argument', |
53 |
| - Deprecation::SCOPE_GLOBAL); |
54 |
| - } |
55 |
| - if(is_string($emptyString)) { |
56 |
| - Deprecation::notice('3.1', 'Please use setEmptyString() instead of passing a string emptyString argument.', |
57 |
| - Deprecation::SCOPE_GLOBAL); |
58 |
| - } |
59 |
| - |
60 |
| - if($emptyString) $this->setHasEmptyDefault(true); |
61 |
| - if(is_string($emptyString)) $this->setEmptyString($emptyString); |
62 |
| - |
63 |
| - parent::__construct($name, ($title===null) ? $name : $title, $value, $form); |
64 | 12 |
|
| 13 | + parent::__construct($name, ($title===null) ? $name : $title, $sourceObject, $value, $form); |
| 14 | + |
65 | 15 | $this->addExtraClass('dropdown');
|
66 | 16 | }
|
67 | 17 |
|
68 | 18 | public function Field($properties = array()) {
|
| 19 | + |
69 | 20 | $dirName = basename(dirname(dirname(__FILE__)));;
|
70 | 21 |
|
71 | 22 | Requirements::javascript($dirName.'/javascript/Polyfill.js');
|
72 | 23 | Requirements::javascript($dirName.'/javascript/ImageSelect.jquery.js');
|
73 | 24 | Requirements::css($dirName.'/css/ImageSelect.css');
|
74 | 25 |
|
75 |
| - $source = $this->getSourceObject(); |
| 26 | + $source = $this->getSource(); |
76 | 27 | $options = array();
|
77 | 28 | if($source) {
|
78 |
| - // SQLMap needs this to add an empty value to the options |
79 | 29 | if(is_object($source) && $this->emptyString) {
|
80 | 30 | $options[] = new ArrayData(array(
|
81 | 31 | 'Value' => '',
|
@@ -125,102 +75,19 @@ public function Field($properties = array()) {
|
125 | 75 |
|
126 | 76 | $properties = array_merge($properties, array('Options' => new ArrayList($options)));
|
127 | 77 |
|
128 |
| - return parent::Field($properties); |
129 |
| - } |
130 |
| - |
131 |
| - /** |
132 |
| - * Mark certain elements as disabled, |
133 |
| - * regardless of the {@link setDisabled()} settings. |
134 |
| - * |
135 |
| - * @param array $items Collection of array keys, as defined in the $source array |
136 |
| - */ |
137 |
| - public function setDisabledItems($items){ |
138 |
| - $this->disabledItems = $items; |
139 |
| - return $this; |
| 78 | + return FormField::Field($properties); |
140 | 79 | }
|
141 | 80 |
|
142 |
| - /** |
143 |
| - * @return Array |
144 |
| - */ |
145 |
| - public function getDisabledItems(){ |
146 |
| - return $this->disabledItems; |
147 |
| - } |
148 |
| - |
149 |
| - public function getAttributes() { |
150 |
| - return array_merge( |
151 |
| - parent::getAttributes(), |
152 |
| - array('type' => null, 'value' => null) |
153 |
| - ); |
154 |
| - } |
155 |
| - |
156 |
| - /** |
157 |
| - * @return boolean |
158 |
| - */ |
159 |
| - public function isSelected() { |
160 |
| - return $this->isSelected; |
161 |
| - } |
162 |
| - |
163 | 81 | /**
|
164 | 82 | * Gets the source array including any empty default values.
|
165 | 83 | *
|
166 | 84 | * @return array
|
167 | 85 | */
|
168 | 86 | public function getSourceObject() {
|
169 |
| - if(is_array($this->sourceObject) && $this->getHasEmptyDefault()) { |
170 |
| - return ArrayList::create()->unshift(array($this->keyField = '', $this->labelField = $this->emptyString, $this->imageField = '')); |
| 87 | + if(is_object($this->source) && $this->getHasEmptyDefault()) { |
| 88 | + return $this->source->unshift(array($this->keyField = '', $this->labelField = $this->emptyString, $this->imageField = '')); |
171 | 89 | } else {
|
172 |
| - return $this->sourceObject; |
| 90 | + return $this->source; |
173 | 91 | }
|
174 | 92 | }
|
175 |
| - |
176 |
| - /** |
177 |
| - * @param array $source |
178 |
| - */ |
179 |
| - public function setSourceObject($source) { |
180 |
| - $this->sourceObject = $source; |
181 |
| - return $this; |
182 |
| - } |
183 |
| - |
184 |
| - /** |
185 |
| - * @param boolean $bool |
186 |
| - */ |
187 |
| - public function setHasEmptyDefault($bool) { |
188 |
| - $this->hasEmptyDefault = $bool; |
189 |
| - return $this; |
190 |
| - } |
191 |
| - |
192 |
| - /** |
193 |
| - * @return boolean |
194 |
| - */ |
195 |
| - public function getHasEmptyDefault() { |
196 |
| - return $this->hasEmptyDefault; |
197 |
| - } |
198 |
| - |
199 |
| - /** |
200 |
| - * Set the default selection label, e.g. "select...". |
201 |
| - * Defaults to an empty string. Automatically sets |
202 |
| - * {@link $hasEmptyDefault} to true. |
203 |
| - * |
204 |
| - * @param string $str |
205 |
| - */ |
206 |
| - public function setEmptyString($str) { |
207 |
| - $this->setHasEmptyDefault(true); |
208 |
| - $this->emptyString = $str; |
209 |
| - return $this; |
210 |
| - } |
211 |
| - |
212 |
| - /** |
213 |
| - * @return string |
214 |
| - */ |
215 |
| - public function getEmptyString() { |
216 |
| - return $this->emptyString; |
217 |
| - } |
218 |
| - |
219 |
| - public function performReadonlyTransformation() { |
220 |
| - $field = $this->castedCopy('LookupField'); |
221 |
| - $field->setSourceObject($this->getSourceObject()); |
222 |
| - $field->setReadonly(true); |
223 |
| - |
224 |
| - return $field; |
225 |
| - } |
226 | 93 | }
|
0 commit comments