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    {
16        $disable = '';
17        $nochoice = '';
18
19        if ($this->isProtected()) {
20            $value = $this->protected;
21            $disable = ' disabled="disabled"';
22        } else {
23            $value = is_null($this->local) ? $this->default : $this->local;
24        }
25
26        // ensure current value is included
27        if (!in_array($value, $this->choices)) {
28            $this->choices[] = $value;
29        }
30        // disable if no other choices
31        if (!$this->isProtected() && count($this->choices) <= 1) {
32            $disable = ' disabled="disabled"';
33            $nochoice = $plugin->getLang('nochoice');
34        }
35
36        $key = htmlspecialchars($this->key);
37
38        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
39
40        $input = "<div class=\"input\">\n";
41        $input .= '<select class="edit" id="config___' . $key . '" name="config[' . $key . ']"' . $disable . '>' . "\n";
42        foreach ($this->choices as $choice) {
43            $selected = ($value == $choice) ? ' selected="selected"' : '';
44            $option = $plugin->getLang($this->key . '_o_' . $choice);
45            if (!$option && isset($this->lang[$this->key . '_o_' . $choice])) {
46                $option = $this->lang[$this->key . '_o_' . $choice];
47            }
48            if (!$option) $option = $choice;
49
50            $choice = htmlspecialchars($choice);
51            $option = htmlspecialchars($option);
52            $input .= '  <option value="' . $choice . '"' . $selected . ' >' . $option . '</option>' . "\n";
53        }
54        $input .= "</select> $nochoice \n";
55        $input .= "</div>\n";
56
57        return [$label, $input];
58    }
59
60    /** @inheritdoc */
61    public function update($input)
62    {
63        if (is_null($input)) return false;
64        if ($this->isProtected()) return false;
65
66        $value = is_null($this->local) ? $this->default : $this->local;
67        if ($value == $input) return false;
68
69        if (!in_array($input, $this->choices)) return false;
70
71        $this->local = $input;
72        return true;
73    }
74}
75