XmlSelect.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Class for generating HTML <select> elements.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Class for generating HTML <select> or <datalist> elements.
  24. */
  25. class XmlSelect {
  26. protected $options = [];
  27. protected $default = false;
  28. protected $tagName = 'select';
  29. protected $attributes = [];
  30. public function __construct( $name = false, $id = false, $default = false ) {
  31. if ( $name ) {
  32. $this->setAttribute( 'name', $name );
  33. }
  34. if ( $id ) {
  35. $this->setAttribute( 'id', $id );
  36. }
  37. if ( $default !== false ) {
  38. $this->default = $default;
  39. }
  40. }
  41. /**
  42. * @param string|array $default
  43. */
  44. public function setDefault( $default ) {
  45. $this->default = $default;
  46. }
  47. /**
  48. * @param string|array $tagName
  49. */
  50. public function setTagName( $tagName ) {
  51. $this->tagName = $tagName;
  52. }
  53. /**
  54. * @param string $name
  55. * @param string $value
  56. */
  57. public function setAttribute( $name, $value ) {
  58. $this->attributes[$name] = $value;
  59. }
  60. /**
  61. * @param string $name
  62. * @return string|null
  63. */
  64. public function getAttribute( $name ) {
  65. return $this->attributes[$name] ?? null;
  66. }
  67. /**
  68. * @param string $label
  69. * @param string|false $value If not given, assumed equal to $label
  70. */
  71. public function addOption( $label, $value = false ) {
  72. $value = $value !== false ? $value : $label;
  73. $this->options[] = [ $label => $value ];
  74. }
  75. /**
  76. * This accepts an array of form
  77. * label => value
  78. * label => ( label => value, label => value )
  79. *
  80. * @param array $options
  81. */
  82. public function addOptions( $options ) {
  83. $this->options[] = $options;
  84. }
  85. /**
  86. * This accepts an array of form:
  87. * label => value
  88. * label => ( label => value, label => value )
  89. *
  90. * @param array $options
  91. * @param string|array|false $default
  92. * @return string
  93. */
  94. static function formatOptions( $options, $default = false ) {
  95. $data = '';
  96. foreach ( $options as $label => $value ) {
  97. if ( is_array( $value ) ) {
  98. $contents = self::formatOptions( $value, $default );
  99. $data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
  100. } else {
  101. // If $default is an array, then the <select> probably has the multiple attribute,
  102. // so we should check if each $value is in $default, rather than checking if
  103. // $value is equal to $default.
  104. $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
  105. $data .= Xml::option( $label, $value, $selected ) . "\n";
  106. }
  107. }
  108. return $data;
  109. }
  110. /**
  111. * @return string
  112. */
  113. public function getHTML() {
  114. $contents = '';
  115. foreach ( $this->options as $options ) {
  116. $contents .= self::formatOptions( $options, $this->default );
  117. }
  118. return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
  119. }
  120. }