xref: /plugin/struct/types/Dropdown.php (revision 69068579f7c361d64f60f59b110aee637760884e)
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     * A Dropdown with a single value to pick
12     *
13     * @param string $name
14     * @param string $value
15     * @return string
16     */
17    public function valueEditor($name, $value) {
18        $class = 'struct_'.strtolower($this->getClass());
19
20        $options = explode(',', $this->config['values']);
21        $options = array_map('trim', $options);
22        $options = array_filter($options);
23
24        $name = hsc($name);
25        $html = "<select name=\"$name\" class=\"$class\">";
26        foreach ($options as $opt) {
27            if($opt == $value) {
28                $selected = 'selected="selected"';
29            } else {
30                $selected = '';
31            }
32
33            $html .= "<option $selected value=\"".hsc($opt)."\">".hsc($opt).'</option>';
34        }
35        $html .= '</select>';
36
37        return $html;
38    }
39
40    /**
41     * A dropdown that allows to pick multiple values
42     *
43     * @param string $name
44     * @param \string[] $values
45     * @return string
46     */
47    public function multiValueEditor($name, $values) {
48        $class = 'struct_'.strtolower($this->getClass());
49
50        $options = explode(',', $this->config['values']);
51        $options = array_map('trim', $options);
52        $options = array_filter($options);
53
54        $name = hsc($name);
55        $html = "<select name=\"{$name}[]\" class=\"$class\" multiple=\"multiple\" size=\"5\">";
56        foreach ($options as $opt) {
57            if(in_array($opt, $values)) {
58                $selected = 'selected="selected"';
59            } else {
60                $selected = '';
61            }
62
63            $html .= "<option $selected value=\"".hsc($opt)."\">".hsc($opt).'</option>';
64
65        }
66        $html .= '</select> ';
67        $html .= '<small>'.$this->getLang('multidropdown').'</small>';
68        return $html;
69    }
70
71}
72