1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Elgg select input |
4
|
|
|
* Displays a select input field |
5
|
|
|
* |
6
|
|
|
* @warning Values of FALSE or NULL will match '' (empty string) but not 0. |
7
|
|
|
* |
8
|
|
|
* @package Elgg |
9
|
|
|
* @subpackage Core |
10
|
|
|
* |
11
|
|
|
* @uses $vars['value'] The current value or an array of current values if multiple is true |
12
|
|
|
* @uses $vars['options'] An array of strings representing the options for the dropdown field |
13
|
|
|
* @uses $vars['options_values'] An associative array of "value" => "option" |
14
|
|
|
* where "value" is the name and "option" is |
15
|
|
|
* the value displayed on the button. Replaces |
16
|
|
|
* $vars['options'] when defined. |
17
|
|
|
* @uses $vars['multiple'] If true, multiselect of values will be allowed in the select box |
18
|
|
|
* @uses $vars['class'] Additional CSS class |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
$vars['class'] = (array) elgg_extract('class', $vars, []); |
22
|
|
|
$vars['class'][] = 'elgg-input-dropdown'; |
23
|
|
|
|
24
|
|
|
$defaults = array( |
25
|
|
|
'disabled' => false, |
26
|
|
|
'value' => '', |
27
|
|
|
'options_values' => array(), |
28
|
|
|
'options' => array(), |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$vars = array_merge($defaults, $vars); |
32
|
|
|
|
33
|
|
|
$options_values = $vars['options_values']; |
34
|
|
|
unset($vars['options_values']); |
35
|
|
|
|
36
|
|
|
$options = $vars['options']; |
37
|
|
|
unset($vars['options']); |
38
|
|
|
|
39
|
|
|
$value = is_array($vars['value']) ? $vars['value'] : array($vars['value']); |
40
|
|
|
$value = array_map('strval', $value); |
41
|
|
|
unset($vars['value']); |
42
|
|
|
|
43
|
|
|
$vars['multiple'] = !empty($vars['multiple']); |
44
|
|
|
|
45
|
|
|
// Add trailing [] to name if multiple is enabled to allow the form to send multiple values |
46
|
|
View Code Duplication |
if ($vars['multiple'] && !empty($vars['name']) && is_string($vars['name'])) { |
47
|
|
|
if (substr($vars['name'], -2) != '[]') { |
48
|
|
|
$vars['name'] = $vars['name'] . '[]'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$options_list = ''; |
53
|
|
|
|
54
|
|
View Code Duplication |
if ($options_values) { |
|
|
|
|
55
|
|
|
foreach ($options_values as $opt_value => $option) { |
56
|
|
|
|
57
|
|
|
$option_attrs = array( |
58
|
|
|
'value' => $opt_value, |
59
|
|
|
'selected' => in_array((string)$opt_value, $value), |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$options_list .= elgg_format_element('option', $option_attrs, $option); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
if (is_array($options)) { |
66
|
|
|
foreach ($options as $option) { |
67
|
|
|
|
68
|
|
|
$option_attrs = ['selected' => in_array((string)$option, $value)]; |
69
|
|
|
|
70
|
|
|
$options_list .= elgg_format_element('option', $option_attrs, $option); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
echo elgg_format_element('select', $vars, $options_list); |
76
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.