xref: /dokuwiki/inc/Form/OptGroup.php (revision 017eef936eafc9304f42c72fd26ec01cc58ef0fc)
1238a072bSMichael Grosse<?php
2238a072bSMichael Grosse
3238a072bSMichael Grossenamespace dokuwiki\Form;
4238a072bSMichael Grosse
5238a072bSMichael Grosse
6238a072bSMichael Grosseclass OptGroup extends Element {
7238a072bSMichael Grosse    protected $options = array();
8238a072bSMichael Grosse    protected $value;
9238a072bSMichael Grosse
10238a072bSMichael Grosse    /**
11238a072bSMichael Grosse     * @param string $label The label text for this element (will be autoescaped)
12*017eef93SMichael Grosse     * @param array  $options The available options
13238a072bSMichael Grosse     */
14*017eef93SMichael Grosse    public function __construct($label, $options) {
15*017eef93SMichael Grosse        parent::__construct('optGroup', array('label' => $label));
16238a072bSMichael Grosse        $this->options($options);
17238a072bSMichael Grosse    }
18238a072bSMichael Grosse
19238a072bSMichael Grosse    /**
209c3fca6dSMichael Grosse     * Store the given value so it can be used during rendering
21238a072bSMichael Grosse     *
22238a072bSMichael Grosse     * This is intended to be only called from within @see DropdownElement::val()
23238a072bSMichael Grosse     *
24238a072bSMichael Grosse     * @param string $value
259c3fca6dSMichael Grosse     * @return bool true if an option with the given value exists, false otherwise
26238a072bSMichael Grosse     */
279c3fca6dSMichael Grosse    public function storeValue($value) {
28238a072bSMichael Grosse        $this->value = $value;
29238a072bSMichael Grosse        return isset($this->options[$value]);
30238a072bSMichael Grosse    }
31238a072bSMichael Grosse
32238a072bSMichael Grosse    /**
33238a072bSMichael Grosse     * Get or set the options of the optgroup
34238a072bSMichael Grosse     *
35238a072bSMichael Grosse     * Options can be given as associative array (value => label) or as an
36238a072bSMichael Grosse     * indexd array (label = value) or as an array of arrays. In the latter
37238a072bSMichael Grosse     * case an element has to look as follows:
38238a072bSMichael Grosse     * option-value => array (
39238a072bSMichael Grosse     *                 'label' => option-label,
40238a072bSMichael Grosse     *                 'attrs' => array (
41238a072bSMichael Grosse     *                                    attr-key => attr-value, ...
42238a072bSMichael Grosse     *                                  )
43238a072bSMichael Grosse     *                 )
44238a072bSMichael Grosse     *
45238a072bSMichael Grosse     * @param null|array $options
46238a072bSMichael Grosse     * @return $this|array
47238a072bSMichael Grosse     */
48238a072bSMichael Grosse    public function options($options = null) {
49238a072bSMichael Grosse        if($options === null) return $this->options;
50238a072bSMichael Grosse        if(!is_array($options)) throw new \InvalidArgumentException('Options have to be an array');
51238a072bSMichael Grosse        $this->options = array();
52238a072bSMichael Grosse        foreach($options as $key => $val) {
53238a072bSMichael Grosse            if(is_int($key)) {
54238a072bSMichael Grosse                $this->options[$val] = array('label' => (string) $val);
55238a072bSMichael Grosse            } elseif (!is_array($val)) {
56238a072bSMichael Grosse                $this->options[$key] = array('label' => (string) $val);
57238a072bSMichael Grosse            } else {
58238a072bSMichael Grosse                if (!key_exists('label', $val)) throw new \InvalidArgumentException('If option is given as array, it has to have a "label"-key!');
59238a072bSMichael Grosse                $this->options[$key] = $val;
60238a072bSMichael Grosse            }
61238a072bSMichael Grosse        }
62238a072bSMichael Grosse        return $this;
63238a072bSMichael Grosse    }
64238a072bSMichael Grosse
65238a072bSMichael Grosse
66238a072bSMichael Grosse    /**
67238a072bSMichael Grosse     * The HTML representation of this element
68238a072bSMichael Grosse     *
69238a072bSMichael Grosse     * @return string
70238a072bSMichael Grosse     */
71238a072bSMichael Grosse    public function toHTML() {
72238a072bSMichael Grosse        if ($this->attributes['label'] === null) {
73238a072bSMichael Grosse            return $this->renderOptions();
74238a072bSMichael Grosse        }
75238a072bSMichael Grosse        $html = '<optgroup '. buildAttributes($this->attrs()) . '>';
76238a072bSMichael Grosse        $html .= $this->renderOptions();
77238a072bSMichael Grosse        $html .= '</optgroup>';
78238a072bSMichael Grosse        return $html;
79238a072bSMichael Grosse    }
80238a072bSMichael Grosse
81238a072bSMichael Grosse
82238a072bSMichael Grosse    /**
83238a072bSMichael Grosse     * @return string
84238a072bSMichael Grosse     */
85238a072bSMichael Grosse    protected function renderOptions() {
86238a072bSMichael Grosse        $html = '';
87238a072bSMichael Grosse        foreach($this->options as $key => $val) {
88238a072bSMichael Grosse            $selected = ($key == $this->value) ? ' selected="selected"' : '';
89238a072bSMichael Grosse            $attrs = '';
90d1bbf588SMichael Grosse            if (!empty($val['attrs']) && is_array($val['attrs'])) {
91d1bbf588SMichael Grosse                $attrs = buildAttributes($val['attrs']);
92238a072bSMichael Grosse            }
93238a072bSMichael Grosse            $html .= '<option' . $selected . ' value="' . hsc($key) . '" '.$attrs.'>' . hsc($val['label']) . '</option>';
94238a072bSMichael Grosse        }
95238a072bSMichael Grosse        return $html;
96238a072bSMichael Grosse    }
97238a072bSMichael Grosse}
98