xref: /dokuwiki/inc/Form/OptGroup.php (revision 9d01c1d91a93cf50f37d1486481a6493e98be821)
1238a072bSMichael Grosse<?php
2238a072bSMichael Grosse
3238a072bSMichael Grossenamespace dokuwiki\Form;
4238a072bSMichael Grosse
5238a072bSMichael Grosse
6*9d01c1d9SSatoshi Saharaclass OptGroup extends Element
7*9d01c1d9SSatoshi Sahara{
8238a072bSMichael Grosse    protected $options = array();
9238a072bSMichael Grosse    protected $value;
10238a072bSMichael Grosse
11238a072bSMichael Grosse    /**
12238a072bSMichael Grosse     * @param string $label The label text for this element (will be autoescaped)
13017eef93SMichael Grosse     * @param array  $options The available options
14238a072bSMichael Grosse     */
15*9d01c1d9SSatoshi Sahara    public function __construct($label, $options)
16*9d01c1d9SSatoshi Sahara    {
17017eef93SMichael Grosse        parent::__construct('optGroup', array('label' => $label));
18238a072bSMichael Grosse        $this->options($options);
19238a072bSMichael Grosse    }
20238a072bSMichael Grosse
21238a072bSMichael Grosse    /**
229c3fca6dSMichael Grosse     * Store the given value so it can be used during rendering
23238a072bSMichael Grosse     *
24238a072bSMichael Grosse     * This is intended to be only called from within @see DropdownElement::val()
25238a072bSMichael Grosse     *
26238a072bSMichael Grosse     * @param string $value
279c3fca6dSMichael Grosse     * @return bool true if an option with the given value exists, false otherwise
28238a072bSMichael Grosse     */
29*9d01c1d9SSatoshi Sahara    public function storeValue($value)
30*9d01c1d9SSatoshi Sahara    {
31238a072bSMichael Grosse        $this->value = $value;
32238a072bSMichael Grosse        return isset($this->options[$value]);
33238a072bSMichael Grosse    }
34238a072bSMichael Grosse
35238a072bSMichael Grosse    /**
36238a072bSMichael Grosse     * Get or set the options of the optgroup
37238a072bSMichael Grosse     *
38238a072bSMichael Grosse     * Options can be given as associative array (value => label) or as an
39238a072bSMichael Grosse     * indexd array (label = value) or as an array of arrays. In the latter
40238a072bSMichael Grosse     * case an element has to look as follows:
41238a072bSMichael Grosse     * option-value => array (
42238a072bSMichael Grosse     *                 'label' => option-label,
43238a072bSMichael Grosse     *                 'attrs' => array (
44238a072bSMichael Grosse     *                                    attr-key => attr-value, ...
45238a072bSMichael Grosse     *                                  )
46238a072bSMichael Grosse     *                 )
47238a072bSMichael Grosse     *
48238a072bSMichael Grosse     * @param null|array $options
49238a072bSMichael Grosse     * @return $this|array
50238a072bSMichael Grosse     */
51*9d01c1d9SSatoshi Sahara    public function options($options = null)
52*9d01c1d9SSatoshi Sahara    {
53238a072bSMichael Grosse        if ($options === null) return $this->options;
54238a072bSMichael Grosse        if (!is_array($options)) throw new \InvalidArgumentException('Options have to be an array');
55238a072bSMichael Grosse        $this->options = array();
56238a072bSMichael Grosse        foreach ($options as $key => $val) {
571b26d16dSMichael Grosse            if (is_array($val)) {
5864159a61SAndreas Gohr                if (!key_exists('label', $val)) throw new \InvalidArgumentException(
5964159a61SAndreas Gohr                    'If option is given as array, it has to have a "label"-key!'
6064159a61SAndreas Gohr                );
6112a9bc03SLarsDW223                if (key_exists('attrs', $val) && is_array($val['attrs']) && key_exists('selected', $val['attrs'])) {
6264159a61SAndreas Gohr                    throw new \InvalidArgumentException(
6364159a61SAndreas Gohr                        'Please use function "DropdownElement::val()" to set the selected option'
6464159a61SAndreas Gohr                    );
6512a9bc03SLarsDW223                }
66238a072bSMichael Grosse                $this->options[$key] = $val;
671b26d16dSMichael Grosse            } elseif (is_int($key)) {
681b26d16dSMichael Grosse                $this->options[$val] = array('label' => (string) $val);
691b26d16dSMichael Grosse            } else {
701b26d16dSMichael Grosse                $this->options[$key] = array('label' => (string) $val);
71238a072bSMichael Grosse            }
72238a072bSMichael Grosse        }
73238a072bSMichael Grosse        return $this;
74238a072bSMichael Grosse    }
75238a072bSMichael Grosse
76238a072bSMichael Grosse    /**
77238a072bSMichael Grosse     * The HTML representation of this element
78238a072bSMichael Grosse     *
79238a072bSMichael Grosse     * @return string
80238a072bSMichael Grosse     */
81*9d01c1d9SSatoshi Sahara    public function toHTML()
82*9d01c1d9SSatoshi Sahara    {
83238a072bSMichael Grosse        if ($this->attributes['label'] === null) {
84238a072bSMichael Grosse            return $this->renderOptions();
85238a072bSMichael Grosse        }
86238a072bSMichael Grosse        $html = '<optgroup '. buildAttributes($this->attrs()) . '>';
87238a072bSMichael Grosse        $html .= $this->renderOptions();
88238a072bSMichael Grosse        $html .= '</optgroup>';
89238a072bSMichael Grosse        return $html;
90238a072bSMichael Grosse    }
91238a072bSMichael Grosse
92238a072bSMichael Grosse
93238a072bSMichael Grosse    /**
94238a072bSMichael Grosse     * @return string
95238a072bSMichael Grosse     */
96*9d01c1d9SSatoshi Sahara    protected function renderOptions()
97*9d01c1d9SSatoshi Sahara    {
98238a072bSMichael Grosse        $html = '';
99238a072bSMichael Grosse        foreach ($this->options as $key => $val) {
100389e1856SMichael Große            $selected = ((string)$key === (string)$this->value) ? ' selected="selected"' : '';
101238a072bSMichael Grosse            $attrs = '';
102d1bbf588SMichael Grosse            if (!empty($val['attrs']) && is_array($val['attrs'])) {
103d1bbf588SMichael Grosse                $attrs = buildAttributes($val['attrs']);
104238a072bSMichael Grosse            }
10564159a61SAndreas Gohr            $html .= '<option' . $selected . ' value="' . hsc($key) . '" '.$attrs.'>';
10664159a61SAndreas Gohr            $html .= hsc($val['label']);
10764159a61SAndreas Gohr            $html .= '</option>';
108238a072bSMichael Grosse        }
109238a072bSMichael Grosse        return $html;
110238a072bSMichael Grosse    }
111238a072bSMichael Grosse}
112