1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5/**
6 * Class setting_onoff
7 */
8class SettingOnoff extends SettingNumeric
9{
10    /**
11     * We treat the strings 'false' and 'off' as false
12     * @inheritdoc
13     */
14    protected function cleanValue($value)
15    {
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    {
30        $disable = '';
31
32        if ($this->isProtected()) {
33            $value = $this->protected;
34            $disable = ' disabled="disabled"';
35        } else {
36            $value = is_null($this->local) ? $this->default : $this->local;
37        }
38
39        $key = htmlspecialchars($this->key);
40        $checked = ($value) ? ' checked="checked"' : '';
41
42        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
43        $input = '<div class="input"><input id="config___' . $key . '" name="config[' . $key .
44            ']" type="checkbox" class="checkbox" value="1"' . $checked . $disable . '/></div>';
45        return [$label, $input];
46    }
47
48    /** @inheritdoc */
49    public function update($input)
50    {
51        if ($this->isProtected()) return false;
52
53        $input = ($input) ? 1 : 0;
54        $value = is_null($this->local) ? $this->default : $this->local;
55        if ($value == $input) return false;
56
57        $this->local = $input;
58        return true;
59    }
60}
61