Fix bug in HTMLForm where password fields only got the type="password" attribute...
[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 'selectorother' => 'HTMLSelectOrOtherField',
25 # HTMLTextField will output the correct type="" attribute automagically.
26 # There are about four zillion other HTML 5 input types, like url, but
27 # we don't use those at the moment, so no point in adding all of them.
28 'email' => 'HTMLTextField',
29 'password' => '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 Html::rawElement(
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 .= Html::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
198 $html .= Html::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 .= Html::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 = Html::rawElement( '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 .= Html::rawElement(
255 'li',
256 null,
257 wfMsgExt( $msg, array( 'parseinline' ), $error )
258 );
259 }
260
261 $errorstr = Html::rawElement( 'ul', array(), $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 = Html::rawElement( 'table', array( 'class' => $classes ),
317 Html::rawElement( 'tbody', array(), "\n$tableHtml\n" ) ) . "\n";
318
319 return $subsectionHtml . "\n" . $tableHtml;
320 }
321
322 function loadData() {
323 global $wgRequest;
324
325 $fieldData = array();
326
327 foreach( $this->mFlatFields as $fieldname => $field ) {
328 if ( !empty( $field->mParams['nodata'] ) ) continue;
329 if ( !empty( $field->mParams['disabled'] ) ) {
330 $fieldData[$fieldname] = $field->getDefault();
331 } else {
332 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
333 }
334 }
335
336 // Filter data.
337 foreach( $fieldData as $name => &$value ) {
338 $field = $this->mFlatFields[$name];
339 $value = $field->filter( $value, $this->mFlatFields );
340 }
341
342 $this->mFieldData = $fieldData;
343 }
344
345 function importData( $fieldData ) {
346 // Filter data.
347 foreach( $fieldData as $name => &$value ) {
348 $field = $this->mFlatFields[$name];
349 $value = $field->filter( $value, $this->mFlatFields );
350 }
351
352 foreach( $this->mFlatFields as $fieldname => $field ) {
353 if ( !isset( $fieldData[$fieldname] ) )
354 $fieldData[$fieldname] = $field->getDefault();
355 }
356
357 $this->mFieldData = $fieldData;
358 }
359
360 function suppressReset( $suppressReset = true ) {
361 $this->mShowReset = !$suppressReset;
362 }
363
364 function filterDataForSubmit( $data ) {
365 return $data;
366 }
367 }
368
369 abstract class HTMLFormField {
370 abstract function getInputHTML( $value );
371
372 function validate( $value, $alldata ) {
373 if ( isset( $this->mValidationCallback ) ) {
374 return call_user_func( $this->mValidationCallback, $value, $alldata );
375 }
376
377 return true;
378 }
379
380 function filter( $value, $alldata ) {
381 if( isset( $this->mFilterCallback ) ) {
382 $value = call_user_func( $this->mFilterCallback, $value, $alldata );
383 }
384
385 return $value;
386 }
387
388 /**
389 * Should this field have a label, or is there no input element with the
390 * appropriate id for the label to point to?
391 *
392 * @return bool True to output a label, false to suppress
393 */
394 protected function needsLabel() {
395 return true;
396 }
397
398 function loadDataFromRequest( $request ) {
399 if( $request->getCheck( $this->mName ) ) {
400 return $request->getText( $this->mName );
401 } else {
402 return $this->getDefault();
403 }
404 }
405
406 function __construct( $params ) {
407 $this->mParams = $params;
408
409 if( isset( $params['label-message'] ) ) {
410 $msgInfo = $params['label-message'];
411
412 if ( is_array( $msgInfo ) ) {
413 $msg = array_shift( $msgInfo );
414 } else {
415 $msg = $msgInfo;
416 $msgInfo = array();
417 }
418
419 $this->mLabel = wfMsgExt( $msg, 'parseinline', $msgInfo );
420 } elseif ( isset( $params['label'] ) ) {
421 $this->mLabel = $params['label'];
422 }
423
424 if ( isset( $params['name'] ) ) {
425 $name = $params['name'];
426 $validName = Sanitizer::escapeId( $name );
427 if( $name != $validName ) {
428 throw new MWException("Invalid name '$name' passed to " . __METHOD__ );
429 }
430 $this->mName = 'wp'.$name;
431 $this->mID = 'mw-input-'.$name;
432 }
433
434 if ( isset( $params['default'] ) ) {
435 $this->mDefault = $params['default'];
436 }
437
438 if ( isset( $params['id'] ) ) {
439 $id = $params['id'];
440 $validId = Sanitizer::escapeId( $id );
441 if( $id != $validId ) {
442 throw new MWException("Invalid id '$id' passed to " . __METHOD__ );
443 }
444 $this->mID = $id;
445 }
446
447 if ( isset( $params['validation-callback'] ) ) {
448 $this->mValidationCallback = $params['validation-callback'];
449 }
450
451 if ( isset( $params['filter-callback'] ) ) {
452 $this->mFilterCallback = $params['filter-callback'];
453 }
454 }
455
456 function getTableRow( $value ) {
457 // Check for invalid data.
458 global $wgRequest;
459
460 $errors = $this->validate( $value, $this->mParent->mFieldData );
461 if ( $errors === true || !$wgRequest->wasPosted() ) {
462 $errors = '';
463 } else {
464 $errors = Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
465 }
466
467 $html = '';
468
469 # Don't output a for= attribute for labels with no associated input.
470 # Kind of hacky here, possibly we don't want these to be <label>s at all.
471 $for = array();
472 if ( $this->needsLabel() ) {
473 $for['for'] = $this->mID;
474 }
475 $html .= Html::rawElement( 'td', array( 'class' => 'mw-label' ),
476 Html::rawElement( 'label', $for, $this->getLabel() )
477 );
478 $html .= Html::rawElement( 'td', array( 'class' => 'mw-input' ),
479 $this->getInputHTML( $value ) ."\n$errors" );
480
481 $fieldType = get_class( $this );
482
483 $html = Html::rawElement( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
484 $html ) . "\n";
485
486 $helptext = null;
487 if ( isset( $this->mParams['help-message'] ) ) {
488 $msg = $this->mParams['help-message'];
489 $helptext = wfMsgExt( $msg, 'parseinline' );
490 if ( wfEmptyMsg( $msg, $helptext ) ) {
491 # Never mind
492 $helptext = null;
493 }
494 } elseif ( isset( $this->mParams['help'] ) ) {
495 $helptext = $this->mParams['help'];
496 }
497
498 if ( !is_null( $helptext ) ) {
499 $row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
500 $helptext );
501 $row = Html::rawElement( 'tr', array(), $row );
502 $html .= "$row\n";
503 }
504
505 return $html;
506 }
507
508 function getLabel() {
509 return $this->mLabel;
510 }
511
512 function getDefault() {
513 if ( isset( $this->mDefault ) ) {
514 return $this->mDefault;
515 } else {
516 return null;
517 }
518 }
519
520 static function flattenOptions( $options ) {
521 $flatOpts = array();
522
523 foreach( $options as $key => $value ) {
524 if ( is_array( $value ) ) {
525 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
526 } else {
527 $flatOpts[] = $value;
528 }
529 }
530
531 return $flatOpts;
532 }
533 }
534
535 class HTMLTextField extends HTMLFormField {
536 # Override in derived classes to use other Xml::... functions
537 protected $mFunction = 'input';
538
539 function getSize() {
540 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
541 }
542
543 function getInputHTML( $value ) {
544 global $wgHtml5;
545 $attribs = array(
546 'id' => $this->mID,
547 'name' => $this->mName,
548 'size' => $this->getSize(),
549 'value' => $value,
550 );
551
552 if ( isset( $this->mParams['maxlength'] ) ) {
553 $attribs['maxlength'] = $this->mParams['maxlength'];
554 }
555
556 if ( !empty( $this->mParams['disabled'] ) ) {
557 $attribs['disabled'] = 'disabled';
558 }
559
560 if ( $wgHtml5 ) {
561 # TODO: Enforce pattern, step, required, readonly on the server
562 # side as well
563 foreach ( array( 'min', 'max', 'pattern', 'title', 'step',
564 'placeholder' ) as $param ) {
565 if ( isset( $this->mParams[$param] ) ) {
566 $attribs[$param] = $this->mParams[$param];
567 }
568 }
569 foreach ( array( 'required', 'autofocus', 'multiple', 'readonly' )
570 as $param ) {
571 if ( isset( $this->mParams[$param] ) ) {
572 $attribs[$param] = '';
573 }
574 }
575 }
576
577 # Implement tiny differences between some field variants
578 # here, rather than creating a new class for each one which
579 # is essentially just a clone of this one.
580 if ( isset( $this->mParams['type'] ) ) {
581 # Options that apply only to HTML5
582 if( $wgHtml5 ){
583 switch ( $this->mParams['type'] ) {
584 case 'email':
585 $attribs['type'] = 'email';
586 break;
587 case 'int':
588 $attribs['type'] = 'number';
589 break;
590 case 'float':
591 $attribs['type'] = 'number';
592 $attribs['step'] = 'any';
593 break;
594 }
595 }
596 # Options that apply to HTML4 as well
597 switch( $this->mParams['type'] ){
598 case 'password':
599 $attribs['type'] = 'password';
600 break;
601 }
602 }
603
604 return Html::element( 'input', $attribs );
605 }
606 }
607
608 class HTMLFloatField extends HTMLTextField {
609 function getSize() {
610 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 20;
611 }
612
613 function validate( $value, $alldata ) {
614 $p = parent::validate( $value, $alldata );
615
616 if ( $p !== true ) return $p;
617
618 if ( floatval( $value ) != $value ) {
619 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
620 }
621
622 $in_range = true;
623
624 # The "int" part of these message names is rather confusing. They make
625 # equal sense for all numbers.
626 if ( isset( $this->mParams['min'] ) ) {
627 $min = $this->mParams['min'];
628 if ( $min > $value )
629 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
630 }
631
632 if ( isset( $this->mParams['max'] ) ) {
633 $max = $this->mParams['max'];
634 if( $max < $value )
635 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
636 }
637
638 return true;
639 }
640 }
641
642 class HTMLIntField extends HTMLFloatField {
643 function validate( $value, $alldata ) {
644 $p = parent::validate( $value, $alldata );
645
646 if ( $p !== true ) return $p;
647
648 if ( intval( $value ) != $value ) {
649 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
650 }
651
652 return true;
653 }
654 }
655
656 class HTMLCheckField extends HTMLFormField {
657 function getInputHTML( $value ) {
658 if ( !empty( $this->mParams['invert'] ) )
659 $value = !$value;
660
661 $attr = array( 'id' => $this->mID );
662 if( !empty( $this->mParams['disabled'] ) ) {
663 $attr['disabled'] = 'disabled';
664 }
665
666 return Xml::check( $this->mName, $value, $attr ) . '&nbsp;' .
667 Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
668 }
669
670 function getLabel() {
671 return '&nbsp;'; // In the right-hand column.
672 }
673
674 function loadDataFromRequest( $request ) {
675 $invert = false;
676 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
677 $invert = true;
678 }
679
680 // GetCheck won't work like we want for checks.
681 if( $request->getCheck( 'wpEditToken' ) ) {
682 // XOR has the following truth table, which is what we want
683 // INVERT VALUE | OUTPUT
684 // true true | false
685 // false true | true
686 // false false | false
687 // true false | true
688 return $request->getBool( $this->mName ) xor $invert;
689 } else {
690 return $this->getDefault();
691 }
692 }
693 }
694
695 class HTMLSelectField extends HTMLFormField {
696
697 function validate( $value, $alldata ) {
698 $p = parent::validate( $value, $alldata );
699 if( $p !== true ) return $p;
700
701 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
702 if ( in_array( $value, $validOptions ) )
703 return true;
704 else
705 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
706 }
707
708 function getInputHTML( $value ) {
709 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
710
711 // If one of the options' 'name' is int(0), it is automatically selected.
712 // because PHP sucks and things int(0) == 'some string'.
713 // Working around this by forcing all of them to strings.
714 $options = array_map( 'strval', $this->mParams['options'] );
715
716 if( !empty( $this->mParams['disabled'] ) ) {
717 $select->setAttribute( 'disabled', 'disabled' );
718 }
719
720 $select->addOptions( $options );
721
722 return $select->getHTML();
723 }
724 }
725
726 class HTMLSelectOrOtherField extends HTMLTextField {
727 static $jsAdded = false;
728
729 function __construct( $params ) {
730 if( !in_array( 'other', $params['options'], true ) ) {
731 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
732 }
733
734 parent::__construct( $params );
735 }
736
737 static function forceToStringRecursive( $array ) {
738 if ( is_array($array) ) {
739 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array);
740 } else {
741 return strval($array);
742 }
743 }
744
745 function getInputHTML( $value ) {
746 $valInSelect = false;
747
748 if( $value !== false )
749 $valInSelect = in_array( $value,
750 HTMLFormField::flattenOptions( $this->mParams['options'] ) );
751
752 $selected = $valInSelect ? $value : 'other';
753
754 $opts = self::forceToStringRecursive( $this->mParams['options'] );
755
756 $select = new XmlSelect( $this->mName, $this->mID, $selected );
757 $select->addOptions( $opts );
758
759 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
760
761 $tbAttribs = array( 'id' => $this->mID . '-other', 'size' => $this->getSize() );
762 if( !empty( $this->mParams['disabled'] ) ) {
763 $select->setAttribute( 'disabled', 'disabled' );
764 $tbAttribs['disabled'] = 'disabled';
765 }
766
767 $select = $select->getHTML();
768
769 if ( isset( $this->mParams['maxlength'] ) ) {
770 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
771 }
772
773 $textbox = Html::input( $this->mName . '-other',
774 $valInSelect ? '' : $value,
775 'text',
776 $tbAttribs );
777
778 return "$select<br/>\n$textbox";
779 }
780
781 function loadDataFromRequest( $request ) {
782 if( $request->getCheck( $this->mName ) ) {
783 $val = $request->getText( $this->mName );
784
785 if( $val == 'other' ) {
786 $val = $request->getText( $this->mName . '-other' );
787 }
788
789 return $val;
790 } else {
791 return $this->getDefault();
792 }
793 }
794 }
795
796 class HTMLMultiSelectField extends HTMLFormField {
797 function validate( $value, $alldata ) {
798 $p = parent::validate( $value, $alldata );
799 if( $p !== true ) return $p;
800
801 if( !is_array( $value ) ) return false;
802
803 // If all options are valid, array_intersect of the valid options and the provided
804 // options will return the provided options.
805 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
806
807 $validValues = array_intersect( $value, $validOptions );
808 if ( count( $validValues ) == count( $value ) )
809 return true;
810 else
811 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
812 }
813
814 function getInputHTML( $value ) {
815 $html = $this->formatOptions( $this->mParams['options'], $value );
816
817 return $html;
818 }
819
820 function formatOptions( $options, $value ) {
821 $html = '';
822
823 $attribs = array();
824 if ( !empty( $this->mParams['disabled'] ) ) {
825 $attribs['disabled'] = 'disabled';
826 }
827
828 foreach( $options as $label => $info ) {
829 if( is_array( $info ) ) {
830 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
831 $html .= $this->formatOptions( $info, $value );
832 } else {
833 $thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info );
834
835 $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value ),
836 $attribs + $thisAttribs );
837 $checkbox .= '&nbsp;' . Html::rawElement( 'label', array( 'for' => $this->mID . "-$info" ), $label );
838
839 $html .= $checkbox . '<br />';
840 }
841 }
842
843 return $html;
844 }
845
846 function loadDataFromRequest( $request ) {
847 // won't work with getCheck
848 if( $request->getCheck( 'wpEditToken' ) ) {
849 $arr = $request->getArray( $this->mName );
850
851 if( !$arr )
852 $arr = array();
853
854 return $arr;
855 } else {
856 return $this->getDefault();
857 }
858 }
859
860 function getDefault() {
861 if ( isset( $this->mDefault ) ) {
862 return $this->mDefault;
863 } else {
864 return array();
865 }
866 }
867
868 protected function needsLabel() {
869 return false;
870 }
871 }
872
873 class HTMLRadioField extends HTMLFormField {
874 function validate( $value, $alldata ) {
875 $p = parent::validate( $value, $alldata );
876 if( $p !== true ) return $p;
877
878 if( !is_string( $value ) && !is_int( $value ) )
879 return false;
880
881 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
882
883 if ( in_array( $value, $validOptions ) )
884 return true;
885 else
886 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
887 }
888
889 function getInputHTML( $value ) {
890 $html = $this->formatOptions( $this->mParams['options'], $value );
891
892 return $html;
893 }
894
895 function formatOptions( $options, $value ) {
896 $html = '';
897
898 $attribs = array();
899 if ( !empty( $this->mParams['disabled'] ) ) {
900 $attribs['disabled'] = 'disabled';
901 }
902
903 foreach( $options as $label => $info ) {
904 if( is_array( $info ) ) {
905 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
906 $html .= $this->formatOptions( $info, $value );
907 } else {
908 $id = Sanitizer::escapeId( $this->mID . "-$info" );
909 $html .= Xml::radio( $this->mName, $info, $info == $value,
910 $attribs + array( 'id' => $id ) );
911 $html .= '&nbsp;' .
912 Html::rawElement( 'label', array( 'for' => $id ), $label );
913
914 $html .= "<br/>\n";
915 }
916 }
917
918 return $html;
919 }
920
921 protected function needsLabel() {
922 return false;
923 }
924 }
925
926 class HTMLInfoField extends HTMLFormField {
927 function __construct( $info ) {
928 $info['nodata'] = true;
929
930 parent::__construct( $info );
931 }
932
933 function getInputHTML( $value ) {
934 return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
935 }
936
937 function getTableRow( $value ) {
938 if ( !empty( $this->mParams['rawrow'] ) ) {
939 return $value;
940 }
941
942 return parent::getTableRow( $value );
943 }
944
945 protected function needsLabel() {
946 return false;
947 }
948 }