-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDropdownElement.php
More file actions
145 lines (126 loc) · 3.81 KB
/
DropdownElement.php
File metadata and controls
145 lines (126 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace dokuwiki\plugin\data\Form;
use dokuwiki\Form\InputElement;
use dokuwiki\Form\OptGroup;
/**
* Overrides some methods in parent in order to add not yet supported
* multivalue capabilities.
*/
class DropdownElement extends \dokuwiki\Form\DropdownElement
{
/** @var string[] the currently set values */
protected $values = [];
/** @var \dokuwiki\plugin\data\Form\OptGroup[] */
protected $optGroups = [];
/**
* Override the parent constructor because it instantiates an OptGroup
* which does not handle multivalues
*
* @param string $name
* @param array $options
* @param string $label
*/
public function __construct($name, $options, $label = '')
{
InputElement::__construct('dropdown', $name, $label);
$this->rmattr('type');
$this->optGroups[''] = new \dokuwiki\plugin\data\Form\OptGroup(null, $options);
$this->val('');
}
/**
* Adds multivalue capabilities
*
* @param array $value
* @return DropdownElement|string|array
*/
public function val($value = null)
{
// getter
if ($value === null) {
if (isset($this->attributes['multiple'])) {
return $this->values;
} else {
return $this->values[0];
}
}
// setter
$this->values = $this->setValuesInOptGroups((array)$value);
if (!$this->values) {
// unknown value set, select first option instead
$this->values = $this->setValuesInOptGroups((array)$this->getFirstOptionKey());
}
return $this;
}
/**
* Skips over parent's \InvalidArgumentException thrown for 'multiple'
*
* @param $name
* @param $value
* @return DropdownElement|string
*/
public function attr($name, $value = null)
{
return InputElement::attr($name, $value);
}
/**
* Returns the first option's key
*
* @return string
*/
protected function getFirstOptionKey()
{
$options = $this->options();
if (!empty($options)) {
$keys = array_keys($options);
return (string)array_shift($keys);
}
foreach ($this->optGroups as $optGroup) {
$options = $optGroup->options();
if (!empty($options)) {
$keys = array_keys($options);
return (string)array_shift($keys);
}
}
return ''; // should not happen
}
/**
* Set the value in the OptGroups, including the optgroup for the options without optgroup.
*
* @param string[] $values The values to be set
* @return string[] The values actually set
*/
protected function setValuesInOptGroups($values)
{
$valueset = [];
/** @var \dokuwiki\plugin\data\Form\OptGroup $optGroup */
foreach ($this->optGroups as $optGroup) {
$found = $optGroup->storeValues($values);
$values = array_diff($values, $found);
$valueset = array_merge($valueset, $found);
}
return $valueset;
}
/**
* @inheritDoc
*/
protected function mainElementHTML()
{
$attr = $this->attrs();
if (isset($attr['multiple'])) {
// use array notation when multiple values are allowed
$attr['name'] .= '[]';
} elseif ($this->useInput) {
// prefilling is only supported for non-multi fields
$this->prefillInput();
}
unset($attr['selected']);
$html = '<select ' . buildAttributes($attr) . '>';
$html = array_reduce(
$this->optGroups,
static fn($html, OptGroup $optGroup) => $html . $optGroup->toHTML(),
$html
);
$html .= '</select>';
return $html;
}
}