xref: /dokuwiki/lib/plugins/config/core/Setting/SettingMultichoice.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
1<?php
2
3namespace dokuwiki\plugin\config\core\Setting;
4
5/**
6 * Class setting_multichoice
7 */
8class SettingMultichoice extends SettingString
9{
10    protected $choices = [];
11    public $lang; //some custom language strings are stored in setting
12
13    /** @inheritdoc */
14    public function html(\admin_plugin_config $plugin, $echo = false) {
15        $disable = '';
16        $nochoice = '';
17
18        if($this->isProtected()) {
19            $value = $this->protected;
20            $disable = ' disabled="disabled"';
21        } else {
22            $value = is_null($this->local) ? $this->default : $this->local;
23        }
24
25        // ensure current value is included
26        if(!in_array($value, $this->choices)) {
27            $this->choices[] = $value;
28        }
29        // disable if no other choices
30        if(!$this->isProtected() && count($this->choices) <= 1) {
31            $disable = ' disabled="disabled"';
32            $nochoice = $plugin->getLang('nochoice');
33        }
34
35        $key = htmlspecialchars($this->key);
36
37        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
38
39        $input = "<div class=\"input\">\n";
40        $input .= '<select class="edit" id="config___' . $key . '" name="config[' . $key . ']"' . $disable . '>' . "\n";
41        foreach($this->choices as $choice) {
42            $selected = ($value == $choice) ? ' selected="selected"' : '';
43            $option = $plugin->getLang($this->key . '_o_' . $choice);
44            if(!$option && isset($this->lang[$this->key . '_o_' . $choice])) {
45                $option = $this->lang[$this->key . '_o_' . $choice];
46            }
47            if(!$option) $option = $choice;
48
49            $choice = htmlspecialchars($choice);
50            $option = htmlspecialchars($option);
51            $input .= '  <option value="' . $choice . '"' . $selected . ' >' . $option . '</option>' . "\n";
52        }
53        $input .= "</select> $nochoice \n";
54        $input .= "</div>\n";
55
56        return [$label, $input];
57    }
58
59    /** @inheritdoc */
60    public function update($input) {
61        if(is_null($input)) return false;
62        if($this->isProtected()) return false;
63
64        $value = is_null($this->local) ? $this->default : $this->local;
65        if($value == $input) return false;
66
67        if(!in_array($input, $this->choices)) return false;
68
69        $this->local = $input;
70        return true;
71    }
72}
73