xref: /dokuwiki/inc/Form/CheckableElement.php (revision 7ec977678de1e8e9feb77e6552e2a84d8ca59361)
112a4e4d1SAndreas Gohr<?php
212a4e4d1SAndreas Gohrnamespace dokuwiki\Form;
312a4e4d1SAndreas Gohr
412a4e4d1SAndreas Gohr/**
512a4e4d1SAndreas Gohr * Class CheckableElement
612a4e4d1SAndreas Gohr *
712a4e4d1SAndreas Gohr * For Radio- and Checkboxes
812a4e4d1SAndreas Gohr *
9*7ec97767SGerrit Uitslag * @package dokuwiki\Form
1012a4e4d1SAndreas Gohr */
1112a4e4d1SAndreas Gohrclass CheckableElement extends InputElement {
1212a4e4d1SAndreas Gohr
1312a4e4d1SAndreas Gohr    /**
1412a4e4d1SAndreas Gohr     * @param string $type The type of this element
1512a4e4d1SAndreas Gohr     * @param string $name The name of this form element
1612a4e4d1SAndreas Gohr     * @param string $label The label text for this element
1712a4e4d1SAndreas Gohr     */
1812a4e4d1SAndreas Gohr    public function __construct($type, $name, $label) {
1912a4e4d1SAndreas Gohr        parent::__construct($type, $name, $label);
2012a4e4d1SAndreas Gohr        // default value is 1
2112a4e4d1SAndreas Gohr        $this->attr('value', 1);
2212a4e4d1SAndreas Gohr    }
2312a4e4d1SAndreas Gohr
2412a4e4d1SAndreas Gohr    /**
2512a4e4d1SAndreas Gohr     * Handles the useInput flag and sets the checked attribute accordingly
2612a4e4d1SAndreas Gohr     */
2712a4e4d1SAndreas Gohr    protected function prefillInput() {
2812a4e4d1SAndreas Gohr        global $INPUT;
2912a4e4d1SAndreas Gohr        list($name, $key) = $this->getInputName();
3012a4e4d1SAndreas Gohr        $myvalue = $this->val();
3112a4e4d1SAndreas Gohr
3212a4e4d1SAndreas Gohr        if(!$INPUT->has($name)) return;
3312a4e4d1SAndreas Gohr
3412a4e4d1SAndreas Gohr        if($key === null) {
3512a4e4d1SAndreas Gohr            // no key - single value
3612a4e4d1SAndreas Gohr            $value = $INPUT->str($name);
3712a4e4d1SAndreas Gohr            if($value == $myvalue) {
3812a4e4d1SAndreas Gohr                $this->attr('checked', 'checked');
3912a4e4d1SAndreas Gohr            } else {
4012a4e4d1SAndreas Gohr                $this->rmattr('checked');
4112a4e4d1SAndreas Gohr            }
4212a4e4d1SAndreas Gohr        } else {
4312a4e4d1SAndreas Gohr            // we have an array, there might be several values in it
4412a4e4d1SAndreas Gohr            $input = $INPUT->arr($name);
4512a4e4d1SAndreas Gohr            if(isset($input[$key])) {
4612a4e4d1SAndreas Gohr                $this->rmattr('checked');
4712a4e4d1SAndreas Gohr
4812a4e4d1SAndreas Gohr                // values seem to be in another sub array
4912a4e4d1SAndreas Gohr                if(is_array($input[$key])) {
5012a4e4d1SAndreas Gohr                    $input = $input[$key];
5112a4e4d1SAndreas Gohr                }
5212a4e4d1SAndreas Gohr
5312a4e4d1SAndreas Gohr                foreach($input as $value) {
5412a4e4d1SAndreas Gohr                    if($value == $myvalue) {
5512a4e4d1SAndreas Gohr                        $this->attr('checked', 'checked');
5612a4e4d1SAndreas Gohr                    }
5712a4e4d1SAndreas Gohr                }
5812a4e4d1SAndreas Gohr            }
5912a4e4d1SAndreas Gohr        }
6012a4e4d1SAndreas Gohr    }
6112a4e4d1SAndreas Gohr
6212a4e4d1SAndreas Gohr}
63