XmlSelect.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. if ( isset( $this->attributes[$name] ) ) {
  66. return $this->attributes[$name];
  67. } else {
  68. return null;
  69. }
  70. }
  71. /**
  72. * @param string $label
  73. * @param string $value If not given, assumed equal to $label
  74. */
  75. public function addOption( $label, $value = false ) {
  76. $value = $value !== false ? $value : $label;
  77. $this->options[] = [ $label => $value ];
  78. }
  79. /**
  80. * This accepts an array of form
  81. * label => value
  82. * label => ( label => value, label => value )
  83. *
  84. * @param array $options
  85. */
  86. public function addOptions( $options ) {
  87. $this->options[] = $options;
  88. }
  89. /**
  90. * This accepts an array of form:
  91. * label => value
  92. * label => ( label => value, label => value )
  93. *
  94. * @param array $options
  95. * @param string|array $default
  96. * @return string
  97. */
  98. static function formatOptions( $options, $default = false ) {
  99. $data = '';
  100. foreach ( $options as $label => $value ) {
  101. if ( is_array( $value ) ) {
  102. $contents = self::formatOptions( $value, $default );
  103. $data .= Html::rawElement( 'optgroup', [ 'label' => $label ], $contents ) . "\n";
  104. } else {
  105. // If $default is an array, then the <select> probably has the multiple attribute,
  106. // so we should check if each $value is in $default, rather than checking if
  107. // $value is equal to $default.
  108. $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
  109. $data .= Xml::option( $label, $value, $selected ) . "\n";
  110. }
  111. }
  112. return $data;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function getHTML() {
  118. $contents = '';
  119. foreach ( $this->options as $options ) {
  120. $contents .= self::formatOptions( $options, $this->default );
  121. }
  122. return Html::rawElement( $this->tagName, $this->attributes, rtrim( $contents ) );
  123. }
  124. }