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