Make Xml::hidden() a wrapper around Html::hidden()
[lhc/web/wiklou.git] / includes / Xml.php
1 <?php
2
3 /**
4 * Module of static functions for generating XML
5 */
6
7 class Xml {
8 /**
9 * Format an XML element with given attributes and, optionally, text content.
10 * Element and attribute names are assumed to be ready for literal inclusion.
11 * Strings are assumed to not contain XML-illegal characters; special
12 * characters (<, >, &) are escaped but illegals are not touched.
13 *
14 * @param $element String: element name
15 * @param $attribs Array: Name=>value pairs. Values will be escaped.
16 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
17 * @param $allowShortTag Bool: whether '' in $contents will result in a contentless closed tag
18 * @return string
19 */
20 public static function element( $element, $attribs = null, $contents = '', $allowShortTag = true ) {
21 $out = '<' . $element;
22 if( !is_null( $attribs ) ) {
23 $out .= self::expandAttributes( $attribs );
24 }
25 if( is_null( $contents ) ) {
26 $out .= '>';
27 } else {
28 if( $allowShortTag && $contents === '' ) {
29 $out .= ' />';
30 } else {
31 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
32 }
33 }
34 return $out;
35 }
36
37 /**
38 * Given an array of ('attributename' => 'value'), it generates the code
39 * to set the XML attributes : attributename="value".
40 * The values are passed to Sanitizer::encodeAttribute.
41 * Return null if no attributes given.
42 * @param $attribs Array of attributes for an XML element
43 */
44 public static function expandAttributes( $attribs ) {
45 $out = '';
46 if( is_null( $attribs ) ) {
47 return null;
48 } elseif( is_array( $attribs ) ) {
49 foreach( $attribs as $name => $val )
50 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
51 return $out;
52 } else {
53 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
54 }
55 }
56
57 /**
58 * Format an XML element as with self::element(), but run text through the
59 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
60 * is passed.
61 *
62 * @param $element String:
63 * @param $attribs Array: Name=>value pairs. Values will be escaped.
64 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
65 * @return string
66 */
67 public static function elementClean( $element, $attribs = array(), $contents = '') {
68 if( $attribs ) {
69 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
70 }
71 if( $contents ) {
72 wfProfileIn( __METHOD__ . '-norm' );
73 $contents = UtfNormal::cleanUp( $contents );
74 wfProfileOut( __METHOD__ . '-norm' );
75 }
76 return self::element( $element, $attribs, $contents );
77 }
78
79 /**
80 * This opens an XML element
81 *
82 * @param $element name of the element
83 * @param $attribs array of attributes, see Xml::expandAttributes()
84 * @return string
85 */
86 public static function openElement( $element, $attribs = null ) {
87 return '<' . $element . self::expandAttributes( $attribs ) . '>';
88 }
89
90 /**
91 * Shortcut to close an XML element
92 * @param $element element name
93 * @return string
94 */
95 public static function closeElement( $element ) { return "</$element>"; }
96
97 /**
98 * Same as Xml::element(), but does not escape contents. Handy when the
99 * content you have is already valid xml.
100 *
101 * @param $element element name
102 * @param $attribs array of attributes
103 * @param $contents content of the element
104 * @return string
105 */
106 public static function tags( $element, $attribs = null, $contents ) {
107 return self::openElement( $element, $attribs ) . $contents . "</$element>";
108 }
109
110 /**
111 * Build a drop-down box for selecting a namespace
112 *
113 * @param $selected Mixed: Namespace which should be pre-selected
114 * @param $all Mixed: Value of an item denoting all namespaces, or null to omit
115 * @param $element_name String: value of the "name" attribute of the select tag
116 * @param $label String: optional label to add to the field
117 * @return string
118 */
119 public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
120 global $wgContLang;
121 $namespaces = $wgContLang->getFormattedNamespaces();
122 $options = array();
123
124 // Godawful hack... we'll be frequently passed selected namespaces
125 // as strings since PHP is such a shithole.
126 // But we also don't want blanks and nulls and "all"s matching 0,
127 // so let's convert *just* string ints to clean ints.
128 if( preg_match( '/^\d+$/', $selected ) ) {
129 $selected = intval( $selected );
130 }
131
132 if( !is_null( $all ) )
133 $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
134 foreach( $namespaces as $index => $name ) {
135 if( $index < NS_MAIN )
136 continue;
137 if( $index === 0 )
138 $name = wfMsg( 'blanknamespace' );
139 $options[] = self::option( $name, $index, $index === $selected );
140 }
141
142 $ret = Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => $element_name,
143 'class' => 'namespaceselector' ) )
144 . "\n"
145 . implode( "\n", $options )
146 . "\n"
147 . Xml::closeElement( 'select' );
148 if ( !is_null( $label ) ) {
149 $ret = Xml::label( $label, $element_name ) . '&nbsp;' . $ret;
150 }
151 return $ret;
152 }
153
154 /**
155 * Create a date selector
156 *
157 * @param $selected Mixed: the month which should be selected, default ''
158 * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
159 * @param $id String: Element identifier
160 * @return String: Html string containing the month selector
161 */
162 public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
163 global $wgLang;
164 $options = array();
165 if( is_null( $selected ) )
166 $selected = '';
167 if( !is_null( $allmonths ) )
168 $options[] = self::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
169 for( $i = 1; $i < 13; $i++ )
170 $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
171 return self::openElement( 'select', array( 'id' => $id, 'name' => 'month', 'class' => 'mw-month-selector' ) )
172 . implode( "\n", $options )
173 . self::closeElement( 'select' );
174 }
175
176 /**
177 * @param $year Integer
178 * @param $month Integer
179 * @return string Formatted HTML
180 */
181 public static function dateMenu( $year, $month ) {
182 # Offset overrides year/month selection
183 if( $month && $month !== -1 ) {
184 $encMonth = intval( $month );
185 } else {
186 $encMonth = '';
187 }
188 if( $year ) {
189 $encYear = intval( $year );
190 } else if( $encMonth ) {
191 $thisMonth = intval( gmdate( 'n' ) );
192 $thisYear = intval( gmdate( 'Y' ) );
193 if( intval($encMonth) > $thisMonth ) {
194 $thisYear--;
195 }
196 $encYear = $thisYear;
197 } else {
198 $encYear = '';
199 }
200 return Xml::label( wfMsg( 'year' ), 'year' ) . ' '.
201 Xml::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) . ' '.
202 Xml::label( wfMsg( 'month' ), 'month' ) . ' '.
203 Xml::monthSelector( $encMonth, -1 );
204 }
205
206 /**
207 *
208 * @param $selected The language code of the selected language
209 * @param $customisedOnly If true only languages which have some content are listed
210 * @return array of label and select
211 */
212 public static function languageSelector( $selected, $customisedOnly = true ) {
213 global $wgContLanguageCode;
214 /**
215 * Make sure the site language is in the list; a custom language code
216 * might not have a defined name...
217 */
218 $languages = Language::getLanguageNames( $customisedOnly );
219 if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
220 $languages[$wgContLanguageCode] = $wgContLanguageCode;
221 }
222 ksort( $languages );
223
224 /**
225 * If a bogus value is set, default to the content language.
226 * Otherwise, no default is selected and the user ends up
227 * with an Afrikaans interface since it's first in the list.
228 */
229 $selected = isset( $languages[$selected] ) ? $selected : $wgContLanguageCode;
230 $options = "\n";
231 foreach( $languages as $code => $name ) {
232 $options .= Xml::option( "$code - $name", $code, ($code == $selected) ) . "\n";
233 }
234
235 return array(
236 Xml::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
237 Xml::tags( 'select',
238 array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
239 $options
240 )
241 );
242
243 }
244
245 /**
246 * Shortcut to make a span element
247 * @param $text content of the element, will be escaped
248 * @param $class class name of the span element
249 * @param $attribs other attributes
250 * @return string
251 */
252 public static function span( $text, $class, $attribs=array() ) {
253 return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
254 }
255
256 /**
257 * Shortcut to make a specific element with a class attribute
258 * @param $text content of the element, will be escaped
259 * @param $class class name of the span element
260 * @param $tag element name
261 * @param $attribs other attributes
262 * @return string
263 */
264 public static function wrapClass( $text, $class, $tag='span', $attribs=array() ) {
265 return self::tags( $tag, array( 'class' => $class ) + $attribs, $text );
266 }
267
268 /**
269 * Convenience function to build an HTML text input field
270 * @param $name value of the name attribute
271 * @param $size value of the size attribute
272 * @param $value value of the value attribute
273 * @param $attribs other attributes
274 * @return string HTML
275 */
276 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
277 return self::element( 'input', array(
278 'name' => $name,
279 'size' => $size,
280 'value' => $value ) + $attribs );
281 }
282
283 /**
284 * Convenience function to build an HTML password input field
285 * @param $name value of the name attribute
286 * @param $size value of the size attribute
287 * @param $value value of the value attribute
288 * @param $attribs other attributes
289 * @return string HTML
290 */
291 public static function password( $name, $size=false, $value=false, $attribs=array() ) {
292 return self::input( $name, $size, $value, array_merge($attribs, array('type' => 'password')));
293 }
294
295 /**
296 * Internal function for use in checkboxes and radio buttons and such.
297 * @return array
298 */
299 public static function attrib( $name, $present = true ) {
300 return $present ? array( $name => $name ) : array();
301 }
302
303 /**
304 * Convenience function to build an HTML checkbox
305 * @param $name value of the name attribute
306 * @param $checked Whether the checkbox is checked or not
307 * @param $attribs other attributes
308 * @return string HTML
309 */
310 public static function check( $name, $checked=false, $attribs=array() ) {
311 return self::element( 'input', array_merge(
312 array(
313 'name' => $name,
314 'type' => 'checkbox',
315 'value' => 1 ),
316 self::attrib( 'checked', $checked ),
317 $attribs ) );
318 }
319
320 /**
321 * Convenience function to build an HTML radio button
322 * @param $name value of the name attribute
323 * @param $value value of the value attribute
324 * @param $checked Whether the checkbox is checked or not
325 * @param $attribs other attributes
326 * @return string HTML
327 */
328 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
329 return self::element( 'input', array(
330 'name' => $name,
331 'type' => 'radio',
332 'value' => $value ) + self::attrib( 'checked', $checked ) + $attribs );
333 }
334
335 /**
336 * Convenience function to build an HTML form label
337 * @param $label text of the label
338 * @param $id
339 * @return string HTML
340 */
341 public static function label( $label, $id ) {
342 return self::element( 'label', array( 'for' => $id ), $label );
343 }
344
345 /**
346 * Convenience function to build an HTML text input field with a label
347 * @param $label text of the label
348 * @param $name value of the name attribute
349 * @param $id id of the input
350 * @param $size value of the size attribute
351 * @param $value value of the value attribute
352 * @param $attribs other attributes
353 * @return string HTML
354 */
355 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
356 list( $label, $input ) = self::inputLabelSep( $label, $name, $id, $size, $value, $attribs );
357 return $label . '&nbsp;' . $input;
358 }
359
360 /**
361 * Same as Xml::inputLabel() but return input and label in an array
362 */
363 public static function inputLabelSep( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
364 return array(
365 Xml::label( $label, $id ),
366 self::input( $name, $size, $value, array( 'id' => $id ) + $attribs )
367 );
368 }
369
370 /**
371 * Convenience function to build an HTML checkbox with a label
372 * @return string HTML
373 */
374 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
375 return self::check( $name, $checked, array( 'id' => $id ) + $attribs ) .
376 '&nbsp;' .
377 self::label( $label, $id );
378 }
379
380 /**
381 * Convenience function to build an HTML radio button with a label
382 * @return string HTML
383 */
384 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
385 return self::radio( $name, $value, $checked, array( 'id' => $id ) + $attribs ) .
386 '&nbsp;' .
387 self::label( $label, $id );
388 }
389
390 /**
391 * Convenience function to build an HTML submit button
392 * @param $value String: label text for the button
393 * @param $attribs Array: optional custom attributes
394 * @return string HTML
395 */
396 public static function submitButton( $value, $attribs=array() ) {
397 return self::element( 'input', array( 'type' => 'submit', 'value' => $value ) + $attribs );
398 }
399
400 /**
401 * @deprecated Synonymous to Html::hidden()
402 */
403 public static function hidden( $name, $value, $attribs = array() ) {
404 return Html::hidden( $name, $value, $attribs );
405 }
406
407 /**
408 * Convenience function to build an HTML drop-down list item.
409 * @param $text String: text for this item
410 * @param $value String: form submission value; if empty, use text
411 * @param $selected boolean: if true, will be the default selected item
412 * @param $attribs array: optional additional HTML attributes
413 * @return string HTML
414 */
415 public static function option( $text, $value=null, $selected=false,
416 $attribs=array() ) {
417 if( !is_null( $value ) ) {
418 $attribs['value'] = $value;
419 }
420 if( $selected ) {
421 $attribs['selected'] = 'selected';
422 }
423 return self::element( 'option', $attribs, $text );
424 }
425
426 /**
427 * Build a drop-down box from a textual list.
428 *
429 * @param $name Mixed: Name and id for the drop-down
430 * @param $class Mixed: CSS classes for the drop-down
431 * @param $other Mixed: Text for the "Other reasons" option
432 * @param $list Mixed: Correctly formatted text to be used to generate the options
433 * @param $selected Mixed: Option which should be pre-selected
434 * @param $tabindex Mixed: Value of the tabindex attribute
435 * @return string
436 */
437 public static function listDropDown( $name= '', $list = '', $other = '', $selected = '', $class = '', $tabindex = Null ) {
438 $options = '';
439 $optgroup = false;
440
441 $options = self::option( $other, 'other', $selected === 'other' );
442
443 foreach ( explode( "\n", $list ) as $option) {
444 $value = trim( $option );
445 if ( $value == '' ) {
446 continue;
447 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
448 // A new group is starting ...
449 $value = trim( substr( $value, 1 ) );
450 if( $optgroup ) $options .= self::closeElement('optgroup');
451 $options .= self::openElement( 'optgroup', array( 'label' => $value ) );
452 $optgroup = true;
453 } elseif ( substr( $value, 0, 2) == '**' ) {
454 // groupmember
455 $value = trim( substr( $value, 2 ) );
456 $options .= self::option( $value, $value, $selected === $value );
457 } else {
458 // groupless reason list
459 if( $optgroup ) $options .= self::closeElement('optgroup');
460 $options .= self::option( $value, $value, $selected === $value );
461 $optgroup = false;
462 }
463 }
464 if( $optgroup ) $options .= self::closeElement('optgroup');
465
466 $attribs = array();
467 if( $name ) {
468 $attribs['id'] = $name;
469 $attribs['name'] = $name;
470 }
471 if( $class ) {
472 $attribs['class'] = $class;
473 }
474 if( $tabindex ) {
475 $attribs['tabindex'] = $tabindex;
476 }
477 return Xml::openElement( 'select', $attribs )
478 . "\n"
479 . $options
480 . "\n"
481 . Xml::closeElement( 'select' );
482 }
483
484 /**
485 * Shortcut for creating fieldsets.
486 *
487 * @param $legend Legend of the fieldset. If evaluates to false, legend is not added.
488 * @param $content Pre-escaped content for the fieldset. If false, only open fieldset is returned.
489 * @param $attribs Any attributes to fieldset-element.
490 */
491 public static function fieldset( $legend = false, $content = false, $attribs = array() ) {
492 $s = Xml::openElement( 'fieldset', $attribs ) . "\n";
493 if ( $legend ) {
494 $s .= Xml::element( 'legend', null, $legend ) . "\n";
495 }
496 if ( $content !== false ) {
497 $s .= $content . "\n";
498 $s .= Xml::closeElement( 'fieldset' ) . "\n";
499 }
500
501 return $s;
502 }
503
504 /**
505 * Shortcut for creating textareas.
506 *
507 * @param $name The 'name' for the textarea
508 * @param $content Content for the textarea
509 * @param $cols The number of columns for the textarea
510 * @param $rows The number of rows for the textarea
511 * @param $attribs Any other attributes for the textarea
512 */
513 public static function textarea( $name, $content, $cols = 40, $rows = 5, $attribs = array() ) {
514 return self::element( 'textarea',
515 array( 'name' => $name,
516 'id' => $name,
517 'cols' => $cols,
518 'rows' => $rows
519 ) + $attribs,
520 $content, false );
521 }
522
523 /**
524 * Returns an escaped string suitable for inclusion in a string literal
525 * for JavaScript source code.
526 * Illegal control characters are assumed not to be present.
527 *
528 * @param $string String to escape
529 * @return String
530 */
531 public static function escapeJsString( $string ) {
532 // See ECMA 262 section 7.8.4 for string literal format
533 $pairs = array(
534 "\\" => "\\\\",
535 "\"" => "\\\"",
536 '\'' => '\\\'',
537 "\n" => "\\n",
538 "\r" => "\\r",
539
540 # To avoid closing the element or CDATA section
541 "<" => "\\x3c",
542 ">" => "\\x3e",
543
544 # To avoid any complaints about bad entity refs
545 "&" => "\\x26",
546
547 # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
548 # Encode certain Unicode formatting chars so affected
549 # versions of Gecko don't misinterpret our strings;
550 # this is a common problem with Farsi text.
551 "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
552 "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
553 );
554 return strtr( $string, $pairs );
555 }
556
557 /**
558 * Encode a variable of unknown type to JavaScript.
559 * Arrays are converted to JS arrays, objects are converted to JS associative
560 * arrays (objects). So cast your PHP associative arrays to objects before
561 * passing them to here.
562 */
563 public static function encodeJsVar( $value ) {
564 if ( is_bool( $value ) ) {
565 $s = $value ? 'true' : 'false';
566 } elseif ( is_null( $value ) ) {
567 $s = 'null';
568 } elseif ( is_int( $value ) ) {
569 $s = $value;
570 } elseif ( is_array( $value ) ) {
571 $s = '[';
572 foreach ( $value as $elt ) {
573 if ( $s != '[' ) {
574 $s .= ', ';
575 }
576 $s .= self::encodeJsVar( $elt );
577 }
578 $s .= ']';
579 } elseif ( is_object( $value ) ) {
580 $s = '{';
581 foreach ( (array)$value as $name => $elt ) {
582 if ( $s != '{' ) {
583 $s .= ', ';
584 }
585 $s .= '"' . self::escapeJsString( $name ) . '": ' .
586 self::encodeJsVar( $elt );
587 }
588 $s .= '}';
589 } else {
590 $s = '"' . self::escapeJsString( $value ) . '"';
591 }
592 return $s;
593 }
594
595
596 /**
597 * Check if a string is well-formed XML.
598 * Must include the surrounding tag.
599 *
600 * @param $text String: string to test.
601 * @return bool
602 *
603 * @todo Error position reporting return
604 */
605 public static function isWellFormed( $text ) {
606 $parser = xml_parser_create( "UTF-8" );
607
608 # case folding violates XML standard, turn it off
609 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
610
611 if( !xml_parse( $parser, $text, true ) ) {
612 //$err = xml_error_string( xml_get_error_code( $parser ) );
613 //$position = xml_get_current_byte_index( $parser );
614 //$fragment = $this->extractFragment( $html, $position );
615 //$this->mXmlError = "$err at byte $position:\n$fragment";
616 xml_parser_free( $parser );
617 return false;
618 }
619 xml_parser_free( $parser );
620 return true;
621 }
622
623 /**
624 * Check if a string is a well-formed XML fragment.
625 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
626 * and can use HTML named entities.
627 *
628 * @param $text String:
629 * @return bool
630 */
631 public static function isWellFormedXmlFragment( $text ) {
632 $html =
633 Sanitizer::hackDocType() .
634 '<html>' .
635 $text .
636 '</html>';
637 return Xml::isWellFormed( $html );
638 }
639
640 /**
641 * Replace " > and < with their respective HTML entities ( &quot;,
642 * &gt;, &lt;)
643 *
644 * @param $in String: text that might contain HTML tags.
645 * @return string Escaped string
646 */
647 public static function escapeTagsOnly( $in ) {
648 return str_replace(
649 array( '"', '>', '<' ),
650 array( '&quot;', '&gt;', '&lt;' ),
651 $in );
652 }
653
654 /**
655 * Generate a form (without the opening form element).
656 * Output optionally includes a submit button.
657 * @param $fields Associative array, key is message corresponding to a description for the field (colon is in the message), value is appropriate input.
658 * @param $submitLabel A message containing a label for the submit button.
659 * @return string HTML form.
660 */
661 public static function buildForm( $fields, $submitLabel = null ) {
662 $form = '';
663 $form .= "<table><tbody>";
664
665 foreach( $fields as $labelmsg => $input ) {
666 $id = "mw-$labelmsg";
667 $form .= Xml::openElement( 'tr', array( 'id' => $id ) );
668 $form .= Xml::tags( 'td', array('class' => 'mw-label'), wfMsgExt( $labelmsg, array('parseinline') ) );
669 $form .= Xml::openElement( 'td', array( 'class' => 'mw-input' ) ) . $input . Xml::closeElement( 'td' );
670 $form .= Xml::closeElement( 'tr' );
671 }
672
673 if( $submitLabel ) {
674 $form .= Xml::openElement( 'tr' );
675 $form .= Xml::tags( 'td', array(), '' );
676 $form .= Xml::openElement( 'td', array( 'class' => 'mw-submit' ) ) . Xml::submitButton( wfMsg( $submitLabel ) ) . Xml::closeElement( 'td' );
677 $form .= Xml::closeElement( 'tr' );
678 }
679
680 $form .= "</tbody></table>";
681
682
683 return $form;
684 }
685
686 /**
687 * Build a table of data
688 * @param $rows An array of arrays of strings, each to be a row in a table
689 * @param $attribs An array of attributes to apply to the table tag [optional]
690 * @param $headers An array of strings to use as table headers [optional]
691 * @return string
692 */
693 public static function buildTable( $rows, $attribs = array(), $headers = null ) {
694 $s = Xml::openElement( 'table', $attribs );
695 if ( is_array( $headers ) ) {
696 foreach( $headers as $id => $header ) {
697 $attribs = array();
698 if ( is_string( $id ) ) $attribs['id'] = $id;
699 $s .= Xml::element( 'th', $attribs, $header );
700 }
701 }
702 foreach( $rows as $id => $row ) {
703 $attribs = array();
704 if ( is_string( $id ) ) $attribs['id'] = $id;
705 $s .= Xml::buildTableRow( $attribs, $row );
706 }
707 $s .= Xml::closeElement( 'table' );
708 return $s;
709 }
710
711 /**
712 * Build a row for a table
713 * @param $cells An array of strings to put in <td>
714 * @return string
715 */
716 public static function buildTableRow( $attribs, $cells ) {
717 $s = Xml::openElement( 'tr', $attribs );
718 foreach( $cells as $id => $cell ) {
719 $attribs = array();
720 if ( is_string( $id ) ) $attribs['id'] = $id;
721 $s .= Xml::element( 'td', $attribs, $cell );
722 }
723 $s .= Xml::closeElement( 'tr' );
724 return $s;
725 }
726 }
727
728 class XmlSelect {
729 protected $options = array();
730 protected $default = false;
731 protected $attributes = array();
732
733 public function __construct( $name = false, $id = false, $default = false ) {
734 if ( $name ) $this->setAttribute( 'name', $name );
735 if ( $id ) $this->setAttribute( 'id', $id );
736 if ( $default ) $this->default = $default;
737 }
738
739 public function setDefault( $default ) {
740 $this->default = $default;
741 }
742
743 public function setAttribute( $name, $value ) {
744 $this->attributes[$name] = $value;
745 }
746
747 public function getAttribute( $name ) {
748 if ( isset($this->attributes[$name]) ) {
749 return $this->attributes[$name];
750 } else {
751 return null;
752 }
753 }
754
755 public function addOption( $name, $value = false ) {
756 // Stab stab stab
757 $value = ($value !== false) ? $value : $name;
758 $this->options[] = Xml::option( $name, $value, $value === $this->default );
759 }
760
761 // This accepts an array of form
762 // label => value
763 // label => ( label => value, label => value )
764 public function addOptions( $options ) {
765 $this->options[] = trim(self::formatOptions( $options, $this->default ));
766 }
767
768 // This accepts an array of form
769 // label => value
770 // label => ( label => value, label => value )
771 static function formatOptions( $options, $default = false ) {
772 $data = '';
773 foreach( $options as $label => $value ) {
774 if ( is_array( $value ) ) {
775 $contents = self::formatOptions( $value, $default );
776 $data .= Xml::tags( 'optgroup', array( 'label' => $label ), $contents ) . "\n";
777 } else {
778 $data .= Xml::option( $label, $value, $value === $default ) . "\n";
779 }
780 }
781
782 return $data;
783 }
784
785 public function getHTML() {
786 return Xml::tags( 'select', $this->attributes, implode( "\n", $this->options ) );
787 }
788
789 }