xref: /dokuwiki/lib/plugins/config/core/Setting/SettingMultichoice.php (revision 177d6836e2f75d0e404be9c566e61725852a1e07)
10a5b05ebSAndreas Gohr<?php
20a5b05ebSAndreas Gohr
30a5b05ebSAndreas Gohrnamespace dokuwiki\plugin\config\core\Setting;
40a5b05ebSAndreas Gohr
50a5b05ebSAndreas Gohr/**
60a5b05ebSAndreas Gohr * Class setting_multichoice
70a5b05ebSAndreas Gohr */
88c7c53b0SAndreas Gohrclass SettingMultichoice extends SettingString
98c7c53b0SAndreas Gohr{
10467c1427SAndreas Gohr    protected $choices = [];
110a5b05ebSAndreas Gohr    public $lang; //some custom language strings are stored in setting
120a5b05ebSAndreas Gohr
130a5b05ebSAndreas Gohr    /** @inheritdoc */
14*d868eb89SAndreas Gohr    public function html(\admin_plugin_config $plugin, $echo = false)
15*d868eb89SAndreas Gohr    {
160a5b05ebSAndreas Gohr        $disable = '';
170a5b05ebSAndreas Gohr        $nochoice = '';
180a5b05ebSAndreas Gohr
190a5b05ebSAndreas Gohr        if ($this->isProtected()) {
200a5b05ebSAndreas Gohr            $value = $this->protected;
210a5b05ebSAndreas Gohr            $disable = ' disabled="disabled"';
220a5b05ebSAndreas Gohr        } else {
230a5b05ebSAndreas Gohr            $value = is_null($this->local) ? $this->default : $this->local;
240a5b05ebSAndreas Gohr        }
250a5b05ebSAndreas Gohr
260a5b05ebSAndreas Gohr        // ensure current value is included
270a5b05ebSAndreas Gohr        if (!in_array($value, $this->choices)) {
280a5b05ebSAndreas Gohr            $this->choices[] = $value;
290a5b05ebSAndreas Gohr        }
300a5b05ebSAndreas Gohr        // disable if no other choices
310a5b05ebSAndreas Gohr        if (!$this->isProtected() && count($this->choices) <= 1) {
320a5b05ebSAndreas Gohr            $disable = ' disabled="disabled"';
330a5b05ebSAndreas Gohr            $nochoice = $plugin->getLang('nochoice');
340a5b05ebSAndreas Gohr        }
350a5b05ebSAndreas Gohr
360a5b05ebSAndreas Gohr        $key = htmlspecialchars($this->key);
370a5b05ebSAndreas Gohr
380a5b05ebSAndreas Gohr        $label = '<label for="config___' . $key . '">' . $this->prompt($plugin) . '</label>';
390a5b05ebSAndreas Gohr
400a5b05ebSAndreas Gohr        $input = "<div class=\"input\">\n";
410a5b05ebSAndreas Gohr        $input .= '<select class="edit" id="config___' . $key . '" name="config[' . $key . ']"' . $disable . '>' . "\n";
420a5b05ebSAndreas Gohr        foreach ($this->choices as $choice) {
430a5b05ebSAndreas Gohr            $selected = ($value == $choice) ? ' selected="selected"' : '';
440a5b05ebSAndreas Gohr            $option = $plugin->getLang($this->key . '_o_' . $choice);
450a5b05ebSAndreas Gohr            if (!$option && isset($this->lang[$this->key . '_o_' . $choice])) {
460a5b05ebSAndreas Gohr                $option = $this->lang[$this->key . '_o_' . $choice];
470a5b05ebSAndreas Gohr            }
480a5b05ebSAndreas Gohr            if (!$option) $option = $choice;
490a5b05ebSAndreas Gohr
500a5b05ebSAndreas Gohr            $choice = htmlspecialchars($choice);
510a5b05ebSAndreas Gohr            $option = htmlspecialchars($option);
520a5b05ebSAndreas Gohr            $input .= '  <option value="' . $choice . '"' . $selected . ' >' . $option . '</option>' . "\n";
530a5b05ebSAndreas Gohr        }
540a5b05ebSAndreas Gohr        $input .= "</select> $nochoice \n";
550a5b05ebSAndreas Gohr        $input .= "</div>\n";
560a5b05ebSAndreas Gohr
57467c1427SAndreas Gohr        return [$label, $input];
580a5b05ebSAndreas Gohr    }
590a5b05ebSAndreas Gohr
600a5b05ebSAndreas Gohr    /** @inheritdoc */
61*d868eb89SAndreas Gohr    public function update($input)
62*d868eb89SAndreas Gohr    {
630a5b05ebSAndreas Gohr        if (is_null($input)) return false;
640a5b05ebSAndreas Gohr        if ($this->isProtected()) return false;
650a5b05ebSAndreas Gohr
660a5b05ebSAndreas Gohr        $value = is_null($this->local) ? $this->default : $this->local;
670a5b05ebSAndreas Gohr        if ($value == $input) return false;
680a5b05ebSAndreas Gohr
690a5b05ebSAndreas Gohr        if (!in_array($input, $this->choices)) return false;
700a5b05ebSAndreas Gohr
710a5b05ebSAndreas Gohr        $this->local = $input;
720a5b05ebSAndreas Gohr        return true;
730a5b05ebSAndreas Gohr    }
740a5b05ebSAndreas Gohr}
75