xref: /dokuwiki/lib/plugins/config/core/Setting/SettingOnoff.php (revision 4b230b99ca279d0f160ebd0981007d2de6120995)
1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5/**
6 * Class setting_onoff
7 */
8class SettingOnoff extends SettingNumeric
9{
10
11    /**
12     * We treat the strings 'false' and 'off' as false
13     * @inheritdoc
14     */
15    protected function cleanValue($value)
16    {
17        if ($value === null) return null;
18
19        if (is_string($value)) {
20            if (strtolower($value) === 'false') return 0;
21            if (strtolower($value) === 'off') return 0;
22            if (trim($value) === '') return 0;
23        }
24
25        return (int) (bool) $value;
26    }
27
28    /** @inheritdoc */
29    public function html(\admin_plugin_config $plugin, $echo = false)
30    {
31        $disable = '';
32
33        if ($this->isProtected()) {
34            $value = $this->protected;
35            $disable = ' disabled="disabled"';
36        } else {
37            $value = is_null($this->local) ? $this->default : $this->local;
38        }
39
40        $key = htmlspecialchars($this->key);
41        $checked = ($value) ? ' checked="checked"' : '';
42
43        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
44        $input = '<div class="input"><input id="config___' . $key . '" name="config[' . $key .
45            ']" type="checkbox" class="checkbox" value="1"' . $checked . $disable . '/></div>';
46        return [$label, $input];
47    }
48
49    /** @inheritdoc */
50    public function update($input)
51    {
52        if ($this->isProtected()) return false;
53
54        $input = ($input) ? 1 : 0;
55        $value = is_null($this->local) ? $this->default : $this->local;
56        if ($value == $input) return false;
57
58        $this->local = $input;
59        return true;
60    }
61}
62