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