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