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)) { 54*64159a61SAndreas Gohr if (!key_exists('label', $val)) throw new \InvalidArgumentException( 55*64159a61SAndreas Gohr 'If option is given as array, it has to have a "label"-key!' 56*64159a61SAndreas Gohr ); 5712a9bc03SLarsDW223 if (key_exists('attrs', $val) && is_array($val['attrs']) && key_exists('selected', $val['attrs'])) { 58*64159a61SAndreas Gohr throw new \InvalidArgumentException( 59*64159a61SAndreas Gohr 'Please use function "DropdownElement::val()" to set the selected option' 60*64159a61SAndreas Gohr ); 6112a9bc03SLarsDW223 } 62238a072bSMichael Grosse $this->options[$key] = $val; 631b26d16dSMichael Grosse } elseif(is_int($key)) { 641b26d16dSMichael Grosse $this->options[$val] = array('label' => (string) $val); 651b26d16dSMichael Grosse } else { 661b26d16dSMichael Grosse $this->options[$key] = array('label' => (string) $val); 67238a072bSMichael Grosse } 68238a072bSMichael Grosse } 69238a072bSMichael Grosse return $this; 70238a072bSMichael Grosse } 71238a072bSMichael Grosse 72238a072bSMichael Grosse 73238a072bSMichael Grosse /** 74238a072bSMichael Grosse * The HTML representation of this element 75238a072bSMichael Grosse * 76238a072bSMichael Grosse * @return string 77238a072bSMichael Grosse */ 78238a072bSMichael Grosse public function toHTML() { 79238a072bSMichael Grosse if ($this->attributes['label'] === null) { 80238a072bSMichael Grosse return $this->renderOptions(); 81238a072bSMichael Grosse } 82238a072bSMichael Grosse $html = '<optgroup '. buildAttributes($this->attrs()) . '>'; 83238a072bSMichael Grosse $html .= $this->renderOptions(); 84238a072bSMichael Grosse $html .= '</optgroup>'; 85238a072bSMichael Grosse return $html; 86238a072bSMichael Grosse } 87238a072bSMichael Grosse 88238a072bSMichael Grosse 89238a072bSMichael Grosse /** 90238a072bSMichael Grosse * @return string 91238a072bSMichael Grosse */ 92238a072bSMichael Grosse protected function renderOptions() { 93238a072bSMichael Grosse $html = ''; 94238a072bSMichael Grosse foreach($this->options as $key => $val) { 95389e1856SMichael Große $selected = ((string)$key === (string)$this->value) ? ' selected="selected"' : ''; 96238a072bSMichael Grosse $attrs = ''; 97d1bbf588SMichael Grosse if (!empty($val['attrs']) && is_array($val['attrs'])) { 98d1bbf588SMichael Grosse $attrs = buildAttributes($val['attrs']); 99238a072bSMichael Grosse } 100*64159a61SAndreas Gohr $html .= '<option' . $selected . ' value="' . hsc($key) . '" '.$attrs.'>'; 101*64159a61SAndreas Gohr $html .= hsc($val['label']); 102*64159a61SAndreas Gohr $html .= '</option>'; 103238a072bSMichael Grosse } 104238a072bSMichael Grosse return $html; 105238a072bSMichael Grosse } 106238a072bSMichael Grosse} 107