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