59429c90773b8032582d88fa65ec8ded0d87d793
[lhc/web/wiklou.git] / includes / HTMLForm.php
1 <?php
2
3 class HTMLForm {
4
5 static $jsAdded = false;
6
7 /* The descriptor is an array of arrays.
8 i.e. array(
9 'fieldname' => array( 'section' => 'section/subsection',
10 properties... ),
11 ...
12 )
13 */
14
15 static $typeMappings = array(
16 'text' => 'HTMLTextField',
17 'select' => 'HTMLSelectField',
18 'radio' => 'HTMLRadioField',
19 'multiselect' => 'HTMLMultiSelectField',
20 'check' => 'HTMLCheckField',
21 'toggle' => 'HTMLCheckField',
22 'int' => 'HTMLIntField',
23 'float' => 'HTMLFloatField',
24 'info' => 'HTMLInfoField',
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 function getSize() {
520 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
521 }
522
523 function getInputHTML( $value ) {
524 global $wgHtml5;
525 $attribs = array( 'id' => $this->mID );
526
527 if ( isset( $this->mParams['maxlength'] ) ) {
528 $attribs['maxlength'] = $this->mParams['maxlength'];
529 }
530
531 if ( !empty( $this->mParams['disabled'] ) ) {
532 $attribs['disabled'] = 'disabled';
533 }
534
535 if ( $wgHtml5 ) {
536 # TODO: Enforce pattern, step, required, readonly on the server
537 # side as well
538 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
539 'placeholder' ) as $param ) {
540 if ( isset( $this->mParams[$param] ) ) {
541 $attribs[$param] = $this->mParams[$param];
542 }
543 }
544 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' )
545 as $param ) {
546 if ( isset( $this->mParams[$param] ) ) {
547 $attribs[$param] = '';
548 }
549 }
550 if ( isset( $this->mParams['type'] ) ) {
551 switch ( $this->mParams['type'] ) {
552 case 'email':
553 $attribs['type'] = 'email';
554 break;
555 case 'int':
556 $attribs['type'] = 'number';
557 break;
558 case 'float':
559 $attribs['type'] = 'number';
560 $attribs['step'] = 'any';
561 break;
562 }
563 }
564 }
565
566 return Xml::input(
567 $this->mName,
568 $this->getSize(),
569 $value,
570 $attribs
571 );
572 }
573 }
574
575 class HTMLFloatField extends HTMLTextField {
576 function getSize() {
577 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 20;
578 }
579
580 function validate( $value, $alldata ) {
581 $p = parent::validate( $value, $alldata );
582
583 if ( $p !== true ) return $p;
584
585 if ( floatval( $value ) != $value ) {
586 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
587 }
588
589 $in_range = true;
590
591 # The "int" part of these message names is rather confusing. They make
592 # equal sense for all numbers.
593 if ( isset( $this->mParams['min'] ) ) {
594 $min = $this->mParams['min'];
595 if ( $min > $value )
596 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
597 }
598
599 if ( isset( $this->mParams['max'] ) ) {
600 $max = $this->mParams['max'];
601 if( $max < $value )
602 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
603 }
604
605 return true;
606 }
607 }
608
609 class HTMLIntField extends HTMLFloatField {
610 function validate( $value, $alldata ) {
611 $p = parent::validate( $value, $alldata );
612
613 if ( $p !== true ) return $p;
614
615 if ( intval( $value ) != $value ) {
616 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
617 }
618
619 return true;
620 }
621 }
622
623 class HTMLCheckField extends HTMLFormField {
624 function getInputHTML( $value ) {
625 if ( !empty( $this->mParams['invert'] ) )
626 $value = !$value;
627
628 $attr = array( 'id' => $this->mID );
629 if( !empty( $this->mParams['disabled'] ) ) {
630 $attr['disabled'] = 'disabled';
631 }
632
633 return Xml::check( $this->mName, $value, $attr ) . '&nbsp;' .
634 Xml::tags( 'label', array( 'for' => $this->mID ), $this->mLabel );
635 }
636
637 function getLabel() {
638 return '&nbsp;'; // In the right-hand column.
639 }
640
641 function loadDataFromRequest( $request ) {
642 $invert = false;
643 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
644 $invert = true;
645 }
646
647 // GetCheck won't work like we want for checks.
648 if( $request->getCheck( 'wpEditToken' ) ) {
649 // XOR has the following truth table, which is what we want
650 // INVERT VALUE | OUTPUT
651 // true true | false
652 // false true | true
653 // false false | false
654 // true false | true
655 return $request->getBool( $this->mName ) xor $invert;
656 } else {
657 return $this->getDefault();
658 }
659 }
660 }
661
662 class HTMLSelectField extends HTMLFormField {
663
664 function validate( $value, $alldata ) {
665 $p = parent::validate( $value, $alldata );
666 if( $p !== true ) return $p;
667
668 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
669 if ( in_array( $value, $validOptions ) )
670 return true;
671 else
672 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
673 }
674
675 function getInputHTML( $value ) {
676 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
677
678 // If one of the options' 'name' is int(0), it is automatically selected.
679 // because PHP sucks and things int(0) == 'some string'.
680 // Working around this by forcing all of them to strings.
681 $options = array_map( 'strval', $this->mParams['options'] );
682
683 if( !empty( $this->mParams['disabled'] ) ) {
684 $select->setAttribute( 'disabled', 'disabled' );
685 }
686
687 $select->addOptions( $options );
688
689 return $select->getHTML();
690 }
691 }
692
693 class HTMLSelectOrOtherField extends HTMLTextField {
694 static $jsAdded = false;
695
696 function __construct( $params ) {
697 if( !in_array( 'other', $params['options'] ) ) {
698 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
699 }
700
701 parent::__construct( $params );
702 }
703
704 static function forceToStringRecursive( $array ) {
705 if ( is_array($array) ) {
706 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array);
707 } else {
708 return strval($array);
709 }
710 }
711
712 function getInputHTML( $value ) {
713 $valInSelect = false;
714
715 if( $value !== false )
716 $valInSelect = in_array( $value,
717 HTMLFormField::flattenOptions( $this->mParams['options'] ) );
718
719 $selected = $valInSelect ? $value : 'other';
720
721 $opts = self::forceToStringRecursive( $this->mParams['options'] );
722
723 $select = new XmlSelect( $this->mName, $this->mID, $selected );
724 $select->addOptions( $opts );
725
726 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
727
728 $tbAttribs = array( 'id' => $this->mID . '-other' );
729 if( !empty( $this->mParams['disabled'] ) ) {
730 $select->setAttribute( 'disabled', 'disabled' );
731 $tbAttribs['disabled'] = 'disabled';
732 }
733
734 $select = $select->getHTML();
735
736 if ( isset( $this->mParams['maxlength'] ) ) {
737 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
738 }
739
740 $textbox = Xml::input( $this->mName . '-other',
741 $this->getSize(),
742 $valInSelect ? '' : $value,
743 $tbAttribs );
744
745 return "$select<br/>\n$textbox";
746 }
747
748 function loadDataFromRequest( $request ) {
749 if( $request->getCheck( $this->mName ) ) {
750 $val = $request->getText( $this->mName );
751
752 if( $val == 'other' ) {
753 $val = $request->getText( $this->mName . '-other' );
754 }
755
756 return $val;
757 } else {
758 return $this->getDefault();
759 }
760 }
761 }
762
763 class HTMLMultiSelectField extends HTMLFormField {
764 function validate( $value, $alldata ) {
765 $p = parent::validate( $value, $alldata );
766 if( $p !== true ) return $p;
767
768 if( !is_array( $value ) ) return false;
769
770 // If all options are valid, array_intersect of the valid options and the provided
771 // options will return the provided options.
772 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
773
774 $validValues = array_intersect( $value, $validOptions );
775 if ( count( $validValues ) == count( $value ) )
776 return true;
777 else
778 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
779 }
780
781 function getInputHTML( $value ) {
782 $html = $this->formatOptions( $this->mParams['options'], $value );
783
784 return $html;
785 }
786
787 function formatOptions( $options, $value ) {
788 $html = '';
789
790 $attribs = array();
791 if ( !empty( $this->mParams['disabled'] ) ) {
792 $attribs['disabled'] = 'disabled';
793 }
794
795 foreach( $options as $label => $info ) {
796 if( is_array( $info ) ) {
797 $html .= Xml::tags( 'h1', null, $label ) . "\n";
798 $html .= $this->formatOptions( $info, $value );
799 } else {
800 $thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info );
801
802 $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value ),
803 $attribs + $thisAttribs );
804 $checkbox .= '&nbsp;' . Xml::tags( 'label', array( 'for' => $this->mID . "-$info" ), $label );
805
806 $html .= $checkbox . '<br />';
807 }
808 }
809
810 return $html;
811 }
812
813 function loadDataFromRequest( $request ) {
814 // won't work with getCheck
815 if( $request->getCheck( 'wpEditToken' ) ) {
816 $arr = $request->getArray( $this->mName );
817
818 if( !$arr )
819 $arr = array();
820
821 return $arr;
822 } else {
823 return $this->getDefault();
824 }
825 }
826
827 function getDefault() {
828 if ( isset( $this->mDefault ) ) {
829 return $this->mDefault;
830 } else {
831 return array();
832 }
833 }
834 }
835
836 class HTMLRadioField extends HTMLFormField {
837 function validate( $value, $alldata ) {
838 $p = parent::validate( $value, $alldata );
839 if( $p !== true ) return $p;
840
841 if( !is_string( $value ) && !is_int( $value ) )
842 return false;
843
844 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
845
846 if ( in_array( $value, $validOptions ) )
847 return true;
848 else
849 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
850 }
851
852 function getInputHTML( $value ) {
853 $html = $this->formatOptions( $this->mParams['options'], $value );
854
855 return $html;
856 }
857
858 function formatOptions( $options, $value ) {
859 $html = '';
860
861 $attribs = array();
862 if ( !empty( $this->mParams['disabled'] ) ) {
863 $attribs['disabled'] = 'disabled';
864 }
865
866 foreach( $options as $label => $info ) {
867 if( is_array( $info ) ) {
868 $html .= Xml::tags( 'h1', null, $label ) . "\n";
869 $html .= $this->formatOptions( $info, $value );
870 } else {
871 $id = Sanitizer::escapeId( $this->mID . "-$info" );
872 $html .= Xml::radio( $this->mName, $info, $info == $value,
873 $attribs + array( 'id' => $id ) );
874 $html .= '&nbsp;' .
875 Xml::tags( 'label', array( 'for' => $id ), $label );
876
877 $html .= "<br/>\n";
878 }
879 }
880
881 return $html;
882 }
883 }
884
885 class HTMLInfoField extends HTMLFormField {
886 function __construct( $info ) {
887 $info['nodata'] = true;
888
889 parent::__construct( $info );
890 }
891
892 function getInputHTML( $value ) {
893 return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
894 }
895
896 function getTableRow( $value ) {
897 if ( !empty( $this->mParams['rawrow'] ) ) {
898 return $value;
899 }
900
901 return parent::getTableRow( $value );
902 }
903 }