xref: /plugin/struct/types/Dropdown.php (revision 7938ca48e9616e2e020a922137123f8442612482)
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        $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) {
32        $class = 'struct_' . strtolower($this->getClass());
33
34        $name = hsc($name);
35        $html = "<select name=\"$name\" class=\"$class\">";
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     * @return string
56     */
57    public function multiValueEditor($name, $rawvalues) {
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, $rawvalues)) {
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