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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
<?php
/**
* Sidebar widget for lists of selectable items.
*
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL 2 or later
* @since 3.1
*/
class SelectWidget extends SidebarWidget
{
/**
* Constructs the widget by defining a special template.
*
* @param string $title Diplayed title
* @param string $url Target url
* @param string $name Name of the input element
* @param string $method Request method
* @param boolean $multiple Defines whether selecting multiple values is allowed
*/
public function __construct($title, $url, $name, $method = 'get', $multiple = false)
{
$this->template = 'sidebar/select-widget';
$this->setTitle($title);
$this->setUrl($url);
$this->setSelectParameterName($name);
$this->setRequestMethod($method);
$this->setMultiple($multiple);
$this->template_variables['max_length'] = 30;
}
/**
* Sets the target url
* @param string $url Target url
*/
public function setUrl($url)
{
// TODO: Remove this some versions after 5.0
$url = html_entity_decode($url);
$query = parse_url($url, PHP_URL_QUERY);
if ($query) {
$url = str_replace('?' . $query , '', $url);
parse_str($query ?: '', $query_params);
} else {
$query_params = [];
}
$this->template_variables['url'] = $url;
$this->template_variables['params'] = $query_params;
}
/**
* Sets the maximum length of the input
* @param int $length Maximum length
*/
public function setMaxLength($length)
{
$this->template_variables['max_length'] = $length;
}
/**
* Sets the name of the select input element
* @param String $name Name of the input element
*/
public function setSelectParameterName($name)
{
$this->template_variables['name'] = $name;
}
/**
* Sets the selected value.
* @param mixed $value Selected value
*/
public function setSelection($value)
{
$this->template_variables['value'] = $value;
}
/**
* Sets the request method
* @param string $method [description]
*/
public function setRequestMethod($method)
{
$this->template_variables['method'] = $method;
}
/**
* Sets whether selecting multiple values is allowed or not
* @param bool $multiple true if selection multiple values should be allowed
*/
public function setMultiple($multiple)
{
$this->template_variables['multiple'] = $multiple;
}
/**
* Sets the options for the select element
* @param array $options Options as associative array (value => label)
* @param mixed $selected The initially selected value
*/
public function setOptions(array $options, $selected = false)
{
$selected = $selected ?: ($this->multiple ? Request::getArray($this->name) : Request::get($this->name));
//if selected is one single value
if (!is_array($selected)) {
$selected = [$selected];
}
foreach ($options as $key => $label) {
$element = new SelectElement($key, $label, in_array($key, $selected));
$this->addElement($element);
}
}
/**
* Renders the select widget
* @param array $variables Additional vaiarbles
* @return string rendered widget as ghtml
*/
public function render($variables = [])
{
$variables['__is_nested'] = $this->hasNestedElements();
//submit-upon-select is not helpful if we have the multiple version
if (!$this->template_variables['multiple']) {
if (!array_key_exists('class', $this->template_variables)) {
$this->template_variables['class'] = '';
}
$this->template_variables['class'] .= ' submit-upon-select';
}
return parent::render($variables);
}
/**
* Returns whether this element has nested subelements
* @return boolean true if element has nested subelements
*/
protected function hasNestedElements()
{
foreach ($this->elements as $element) {
if ($element instanceof SelectElement
&& ($element->isHeader() || $element->getIndentLevel() > 0))
{
return true;
}
}
// use nested if multiple
return $this->template_variables['multiple'];
}
/**
* Returns whether the given array has associative keys.
* @param array $array Array to test
* @return boolean true if array keys are not ascending from 0
*/
private static function isArrayAssoc(array $array)
{
if (!$array) {
return false;
}
return array_keys($array) !== range(0, count($array) - 1);
}
/**
* Converts the given array to a list of hidden inputs
* FIXME this duplicates what addHiddenFields() does.
* @param array $array Array to convert
* @param string $prefix Optional prefix for the input name
* @return string list of hidden inputs as html
*/
public static function arrayToHiddenInput(array $array, $prefix = '')
{
$string = '';
if (self::isArrayAssoc($array)) {
foreach ($array as $key => $value) {
if (empty($prefix)) {
$name = $key;
} else {
$name = $prefix.'['.$key.']';
}
if (is_array($value)) {
$string .= self::arrayToHiddenInput($value, $name);
} else {
$string .= sprintf('<input type="hidden" value="%s" name="%s">'."\n", htmlReady($value), htmlReady($name));
}
}
} else {
foreach ($array as $i => $item) {
if (is_array($item)) {
$string .= self::arrayToHiddenInput($item, $prefix.'['.((int) $i).']');
} else {
$string .= sprintf('<input type="hidden" name="%s[%d]" value="%s">'."\n", htmlReady($prefix), $i, htmlReady($item));
}
}
}
return $string;
}
}
|