Reimplement r55876 properly
[lhc/web/wiklou.git] / includes / HTMLForm.php
1 <?php
2
3 class HTMLForm {
4 static $jsAdded = false;
5
6 /* The descriptor is an array of arrays.
7 i.e. array(
8 'fieldname' => array( 'section' => 'section/subsection',
9 properties... ),
10 ...
11 )
12 */
13
14 static $typeMappings = array(
15 'text' => 'HTMLTextField',
16 'select' => 'HTMLSelectField',
17 'radio' => 'HTMLRadioField',
18 'multiselect' => 'HTMLMultiSelectField',
19 'check' => 'HTMLCheckField',
20 'toggle' => 'HTMLCheckField',
21 'int' => 'HTMLIntField',
22 'float' => 'HTMLFloatField',
23 'info' => 'HTMLInfoField',
24 'password' => 'HTMLPasswordField',
25 'selectorother' => 'HTMLSelectOrOtherField',
26 # HTMLTextField will output the correct type="" attribute automagically.
27 # There are about four zillion other HTML 5 input types, like url, but
28 # we don't use those at the moment, so no point in adding all of them.
29 'email' => 'HTMLTextField',
30 );
31
32 function __construct( $descriptor, $messagePrefix ) {
33 $this->mMessagePrefix = $messagePrefix;
34
35 // Expand out into a tree.
36 $loadedDescriptor = array();
37 $this->mFlatFields = array();
38
39 foreach( $descriptor as $fieldname => $info ) {
40 $section = '';
41 if ( isset( $info['section'] ) )
42 $section = $info['section'];
43
44 $info['name'] = $fieldname;
45
46 $field = $this->loadInputFromParameters( $info );
47 $field->mParent = $this;
48
49 $setSection =& $loadedDescriptor;
50 if( $section ) {
51 $sectionParts = explode( '/', $section );
52
53 while( count( $sectionParts ) ) {
54 $newName = array_shift( $sectionParts );
55
56 if ( !isset( $setSection[$newName] ) ) {
57 $setSection[$newName] = array();
58 }
59
60 $setSection =& $setSection[$newName];
61 }
62 }
63
64 $setSection[$fieldname] = $field;
65 $this->mFlatFields[$fieldname] = $field;
66 }
67
68 $this->mFieldTree = $loadedDescriptor;
69
70 $this->mShowReset = true;
71 }
72
73 static function addJS() {
74 if( self::$jsAdded ) return;
75
76 global $wgOut;
77
78 $wgOut->addScriptClass( 'htmlform' );
79 }
80
81 static function loadInputFromParameters( $descriptor ) {
82 if ( isset( $descriptor['class'] ) ) {
83 $class = $descriptor['class'];
84 } elseif ( isset( $descriptor['type'] ) ) {
85 $class = self::$typeMappings[$descriptor['type']];
86 $descriptor['class'] = $class;
87 }
88
89 if( !$class ) {
90 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
91 }
92
93 $obj = new $class( $descriptor );
94
95 return $obj;
96 }
97
98 function show() {
99 $html = '';
100
101 self::addJS();
102
103 // Load data from the request.
104 $this->loadData();
105
106 // Try a submission
107 global $wgUser, $wgRequest;
108 $editToken = $wgRequest->getVal( 'wpEditToken' );
109
110 $result = false;
111 if ( $wgUser->matchEditToken( $editToken ) )
112 $result = $this->trySubmit();
113
114 if( $result === true )
115 return $result;
116
117 // Display form.
118 $this->displayForm( $result );
119 }
120
121 /** Return values:
122 * TRUE == Successful submission
123 * FALSE == No submission attempted
124 * Anything else == Error to display.
125 */
126 function trySubmit() {
127 // Check for validation
128 foreach( $this->mFlatFields as $fieldname => $field ) {
129 if ( !empty( $field->mParams['nodata'] ) ) continue;
130 if ( $field->validate( $this->mFieldData[$fieldname],
131 $this->mFieldData ) !== true ) {
132 return isset( $this->mValidationErrorMessage ) ?
133 $this->mValidationErrorMessage : array( 'htmlform-invalid-input' );
134 }
135 }
136
137 $callback = $this->mSubmitCallback;
138
139 $data = $this->filterDataForSubmit( $this->mFieldData );
140
141 $res = call_user_func( $callback, $data );
142
143 return $res;
144 }
145
146 function setSubmitCallback( $cb ) {
147 $this->mSubmitCallback = $cb;
148 }
149
150 function setValidationErrorMessage( $msg ) {
151 $this->mValidationErrorMessage = $msg;
152 }
153
154 function setIntro( $msg ) {
155 $this->mIntro = $msg;
156 }
157
158 function displayForm( $submitResult ) {
159 global $wgOut;
160
161 if ( $submitResult !== false ) {
162 $this->displayErrors( $submitResult );
163 }
164
165 if ( isset( $this->mIntro ) ) {
166 $wgOut->addHTML( $this->mIntro );
167 }
168
169 $html = $this->getBody();
170
171 // Hidden fields
172 $html .= $this->getHiddenFields();
173
174 // Buttons
175 $html .= $this->getButtons();
176
177 $html = $this->wrapForm( $html );
178
179 $wgOut->addHTML( $html );
180 }
181
182 function wrapForm( $html ) {
183 return Xml::tags(
184 'form',
185 array(
186 'action' => $this->getTitle()->getFullURL(),
187 'method' => 'post',
188 ),
189 $html
190 );
191 }
192
193 function getHiddenFields() {
194 global $wgUser;
195 $html = '';
196
197 $html .= Xml::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
198 $html .= Xml::hidden( 'title', $this->getTitle() ) . "\n";
199
200 return $html;
201 }
202
203 function getButtons() {
204 $html = '';
205
206 $attribs = array();
207
208 if ( isset( $this->mSubmitID ) )
209 $attribs['id'] = $this->mSubmitID;
210
211 $attribs['class'] = 'mw-htmlform-submit';
212
213 $html .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
214
215 if( $this->mShowReset ) {
216 $html .= Xml::element(
217 'input',
218 array(
219 'type' => 'reset',
220 'value' => wfMsg( 'htmlform-reset' )
221 )
222 ) . "\n";
223 }
224
225 return $html;
226 }
227
228 function getBody() {
229 return $this->displaySection( $this->mFieldTree );
230 }
231
232 function displayErrors( $errors ) {
233 if ( is_array( $errors ) ) {
234 $errorstr = $this->formatErrors( $errors );
235 } else {
236 $errorstr = $errors;
237 }
238
239 $errorstr = Xml::tags( 'div', array( 'class' => 'error' ), $errorstr );
240
241 global $wgOut;
242 $wgOut->addHTML( $errorstr );
243 }
244
245 static function formatErrors( $errors ) {
246 $errorstr = '';
247 foreach ( $errors as $error ) {
248 if( is_array( $error ) ) {
249 $msg = array_shift( $error );
250 } else {
251 $msg = $error;
252 $error = array();
253 }
254 $errorstr .= Xml::tags(
255 'li',
256 null,
257 wfMsgExt( $msg, array( 'parseinline' ), $error )
258 );
259 }
260
261 $errorstr = Xml::tags( 'ul', null, $errorstr );
262
263 return $errorstr;
264 }
265
266 function setSubmitText( $t ) {
267 $this->mSubmitText = $t;
268 }
269
270 function getSubmitText() {
271 return isset( $this->mSubmitText ) ? $this->mSubmitText : wfMsg( 'htmlform-submit' );
272 }
273
274 function setSubmitID( $t ) {
275 $this->mSubmitID = $t;
276 }
277
278 function setMessagePrefix( $p ) {
279 $this->mMessagePrefix = $p;
280 }
281
282 function setTitle( $t ) {
283 $this->mTitle = $t;
284 }
285
286 function getTitle() {
287 return $this->mTitle;
288 }
289
290 function displaySection( $fields ) {
291 $tableHtml = '';
292 $subsectionHtml = '';
293 $hasLeftColumn = false;
294
295 foreach( $fields as $key => $value ) {
296 if ( is_object( $value ) ) {
297 $v = empty( $value->mParams['nodata'] )
298 ? $this->mFieldData[$key]
299 : $value->getDefault();
300 $tableHtml .= $value->getTableRow( $v );
301
302 if( $value->getLabel() != '&nbsp;' )
303 $hasLeftColumn = true;
304 } elseif ( is_array( $value ) ) {
305 $section = $this->displaySection( $value );
306 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
307 $subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
308 }
309 }
310
311 $classes = array();
312 if( !$hasLeftColumn ) // Avoid strange spacing when no labels exist
313 $classes[] = 'mw-htmlform-nolabel';
314 $classes = implode( ' ', $classes );
315
316 $tableHtml = "<table class='$classes'><tbody>\n$tableHtml\n</tbody></table>\n";
317
318 return $subsectionHtml . "\n" . $tableHtml;
319 }
320
321 function loadData() {
322 global $wgRequest;
323
324 $fieldData = array();
325
326 foreach( $this->mFlatFields as $fieldname => $field ) {
327 if ( !empty( $field->mParams['nodata'] ) ) continue;
328 if ( !empty( $field->mParams['disabled'] ) ) {
329 $fieldData[$fieldname] = $field->getDefault();
330 } else {
331 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
332 }
333 }
334
335 // Filter data.
336 foreach( $fieldData as $name => &$value ) {
337 $field = $this->mFlatFields[$name];
338 $value = $field->filter( $value, $this->mFlatFields );
339 }
340
341 $this->mFieldData = $fieldData;
342 }
343
344 function importData( $fieldData ) {
345 // Filter data.
346 foreach( $fieldData as $name => &$value ) {
347 $field = $this->mFlatFields[$name];
348 $value = $field->filter( $value, $this->mFlatFields );
349 }
350
351 foreach( $this->mFlatFields as $fieldname => $field ) {
352 if ( !isset( $fieldData[$fieldname] ) )
353 $fieldData[$fieldname] = $field->getDefault();
354 }
355
356 $this->mFieldData = $fieldData;
357 }
358
359 function suppressReset( $suppressReset = true ) {
360 $this->mShowReset = !$suppressReset;
361 }
362
363 function filterDataForSubmit( $data ) {
364 return $data;
365 }
366 }
367
368 abstract class HTMLFormField {
369 abstract function getInputHTML( $value );
370
371 function validate( $value, $alldata ) {
372 if ( isset( $this->mValidationCallback ) ) {
373 return call_user_func( $this->mValidationCallback, $value, $alldata );
374 }
375
376 return true;
377 }
378
379 function filter( $value, $alldata ) {
380 if( isset( $this->mFilterCallback ) ) {
381 $value = call_user_func( $this->mFilterCallback, $value, $alldata );
382 }
383
384 return $value;
385 }
386
387 function loadDataFromRequest( $request ) {
388 if( $request->getCheck( $this->mName ) ) {
389 return $request->getText( $this->mName );
390 } else {
391 return $this->getDefault();
392 }
393 }
394
395 function __construct( $params ) {
396 $this->mParams = $params;
397
398 if( isset( $params['label-message'] ) ) {
399 $msgInfo = $params['label-message'];
400
401 if ( is_array( $msgInfo ) ) {
402 $msg = array_shift( $msgInfo );
403 } else {
404 $msg = $msgInfo;
405 $msgInfo = array();
406 }
407
408 $this->mLabel = wfMsgExt( $msg, 'parseinline', $msgInfo );
409 } elseif ( isset( $params['label'] ) ) {
410 $this->mLabel = $params['label'];
411 }
412
413 if ( isset( $params['name'] ) ) {
414 $name = $params['name'];
415 $validName = Sanitizer::escapeId( $name );
416 if( $name != $validName ) {
417 throw new MWException("Invalid name '$name' passed to " . __METHOD__ );
418 }
419 $this->mName = 'wp'.$name;
420 $this->mID = 'mw-input-'.$name;
421 }
422
423 if ( isset( $params['default'] ) ) {
424 $this->mDefault = $params['default'];
425 }
426
427 if ( isset( $params['id'] ) ) {
428 $id = $params['id'];
429 $validId = Sanitizer::escapeId( $id );
430 if( $id != $validId ) {
431 throw new MWException("Invalid id '$id' passed to " . __METHOD__ );
432 }
433 $this->mID = $id;
434 }
435
436 if ( isset( $params['validation-callback'] ) ) {
437 $this->mValidationCallback = $params['validation-callback'];
438 }
439
440 if ( isset( $params['filter-callback'] ) ) {
441 $this->mFilterCallback = $params['filter-callback'];
442 }
443 }
444
445 function getTableRow( $value ) {
446 // Check for invalid data.
447 global $wgRequest;
448
449 $errors = $this->validate( $value, $this->mParent->mFieldData );
450 if ( $errors === true || !$wgRequest->wasPosted() ) {
451 $errors = '';
452 } else {
453 $errors = Xml::tags( 'span', array( 'class' => 'error' ), $errors );
454 }
455
456 $html = '';
457
458 $html .= Xml::tags( 'td', array( 'class' => 'mw-label' ),
459 Xml::tags( 'label', array( 'for' => $this->mID ), $this->getLabel() )
460 );
461 $html .= Xml::tags( 'td', array( 'class' => 'mw-input' ),
462 $this->getInputHTML( $value ) ."\n$errors" );
463
464 $fieldType = get_class( $this );
465
466 $html = Xml::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
467 $html ) . "\n";
468
469 $helptext = null;
470 if ( isset( $this->mParams['help-message'] ) ) {
471 $msg = $this->mParams['help-message'];
472 $helptext = wfMsgExt( $msg, 'parseinline' );
473 if ( wfEmptyMsg( $msg, $helptext ) ) {
474 # Never mind
475 $helptext = null;
476 }
477 } elseif ( isset( $this->mParams['help'] ) ) {
478 $helptext = $this->mParams['help'];
479 }
480
481 if ( !is_null( $helptext ) ) {
482 $row = Xml::tags( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
483 $helptext );
484 $row = Xml::tags( 'tr', null, $row );
485 $html .= "$row\n";
486 }
487
488 return $html;
489 }
490
491 function getLabel() {
492 return $this->mLabel;
493 }
494
495 function getDefault() {
496 if ( isset( $this->mDefault ) ) {
497 return $this->mDefault;
498 } else {
499 return null;
500 }
501 }
502
503 static function flattenOptions( $options ) {
504 $flatOpts = array();
505
506 foreach( $options as $key => $value ) {
507 if ( is_array( $value ) ) {
508 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
509 } else {
510 $flatOpts[] = $value;
511 }
512 }
513
514 return $flatOpts;
515 }
516 }
517
518 class HTMLTextField extends HTMLFormField {
519 # Override in derived classes to use other Xml::... functions
520 protected $mFunction = 'input';
521
522 function getSize() {
523 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
524 }
525
526 function getInputHTML( $value ) {
527 global $wgHtml5;
528 $attribs = array(
529 'id' => $this->mID,
530 'name' => $this->mName,
531 'size' => $this->getSize(),
532 'value' => $value,
533 );
534
535 if ( isset( $this->mParams['maxlength'] ) ) {
536 $attribs['maxlength'] = $this->mParams['maxlength'];
537 }
538
539 if ( !empty( $this->mParams['disabled'] ) ) {
540 $attribs['disabled'] = 'disabled';
541 }
542
543 if ( $wgHtml5 ) {
544 # TODO: Enforce pattern, step, required, readonly on the server
545 # side as well
546 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
547 'placeholder' ) as $param ) {
548 if ( isset( $this->mParams[$param] ) ) {
549 $attribs[$param] = $this->mParams[$param];
550 }
551 }
552 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' )
553 as $param ) {
554 if ( isset( $this->mParams[$param] ) ) {
555 $attribs[$param] = '';
556 }
557 }
558 if ( isset( $this->mParams['type'] ) ) {
559 switch ( $this->mParams['type'] ) {
560 case 'email':
561 $attribs['type'] = 'email';
562 break;
563 case 'int':
564 $attribs['type'] = 'number';
565 break;
566 case 'float':
567 $attribs['type'] = 'number';
568 $attribs['step'] = 'any';
569 break;
570 case 'password':
571 $attribs['type'] = 'password';
572 break;
573 }
574 }
575 }
576
577 return Html::element( 'input', $attribs );
578 }
579 }
580
581 class HTMLPasswordField extends HTMLTextField {}
582
583 class HTMLFloatField extends HTMLTextField {
584 function getSize() {
585 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 20;
586 }
587
588 function validate( $value, $alldata ) {
589 $p = parent::validate( $value, $alldata );
590
591 if ( $p !== true ) return $p;
592
593 if ( floatval( $value ) != $value ) {
594 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
595 }
596
597 $in_range = true;
598
599 # The "int" part of these message names is rather confusing. They make
600 # equal sense for all numbers.
601 if ( isset( $this->mParams['min'] ) ) {
602 $min = $this->mParams['min'];
603 if ( $min > $value )
604 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
605 }
606
607 if ( isset( $this->mParams['max'] ) ) {
608 $max = $this->mParams['max'];
609 if( $max < $value )
610 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
611 }
612
613 return true;
614 }
615 }
616
617 class HTMLIntField extends HTMLFloatField {
618 function validate( $value, $alldata ) {
619 $p = parent::validate( $value, $alldata );
620
621 if ( $p !== true ) return $p;
622
623 if ( intval( $value ) != $value ) {
624 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
625 }
626
627 return true;
628 }
629 }
630
631 class HTMLCheckField extends HTMLFormField {
632 function getInputHTML( $value ) {
633 if ( !empty( $this->mParams['invert'] ) )
634 $value = !$value;
635
636 $attr = array( 'id' => $this->mID );
637 if( !empty( $this->mParams['disabled'] ) ) {
638 $attr['disabled'] = 'disabled';
639 }
640
641 return Xml::check( $this->mName, $value, $attr ) . '&nbsp;' .
642 Xml::tags( 'label', array( 'for' => $this->mID ), $this->mLabel );
643 }
644
645 function getLabel() {
646 return '&nbsp;'; // In the right-hand column.
647 }
648
649 function loadDataFromRequest( $request ) {
650 $invert = false;
651 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
652 $invert = true;
653 }
654
655 // GetCheck won't work like we want for checks.
656 if( $request->getCheck( 'wpEditToken' ) ) {
657 // XOR has the following truth table, which is what we want
658 // INVERT VALUE | OUTPUT
659 // true true | false
660 // false true | true
661 // false false | false
662 // true false | true
663 return $request->getBool( $this->mName ) xor $invert;
664 } else {
665 return $this->getDefault();
666 }
667 }
668 }
669
670 class HTMLSelectField extends HTMLFormField {
671
672 function validate( $value, $alldata ) {
673 $p = parent::validate( $value, $alldata );
674 if( $p !== true ) return $p;
675
676 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
677 if ( in_array( $value, $validOptions ) )
678 return true;
679 else
680 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
681 }
682
683 function getInputHTML( $value ) {
684 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
685
686 // If one of the options' 'name' is int(0), it is automatically selected.
687 // because PHP sucks and things int(0) == 'some string'.
688 // Working around this by forcing all of them to strings.
689 $options = array_map( 'strval', $this->mParams['options'] );
690
691 if( !empty( $this->mParams['disabled'] ) ) {
692 $select->setAttribute( 'disabled', 'disabled' );
693 }
694
695 $select->addOptions( $options );
696
697 return $select->getHTML();
698 }
699 }
700
701 class HTMLSelectOrOtherField extends HTMLTextField {
702 static $jsAdded = false;
703
704 function __construct( $params ) {
705 if( !in_array( 'other', $params['options'] ) ) {
706 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
707 }
708
709 parent::__construct( $params );
710 }
711
712 static function forceToStringRecursive( $array ) {
713 if ( is_array($array) ) {
714 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array);
715 } else {
716 return strval($array);
717 }
718 }
719
720 function getInputHTML( $value ) {
721 $valInSelect = false;
722
723 if( $value !== false )
724 $valInSelect = in_array( $value,
725 HTMLFormField::flattenOptions( $this->mParams['options'] ) );
726
727 $selected = $valInSelect ? $value : 'other';
728
729 $opts = self::forceToStringRecursive( $this->mParams['options'] );
730
731 $select = new XmlSelect( $this->mName, $this->mID, $selected );
732 $select->addOptions( $opts );
733
734 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
735
736 $tbAttribs = array( 'id' => $this->mID . '-other' );
737 if( !empty( $this->mParams['disabled'] ) ) {
738 $select->setAttribute( 'disabled', 'disabled' );
739 $tbAttribs['disabled'] = 'disabled';
740 }
741
742 $select = $select->getHTML();
743
744 if ( isset( $this->mParams['maxlength'] ) ) {
745 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
746 }
747
748 $textbox = Xml::input( $this->mName . '-other',
749 $this->getSize(),
750 $valInSelect ? '' : $value,
751 $tbAttribs );
752
753 return "$select<br/>\n$textbox";
754 }
755
756 function loadDataFromRequest( $request ) {
757 if( $request->getCheck( $this->mName ) ) {
758 $val = $request->getText( $this->mName );
759
760 if( $val == 'other' ) {
761 $val = $request->getText( $this->mName . '-other' );
762 }
763
764 return $val;
765 } else {
766 return $this->getDefault();
767 }
768 }
769 }
770
771 class HTMLMultiSelectField extends HTMLFormField {
772 function validate( $value, $alldata ) {
773 $p = parent::validate( $value, $alldata );
774 if( $p !== true ) return $p;
775
776 if( !is_array( $value ) ) return false;
777
778 // If all options are valid, array_intersect of the valid options and the provided
779 // options will return the provided options.
780 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
781
782 $validValues = array_intersect( $value, $validOptions );
783 if ( count( $validValues ) == count( $value ) )
784 return true;
785 else
786 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
787 }
788
789 function getInputHTML( $value ) {
790 $html = $this->formatOptions( $this->mParams['options'], $value );
791
792 return $html;
793 }
794
795 function formatOptions( $options, $value ) {
796 $html = '';
797
798 $attribs = array();
799 if ( !empty( $this->mParams['disabled'] ) ) {
800 $attribs['disabled'] = 'disabled';
801 }
802
803 foreach( $options as $label => $info ) {
804 if( is_array( $info ) ) {
805 $html .= Xml::tags( 'h1', null, $label ) . "\n";
806 $html .= $this->formatOptions( $info, $value );
807 } else {
808 $thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info );
809
810 $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value ),
811 $attribs + $thisAttribs );
812 $checkbox .= '&nbsp;' . Xml::tags( 'label', array( 'for' => $this->mID . "-$info" ), $label );
813
814 $html .= $checkbox . '<br />';
815 }
816 }
817
818 return $html;
819 }
820
821 function loadDataFromRequest( $request ) {
822 // won't work with getCheck
823 if( $request->getCheck( 'wpEditToken' ) ) {
824 $arr = $request->getArray( $this->mName );
825
826 if( !$arr )
827 $arr = array();
828
829 return $arr;
830 } else {
831 return $this->getDefault();
832 }
833 }
834
835 function getDefault() {
836 if ( isset( $this->mDefault ) ) {
837 return $this->mDefault;
838 } else {
839 return array();
840 }
841 }
842 }
843
844 class HTMLRadioField extends HTMLFormField {
845 function validate( $value, $alldata ) {
846 $p = parent::validate( $value, $alldata );
847 if( $p !== true ) return $p;
848
849 if( !is_string( $value ) && !is_int( $value ) )
850 return false;
851
852 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
853
854 if ( in_array( $value, $validOptions ) )
855 return true;
856 else
857 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
858 }
859
860 function getInputHTML( $value ) {
861 $html = $this->formatOptions( $this->mParams['options'], $value );
862
863 return $html;
864 }
865
866 function formatOptions( $options, $value ) {
867 $html = '';
868
869 $attribs = array();
870 if ( !empty( $this->mParams['disabled'] ) ) {
871 $attribs['disabled'] = 'disabled';
872 }
873
874 foreach( $options as $label => $info ) {
875 if( is_array( $info ) ) {
876 $html .= Xml::tags( 'h1', null, $label ) . "\n";
877 $html .= $this->formatOptions( $info, $value );
878 } else {
879 $id = Sanitizer::escapeId( $this->mID . "-$info" );
880 $html .= Xml::radio( $this->mName, $info, $info == $value,
881 $attribs + array( 'id' => $id ) );
882 $html .= '&nbsp;' .
883 Xml::tags( 'label', array( 'for' => $id ), $label );
884
885 $html .= "<br/>\n";
886 }
887 }
888
889 return $html;
890 }
891 }
892
893 class HTMLInfoField extends HTMLFormField {
894 function __construct( $info ) {
895 $info['nodata'] = true;
896
897 parent::__construct( $info );
898 }
899
900 function getInputHTML( $value ) {
901 return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
902 }
903
904 function getTableRow( $value ) {
905 if ( !empty( $this->mParams['rawrow'] ) ) {
906 return $value;
907 }
908
909 return parent::getTableRow( $value );
910 }
911 }