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