5 static $jsAdded = false;
7 /* The descriptor is an array of arrays.
9 'fieldname' => array( 'section' => 'section/subsection',
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 'info' => 'HTMLInfoField',
24 'selectorother' => 'HTMLSelectOrOtherField',
27 function __construct( $descriptor, $messagePrefix ) {
28 $this->mMessagePrefix
= $messagePrefix;
30 // Expand out into a tree.
31 $loadedDescriptor = array();
32 $this->mFlatFields
= array();
34 foreach( $descriptor as $fieldname => $info ) {
36 if ( isset( $info['section'] ) )
37 $section = $info['section'];
39 $info['name'] = $fieldname;
41 $field = $this->loadInputFromParameters( $info );
42 $field->mParent
= $this;
44 $setSection =& $loadedDescriptor;
46 $sectionParts = explode( '/', $section );
48 while( count( $sectionParts ) ) {
49 $newName = array_shift( $sectionParts );
51 if ( !isset( $setSection[$newName] ) ) {
52 $setSection[$newName] = array();
55 $setSection =& $setSection[$newName];
59 $setSection[$fieldname] = $field;
60 $this->mFlatFields
[$fieldname] = $field;
63 $this->mFieldTree
= $loadedDescriptor;
65 $this->mShowReset
= true;
68 static function addJS() {
69 if( self
::$jsAdded ) return;
71 global $wgOut, $wgStylePath;
73 $wgOut->addScriptFile( "$wgStylePath/common/htmlform.js" );
76 static function loadInputFromParameters( $descriptor ) {
77 if ( isset( $descriptor['class'] ) ) {
78 $class = $descriptor['class'];
79 } elseif ( isset( $descriptor['type'] ) ) {
80 $class = self
::$typeMappings[$descriptor['type']];
81 $descriptor['class'] = $class;
85 throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
88 $obj = new $class( $descriptor );
98 // Load data from the request.
102 global $wgUser, $wgRequest;
103 $editToken = $wgRequest->getVal( 'wpEditToken' );
106 if ( $wgUser->matchEditToken( $editToken ) )
107 $result = $this->trySubmit();
109 if( $result === true )
113 $this->displayForm( $result );
117 * TRUE == Successful submission
118 * FALSE == No submission attempted
119 * Anything else == Error to display.
121 function trySubmit() {
122 // Check for validation
123 foreach( $this->mFlatFields
as $fieldname => $field ) {
124 if ( !empty( $field->mParams
['nodata'] ) ) continue;
125 if ( $field->validate( $this->mFieldData
[$fieldname],
126 $this->mFieldData
) !== true ) {
127 return isset( $this->mValidationErrorMessage
) ?
128 $this->mValidationErrorMessage
: array( 'htmlform-invalid-input' );
132 $callback = $this->mSubmitCallback
;
134 $data = $this->filterDataForSubmit( $this->mFieldData
);
136 $res = call_user_func( $callback, $data );
141 function setSubmitCallback( $cb ) {
142 $this->mSubmitCallback
= $cb;
145 function setValidationErrorMessage( $msg ) {
146 $this->mValidationErrorMessage
= $msg;
149 function setIntro( $msg ) {
150 $this->mIntro
= $msg;
153 function displayForm( $submitResult ) {
156 if ( $submitResult !== false ) {
157 $this->displayErrors( $submitResult );
160 if ( isset( $this->mIntro
) ) {
161 $wgOut->addHTML( $this->mIntro
);
164 $html = $this->getBody();
167 $html .= $this->getHiddenFields();
170 $html .= $this->getButtons();
172 $html = $this->wrapForm( $html );
174 $wgOut->addHTML( $html );
177 function wrapForm( $html ) {
181 'action' => $this->getTitle()->getFullURL(),
188 function getHiddenFields() {
192 $html .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
193 $html .= Xml
::hidden( 'title', $this->getTitle() ) . "\n";
198 function getButtons() {
203 if ( isset( $this->mSubmitID
) )
204 $attribs['id'] = $this->mSubmitID
;
206 $attribs['class'] = 'mw-htmlform-submit';
208 $html .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
210 if( $this->mShowReset
) {
211 $html .= Xml
::element(
215 'value' => wfMsg( 'htmlform-reset' )
224 return $this->displaySection( $this->mFieldTree
);
227 function displayErrors( $errors ) {
228 if ( is_array( $errors ) ) {
229 $errorstr = $this->formatErrors( $errors );
234 $errorstr = Xml
::tags( 'div', array( 'class' => 'error' ), $errorstr );
237 $wgOut->addHTML( $errorstr );
240 static function formatErrors( $errors ) {
242 foreach ( $errors as $error ) {
243 if( is_array( $error ) ) {
244 $msg = array_shift( $error );
249 $errorstr .= Xml
::tags(
252 wfMsgExt( $msg, array( 'parseinline' ), $error )
256 $errorstr = Xml
::tags( 'ul', null, $errorstr );
261 function setSubmitText( $t ) {
262 $this->mSubmitText
= $t;
265 function getSubmitText() {
266 return isset( $this->mSubmitText
) ?
$this->mSubmitText
: wfMsg( 'htmlform-submit' );
269 function setSubmitID( $t ) {
270 $this->mSubmitID
= $t;
273 function setMessagePrefix( $p ) {
274 $this->mMessagePrefix
= $p;
277 function setTitle( $t ) {
281 function getTitle() {
282 return $this->mTitle
;
285 function displaySection( $fields ) {
287 $subsectionHtml = '';
288 $hasLeftColumn = false;
290 foreach( $fields as $key => $value ) {
291 if ( is_object( $value ) ) {
292 $v = empty( $value->mParams
['nodata'] )
293 ?
$this->mFieldData
[$key]
294 : $value->getDefault();
295 $tableHtml .= $value->getTableRow( $v );
297 if( $value->getLabel() != ' ' )
298 $hasLeftColumn = true;
299 } elseif ( is_array( $value ) ) {
300 $section = $this->displaySection( $value );
301 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
302 $subsectionHtml .= Xml
::fieldset( $legend, $section ) . "\n";
307 if( !$hasLeftColumn ) // Avoid strange spacing when no labels exist
308 $classes[] = 'mw-htmlform-nolabel';
309 $classes = implode( ' ', $classes );
311 $tableHtml = "<table class='$classes'><tbody>\n$tableHtml\n</tbody></table>\n";
313 return $subsectionHtml . "\n" . $tableHtml;
316 function loadData() {
319 $fieldData = array();
321 foreach( $this->mFlatFields
as $fieldname => $field ) {
322 if ( !empty( $field->mParams
['nodata'] ) ) continue;
323 if ( !empty( $field->mParams
['disabled'] ) ) {
324 $fieldData[$fieldname] = $field->getDefault();
326 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
331 foreach( $fieldData as $name => &$value ) {
332 $field = $this->mFlatFields
[$name];
333 $value = $field->filter( $value, $this->mFlatFields
);
336 $this->mFieldData
= $fieldData;
339 function importData( $fieldData ) {
341 foreach( $fieldData as $name => &$value ) {
342 $field = $this->mFlatFields
[$name];
343 $value = $field->filter( $value, $this->mFlatFields
);
346 foreach( $this->mFlatFields
as $fieldname => $field ) {
347 if ( !isset( $fieldData[$fieldname] ) )
348 $fieldData[$fieldname] = $field->getDefault();
351 $this->mFieldData
= $fieldData;
354 function suppressReset( $suppressReset = true ) {
355 $this->mShowReset
= !$suppressReset;
358 function filterDataForSubmit( $data ) {
363 abstract class HTMLFormField
{
364 abstract function getInputHTML( $value );
366 function validate( $value, $alldata ) {
367 if ( isset( $this->mValidationCallback
) ) {
368 return call_user_func( $this->mValidationCallback
, $value, $alldata );
374 function filter( $value, $alldata ) {
375 if( isset( $this->mFilterCallback
) ) {
376 $value = call_user_func( $this->mFilterCallback
, $value, $alldata );
382 function loadDataFromRequest( $request ) {
383 if( $request->getCheck( $this->mName
) ) {
384 return $request->getText( $this->mName
);
386 return $this->getDefault();
390 function __construct( $params ) {
391 $this->mParams
= $params;
393 if( isset( $params['label-message'] ) ) {
394 $msgInfo = $params['label-message'];
396 if ( is_array( $msgInfo ) ) {
397 $msg = array_shift( $msgInfo );
403 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
404 } elseif ( isset( $params['label'] ) ) {
405 $this->mLabel
= $params['label'];
408 if ( isset( $params['name'] ) ) {
409 $this->mName
= 'wp'.$params['name'];
410 $this->mID
= 'mw-input-'.$params['name'];
413 if ( isset( $params['default'] ) ) {
414 $this->mDefault
= $params['default'];
417 if ( isset( $params['id'] ) ) {
418 $this->mID
= $params['id'];
421 if ( isset( $params['validation-callback'] ) ) {
422 $this->mValidationCallback
= $params['validation-callback'];
425 if ( isset( $params['filter-callback'] ) ) {
426 $this->mFilterCallback
= $params['filter-callback'];
430 function getTableRow( $value ) {
431 // Check for invalid data.
434 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
435 if ( $errors === true ||
!$wgRequest->wasPosted() ) {
438 $errors = Xml
::tags( 'span', array( 'class' => 'error' ), $errors );
443 $html .= Xml
::tags( 'td', array( 'class' => 'mw-label' ),
444 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->getLabel() )
446 $html .= Xml
::tags( 'td', array( 'class' => 'mw-input' ),
447 $this->getInputHTML( $value ) ."\n$errors" );
449 $fieldType = get_class( $this );
451 $html = Xml
::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
455 if ( isset( $this->mParams
['help-message'] ) ) {
456 $msg = $this->mParams
['help-message'];
458 $text = wfMsgExt( $msg, 'parseinline' );
460 if( !wfEmptyMsg( $msg, $text ) ) {
461 $row = Xml
::tags( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
464 $row = Xml
::tags( 'tr', null, $row );
473 function getLabel() {
474 return $this->mLabel
;
477 function getDefault() {
478 if ( isset( $this->mDefault
) ) {
479 return $this->mDefault
;
485 static function flattenOptions( $options ) {
488 foreach( $options as $key => $value ) {
489 if ( is_array( $value ) ) {
490 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
492 $flatOpts[] = $value;
500 class HTMLTextField
extends HTMLFormField
{
503 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 45;
506 function getInputHTML( $value ) {
507 $attribs = array( 'id' => $this->mID
);
509 if ( isset( $this->mParams
['maxlength'] ) ) {
510 $attribs['maxlength'] = $this->mParams
['maxlength'];
513 if( !empty( $this->mParams
['disabled'] ) ) {
514 $attribs['disabled'] = 'disabled';
527 class HTMLIntField
extends HTMLTextField
{
529 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 20;
532 function validate( $value, $alldata ) {
533 $p = parent
::validate( $value, $alldata );
535 if( $p !== true ) return $p;
537 if ( intval( $value ) != $value ) {
538 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
543 if ( isset( $this->mParams
['min'] ) ) {
544 $min = $this->mParams
['min'];
546 return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
549 if ( isset( $this->mParams
['max'] ) ) {
550 $max = $this->mParams
['max'];
552 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
559 class HTMLCheckField
extends HTMLFormField
{
560 function getInputHTML( $value ) {
561 if ( !empty( $this->mParams
['invert'] ) )
564 $attr = array( 'id' => $this->mID
);
565 if( !empty( $this->mParams
['disabled'] ) ) {
566 $attr['disabled'] = 'disabled';
569 return Xml
::check( $this->mName
, $value, $attr ) . ' ' .
570 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->mLabel
);
573 function getLabel() {
574 return ' '; // In the right-hand column.
577 function loadDataFromRequest( $request ) {
579 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
583 // GetCheck won't work like we want for checks.
584 if( $request->getCheck( 'wpEditToken' ) ) {
585 // XOR has the following truth table, which is what we want
586 // INVERT VALUE | OUTPUT
589 // false false | false
591 return $request->getBool( $this->mName
) xor $invert;
593 return $this->getDefault();
598 class HTMLSelectField
extends HTMLFormField
{
600 function validate( $value, $alldata ) {
601 $p = parent
::validate( $value, $alldata );
602 if( $p !== true ) return $p;
604 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
605 if ( in_array( $value, $validOptions ) )
608 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
611 function getInputHTML( $value ) {
612 $select = new XmlSelect( $this->mName
, $this->mID
, strval( $value ) );
614 // If one of the options' 'name' is int(0), it is automatically selected.
615 // because PHP sucks and things int(0) == 'some string'.
616 // Working around this by forcing all of them to strings.
617 $options = array_map( 'strval', $this->mParams
['options'] );
619 if( !empty( $this->mParams
['disabled'] ) ) {
620 $select->setAttribute( 'disabled', 'disabled' );
623 $select->addOptions( $options );
625 return $select->getHTML();
629 class HTMLSelectOrOtherField
extends HTMLTextField
{
630 static $jsAdded = false;
632 function __construct( $params ) {
633 if( !in_array( 'other', $params['options'] ) ) {
634 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
637 parent
::__construct( $params );
640 function getInputHTML( $value ) {
641 $valInSelect = false;
643 if( $value !== false )
644 $valInSelect = in_array( $value,
645 HTMLFormField
::flattenOptions( $this->mParams
['options'] ) );
647 $selected = $valInSelect ?
$value : 'other';
649 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
650 $select->addOptions( $this->mParams
['options'] );
652 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
654 $tbAttribs = array( 'id' => $this->mID
. '-other' );
655 if( !empty( $this->mParams
['disabled'] ) ) {
656 $select->setAttribute( 'disabled', 'disabled' );
657 $tbAttribs['disabled'] = 'disabled';
660 $select = $select->getHTML();
662 if ( isset( $this->mParams
['maxlength'] ) ) {
663 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
666 $textbox = Xml
::input( $this->mName
. '-other',
668 $valInSelect ?
'' : $value,
671 return "$select<br/>\n$textbox";
674 function loadDataFromRequest( $request ) {
675 if( $request->getCheck( $this->mName
) ) {
676 $val = $request->getText( $this->mName
);
678 if( $val == 'other' ) {
679 $val = $request->getText( $this->mName
. '-other' );
684 return $this->getDefault();
689 class HTMLMultiSelectField
extends HTMLFormField
{
690 function validate( $value, $alldata ) {
691 $p = parent
::validate( $value, $alldata );
692 if( $p !== true ) return $p;
694 if( !is_array( $value ) ) return false;
696 // If all options are valid, array_intersect of the valid options and the provided
697 // options will return the provided options.
698 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
700 $validValues = array_intersect( $value, $validOptions );
701 if ( count( $validValues ) == count( $value ) )
704 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
707 function getInputHTML( $value ) {
708 $html = $this->formatOptions( $this->mParams
['options'], $value );
713 function formatOptions( $options, $value ) {
717 if ( !empty( $this->mParams
['disabled'] ) ) {
718 $attribs['disabled'] = 'disabled';
721 foreach( $options as $label => $info ) {
722 if( is_array( $info ) ) {
723 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
724 $html .= $this->formatOptions( $info, $value );
726 $thisAttribs = array( 'id' => $this->mID
. "-$info", 'value' => $info );
728 $checkbox = Xml
::check( $this->mName
. '[]', in_array( $info, $value ),
729 $attribs +
$thisAttribs );
730 $checkbox .= ' ' . Xml
::tags( 'label', array( 'for' => $this->mID
. "-$info" ), $label );
732 $html .= $checkbox . '<br />';
739 function loadDataFromRequest( $request ) {
740 // won't work with getCheck
741 if( $request->getCheck( 'wpEditToken' ) ) {
742 $arr = $request->getArray( $this->mName
);
749 return $this->getDefault();
753 function getDefault() {
754 if ( isset( $this->mDefault
) ) {
755 return $this->mDefault
;
762 class HTMLRadioField
extends HTMLFormField
{
763 function validate( $value, $alldata ) {
764 $p = parent
::validate( $value, $alldata );
765 if( $p !== true ) return $p;
767 if( !is_string( $value ) && !is_int( $value ) )
770 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
772 if ( in_array( $value, $validOptions ) )
775 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
778 function getInputHTML( $value ) {
779 $html = $this->formatOptions( $this->mParams
['options'], $value );
784 function formatOptions( $options, $value ) {
788 if ( !empty( $this->mParams
['disabled'] ) ) {
789 $attribs['disabled'] = 'disabled';
792 foreach( $options as $label => $info ) {
793 if( is_array( $info ) ) {
794 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
795 $html .= $this->formatOptions( $info, $value );
797 $html .= Xml
::radio( $this->mName
, $info, $info == $value,
798 $attribs +
array( 'id' => $this->mID
. "-$info" ) );
800 Xml
::tags( 'label', array( 'for' => $this->mID
. "-$info" ), $label );
810 class HTMLInfoField
extends HTMLFormField
{
811 function __construct( $info ) {
812 $info['nodata'] = true;
814 parent
::__construct( $info );
817 function getInputHTML( $value ) {
818 return !empty( $this->mParams
['raw'] ) ?
$value : htmlspecialchars( $value );
821 function getTableRow( $value ) {
822 if ( !empty( $this->mParams
['rawrow'] ) ) {
826 return parent
::getTableRow( $value );