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