xref: /dokuwiki/lib/plugins/config/core/Setting/SettingMulticheckbox.php (revision d868eb89f182718a31113373a6272670bd7f8012)
1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5/**
6 * Class setting_multicheckbox
7 */
8class SettingMulticheckbox extends SettingString
9{
10
11    protected $choices = [];
12    protected $combine = [];
13    protected $other = 'always';
14
15    /** @inheritdoc */
16    public function update($input)
17    {
18        if($this->isProtected()) return false;
19
20        // split any combined values + convert from array to comma separated string
21        $input = $input ?: [];
22        $input = $this->array2str($input);
23
24        $value = is_null($this->local) ? $this->default : $this->local;
25        if($value == $input) return false;
26
27        if($this->pattern && !preg_match($this->pattern, $input)) {
28            $this->error = true;
29            $this->input = $input;
30            return false;
31        }
32
33        $this->local = $input;
34        return true;
35    }
36
37    /** @inheritdoc */
38    public function html(\admin_plugin_config $plugin, $echo = false)
39    {
40
41        $disable = '';
42
43        if ($this->isProtected()) {
44            $value = $this->protected;
45            $disable = 'disabled="disabled"';
46        } elseif ($echo && $this->error) {
47            $value = $this->input;
48        } else {
49            $value = is_null($this->local) ? $this->default : $this->local;
50        }
51
52        $key = htmlspecialchars($this->key);
53
54        // convert from comma separated list into array + combine complimentary actions
55        $value = $this->str2array($value);
56        $default = $this->str2array($this->default);
57
58        $input = '';
59        foreach($this->choices as $choice) {
60            $idx = array_search($choice, $value);
61            $idx_default = array_search($choice, $default);
62
63            $checked = ($idx !== false) ? 'checked="checked"' : '';
64
65            // @todo ideally this would be handled using a second class of "default"
66            $class = (($idx !== false) === (false !== $idx_default)) ? " selectiondefault" : "";
67
68            $prompt = ($plugin->getLang($this->key . '_' . $choice) ?: htmlspecialchars($choice));
69
70            $input .= '<div class="selection' . $class . '">' . "\n";
71            $input .= '<label for="config___' . $key . '_' . $choice . '">' . $prompt . "</label>\n";
72            $input .= '<input id="config___' . $key . '_' . $choice . '" name="config[' . $key .
73                '][]" type="checkbox" class="checkbox" value="' . $choice . '" ' . $disable . ' ' . $checked . "/>\n";
74            $input .= "</div>\n";
75
76            // remove this action from the disabledactions array
77            if($idx !== false) unset($value[$idx]);
78            if($idx_default !== false) unset($default[$idx_default]);
79        }
80
81        // handle any remaining values
82        if($this->other != 'never') {
83            $other = implode(',', $value);
84            // test equivalent to ($this->_other == 'always' || ($other && $this->_other == 'exists')
85            // use != 'exists' rather than == 'always' to ensure invalid values default to 'always'
86            if($this->other != 'exists' || $other) {
87
88                $class = (
89                    (count($default) === count($value)) &&
90                    (count($value) === count(array_intersect($value, $default)))
91                ) ?
92                    " selectiondefault" : "";
93
94                $input .= '<div class="other' . $class . '">' . "\n";
95                $input .= '<label for="config___' . $key . '_other">' .
96                    $plugin->getLang($key . '_other') .
97                    "</label>\n";
98                $input .= '<input id="config___' . $key . '_other" name="config[' . $key .
99                    '][other]" type="text" class="edit" value="' . htmlspecialchars($other) .
100                    '" ' . $disable . " />\n";
101                $input .= "</div>\n";
102            }
103        }
104        $label = '<label>' . $this->prompt($plugin) . '</label>';
105        return [$label, $input];
106    }
107
108    /**
109     * convert comma separated list to an array and combine any complimentary values
110     *
111     * @param string $str
112     * @return array
113     */
114    protected function str2array($str)
115    {
116        $array = explode(',', $str);
117
118        if(!empty($this->combine)) {
119            foreach($this->combine as $key => $combinators) {
120                $idx = [];
121                foreach($combinators as $val) {
122                    if(($idx[] = array_search($val, $array)) === false) break;
123                }
124
125                if(count($idx) && $idx[count($idx) - 1] !== false) {
126                    foreach($idx as $i) unset($array[$i]);
127                    $array[] = $key;
128                }
129            }
130        }
131
132        return $array;
133    }
134
135    /**
136     * convert array of values + other back to a comma separated list, incl. splitting any combined values
137     *
138     * @param array $input
139     * @return string
140     */
141    protected function array2str($input)
142    {
143
144        // handle other
145        $other = trim($input['other']);
146        $other = empty($other) ? [] : explode(',', str_replace(' ', '', $input['other']));
147        unset($input['other']);
148
149        $array = array_unique(array_merge($input, $other));
150
151        // deconstruct any combinations
152        if(!empty($this->combine)) {
153            foreach($this->combine as $key => $combinators) {
154
155                $idx = array_search($key, $array);
156                if($idx !== false) {
157                    unset($array[$idx]);
158                    $array = array_merge($array, $combinators);
159                }
160            }
161        }
162
163        return implode(',', array_unique($array));
164    }
165}
166