|
2 | 2 |
|
3 | 3 | class DropdownImageField extends DropdownField {
|
4 | 4 |
|
5 |
| - protected $keyField, $labelField, $imageField; |
6 |
| - |
7 |
| - public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value='', $form=null) { |
8 |
| - |
9 |
| - $this->keyField = $keyField; |
10 |
| - $this->labelField = $labelField; |
11 |
| - $this->imageField = $imageField; |
12 |
| - |
13 |
| - parent::__construct($name, ($title===null) ? $name : $title, $sourceObject, $value, $form); |
14 |
| - |
15 |
| - $this->addExtraClass('dropdown'); |
16 |
| - } |
17 |
| - |
18 |
| - public function Field($properties = array()) { |
19 |
| - |
20 |
| - $dirName = basename(dirname(dirname(__FILE__)));; |
21 |
| - |
22 |
| - Requirements::javascript($dirName.'/javascript/Polyfill.js'); |
23 |
| - Requirements::javascript($dirName.'/javascript/ImageSelect.jquery.js'); |
24 |
| - Requirements::css($dirName.'/css/ImageSelect.css'); |
25 |
| - |
26 |
| - $source = $this->getSource(); |
27 |
| - $options = array(); |
28 |
| - if($source) { |
29 |
| - if(is_object($source) && $this->hasEmptyDefault) { |
30 |
| - $options[] = new ArrayData(array( |
31 |
| - 'Value' => '', |
32 |
| - 'Title' => $this->emptyString, |
33 |
| - 'Image' => '' |
34 |
| - )); |
35 |
| - } |
36 |
| - |
37 |
| - foreach($source as $item) { |
38 |
| - $value = $item->{$this->keyField}; |
39 |
| - if (empty($this->labelField)) { |
40 |
| - $title = '--nbsp'; |
41 |
| - } else { |
42 |
| - $title = $item->{$this->labelField}; |
43 |
| - } |
44 |
| - |
45 |
| - $image = $item->{$this->imageField}(); |
46 |
| - |
47 |
| - $selected = false; |
48 |
| - if($value === '' && ($this->value === '' || $this->value === null)) { |
49 |
| - $selected = true; |
50 |
| - } else { |
51 |
| - // check against value, fallback to a type check comparison when !value |
52 |
| - if($value) { |
53 |
| - $selected = ($value == $this->value); |
54 |
| - } else { |
55 |
| - $selected = ($value === $this->value) || (((string) $value) === ((string) $this->value)); |
56 |
| - } |
57 |
| - |
58 |
| - $this->isSelected = $selected; |
59 |
| - } |
60 |
| - |
61 |
| - $disabled = false; |
62 |
| - if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){ |
63 |
| - $disabled = 'disabled'; |
64 |
| - } |
65 |
| - |
66 |
| - $options[] = new ArrayData(array( |
67 |
| - 'Title' => $title, |
68 |
| - 'Value' => $value, |
69 |
| - 'Image' => $image, |
70 |
| - 'Selected' => $selected, |
71 |
| - 'Disabled' => $disabled, |
72 |
| - )); |
73 |
| - } |
74 |
| - } |
75 |
| - |
76 |
| - $properties = array_merge($properties, array('Options' => new ArrayList($options))); |
77 |
| - |
78 |
| - return FormField::Field($properties); |
79 |
| - } |
80 |
| - |
81 |
| - /** |
82 |
| - * Gets the source |
83 |
| - * |
84 |
| - * @return Object |
85 |
| - */ |
86 |
| - public function getSource() { |
87 |
| - return $this->source; |
88 |
| - } |
89 |
| - |
| 5 | + protected $keyField, $labelField, $imageField; |
| 6 | + |
| 7 | + public function __construct($name, $title, $sourceObject, $keyField = 'ID', $labelField = 'Title', $imageField = 'Image', $value = '', $form = null) { |
| 8 | + |
| 9 | + $this->keyField = $keyField; |
| 10 | + $this->labelField = $labelField; |
| 11 | + $this->imageField = $imageField; |
| 12 | + |
| 13 | + parent::__construct($name, ($title === null) ? $name : $title, $sourceObject, $value, $form); |
| 14 | + |
| 15 | + $this->addExtraClass('dropdown'); |
| 16 | + } |
| 17 | + |
| 18 | + public function Field($properties = array()) { |
| 19 | + |
| 20 | + $dirName = basename(dirname(dirname(__FILE__))); |
| 21 | + ; |
| 22 | + |
| 23 | + Requirements::javascript($dirName . '/javascript/Polyfill.js'); |
| 24 | + Requirements::javascript($dirName . '/javascript/ImageSelect.jquery.js'); |
| 25 | + Requirements::css($dirName . '/css/ImageSelect.css'); |
| 26 | + |
| 27 | + $source = $this->getSource(); |
| 28 | + $options = array(); |
| 29 | + if ($source) { |
| 30 | + if (is_object($source) && $this->hasEmptyDefault) { |
| 31 | + $options[] = new ArrayData(array( |
| 32 | + 'Value' => '', |
| 33 | + 'Title' => $this->emptyString, |
| 34 | + 'Image' => '' |
| 35 | + )); |
| 36 | + } |
| 37 | + |
| 38 | + foreach ($source as $item) { |
| 39 | + $value = $item->{$this->keyField}; |
| 40 | + if (empty($this->labelField)) { |
| 41 | + $title = '--nbsp'; |
| 42 | + } else { |
| 43 | + $title = $item->{$this->labelField}; |
| 44 | + } |
| 45 | + |
| 46 | + $image = $item->{$this->imageField}(); |
| 47 | + |
| 48 | + $selected = false; |
| 49 | + if ($value === '' && ($this->value === '' || $this->value === null)) { |
| 50 | + $selected = true; |
| 51 | + } else { |
| 52 | + // check against value, fallback to a type check comparison when !value |
| 53 | + if ($value) { |
| 54 | + $selected = ($value == $this->value); |
| 55 | + } else { |
| 56 | + $selected = ($value === $this->value) || (((string) $value) === ((string) $this->value)); |
| 57 | + } |
| 58 | + |
| 59 | + $this->isSelected = $selected; |
| 60 | + } |
| 61 | + |
| 62 | + $disabled = false; |
| 63 | + if (in_array($value, $this->disabledItems) && $title != $this->emptyString) { |
| 64 | + $disabled = 'disabled'; |
| 65 | + } |
| 66 | + |
| 67 | + $options[] = new ArrayData(array( |
| 68 | + 'Title' => $title, |
| 69 | + 'Value' => $value, |
| 70 | + 'Image' => $image, |
| 71 | + 'Selected' => $selected, |
| 72 | + 'Disabled' => $disabled, |
| 73 | + )); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $properties = array_merge($properties, array('Options' => new ArrayList($options))); |
| 78 | + |
| 79 | + return FormField::Field($properties); |
| 80 | + } |
| 81 | + |
90 | 82 | /**
|
91 |
| - * Get the source of this field as an array |
92 |
| - * Transform the source DataList to an key => value array |
93 |
| - * |
94 |
| - * @return array |
95 |
| - */ |
96 |
| - public function getSourceAsArray() |
97 |
| - { |
98 |
| - $source = $this->getSource(); |
99 |
| - if (is_array($source)) { |
100 |
| - return $source; |
101 |
| - } else { |
102 |
| - $sourceArray = array(); |
103 |
| - foreach ($source as $object) { |
104 |
| - $sourceArray[$object->{$this->keyField}] = $object->{$this->labelField}; |
105 |
| - } |
106 |
| - } |
107 |
| - return $sourceArray; |
108 |
| - } |
| 83 | + * Gets the source |
| 84 | + * |
| 85 | + * @return Object |
| 86 | + */ |
| 87 | + public function getSource() { |
| 88 | + return $this->source; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get the source of this field as an array |
| 93 | + * Transform the source DataList to an key => value array |
| 94 | + * |
| 95 | + * @return array |
| 96 | + */ |
| 97 | + public function getSourceAsArray() { |
| 98 | + $source = $this->getSource(); |
| 99 | + if (is_array($source)) { |
| 100 | + return $source; |
| 101 | + } else { |
| 102 | + $sourceArray = array(); |
| 103 | + foreach ($source as $object) { |
| 104 | + $sourceArray[$object->{$this->keyField}] = $object->{$this->labelField}; |
| 105 | + } |
| 106 | + } |
| 107 | + return $sourceArray; |
| 108 | + } |
| 109 | + |
109 | 110 | }
|
0 commit comments