xref: /plugin/struct/types/Dropdown.php (revision 7f70c93c3bb39755919baa1895060601021f53dd)
169068579SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\types;
469068579SAndreas Gohr
5d6d97f60SAnna Dabrowskaclass Dropdown extends AbstractBaseType
6d6d97f60SAnna Dabrowska{
77fe2cdf2SAndreas Gohr    protected $config = [
8*24eec657SAndreas Gohr        'values' => 'one, two, three',
9*24eec657SAndreas Gohr        'combobox' => false,
107fe2cdf2SAndreas Gohr    ];
1169068579SAndreas Gohr
1269068579SAndreas Gohr    /**
13e90d3288SAndreas Gohr     * Creates the options array
14e90d3288SAndreas Gohr     *
15e90d3288SAndreas Gohr     * @return array
16e90d3288SAndreas Gohr     */
17d6d97f60SAnna Dabrowska    protected function getOptions()
18d6d97f60SAnna Dabrowska    {
19e90d3288SAndreas Gohr        $options = explode(',', $this->config['values']);
20e90d3288SAndreas Gohr        $options = array_map('trim', $options);
21e90d3288SAndreas Gohr        $options = array_filter($options);
22e90d3288SAndreas Gohr        array_unshift($options, '');
2356dced67SAndreas Gohr        $options = array_combine($options, $options);
2456dced67SAndreas Gohr        return $options;
2556dced67SAndreas Gohr    }
2656dced67SAndreas Gohr
2756dced67SAndreas Gohr    /**
2869068579SAndreas Gohr     * A Dropdown with a single value to pick
2969068579SAndreas Gohr     *
3069068579SAndreas Gohr     * @param string $name
31c0230d2cSAndreas Gohr     * @param string $rawvalue
3269068579SAndreas Gohr     * @return string
3369068579SAndreas Gohr     */
34d6d97f60SAnna Dabrowska    public function valueEditor($name, $rawvalue, $htmlID)
35d6d97f60SAnna Dabrowska    {
367fe2cdf2SAndreas Gohr        $params = [
377fe2cdf2SAndreas Gohr            'name' => $name,
387fe2cdf2SAndreas Gohr            'class' => 'struct_' . strtolower($this->getClass()),
397fe2cdf2SAndreas Gohr            'id' => $htmlID
407fe2cdf2SAndreas Gohr        ];
413e7a5b3cSMichael Große        $attributes = buildAttributes($params, true);
423e7a5b3cSMichael Große        $html = "<select $attributes>";
4356dced67SAndreas Gohr        foreach ($this->getOptions() as $opt => $val) {
44c0230d2cSAndreas Gohr            if ($opt == $rawvalue) {
4569068579SAndreas Gohr                $selected = 'selected="selected"';
4669068579SAndreas Gohr            } else {
4769068579SAndreas Gohr                $selected = '';
4869068579SAndreas Gohr            }
4969068579SAndreas Gohr
5056dced67SAndreas Gohr            $html .= "<option $selected value=\"" . hsc($opt) . "\">" . hsc($val) . '</option>';
5169068579SAndreas Gohr        }
5269068579SAndreas Gohr        $html .= '</select>';
5369068579SAndreas Gohr
54*24eec657SAndreas Gohr        if ($this->config['combobox']) {
55*24eec657SAndreas Gohr            $html = "<vanilla-combobox>$html</vanilla-combobox>";
56*24eec657SAndreas Gohr        }
57*24eec657SAndreas Gohr
5869068579SAndreas Gohr        return $html;
5969068579SAndreas Gohr    }
6069068579SAndreas Gohr
6169068579SAndreas Gohr    /**
6269068579SAndreas Gohr     * A dropdown that allows to pick multiple values
6369068579SAndreas Gohr     *
6469068579SAndreas Gohr     * @param string $name
65c0230d2cSAndreas Gohr     * @param \string[] $rawvalues
66ee983135SMichael Große     * @param string $htmlID
67ee983135SMichael Große     *
6869068579SAndreas Gohr     * @return string
6969068579SAndreas Gohr     */
70d6d97f60SAnna Dabrowska    public function multiValueEditor($name, $rawvalues, $htmlID)
71d6d97f60SAnna Dabrowska    {
727fe2cdf2SAndreas Gohr        $params = [
737fe2cdf2SAndreas Gohr            'name' => $name . '[]',
747fe2cdf2SAndreas Gohr            'class' => 'struct_' . strtolower($this->getClass()),
757fe2cdf2SAndreas Gohr            'multiple' => 'multiple',
767fe2cdf2SAndreas Gohr            'size' => '5',
777fe2cdf2SAndreas Gohr            'id' => $htmlID
787fe2cdf2SAndreas Gohr        ];
793e7a5b3cSMichael Große        $attributes = buildAttributes($params, true);
803e7a5b3cSMichael Große        $html = "<select $attributes>";
81a3f28bd5SAndreas Gohr        foreach ($this->getOptions() as $raw => $opt) {
82d329f04cSAndreas Gohr            if (in_array($raw, $rawvalues)) {
8369068579SAndreas Gohr                $selected = 'selected="selected"';
8469068579SAndreas Gohr            } else {
8569068579SAndreas Gohr                $selected = '';
8669068579SAndreas Gohr            }
8769068579SAndreas Gohr
88a3f28bd5SAndreas Gohr            $html .= "<option $selected value=\"" . hsc($raw) . "\">" . hsc($opt) . '</option>';
8969068579SAndreas Gohr        }
9069068579SAndreas Gohr        $html .= '</select> ';
9169068579SAndreas Gohr        $html .= '<small>' . $this->getLang('multidropdown') . '</small>';
92*24eec657SAndreas Gohr
93*24eec657SAndreas Gohr        if ($this->config['combobox']) {
94*24eec657SAndreas Gohr            $html = "<vanilla-combobox>$html</vanilla-combobox>";
95*24eec657SAndreas Gohr        }
96*24eec657SAndreas Gohr
9769068579SAndreas Gohr        return $html;
9869068579SAndreas Gohr    }
9969068579SAndreas Gohr}
100