xref: /plugin/struct/types/Dropdown.php (revision 16b7d914c0b69bf27d1ddd8a08e05beddbeedf02)
1<?php
2namespace 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     * @return string
29     */
30    public function valueEditor($name, $value) {
31        $class = 'struct_' . strtolower($this->getClass());
32
33        $name = hsc($name);
34        $html = "<select name=\"$name\" class=\"$class\">";
35        foreach($this->getOptions() as $opt) {
36            if($opt == $value) {
37                $selected = 'selected="selected"';
38            } else {
39                $selected = '';
40            }
41
42            $html .= "<option $selected value=\"" . hsc($opt) . "\">" . hsc($opt) . '</option>';
43        }
44        $html .= '</select>';
45
46        return $html;
47    }
48
49    /**
50     * A dropdown that allows to pick multiple values
51     *
52     * @param string $name
53     * @param \string[] $values
54     * @return string
55     */
56    public function multiValueEditor($name, $values) {
57        $class = 'struct_' . strtolower($this->getClass());
58
59        $name = hsc($name);
60        $html = "<select name=\"{$name}[]\" class=\"$class\" multiple=\"multiple\" size=\"5\">";
61        foreach($this->getOptions() as $opt) {
62            if(in_array($opt, $values)) {
63                $selected = 'selected="selected"';
64            } else {
65                $selected = '';
66            }
67
68            $html .= "<option $selected value=\"" . hsc($opt) . "\">" . hsc($opt) . '</option>';
69
70        }
71        $html .= '</select> ';
72        $html .= '<small>' . $this->getLang('multidropdown') . '</small>';
73        return $html;
74    }
75
76}
77