|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\ORM\ArrayLib; |
|
6
|
|
|
use SilverStripe\ORM\ArrayList; |
|
7
|
|
|
use SilverStripe\View\ArrayData; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Grouped dropdown, using optgroup tags. |
|
11
|
|
|
* |
|
12
|
|
|
* $source parameter (from DropdownField) must be a two dimensional array. |
|
13
|
|
|
* The first level of the array is used for the optgroup, and the second |
|
14
|
|
|
* level are the <options> for each group. |
|
15
|
|
|
* |
|
16
|
|
|
* Returns a select tag containing all the appropriate option tags, with |
|
17
|
|
|
* optgroup tags around the option tags as required. |
|
18
|
|
|
* |
|
19
|
|
|
* <b>Usage</b> |
|
20
|
|
|
* |
|
21
|
|
|
* <code> |
|
22
|
|
|
* new GroupedDropdownField( |
|
23
|
|
|
* $name = "dropdown", |
|
24
|
|
|
* $title = "Simple Grouped Dropdown", |
|
25
|
|
|
* $source = array( |
|
26
|
|
|
* "numbers" => array( |
|
27
|
|
|
* "1" => "1", |
|
28
|
|
|
* "2" => "2", |
|
29
|
|
|
* "3" => "3", |
|
30
|
|
|
* "4" => "4" |
|
31
|
|
|
* ), |
|
32
|
|
|
* "letters" => array( |
|
33
|
|
|
* "1" => "A", |
|
34
|
|
|
* "2" => "B", |
|
35
|
|
|
* "3" => "C", |
|
36
|
|
|
* "4" => "D", |
|
37
|
|
|
* "5" => "E", |
|
38
|
|
|
* "6" => "F" |
|
39
|
|
|
* ) |
|
40
|
|
|
* ) |
|
41
|
|
|
* ) |
|
42
|
|
|
* </code> |
|
43
|
|
|
* |
|
44
|
|
|
* <b>Disabling individual items</b> |
|
45
|
|
|
* |
|
46
|
|
|
* Unlike the source, disabled items are specified in the same way as |
|
47
|
|
|
* normal DropdownFields, using a single value list. Don't pass in grouped |
|
48
|
|
|
* values here. |
|
49
|
|
|
* |
|
50
|
|
|
* <code> |
|
51
|
|
|
* // Disables first and third option in each group |
|
52
|
|
|
* $groupedDrDownField->setDisabledItems(array("1", "3")) |
|
53
|
|
|
* </code> |
|
54
|
|
|
*/ |
|
55
|
|
|
class GroupedDropdownField extends DropdownField |
|
56
|
|
|
{ |
|
57
|
|
|
|
|
58
|
|
|
// TODO remove this when GroupedDropdownField is implemented |
|
59
|
|
|
// This should be one of SCHEMA_DATA_TYPE_* constants instead |
|
60
|
|
|
/** @skipUpgrade */ |
|
61
|
|
|
protected $schemaDataType = 'GroupedDropdownField'; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Build a potentially nested fieldgroup |
|
65
|
|
|
* |
|
66
|
|
|
* @param mixed $valueOrGroup Value of item, or title of group |
|
67
|
|
|
* @param string|array $titleOrOptions Title of item, or options in grouip |
|
68
|
|
|
* @return ArrayData Data for this item |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getFieldOption($valueOrGroup, $titleOrOptions) |
|
71
|
|
|
{ |
|
72
|
|
|
// Return flat option |
|
73
|
|
|
if (!is_array($titleOrOptions)) { |
|
74
|
|
|
return parent::getFieldOption($valueOrGroup, $titleOrOptions); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
// Build children from options list |
|
78
|
|
|
$options = new ArrayList(); |
|
79
|
|
|
foreach ($titleOrOptions as $childValue => $childTitle) { |
|
80
|
|
|
$options->push($this->getFieldOption($childValue, $childTitle)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return new ArrayData([ |
|
84
|
|
|
'Title' => $valueOrGroup, |
|
85
|
|
|
'Options' => $options |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function Type() |
|
90
|
|
|
{ |
|
91
|
|
|
return 'groupeddropdown dropdown'; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getSourceValues() |
|
95
|
|
|
{ |
|
96
|
|
|
// Flatten values |
|
97
|
|
|
$values = []; |
|
98
|
|
|
$source = $this->getSource(); |
|
99
|
|
|
array_walk_recursive( |
|
100
|
|
|
$source, |
|
|
|
|
|
|
101
|
|
|
// Function to extract value from array key |
|
102
|
|
|
function ($title, $value) use (&$values) { |
|
103
|
|
|
$values[] = $value; |
|
104
|
|
|
} |
|
105
|
|
|
); |
|
106
|
|
|
return $values; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return SingleLookupField |
|
111
|
|
|
*/ |
|
112
|
|
|
public function performReadonlyTransformation() |
|
113
|
|
|
{ |
|
114
|
|
|
$field = parent::performReadonlyTransformation(); |
|
115
|
|
|
$field->setSource(ArrayLib::flatten($this->getSource())); |
|
|
|
|
|
|
116
|
|
|
return $field; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|