xref: /dokuwiki/lib/plugins/config/core/Setting/SettingOnoff.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
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        if($value === null) return null;
17
18        if(is_string($value)) {
19            if(strtolower($value) === 'false') return 0;
20            if(strtolower($value) === 'off') return 0;
21            if(trim($value) === '') return 0;
22        }
23
24        return (int) (bool) $value;
25    }
26
27    /** @inheritdoc */
28    public function html(\admin_plugin_config $plugin, $echo = false) {
29        $disable = '';
30
31        if($this->isProtected()) {
32            $value = $this->protected;
33            $disable = ' disabled="disabled"';
34        } else {
35            $value = is_null($this->local) ? $this->default : $this->local;
36        }
37
38        $key = htmlspecialchars($this->key);
39        $checked = ($value) ? ' checked="checked"' : '';
40
41        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
42        $input = '<div class="input"><input id="config___' . $key . '" name="config[' . $key .
43            ']" type="checkbox" class="checkbox" value="1"' . $checked . $disable . '/></div>';
44        return [$label, $input];
45    }
46
47    /** @inheritdoc */
48    public function update($input) {
49        if($this->isProtected()) return false;
50
51        $input = ($input) ? 1 : 0;
52        $value = is_null($this->local) ? $this->default : $this->local;
53        if($value == $input) return false;
54
55        $this->local = $input;
56        return true;
57    }
58}
59