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 ) {
178 return Xml
::tags( 'form',
180 'action' => $this->getTitle()->getFullURL(),
186 function getHiddenFields() {
190 $html .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
191 $html .= Xml
::hidden( 'title', $this->getTitle() ) . "\n";
196 function getButtons() {
201 if ( isset($this->mSubmitID
) )
202 $attribs['id'] = $this->mSubmitID
;
204 $attribs['class'] = 'mw-htmlform-submit';
206 $html .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
208 if ($this->mShowReset
) {
209 $html .= Xml
::element( 'input',
210 array( 'type' => 'reset',
211 'value' => wfMsg('htmlform-reset')
219 return $this->displaySection( $this->mFieldTree
);
222 function displayErrors( $errors ) {
223 if ( is_array( $errors ) ) {
224 $errorstr = $this->formatErrors( $errors );
229 $errorstr = Xml
::tags( 'div', array( 'class' => 'error' ), $errorstr );
232 $wgOut->addHTML( $errorstr );
235 static function formatErrors( $errors ) {
237 foreach ( $errors as $error ) {
238 if (is_array($error)) {
239 $msg = array_shift($error);
244 $errorstr .= Xml
::tags( 'li',
246 wfMsgExt( $msg, array( 'parseinline' ), $error )
250 $errorstr = Xml
::tags( 'ul', null, $errorstr );
255 function setSubmitText( $t ) {
256 $this->mSubmitText
= $t;
259 function getSubmitText() {
260 return isset($this->mSubmitText
) ?
$this->mSubmitText
: wfMsg('htmlform-submit');
263 function setSubmitID( $t ) {
264 $this->mSubmitID
= $t;
267 function setMessagePrefix( $p ) {
268 $this->mMessagePrefix
= $p;
271 function setTitle( $t ) {
275 function getTitle() {
276 return $this->mTitle
;
279 function displaySection( $fields ) {
281 $subsectionHtml = '';
282 $hasLeftColumn = false;
284 foreach( $fields as $key => $value ) {
285 if ( is_object( $value ) ) {
286 $v = empty($value->mParams
['nodata'])
287 ?
$this->mFieldData
[$key]
288 : $value->getDefault();
289 $tableHtml .= $value->getTableRow( $v );
291 if ($value->getLabel() != ' ')
292 $hasLeftColumn = true;
293 } elseif ( is_array( $value ) ) {
294 $section = $this->displaySection( $value );
295 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
296 $subsectionHtml .= Xml
::fieldset( $legend, $section ) . "\n";
301 if (!$hasLeftColumn) // Avoid strange spacing when no labels exist
302 $classes[] = 'mw-htmlform-nolabel';
303 $classes = implode( ' ', $classes );
305 $tableHtml = "<table class='$classes'><tbody>\n$tableHtml\n</tbody></table>\n";
307 return $subsectionHtml . "\n" . $tableHtml;
310 function loadData() {
313 $fieldData = array();
315 foreach( $this->mFlatFields
as $fieldname => $field ) {
316 if ( !empty($field->mParams
['nodata']) ) continue;
317 if ( !empty($field->mParams
['disabled']) ) {
318 $fieldData[$fieldname] = $field->getDefault();
320 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
325 foreach( $fieldData as $name => &$value ) {
326 $field = $this->mFlatFields
[$name];
327 $value = $field->filter( $value, $this->mFlatFields
);
330 $this->mFieldData
= $fieldData;
333 function importData( $fieldData ) {
335 foreach( $fieldData as $name => &$value ) {
336 $field = $this->mFlatFields
[$name];
337 $value = $field->filter( $value, $this->mFlatFields
);
340 foreach( $this->mFlatFields
as $fieldname => $field ) {
341 if ( !isset($fieldData[$fieldname]) )
342 $fieldData[$fieldname] = $field->getDefault();
345 $this->mFieldData
= $fieldData;
348 function suppressReset( $suppressReset = true ) {
349 $this->mShowReset
= !$suppressReset;
352 function filterDataForSubmit( $data ) {
357 abstract class HTMLFormField
{
358 abstract function getInputHTML( $value );
360 function validate( $value, $alldata ) {
361 if ( isset($this->mValidationCallback
) ) {
362 return call_user_func( $this->mValidationCallback
, $value, $alldata );
368 function filter( $value, $alldata ) {
369 if( isset( $this->mFilterCallback
) ) {
370 $value = call_user_func( $this->mFilterCallback
, $value, $alldata );
376 function loadDataFromRequest( $request ) {
377 if ($request->getCheck( $this->mName
) ) {
378 return $request->getText( $this->mName
);
380 return $this->getDefault();
384 function __construct( $params ) {
385 $this->mParams
= $params;
387 if (isset( $params['label-message'] ) ) {
388 $msgInfo = $params['label-message'];
390 if ( is_array( $msgInfo ) ) {
391 $msg = array_shift( $msgInfo );
397 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
398 } elseif ( isset($params['label']) ) {
399 $this->mLabel
= $params['label'];
402 if ( isset( $params['name'] ) ) {
403 $this->mName
= 'wp'.$params['name'];
404 $this->mID
= 'mw-input-'.$params['name'];
407 if ( isset( $params['default'] ) ) {
408 $this->mDefault
= $params['default'];
411 if ( isset( $params['id'] ) ) {
412 $this->mID
= $params['id'];
415 if ( isset( $params['validation-callback'] ) ) {
416 $this->mValidationCallback
= $params['validation-callback'];
419 if ( isset( $params['filter-callback'] ) ) {
420 $this->mFilterCallback
= $params['filter-callback'];
424 function getTableRow( $value ) {
425 // Check for invalid data.
428 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
429 if ( $errors === true ||
!$wgRequest->wasPosted() ) {
432 $errors = Xml
::tags( 'span', array( 'class' => 'error' ), $errors );
437 $html .= Xml
::tags( 'td', array( 'class' => 'mw-label' ),
438 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->getLabel() )
440 $html .= Xml
::tags( 'td', array( 'class' => 'mw-input' ),
441 $this->getInputHTML( $value ) ."\n$errors" );
443 $fieldType = get_class($this);
445 $html = Xml
::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
449 if ( isset($this->mParams
['help-message']) ) {
450 $msg = $this->mParams
['help-message'];
452 $text = wfMsgExt( $msg, 'parseinline' );
454 if (!wfEmptyMsg( $msg, $text ) ) {
455 $row = Xml
::tags( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
458 $row = Xml
::tags( 'tr', null, $row );
467 function getLabel() {
468 return $this->mLabel
;
471 function getDefault() {
472 if ( isset( $this->mDefault
) ) {
473 return $this->mDefault
;
479 static function flattenOptions( $options ) {
482 foreach( $options as $key => $value ) {
483 if ( is_array( $value ) ) {
484 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
486 $flatOpts[] = $value;
494 class HTMLTextField
extends HTMLFormField
{
497 return isset($this->mParams
['size']) ?
$this->mParams
['size'] : 45;
500 function getInputHTML( $value ) {
501 $attribs = array( 'id' => $this->mID
);
503 if ( isset($this->mParams
['maxlength']) ) {
504 $attribs['maxlength'] = $this->mParams
['maxlength'];
507 if (!empty($this->mParams
['disabled'])) {
508 $attribs['disabled'] = 'disabled';
511 return Xml
::input( $this->mName
,
519 class HTMLIntField
extends HTMLTextField
{
521 return isset($this->mParams
['size']) ?
$this->mParams
['size'] : 20;
524 function validate( $value, $alldata ) {
525 $p = parent
::validate($value, $alldata);
527 if ($p !== true) return $p;
529 if ( intval($value) != $value ) {
530 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
535 if ( isset($this->mParams
['min']) ) {
536 $min = $this->mParams
['min'];
538 return wfMsgExt( 'htmlform-int-toolow', 'parse', array($min) );
541 if ( isset($this->mParams
['max']) ) {
542 $max = $this->mParams
['max'];
544 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array($max) );
551 class HTMLCheckField
extends HTMLFormField
{
552 function getInputHTML( $value ) {
553 if ( !empty( $this->mParams
['invert'] ) )
556 $attr = array( 'id' => $this->mID
);
557 if (!empty($this->mParams
['disabled'])) {
558 $attr['disabled'] = 'disabled';
561 return Xml
::check( $this->mName
, $value, $attr ) . ' ' .
562 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->mLabel
);
565 function getLabel( ) {
566 return ' '; // In the right-hand column.
569 function loadDataFromRequest( $request ) {
571 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
575 // GetCheck won't work like we want for checks.
576 if ($request->getCheck( 'wpEditToken' ) ) {
577 // XOR has the following truth table, which is what we want
578 // INVERT VALUE | OUTPUT
581 // false false | false
583 return $request->getBool( $this->mName
) xor $invert;
585 return $this->getDefault();
590 class HTMLSelectField
extends HTMLFormField
{
592 function validate( $value, $alldata ) {
593 $p = parent
::validate( $value, $alldata );
594 if ($p !== true) return $p;
596 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
597 if ( in_array( $value, $validOptions ) )
600 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
603 function getInputHTML( $value ) {
604 $select = new XmlSelect( $this->mName
, $this->mID
, strval($value) );
606 // If one of the options' 'name' is int(0), it is automatically selected.
607 // because PHP sucks and things int(0) == 'some string'.
608 // Working around this by forcing all of them to strings.
609 $options = array_map( 'strval', $this->mParams
['options'] );
611 if (!empty($this->mParams
['disabled'])) {
612 $select->setAttribute( 'disabled', 'disabled' );
615 $select->addOptions( $options );
617 return $select->getHTML();
621 class HTMLSelectOrOtherField
extends HTMLTextField
{
622 static $jsAdded = false;
624 function __construct( $params ) {
625 if (! array_key_exists('other', $params['options']) ) {
626 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
629 parent
::__construct( $params );
632 function getInputHTML( $value ) {
633 $valInSelect = false;
635 if ($value !== false)
636 $valInSelect = in_array( $value,
637 HTMLFormField
::flattenOptions($this->mParams
['options']) );
639 $selected = $valInSelect ?
$value : 'other';
641 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
642 $select->addOptions( $this->mParams
['options'] );
644 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
646 $tbAttribs = array( 'id' => $this->mID
.'-other' );
647 if (!empty($this->mParams
['disabled'])) {
648 $select->setAttribute( 'disabled', 'disabled' );
649 $tbAttribs['disabled'] = 'disabled';
652 $select = $select->getHTML();
654 if ( isset($this->mParams
['maxlength']) ) {
655 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
658 $textbox = Xml
::input( $this->mName
.'-other',
660 $valInSelect ?
'' : $value,
663 return "$select<br/>\n$textbox";
666 function loadDataFromRequest( $request ) {
667 if ($request->getCheck( $this->mName
) ) {
668 $val = $request->getText( $this->mName
);
670 if ($val == 'other') {
671 $val = $request->getText( $this->mName
.'-other' );
676 return $this->getDefault();
681 class HTMLMultiSelectField
extends HTMLFormField
{
682 function validate( $value, $alldata ) {
683 $p = parent
::validate( $value, $alldata );
684 if ($p !== true) return $p;
686 if (!is_array($value)) return false;
688 // If all options are valid, array_intersect of the valid options and the provided
689 // options will return the provided options.
690 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
692 $validValues = array_intersect( $value, $validOptions );
693 if ( count( $validValues ) == count($value) )
696 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
699 function getInputHTML( $value ) {
700 $html = $this->formatOptions( $this->mParams
['options'], $value );
705 function formatOptions( $options, $value ) {
709 if ( !empty( $this->mParams
['disabled'] ) ) {
710 $attribs['disabled'] = 'disabled';
713 foreach( $options as $label => $info ) {
714 if (is_array($info)) {
715 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
716 $html .= $this->formatOptions( $info, $value );
718 $thisAttribs = array( 'id' => $this->mID
."-$info", 'value' => $info );
720 $checkbox = Xml
::check( $this->mName
.'[]', in_array( $info, $value ),
721 $attribs +
$thisAttribs );
722 $checkbox .= ' ' . Xml
::tags( 'label', array( 'for' => $this->mID
."-$info" ), $label );
724 $html .= $checkbox . '<br />';
731 function loadDataFromRequest( $request ) {
732 // won't work with getCheck
733 if ($request->getCheck( 'wpEditToken' ) ) {
734 $arr = $request->getArray( $this->mName
);
741 return $this->getDefault();
745 function getDefault() {
746 if ( isset( $this->mDefault
) ) {
747 return $this->mDefault
;
754 class HTMLRadioField
extends HTMLFormField
{
755 function validate( $value, $alldata ) {
756 $p = parent
::validate( $value, $alldata );
757 if ($p !== true) return $p;
759 if (!is_string($value) && !is_int($value))
762 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
764 if ( in_array( $value, $validOptions ) )
767 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
770 function getInputHTML( $value ) {
771 $html = $this->formatOptions( $this->mParams
['options'], $value );
776 function formatOptions( $options, $value ) {
780 if ( !empty( $this->mParams
['disabled'] ) ) {
781 $attribs['disabled'] = 'disabled';
784 foreach( $options as $label => $info ) {
785 if (is_array($info)) {
786 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
787 $html .= $this->formatOptions( $info, $value );
789 $html .= Xml
::radio( $this->mName
, $info, $info == $value,
790 $attribs +
array( 'id' => $this->mID
."-$info" ) );
792 Xml
::tags( 'label', array( 'for' => $this->mID
."-$info" ), $label );
802 class HTMLInfoField
extends HTMLFormField
{
803 function __construct( $info ) {
804 $info['nodata'] = true;
806 parent
::__construct($info);
809 function getInputHTML( $value ) {
810 return !empty($this->mParams
['raw']) ?
$value : htmlspecialchars($value);
813 function getTableRow( $value ) {
814 if ( !empty($this->mParams
['rawrow']) ) {
818 return parent
::getTableRow( $value );