Add AJAX category management system. Includes suggestion system, dialogs for setting...
[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 if ( isset( $this->mParams['type'] ) ) {
576 switch ( $this->mParams['type'] ) {
577 case 'email':
578 $attribs['type'] = 'email';
579 break;
580 case 'int':
581 $attribs['type'] = 'number';
582 break;
583 case 'float':
584 $attribs['type'] = 'number';
585 $attribs['step'] = 'any';
586 break;
587 case 'password':
588 $attribs['type'] = 'password';
589 break;
590 }
591 }
592 }
593
594 return Html::element( 'input', $attribs );
595 }
596 }
597
598 class HTMLFloatField extends HTMLTextField {
599 function getSize() {
600 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 20;
601 }
602
603 function validate( $value, $alldata ) {
604 $p = parent::validate( $value, $alldata );
605
606 if ( $p !== true ) return $p;
607
608 if ( floatval( $value ) != $value ) {
609 return wfMsgExt( 'htmlform-float-invalid', 'parse' );
610 }
611
612 $in_range = true;
613
614 # The "int" part of these message names is rather confusing. They make
615 # equal sense for all numbers.
616 if ( isset( $this->mParams['min'] ) ) {
617 $min = $this->mParams['min'];
618 if ( $min > $value )
619 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
620 }
621
622 if ( isset( $this->mParams['max'] ) ) {
623 $max = $this->mParams['max'];
624 if( $max < $value )
625 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
626 }
627
628 return true;
629 }
630 }
631
632 class HTMLIntField extends HTMLFloatField {
633 function validate( $value, $alldata ) {
634 $p = parent::validate( $value, $alldata );
635
636 if ( $p !== true ) return $p;
637
638 if ( intval( $value ) != $value ) {
639 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
640 }
641
642 return true;
643 }
644 }
645
646 class HTMLCheckField extends HTMLFormField {
647 function getInputHTML( $value ) {
648 if ( !empty( $this->mParams['invert'] ) )
649 $value = !$value;
650
651 $attr = array( 'id' => $this->mID );
652 if( !empty( $this->mParams['disabled'] ) ) {
653 $attr['disabled'] = 'disabled';
654 }
655
656 return Xml::check( $this->mName, $value, $attr ) . '&nbsp;' .
657 Html::rawElement( 'label', array( 'for' => $this->mID ), $this->mLabel );
658 }
659
660 function getLabel() {
661 return '&nbsp;'; // In the right-hand column.
662 }
663
664 function loadDataFromRequest( $request ) {
665 $invert = false;
666 if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
667 $invert = true;
668 }
669
670 // GetCheck won't work like we want for checks.
671 if( $request->getCheck( 'wpEditToken' ) ) {
672 // XOR has the following truth table, which is what we want
673 // INVERT VALUE | OUTPUT
674 // true true | false
675 // false true | true
676 // false false | false
677 // true false | true
678 return $request->getBool( $this->mName ) xor $invert;
679 } else {
680 return $this->getDefault();
681 }
682 }
683 }
684
685 class HTMLSelectField extends HTMLFormField {
686
687 function validate( $value, $alldata ) {
688 $p = parent::validate( $value, $alldata );
689 if( $p !== true ) return $p;
690
691 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
692 if ( in_array( $value, $validOptions ) )
693 return true;
694 else
695 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
696 }
697
698 function getInputHTML( $value ) {
699 $select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
700
701 // If one of the options' 'name' is int(0), it is automatically selected.
702 // because PHP sucks and things int(0) == 'some string'.
703 // Working around this by forcing all of them to strings.
704 $options = array_map( 'strval', $this->mParams['options'] );
705
706 if( !empty( $this->mParams['disabled'] ) ) {
707 $select->setAttribute( 'disabled', 'disabled' );
708 }
709
710 $select->addOptions( $options );
711
712 return $select->getHTML();
713 }
714 }
715
716 class HTMLSelectOrOtherField extends HTMLTextField {
717 static $jsAdded = false;
718
719 function __construct( $params ) {
720 if( !in_array( 'other', $params['options'] ) ) {
721 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
722 }
723
724 parent::__construct( $params );
725 }
726
727 static function forceToStringRecursive( $array ) {
728 if ( is_array($array) ) {
729 return array_map( array( __CLASS__, 'forceToStringRecursive' ), $array);
730 } else {
731 return strval($array);
732 }
733 }
734
735 function getInputHTML( $value ) {
736 $valInSelect = false;
737
738 if( $value !== false )
739 $valInSelect = in_array( $value,
740 HTMLFormField::flattenOptions( $this->mParams['options'] ) );
741
742 $selected = $valInSelect ? $value : 'other';
743
744 $opts = self::forceToStringRecursive( $this->mParams['options'] );
745
746 $select = new XmlSelect( $this->mName, $this->mID, $selected );
747 $select->addOptions( $opts );
748
749 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
750
751 $tbAttribs = array( 'id' => $this->mID . '-other' );
752 if( !empty( $this->mParams['disabled'] ) ) {
753 $select->setAttribute( 'disabled', 'disabled' );
754 $tbAttribs['disabled'] = 'disabled';
755 }
756
757 $select = $select->getHTML();
758
759 if ( isset( $this->mParams['maxlength'] ) ) {
760 $tbAttribs['maxlength'] = $this->mParams['maxlength'];
761 }
762
763 $textbox = Html::input( $this->mName . '-other',
764 $this->getSize(),
765 $valInSelect ? 'text' : $value,
766 $tbAttribs );
767
768 return "$select<br/>\n$textbox";
769 }
770
771 function loadDataFromRequest( $request ) {
772 if( $request->getCheck( $this->mName ) ) {
773 $val = $request->getText( $this->mName );
774
775 if( $val == 'other' ) {
776 $val = $request->getText( $this->mName . '-other' );
777 }
778
779 return $val;
780 } else {
781 return $this->getDefault();
782 }
783 }
784 }
785
786 class HTMLMultiSelectField extends HTMLFormField {
787 function validate( $value, $alldata ) {
788 $p = parent::validate( $value, $alldata );
789 if( $p !== true ) return $p;
790
791 if( !is_array( $value ) ) return false;
792
793 // If all options are valid, array_intersect of the valid options and the provided
794 // options will return the provided options.
795 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
796
797 $validValues = array_intersect( $value, $validOptions );
798 if ( count( $validValues ) == count( $value ) )
799 return true;
800 else
801 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
802 }
803
804 function getInputHTML( $value ) {
805 $html = $this->formatOptions( $this->mParams['options'], $value );
806
807 return $html;
808 }
809
810 function formatOptions( $options, $value ) {
811 $html = '';
812
813 $attribs = array();
814 if ( !empty( $this->mParams['disabled'] ) ) {
815 $attribs['disabled'] = 'disabled';
816 }
817
818 foreach( $options as $label => $info ) {
819 if( is_array( $info ) ) {
820 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
821 $html .= $this->formatOptions( $info, $value );
822 } else {
823 $thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info );
824
825 $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value ),
826 $attribs + $thisAttribs );
827 $checkbox .= '&nbsp;' . Html::rawElement( 'label', array( 'for' => $this->mID . "-$info" ), $label );
828
829 $html .= $checkbox . '<br />';
830 }
831 }
832
833 return $html;
834 }
835
836 function loadDataFromRequest( $request ) {
837 // won't work with getCheck
838 if( $request->getCheck( 'wpEditToken' ) ) {
839 $arr = $request->getArray( $this->mName );
840
841 if( !$arr )
842 $arr = array();
843
844 return $arr;
845 } else {
846 return $this->getDefault();
847 }
848 }
849
850 function getDefault() {
851 if ( isset( $this->mDefault ) ) {
852 return $this->mDefault;
853 } else {
854 return array();
855 }
856 }
857
858 protected function needsLabel() {
859 return false;
860 }
861 }
862
863 class HTMLRadioField extends HTMLFormField {
864 function validate( $value, $alldata ) {
865 $p = parent::validate( $value, $alldata );
866 if( $p !== true ) return $p;
867
868 if( !is_string( $value ) && !is_int( $value ) )
869 return false;
870
871 $validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
872
873 if ( in_array( $value, $validOptions ) )
874 return true;
875 else
876 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
877 }
878
879 function getInputHTML( $value ) {
880 $html = $this->formatOptions( $this->mParams['options'], $value );
881
882 return $html;
883 }
884
885 function formatOptions( $options, $value ) {
886 $html = '';
887
888 $attribs = array();
889 if ( !empty( $this->mParams['disabled'] ) ) {
890 $attribs['disabled'] = 'disabled';
891 }
892
893 foreach( $options as $label => $info ) {
894 if( is_array( $info ) ) {
895 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
896 $html .= $this->formatOptions( $info, $value );
897 } else {
898 $id = Sanitizer::escapeId( $this->mID . "-$info" );
899 $html .= Xml::radio( $this->mName, $info, $info == $value,
900 $attribs + array( 'id' => $id ) );
901 $html .= '&nbsp;' .
902 Html::rawElement( 'label', array( 'for' => $id ), $label );
903
904 $html .= "<br/>\n";
905 }
906 }
907
908 return $html;
909 }
910
911 protected function needsLabel() {
912 return false;
913 }
914 }
915
916 class HTMLInfoField extends HTMLFormField {
917 function __construct( $info ) {
918 $info['nodata'] = true;
919
920 parent::__construct( $info );
921 }
922
923 function getInputHTML( $value ) {
924 return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
925 }
926
927 function getTableRow( $value ) {
928 if ( !empty( $this->mParams['rawrow'] ) ) {
929 return $value;
930 }
931
932 return parent::getTableRow( $value );
933 }
934
935 protected function needsLabel() {
936 return false;
937 }
938 }