Skip to content

Commit 57ea82b

Browse files
committed
initial code
0 parents  commit 57ea82b

File tree

4 files changed

+427
-0
lines changed

4 files changed

+427
-0
lines changed

code/DropdownImageField.php

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
<?php
2+
3+
class DropdownImageField extends FormField {
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+
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+
43+
public function __construct($name, $title=null, $sourceObject='Group', $keyField = 'ID', $labelField = null, $imageField = 'Image', $value='', $form=null, $emptyString=null) {
44+
$this->setSourceObject($sourceObject);
45+
46+
$this->keyField = $keyField;
47+
$this->labelField = $labelField;
48+
$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+
65+
$this->addExtraClass('dropdown');
66+
}
67+
68+
public function Field($properties = array()) {
69+
$dirName = basename(dirname(dirname(__FILE__)));;
70+
71+
Requirements::javascript($dirName.'/javascript/ImageSelect.jquery.js');
72+
73+
$source = $this->getSourceObject();
74+
$options = array();
75+
if($source) {
76+
// SQLMap needs this to add an empty value to the options
77+
if(is_object($source) && $this->emptyString) {
78+
$options[] = new ArrayData(array(
79+
'Value' => '',
80+
'Title' => $this->emptyString,
81+
'Image' => ''
82+
));
83+
}
84+
85+
foreach($source as $item) {
86+
$value = $item->{$this->keyField};
87+
$title = $item->{$this->labelField};
88+
$image = $item->{$this->imageField}();
89+
90+
$selected = false;
91+
if($value === '' && ($this->value === '' || $this->value === null)) {
92+
$selected = true;
93+
} else {
94+
// check against value, fallback to a type check comparison when !value
95+
if($value) {
96+
$selected = ($value == $this->value);
97+
} else {
98+
$selected = ($value === $this->value) || (((string) $value) === ((string) $this->value));
99+
}
100+
101+
$this->isSelected = $selected;
102+
}
103+
104+
$disabled = false;
105+
if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){
106+
$disabled = 'disabled';
107+
}
108+
109+
$options[] = new ArrayData(array(
110+
'Title' => $title,
111+
'Value' => $value,
112+
'Image' => $image,
113+
'Selected' => $selected,
114+
'Disabled' => $disabled,
115+
));
116+
}
117+
}
118+
119+
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
120+
121+
return parent::Field($properties);
122+
}
123+
124+
/**
125+
* Mark certain elements as disabled,
126+
* regardless of the {@link setDisabled()} settings.
127+
*
128+
* @param array $items Collection of array keys, as defined in the $source array
129+
*/
130+
public function setDisabledItems($items){
131+
$this->disabledItems = $items;
132+
return $this;
133+
}
134+
135+
/**
136+
* @return Array
137+
*/
138+
public function getDisabledItems(){
139+
return $this->disabledItems;
140+
}
141+
142+
public function getAttributes() {
143+
return array_merge(
144+
parent::getAttributes(),
145+
array('type' => null, 'value' => null)
146+
);
147+
}
148+
149+
/**
150+
* @return boolean
151+
*/
152+
public function isSelected() {
153+
return $this->isSelected;
154+
}
155+
156+
/**
157+
* Gets the source array including any empty default values.
158+
*
159+
* @return array
160+
*/
161+
public function getSourceObject() {
162+
if(is_array($this->sourceObject) && $this->getHasEmptyDefault()) {
163+
return ArrayList::create()->unshift(array($this->keyField = '', $this->labelField = $this->emptyString, $this->imageField = ''));
164+
} else {
165+
return $this->sourceObject;
166+
}
167+
}
168+
169+
/**
170+
* @param array $source
171+
*/
172+
public function setSourceObject($source) {
173+
$this->sourceObject = $source;
174+
return $this;
175+
}
176+
177+
/**
178+
* @param boolean $bool
179+
*/
180+
public function setHasEmptyDefault($bool) {
181+
$this->hasEmptyDefault = $bool;
182+
return $this;
183+
}
184+
185+
/**
186+
* @return boolean
187+
*/
188+
public function getHasEmptyDefault() {
189+
return $this->hasEmptyDefault;
190+
}
191+
192+
/**
193+
* Set the default selection label, e.g. "select...".
194+
* Defaults to an empty string. Automatically sets
195+
* {@link $hasEmptyDefault} to true.
196+
*
197+
* @param string $str
198+
*/
199+
public function setEmptyString($str) {
200+
$this->setHasEmptyDefault(true);
201+
$this->emptyString = $str;
202+
return $this;
203+
}
204+
205+
/**
206+
* @return string
207+
*/
208+
public function getEmptyString() {
209+
return $this->emptyString;
210+
}
211+
212+
public function performReadonlyTransformation() {
213+
$field = $this->castedCopy('LookupField');
214+
$field->setSourceObject($this->getSourceObject());
215+
$field->setReadonly(true);
216+
217+
return $field;
218+
}
219+
}

css/ImageSelect.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* Tag image */
2+
.chose-image {
3+
width:55px;
4+
height:55px;
5+
padding: 2px 5px 3px 0;
6+
}
7+
8+
/* Image for Single mode */
9+
.chose-image-small {
10+
width:18px;
11+
height:18px;
12+
vertical-align: middle;
13+
margin: -3px 0 0 0;
14+
padding: 0 3px 0 0;
15+
}
16+
17+
/* Images appended to the li(s) */
18+
.chose-image-list {
19+
width:18px;
20+
height:18px;
21+
vertical-align: middle;
22+
margin: -3px 0 0 0;
23+
padding: 0 3px 0 0;
24+
}
25+
26+
/* Grey-out image for Multi mode */
27+
div.chosen-container-multi .result-selected img {
28+
opacity: 0.3;
29+
}

0 commit comments

Comments
 (0)