1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Grouped dropdown, using <optgroup> tags. |
4
|
|
|
* |
5
|
|
|
* $source parameter (from DropdownField) must be a two dimensional array. |
6
|
|
|
* The first level of the array is used for the <optgroup>, and the second |
7
|
|
|
* level are the <options> for each group. |
8
|
|
|
* |
9
|
|
|
* Returns a <select> tag containing all the appropriate <option> tags, with |
10
|
|
|
* <optgroup> tags around the <option> tags as required. |
11
|
|
|
* |
12
|
|
|
* <b>Usage</b> |
13
|
|
|
* |
14
|
|
|
* <code> |
15
|
|
|
* new GroupedDropdownField( |
16
|
|
|
* $name = "dropdown", |
17
|
|
|
* $title = "Simple Grouped Dropdown", |
18
|
|
|
* $source = array( |
19
|
|
|
* "numbers" => array( |
20
|
|
|
* "1" => "1", |
21
|
|
|
* "2" => "2", |
22
|
|
|
* "3" => "3", |
23
|
|
|
* "4" => "4" |
24
|
|
|
* ), |
25
|
|
|
* "letters" => array( |
26
|
|
|
* "1" => "A", |
27
|
|
|
* "2" => "B", |
28
|
|
|
* "3" => "C", |
29
|
|
|
* "4" => "D", |
30
|
|
|
* "5" => "E", |
31
|
|
|
* "6" => "F" |
32
|
|
|
* ) |
33
|
|
|
* ) |
34
|
|
|
* ) |
35
|
|
|
* </code> |
36
|
|
|
* |
37
|
|
|
* <b>Disabling individual items</b> |
38
|
|
|
* |
39
|
|
|
* <code> |
40
|
|
|
* $groupedDrDownField->setDisabledItems( |
41
|
|
|
* array( |
42
|
|
|
* "numbers" => array( |
43
|
|
|
* "1" => "1", |
44
|
|
|
* "3" => "3" |
45
|
|
|
* ), |
46
|
|
|
* "letters" => array( |
47
|
|
|
* "3" => "C" |
48
|
|
|
* ) |
49
|
|
|
* ) |
50
|
|
|
* ) |
51
|
|
|
* </code> |
52
|
|
|
* |
53
|
|
|
* @package forms |
54
|
|
|
* @subpackage fields-basic |
55
|
|
|
*/ |
56
|
|
|
class GroupedDropdownField extends DropdownField { |
57
|
|
|
|
58
|
|
|
public function Field($properties = array()) { |
59
|
|
|
$options = ''; |
60
|
|
|
foreach($this->getSource() as $value => $title) { |
61
|
|
|
if(is_array($title)) { |
62
|
|
|
$options .= "<optgroup label=\"$value\">"; |
63
|
|
|
foreach($title as $value2 => $title2) { |
64
|
|
|
$disabled = ''; |
65
|
|
|
if( array_key_exists($value, $this->disabledItems) |
66
|
|
|
&& is_array($this->disabledItems[$value]) |
67
|
|
|
&& in_array($value2, $this->disabledItems[$value]) ){ |
68
|
|
|
$disabled = 'disabled="disabled"'; |
69
|
|
|
} |
70
|
|
|
$selected = $value2 == $this->value ? " selected=\"selected\"" : ""; |
71
|
|
|
$options .= "<option$selected value=\"$value2\" $disabled>$title2</option>"; |
72
|
|
|
} |
73
|
|
|
$options .= "</optgroup>"; |
74
|
|
|
} else { // Fall back to the standard dropdown field |
75
|
|
|
$disabled = ''; |
76
|
|
|
if( in_array($value, $this->disabledItems) ){ |
77
|
|
|
$disabled = 'disabled="disabled"'; |
78
|
|
|
} |
79
|
|
|
$selected = $value == $this->value ? " selected=\"selected\"" : ""; |
80
|
|
|
$options .= "<option$selected value=\"$value\" $disabled>$title</option>"; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return FormField::create_tag('select', $this->getAttributes(), $options); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function Type() { |
88
|
|
|
return 'groupeddropdown dropdown'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Validate this field |
93
|
|
|
* |
94
|
|
|
* @param Validator $validator |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function validate($validator) { |
98
|
|
|
$valid = false; |
99
|
|
|
$source = $this->getSourceAsArray(); |
100
|
|
|
$disabled = $this->getDisabledItems(); |
101
|
|
|
|
102
|
|
|
if ($this->value) { |
103
|
|
|
foreach ($source as $value => $title) { |
104
|
|
|
if (is_array($title) && array_key_exists($this->value, $title)) { |
105
|
|
|
// Check that the set value is not in the list of disabled items |
106
|
|
|
if (!isset($disabled[$value]) || !in_array($this->value, $disabled[$value])) { |
107
|
|
|
$valid = true; |
108
|
|
|
} |
109
|
|
|
// Check that the value matches and is not disabled |
110
|
|
|
} elseif($this->value == $value && !in_array($this->value, $disabled)) { |
111
|
|
|
$valid = true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} elseif ($this->getHasEmptyDefault()) { |
115
|
|
|
$valid = true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!$valid) { |
119
|
|
|
$validator->validationError( |
120
|
|
|
$this->name, |
121
|
|
|
_t( |
122
|
|
|
'DropdownField.SOURCE_VALIDATION', |
123
|
|
|
"Please select a value within the list provided. {value} is not a valid option", |
124
|
|
|
array('value' => $this->value) |
125
|
|
|
), |
126
|
|
|
"validation" |
127
|
|
|
); |
128
|
|
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|