You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

123 lines
3.8 KiB

  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', ['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 = [];
  56. foreach ($options as $key => $val) {
  57. if (is_array($val)) {
  58. if (!array_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 (
  64. array_key_exists('attrs', $val) &&
  65. is_array($val['attrs']) &&
  66. array_key_exists('selected', $val['attrs'])
  67. ) {
  68. throw new \InvalidArgumentException(
  69. 'Please use function "DropdownElement::val()" to set the selected option'
  70. );
  71. }
  72. $this->options[$key] = $val;
  73. } elseif (is_int($key)) {
  74. $this->options[$val] = ['label' => (string)$val];
  75. } else {
  76. $this->options[$key] = ['label' => (string)$val];
  77. }
  78. }
  79. return $this;
  80. }
  81. /**
  82. * The HTML representation of this element
  83. *
  84. * @return string
  85. */
  86. public function toHTML()
  87. {
  88. if ($this->attributes['label'] === null) {
  89. return $this->renderOptions();
  90. }
  91. $html = '<optgroup ' . buildAttributes($this->attrs()) . '>';
  92. $html .= $this->renderOptions();
  93. $html .= '</optgroup>';
  94. return $html;
  95. }
  96. /**
  97. * @return string
  98. */
  99. protected function renderOptions()
  100. {
  101. $html = '';
  102. foreach ($this->options as $key => $val) {
  103. $selected = in_array((string)$key, $this->values) ? ' selected="selected"' : '';
  104. $attrs = '';
  105. if (!empty($val['attrs']) && is_array($val['attrs'])) {
  106. $attrs = buildAttributes($val['attrs']);
  107. }
  108. $html .= '<option' . $selected . ' value="' . hsc($key) . '" ' . $attrs . '>';
  109. $html .= hsc($val['label']);
  110. $html .= '</option>';
  111. }
  112. return $html;
  113. }
  114. }