xref: /plugin/struct/types/Checkbox.php (revision 37d0530f92e9df6b2c4a35e31f369f5a79f6cf0b)
1<?php
2namespace dokuwiki\plugin\struct\types;
3
4class Checkbox 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        return $options;
20    }
21
22    /**
23     * A single checkbox, additional values are ignored
24     *
25     * @param string $name
26     * @param string $value
27     * @param bool $isRaw ignored
28     * @return string
29     */
30    public function valueEditor($name, $value, $isRaw = false) {
31        $class = 'struct_' . strtolower($this->getClass());
32
33        $name = hsc($name);
34        $options = $this->getOptions();
35        $opt = array_shift($options);
36
37        if($value == $opt) {
38            $checked = 'checked="checked"';
39        } else {
40            $checked = '';
41        }
42        $opt = hsc($opt);
43        $html = "<label><input type=\"checkbox\" name=\"$name\" class=\"$class\" value=\"$opt\" $checked>&nbsp;$opt</label>";
44        return $html;
45    }
46
47    /**
48     * Multiple checkboxes
49     *
50     * @param string $name
51     * @param \string[] $values
52     * @return string
53     */
54    public function multiValueEditor($name, $values) {
55        $class = 'struct_' . strtolower($this->getClass());
56
57        $name = hsc($name);
58        $html = '';
59        foreach($this->getOptions() as $opt) {
60            if(in_array($opt, $values)) {
61                $checked = 'checked="checked"';
62            } else {
63                $checked = '';
64            }
65
66            $opt = hsc($opt);
67            $html .= "<label><input type=\"checkbox\" name=\"{$name}[]\" class=\"$class\" value=\"$opt\" $checked>&nbsp;$opt</label>";
68        }
69        return $html;
70    }
71
72}
73