OptGroup.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace dokuwiki\Form;
  3. class OptGroup extends Element
  4. {
  5. protected $options = [];
  6. protected $values = [];
  7. /**
  8. * @param string $label The label text for this element (will be autoescaped)
  9. * @param array $options The available options
  10. */
  11. public function __construct($label, $options)
  12. {
  13. parent::__construct('optGroup', array('label' => $label));
  14. $this->options($options);
  15. }
  16. /**
  17. * Store the given values so they can be used during rendering
  18. *
  19. * This is intended to be only called from within DropdownElement::val()
  20. *
  21. * @param string[] $values the values to set
  22. * @return string[] the values that have been set (options exist)
  23. * @see DropdownElement::val()
  24. */
  25. public function storeValues($values)
  26. {
  27. $this->values = [];
  28. foreach ($values as $value) {
  29. if(isset($this->options[$value])) {
  30. $this->values[] = $value;
  31. }
  32. }
  33. return $this->values;
  34. }
  35. /**
  36. * Get or set the options of the optgroup
  37. *
  38. * Options can be given as associative array (value => label) or as an
  39. * indexd array (label = value) or as an array of arrays. In the latter
  40. * case an element has to look as follows:
  41. * option-value => array (
  42. * 'label' => option-label,
  43. * 'attrs' => array (
  44. * attr-key => attr-value, ...
  45. * )
  46. * )
  47. *
  48. * @param null|array $options
  49. * @return $this|array
  50. */
  51. public function options($options = null)
  52. {
  53. if ($options === null) return $this->options;
  54. if (!is_array($options)) throw new \InvalidArgumentException('Options have to be an array');
  55. $this->options = array();
  56. foreach ($options as $key => $val) {
  57. if (is_array($val)) {
  58. if (!key_exists('label', $val)) {
  59. throw new \InvalidArgumentException(
  60. 'If option is given as array, it has to have a "label"-key!'
  61. );
  62. }
  63. if (key_exists('attrs', $val) && is_array($val['attrs']) && key_exists('selected', $val['attrs'])) {
  64. throw new \InvalidArgumentException(
  65. 'Please use function "DropdownElement::val()" to set the selected option'
  66. );
  67. }
  68. $this->options[$key] = $val;
  69. } elseif (is_int($key)) {
  70. $this->options[$val] = array('label' => (string)$val);
  71. } else {
  72. $this->options[$key] = array('label' => (string)$val);
  73. }
  74. }
  75. return $this;
  76. }
  77. /**
  78. * The HTML representation of this element
  79. *
  80. * @return string
  81. */
  82. public function toHTML()
  83. {
  84. if ($this->attributes['label'] === null) {
  85. return $this->renderOptions();
  86. }
  87. $html = '<optgroup ' . buildAttributes($this->attrs()) . '>';
  88. $html .= $this->renderOptions();
  89. $html .= '</optgroup>';
  90. return $html;
  91. }
  92. /**
  93. * @return string
  94. */
  95. protected function renderOptions()
  96. {
  97. $html = '';
  98. foreach ($this->options as $key => $val) {
  99. $selected = in_array((string)$key, $this->values) ? ' selected="selected"' : '';
  100. $attrs = '';
  101. if (!empty($val['attrs']) && is_array($val['attrs'])) {
  102. $attrs = buildAttributes($val['attrs']);
  103. }
  104. $html .= '<option' . $selected . ' value="' . hsc($key) . '" ' . $attrs . '>';
  105. $html .= hsc($val['label']);
  106. $html .= '</option>';
  107. }
  108. return $html;
  109. }
  110. }