1<?php
2
3namespace dokuwiki\plugin\struct\types;
4
5class Checkbox extends AbstractBaseType
6{
7    protected $config = array(
8        'values' => 'one, two, three',
9    );
10
11    /**
12     * Creates the options array
13     *
14     * @return array
15     */
16    protected function getOptions()
17    {
18        $options = explode(',', $this->config['values']);
19        $options = array_map('trim', $options);
20        $options = array_filter($options);
21        return $options;
22    }
23
24    /**
25     * A single checkbox, additional values are ignored
26     *
27     * @param string $name
28     * @param string $rawvalue
29     * @param string $htmlID
30     *
31     * @return string
32     */
33    public function valueEditor($name, $rawvalue, $htmlID)
34    {
35        $options = $this->getOptions();
36        $opt = array_shift($options);
37
38        if ($rawvalue == $opt) {
39            $checked = 'checked';
40        } else {
41            $checked = '';
42        }
43        $opt = hsc($opt);
44        $params = array(
45            'name' => $name,
46            'value' => $opt,
47            'class' => 'struct_' . strtolower($this->getClass()),
48            'type' => 'checkbox',
49            'id' => $htmlID,
50            'checked' => $checked,
51        );
52        $attributes = buildAttributes($params, true);
53        return "<label><input $attributes>&nbsp;$opt</label>";
54    }
55
56    /**
57     * Multiple checkboxes
58     *
59     * @param string $name
60     * @param \string[] $rawvalues
61     * @param string $htmlID
62     *
63     * @return string
64     */
65    public function multiValueEditor($name, $rawvalues, $htmlID)
66    {
67        $class = 'struct_' . strtolower($this->getClass());
68
69        $html = '';
70        foreach ($this->getOptions() as $opt) {
71            if (in_array($opt, $rawvalues)) {
72                $checked = 'checked';
73            } else {
74                $checked = '';
75            }
76
77            $params = array(
78                'name' => $name . '[]',
79                'value' => $opt,
80                'class' => $class,
81                'type' => 'checkbox',
82                'id' => $htmlID,
83                'checked' => $checked,
84            );
85            $attributes = buildAttributes($params, true);
86            $htmlID = '';
87
88            $opt = hsc($opt);
89            $html .= "<label><input $attributes>&nbsp;$opt</label>";
90        }
91        return $html;
92    }
93}
94