xref: /plugin/struct/types/Dropdown.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5class Dropdown extends AbstractBaseType
6{
7    protected $config = ['values' => 'one, two, three'];
8
9    /**
10     * Creates the options array
11     *
12     * @return array
13     */
14    protected function getOptions()
15    {
16        $options = explode(',', $this->config['values']);
17        $options = array_map('trim', $options);
18        $options = array_filter($options);
19        array_unshift($options, '');
20        $options = array_combine($options, $options);
21        return $options;
22    }
23
24    /**
25     * A Dropdown with a single value to pick
26     *
27     * @param string $name
28     * @param string $rawvalue
29     * @return string
30     */
31    public function valueEditor($name, $rawvalue, $htmlID)
32    {
33        $params = ['name' => $name, 'class' => 'struct_' . strtolower($this->getClass()), 'id' => $htmlID];
34        $attributes = buildAttributes($params, true);
35        $html = "<select $attributes>";
36        foreach ($this->getOptions() as $opt => $val) {
37            if ($opt == $rawvalue) {
38                $selected = 'selected="selected"';
39            } else {
40                $selected = '';
41            }
42
43            $html .= "<option $selected value=\"" . hsc($opt) . "\">" . hsc($val) . '</option>';
44        }
45        $html .= '</select>';
46
47        return $html;
48    }
49
50    /**
51     * A dropdown that allows to pick multiple values
52     *
53     * @param string $name
54     * @param \string[] $rawvalues
55     * @param string $htmlID
56     *
57     * @return string
58     */
59    public function multiValueEditor($name, $rawvalues, $htmlID)
60    {
61        $params = ['name' => $name . '[]', 'class' => 'struct_' . strtolower($this->getClass()), 'multiple' => 'multiple', 'size' => '5', 'id' => $htmlID];
62        $attributes = buildAttributes($params, true);
63        $html = "<select $attributes>";
64        foreach ($this->getOptions() as $raw => $opt) {
65            if (in_array($raw, $rawvalues)) {
66                $selected = 'selected="selected"';
67            } else {
68                $selected = '';
69            }
70
71            $html .= "<option $selected value=\"" . hsc($raw) . "\">" . hsc($opt) . '</option>';
72        }
73        $html .= '</select> ';
74        $html .= '<small>' . $this->getLang('multidropdown') . '</small>';
75        return $html;
76    }
77}
78