Merge "resourceloader: Don't call wfExpandUrl() on load.php urls"
[lhc/web/wiklou.git] / includes / XmlSelect.php
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 /**
24 * Class for generating HTML <select> elements.
25 */
26 class XmlSelect {
27 protected $options = array();
28 protected $default = false;
29 protected $attributes = array();
30
31 public function __construct( $name = false, $id = false, $default = false ) {
32 if ( $name ) {
33 $this->setAttribute( 'name', $name );
34 }
35
36 if ( $id ) {
37 $this->setAttribute( 'id', $id );
38 }
39
40 if ( $default !== false ) {
41 $this->default = $default;
42 }
43 }
44
45 /**
46 * @param string|array $default
47 */
48 public function setDefault( $default ) {
49 $this->default = $default;
50 }
51
52 /**
53 * @param string $name
54 * @param string $value
55 */
56 public function setAttribute( $name, $value ) {
57 $this->attributes[$name] = $value;
58 }
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 /**
73 * @param string $label
74 * @param string $value If not given, assumed equal to $label
75 */
76 public function addOption( $label, $value = false ) {
77 $value = $value !== false ? $value : $label;
78 $this->options[] = array( $label => $value );
79 }
80
81 /**
82 * This accepts an array of form
83 * label => value
84 * label => ( label => value, label => value )
85 *
86 * @param array $options
87 */
88 public function addOptions( $options ) {
89 $this->options[] = $options;
90 }
91
92 /**
93 * This accepts an array of form:
94 * label => value
95 * label => ( label => value, label => value )
96 *
97 * @param array $options
98 * @param string|array $default
99 * @return string
100 */
101 static function formatOptions( $options, $default = false ) {
102 $data = '';
103
104 foreach ( $options as $label => $value ) {
105 if ( is_array( $value ) ) {
106 $contents = self::formatOptions( $value, $default );
107 $data .= Html::rawElement( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
108 } else {
109 // If $default is an array, then the <select> probably has the multiple attribute,
110 // so we should check if each $value is in $default, rather than checking if
111 // $value is equal to $default.
112 $selected = is_array( $default ) ? in_array( $value, $default ) : $value === $default;
113 $data .= Xml::option( $label, $value, $selected ) . "\n";
114 }
115 }
116
117 return $data;
118 }
119
120 /**
121 * @return string
122 */
123 public function getHTML() {
124 $contents = '';
125
126 foreach ( $this->options as $options ) {
127 $contents .= self::formatOptions( $options, $this->default );
128 }
129
130 return Html::rawElement( 'select', $this->attributes, rtrim( $contents ) );
131 }
132 }