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) 12017eef93SMichael Grosse * @param array $options The available options 13238a072bSMichael Grosse */ 14017eef93SMichael Grosse public function __construct($label, $options) { 15017eef93SMichael 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) { 531b26d16dSMichael Grosse if (is_array($val)) { 54238a072bSMichael Grosse if (!key_exists('label', $val)) throw new \InvalidArgumentException('If option is given as array, it has to have a "label"-key!'); 5512a9bc03SLarsDW223 if (key_exists('attrs', $val) && is_array($val['attrs']) && key_exists('selected', $val['attrs'])) { 5612a9bc03SLarsDW223 throw new \InvalidArgumentException('Please use function "DropdownElement::val()" to set the selected option'); 5712a9bc03SLarsDW223 } 58238a072bSMichael Grosse $this->options[$key] = $val; 591b26d16dSMichael Grosse } elseif(is_int($key)) { 601b26d16dSMichael Grosse $this->options[$val] = array('label' => (string) $val); 611b26d16dSMichael Grosse } else { 621b26d16dSMichael Grosse $this->options[$key] = array('label' => (string) $val); 63238a072bSMichael Grosse } 64238a072bSMichael Grosse } 65238a072bSMichael Grosse return $this; 66238a072bSMichael Grosse } 67238a072bSMichael Grosse 68238a072bSMichael Grosse 69238a072bSMichael Grosse /** 70238a072bSMichael Grosse * The HTML representation of this element 71238a072bSMichael Grosse * 72238a072bSMichael Grosse * @return string 73238a072bSMichael Grosse */ 74238a072bSMichael Grosse public function toHTML() { 75238a072bSMichael Grosse if ($this->attributes['label'] === null) { 76238a072bSMichael Grosse return $this->renderOptions(); 77238a072bSMichael Grosse } 78238a072bSMichael Grosse $html = '<optgroup '. buildAttributes($this->attrs()) . '>'; 79238a072bSMichael Grosse $html .= $this->renderOptions(); 80238a072bSMichael Grosse $html .= '</optgroup>'; 81238a072bSMichael Grosse return $html; 82238a072bSMichael Grosse } 83238a072bSMichael Grosse 84238a072bSMichael Grosse 85238a072bSMichael Grosse /** 86238a072bSMichael Grosse * @return string 87238a072bSMichael Grosse */ 88238a072bSMichael Grosse protected function renderOptions() { 89238a072bSMichael Grosse $html = ''; 90238a072bSMichael Grosse foreach($this->options as $key => $val) { 91*389e1856SMichael Große $selected = ((string)$key === (string)$this->value) ? ' selected="selected"' : ''; 92238a072bSMichael Grosse $attrs = ''; 93d1bbf588SMichael Grosse if (!empty($val['attrs']) && is_array($val['attrs'])) { 94d1bbf588SMichael Grosse $attrs = buildAttributes($val['attrs']); 95238a072bSMichael Grosse } 96238a072bSMichael Grosse $html .= '<option' . $selected . ' value="' . hsc($key) . '" '.$attrs.'>' . hsc($val['label']) . '</option>'; 97238a072bSMichael Grosse } 98238a072bSMichael Grosse return $html; 99238a072bSMichael Grosse } 100238a072bSMichael Grosse} 101