xref: /dokuwiki/inc/Form/OptGroup.php (revision 6fd0861fbb9ed43b45640925d9acfe3dba3aad7b)
1238a072bSMichael Grosse<?php
2238a072bSMichael Grosse
3238a072bSMichael Grossenamespace dokuwiki\Form;
4238a072bSMichael Grosse
59d01c1d9SSatoshi Saharaclass OptGroup extends Element
69d01c1d9SSatoshi Sahara{
75a10fbceSAndreas Gohr    protected $options = [];
85a10fbceSAndreas Gohr    protected $values = [];
9238a072bSMichael Grosse
10238a072bSMichael Grosse    /**
11238a072bSMichael Grosse     * @param string $label The label text for this element (will be autoescaped)
12017eef93SMichael Grosse     * @param array $options The available options
13238a072bSMichael Grosse     */
149d01c1d9SSatoshi Sahara    public function __construct($label, $options)
159d01c1d9SSatoshi Sahara    {
16*6fd0861fSAndreas Gohr        parent::__construct('optGroup', ['label' => $label]);
17238a072bSMichael Grosse        $this->options($options);
18238a072bSMichael Grosse    }
19238a072bSMichael Grosse
20238a072bSMichael Grosse    /**
215a10fbceSAndreas Gohr     * Store the given values so they can be used during rendering
22238a072bSMichael Grosse     *
235a10fbceSAndreas Gohr     * This is intended to be only called from within DropdownElement::val()
24238a072bSMichael Grosse     *
255a10fbceSAndreas Gohr     * @param string[] $values the values to set
265a10fbceSAndreas Gohr     * @return string[] the values that have been set (options exist)
275a10fbceSAndreas Gohr     * @see DropdownElement::val()
28238a072bSMichael Grosse     */
295a10fbceSAndreas Gohr    public function storeValues($values)
309d01c1d9SSatoshi Sahara    {
315a10fbceSAndreas Gohr        $this->values = [];
325a10fbceSAndreas Gohr        foreach ($values as $value) {
335a10fbceSAndreas Gohr            if (isset($this->options[$value])) {
345a10fbceSAndreas Gohr                $this->values[] = $value;
355a10fbceSAndreas Gohr            }
365a10fbceSAndreas Gohr        }
375a10fbceSAndreas Gohr
385a10fbceSAndreas Gohr        return $this->values;
39238a072bSMichael Grosse    }
40238a072bSMichael Grosse
41238a072bSMichael Grosse    /**
42238a072bSMichael Grosse     * Get or set the options of the optgroup
43238a072bSMichael Grosse     *
44238a072bSMichael Grosse     * Options can be given as associative array (value => label) or as an
45238a072bSMichael Grosse     * indexd array (label = value) or as an array of arrays. In the latter
46238a072bSMichael Grosse     * case an element has to look as follows:
47238a072bSMichael Grosse     * option-value => array (
48238a072bSMichael Grosse     *                 'label' => option-label,
49238a072bSMichael Grosse     *                 'attrs' => array (
50238a072bSMichael Grosse     *                                    attr-key => attr-value, ...
51238a072bSMichael Grosse     *                                  )
52238a072bSMichael Grosse     *                 )
53238a072bSMichael Grosse     *
54238a072bSMichael Grosse     * @param null|array $options
55238a072bSMichael Grosse     * @return $this|array
56238a072bSMichael Grosse     */
579d01c1d9SSatoshi Sahara    public function options($options = null)
589d01c1d9SSatoshi Sahara    {
59238a072bSMichael Grosse        if ($options === null) return $this->options;
60238a072bSMichael Grosse        if (!is_array($options)) throw new \InvalidArgumentException('Options have to be an array');
61*6fd0861fSAndreas Gohr        $this->options = [];
62238a072bSMichael Grosse        foreach ($options as $key => $val) {
631b26d16dSMichael Grosse            if (is_array($val)) {
64*6fd0861fSAndreas Gohr                if (!array_key_exists('label', $val)) {
655a10fbceSAndreas Gohr                    throw new \InvalidArgumentException(
6664159a61SAndreas Gohr                        'If option is given as array, it has to have a "label"-key!'
6764159a61SAndreas Gohr                    );
685a10fbceSAndreas Gohr                }
69*6fd0861fSAndreas Gohr                if (
70*6fd0861fSAndreas Gohr                    array_key_exists('attrs', $val) &&
71*6fd0861fSAndreas Gohr                    is_array($val['attrs']) &&
72*6fd0861fSAndreas Gohr                    array_key_exists('selected', $val['attrs'])
73*6fd0861fSAndreas Gohr                ) {
7464159a61SAndreas Gohr                    throw new \InvalidArgumentException(
7564159a61SAndreas Gohr                        'Please use function "DropdownElement::val()" to set the selected option'
7664159a61SAndreas Gohr                    );
7712a9bc03SLarsDW223                }
78238a072bSMichael Grosse                $this->options[$key] = $val;
791b26d16dSMichael Grosse            } elseif (is_int($key)) {
80*6fd0861fSAndreas Gohr                $this->options[$val] = ['label' => (string)$val];
811b26d16dSMichael Grosse            } else {
82*6fd0861fSAndreas Gohr                $this->options[$key] = ['label' => (string)$val];
83238a072bSMichael Grosse            }
84238a072bSMichael Grosse        }
85238a072bSMichael Grosse        return $this;
86238a072bSMichael Grosse    }
87238a072bSMichael Grosse
88238a072bSMichael Grosse    /**
89238a072bSMichael Grosse     * The HTML representation of this element
90238a072bSMichael Grosse     *
91238a072bSMichael Grosse     * @return string
92238a072bSMichael Grosse     */
939d01c1d9SSatoshi Sahara    public function toHTML()
949d01c1d9SSatoshi Sahara    {
95238a072bSMichael Grosse        if ($this->attributes['label'] === null) {
96238a072bSMichael Grosse            return $this->renderOptions();
97238a072bSMichael Grosse        }
98238a072bSMichael Grosse        $html = '<optgroup ' . buildAttributes($this->attrs()) . '>';
99238a072bSMichael Grosse        $html .= $this->renderOptions();
100238a072bSMichael Grosse        $html .= '</optgroup>';
101238a072bSMichael Grosse        return $html;
102238a072bSMichael Grosse    }
103238a072bSMichael Grosse
104238a072bSMichael Grosse    /**
105238a072bSMichael Grosse     * @return string
106238a072bSMichael Grosse     */
1079d01c1d9SSatoshi Sahara    protected function renderOptions()
1089d01c1d9SSatoshi Sahara    {
109238a072bSMichael Grosse        $html = '';
110238a072bSMichael Grosse        foreach ($this->options as $key => $val) {
1115a10fbceSAndreas Gohr            $selected = in_array((string)$key, $this->values) ? ' selected="selected"' : '';
112238a072bSMichael Grosse            $attrs = '';
113d1bbf588SMichael Grosse            if (!empty($val['attrs']) && is_array($val['attrs'])) {
114d1bbf588SMichael Grosse                $attrs = buildAttributes($val['attrs']);
115238a072bSMichael Grosse            }
11664159a61SAndreas Gohr            $html .= '<option' . $selected . ' value="' . hsc($key) . '" ' . $attrs . '>';
11764159a61SAndreas Gohr            $html .= hsc($val['label']);
11864159a61SAndreas Gohr            $html .= '</option>';
119238a072bSMichael Grosse        }
120238a072bSMichael Grosse        return $html;
121238a072bSMichael Grosse    }
122238a072bSMichael Grosse}
123