xref: /dokuwiki/inc/Form/CheckableElement.php (revision c5cec702c5deebbeddeb00147d906a0cd0fc7ac0)
1<?php
2namespace dokuwiki\Form;
3
4/**
5 * Class CheckableElement
6 *
7 * For Radio- and Checkboxes
8 *
9 * @package DokuForm
10 */
11class CheckableElement extends InputElement {
12
13    /**
14     * @param string $type The type of this element
15     * @param string $name The name of this form element
16     * @param string $label The label text for this element
17     */
18    public function __construct($type, $name, $label) {
19        parent::__construct($type, $name, $label);
20        // default value is 1
21        $this->attr('value', 1);
22    }
23
24    /**
25     * Handles the useInput flag and sets the checked attribute accordingly
26     */
27    protected function prefillInput() {
28        global $INPUT;
29        list($name, $key) = $this->getInputName();
30        $myvalue = $this->val();
31
32        if(!$INPUT->has($name)) return;
33
34        if($key === null) {
35            // no key - single value
36            $value = $INPUT->str($name);
37            if($value == $myvalue) {
38                $this->attr('checked', 'checked');
39            } else {
40                $this->rmattr('checked');
41            }
42        } else {
43            // we have an array, there might be several values in it
44            $input = $INPUT->arr($name);
45            if(isset($input[$key])) {
46                $this->rmattr('checked');
47
48                // values seem to be in another sub array
49                if(is_array($input[$key])) {
50                    $input = $input[$key];
51                }
52
53                foreach($input as $value) {
54                    if($value == $myvalue) {
55                        $this->attr('checked', 'checked');
56                    }
57                }
58            }
59        }
60    }
61
62}
63