xref: /plugin/struct/types/Dropdown.php (revision 9b4977e0bced4f20e020c234b5e7df43c873613a)
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, $htmlID) {
32        $params = array(
33            'name' => $name,
34            'class' => 'struct_'.strtolower($this->getClass()),
35            'id' => $htmlID
36        );
37        $attributes = buildAttributes($params, true);
38        $html = "<select $attributes>";
39        foreach($this->getOptions() as $opt => $val) {
40            if($opt == $rawvalue) {
41                $selected = 'selected="selected"';
42            } else {
43                $selected = '';
44            }
45
46            $html .= "<option $selected value=\"" . hsc($opt) . "\">" . hsc($val) . '</option>';
47        }
48        $html .= '</select>';
49
50        return $html;
51    }
52
53    /**
54     * A dropdown that allows to pick multiple values
55     *
56     * @param string    $name
57     * @param \string[] $rawvalues
58     * @param string    $htmlID
59     *
60     * @return string
61     */
62    public function multiValueEditor($name, $rawvalues, $htmlID) {
63        $params = array(
64            'name' => $name . '[]',
65            'class' => 'struct_'.strtolower($this->getClass()),
66            'multiple' => 'multiple',
67            'size' => '5',
68            'id' => $htmlID
69        );
70        $attributes = buildAttributes($params, true);
71        $html = "<select $attributes>";
72        foreach($this->getOptions() as $raw => $opt) {
73            if(in_array($raw, $rawvalues)) {
74                $selected = 'selected="selected"';
75            } else {
76                $selected = '';
77            }
78
79            $html .= "<option $selected value=\"" . hsc($raw) . "\">" . hsc($opt) . '</option>';
80
81        }
82        $html .= '</select> ';
83        $html .= '<small>' . $this->getLang('multidropdown') . '</small>';
84        return $html;
85    }
86}
87