1238a072bSMichael Grosse<?php 2238a072bSMichael Grosse 3238a072bSMichael Grossenamespace dokuwiki\Form; 4238a072bSMichael Grosse 59d01c1d9SSatoshi Saharaclass OptGroup extends Element 69d01c1d9SSatoshi Sahara{ 7*5a10fbceSAndreas Gohr protected $options = []; 8*5a10fbceSAndreas Gohr protected $values = []; 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 */ 149d01c1d9SSatoshi Sahara public function __construct($label, $options) 159d01c1d9SSatoshi Sahara { 16017eef93SMichael Grosse parent::__construct('optGroup', array('label' => $label)); 17238a072bSMichael Grosse $this->options($options); 18238a072bSMichael Grosse } 19238a072bSMichael Grosse 20238a072bSMichael Grosse /** 21*5a10fbceSAndreas Gohr * Store the given values so they can be used during rendering 22238a072bSMichael Grosse * 23*5a10fbceSAndreas Gohr * This is intended to be only called from within DropdownElement::val() 24238a072bSMichael Grosse * 25*5a10fbceSAndreas Gohr * @param string[] $values the values to set 26*5a10fbceSAndreas Gohr * @return string[] the values that have been set (options exist) 27*5a10fbceSAndreas Gohr * @see DropdownElement::val() 28238a072bSMichael Grosse */ 29*5a10fbceSAndreas Gohr public function storeValues($values) 309d01c1d9SSatoshi Sahara { 31*5a10fbceSAndreas Gohr $this->values = []; 32*5a10fbceSAndreas Gohr foreach ($values as $value) { 33*5a10fbceSAndreas Gohr if(isset($this->options[$value])) { 34*5a10fbceSAndreas Gohr $this->values[] = $value; 35*5a10fbceSAndreas Gohr } 36*5a10fbceSAndreas Gohr } 37*5a10fbceSAndreas Gohr 38*5a10fbceSAndreas Gohr return $this->values; 39238a072bSMichael Grosse } 40238a072bSMichael Grosse 41238a072bSMichael Grosse /** 42238a072bSMichael Grosse * Get or set the options of the optgroup 43238a072bSMichael Grosse * 44238a072bSMichael Grosse * Options can be given as associative array (value => label) or as an 45238a072bSMichael Grosse * indexd array (label = value) or as an array of arrays. In the latter 46238a072bSMichael Grosse * case an element has to look as follows: 47238a072bSMichael Grosse * option-value => array ( 48238a072bSMichael Grosse * 'label' => option-label, 49238a072bSMichael Grosse * 'attrs' => array ( 50238a072bSMichael Grosse * attr-key => attr-value, ... 51238a072bSMichael Grosse * ) 52238a072bSMichael Grosse * ) 53238a072bSMichael Grosse * 54238a072bSMichael Grosse * @param null|array $options 55238a072bSMichael Grosse * @return $this|array 56238a072bSMichael Grosse */ 579d01c1d9SSatoshi Sahara public function options($options = null) 589d01c1d9SSatoshi Sahara { 59238a072bSMichael Grosse if ($options === null) return $this->options; 60238a072bSMichael Grosse if (!is_array($options)) throw new \InvalidArgumentException('Options have to be an array'); 61238a072bSMichael Grosse $this->options = array(); 62238a072bSMichael Grosse foreach ($options as $key => $val) { 631b26d16dSMichael Grosse if (is_array($val)) { 64*5a10fbceSAndreas Gohr if (!key_exists('label', $val)) { 65*5a10fbceSAndreas Gohr throw new \InvalidArgumentException( 6664159a61SAndreas Gohr 'If option is given as array, it has to have a "label"-key!' 6764159a61SAndreas Gohr ); 68*5a10fbceSAndreas Gohr } 6912a9bc03SLarsDW223 if (key_exists('attrs', $val) && is_array($val['attrs']) && key_exists('selected', $val['attrs'])) { 7064159a61SAndreas Gohr throw new \InvalidArgumentException( 7164159a61SAndreas Gohr 'Please use function "DropdownElement::val()" to set the selected option' 7264159a61SAndreas Gohr ); 7312a9bc03SLarsDW223 } 74238a072bSMichael Grosse $this->options[$key] = $val; 751b26d16dSMichael Grosse } elseif (is_int($key)) { 761b26d16dSMichael Grosse $this->options[$val] = array('label' => (string)$val); 771b26d16dSMichael Grosse } else { 781b26d16dSMichael Grosse $this->options[$key] = array('label' => (string)$val); 79238a072bSMichael Grosse } 80238a072bSMichael Grosse } 81238a072bSMichael Grosse return $this; 82238a072bSMichael Grosse } 83238a072bSMichael Grosse 84238a072bSMichael Grosse /** 85238a072bSMichael Grosse * The HTML representation of this element 86238a072bSMichael Grosse * 87238a072bSMichael Grosse * @return string 88238a072bSMichael Grosse */ 899d01c1d9SSatoshi Sahara public function toHTML() 909d01c1d9SSatoshi Sahara { 91238a072bSMichael Grosse if ($this->attributes['label'] === null) { 92238a072bSMichael Grosse return $this->renderOptions(); 93238a072bSMichael Grosse } 94238a072bSMichael Grosse $html = '<optgroup ' . buildAttributes($this->attrs()) . '>'; 95238a072bSMichael Grosse $html .= $this->renderOptions(); 96238a072bSMichael Grosse $html .= '</optgroup>'; 97238a072bSMichael Grosse return $html; 98238a072bSMichael Grosse } 99238a072bSMichael Grosse 100238a072bSMichael Grosse /** 101238a072bSMichael Grosse * @return string 102238a072bSMichael Grosse */ 1039d01c1d9SSatoshi Sahara protected function renderOptions() 1049d01c1d9SSatoshi Sahara { 105238a072bSMichael Grosse $html = ''; 106238a072bSMichael Grosse foreach ($this->options as $key => $val) { 107*5a10fbceSAndreas Gohr $selected = in_array((string)$key, $this->values) ? ' selected="selected"' : ''; 108238a072bSMichael Grosse $attrs = ''; 109d1bbf588SMichael Grosse if (!empty($val['attrs']) && is_array($val['attrs'])) { 110d1bbf588SMichael Grosse $attrs = buildAttributes($val['attrs']); 111238a072bSMichael Grosse } 11264159a61SAndreas Gohr $html .= '<option' . $selected . ' value="' . hsc($key) . '" ' . $attrs . '>'; 11364159a61SAndreas Gohr $html .= hsc($val['label']); 11464159a61SAndreas Gohr $html .= '</option>'; 115238a072bSMichael Grosse } 116238a072bSMichael Grosse return $html; 117238a072bSMichael Grosse } 118238a072bSMichael Grosse} 119