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