1<?php
2
3namespace dokuwiki\plugin\data\Form;
4
5class OptGroup extends \dokuwiki\Form\OptGroup
6{
7    protected $values = [];
8
9    /**
10     * Store the given values so they can be used during rendering
11     *
12     * This is intended to be only called from within DropdownElement::val()
13     *
14     * @param string[] $values the values to set
15     * @return string[] the values that have been set (options exist)
16     * @see DropdownElement::val()
17     */
18    public function storeValues($values)
19    {
20        $this->values = [];
21        foreach ($values as $value) {
22            if (isset($this->options[$value])) {
23                $this->values[] = $value;
24            }
25        }
26
27        return $this->values;
28    }
29
30    /**
31     * @return string
32     */
33    protected function renderOptions()
34    {
35        $html = '';
36        foreach ($this->options as $key => $val) {
37            $selected = in_array((string)$key, $this->values) ? ' selected="selected"' : '';
38            $attrs = '';
39            if (!empty($val['attrs']) && is_array($val['attrs'])) {
40                $attrs = buildAttributes($val['attrs']);
41            }
42            $html .= '<option' . $selected . ' value="' . hsc($key) . '" ' . $attrs . '>';
43            $html .= hsc($val['label']);
44            $html .= '</option>';
45        }
46        return $html;
47    }
48}
49