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 displayForm( $submitResult ) {
152 if ( $submitResult !== false ) {
153 $this->displayErrors( $submitResult );
156 $html = $this->getBody();
159 $html .= $this->getHiddenFields();
162 $html .= $this->getButtons();
164 $html = $this->wrapForm( $html );
166 $wgOut->addHTML( $html );
169 function wrapForm( $html ) {
170 return Xml
::tags( 'form',
172 'action' => $this->getTitle()->getFullURL(),
178 function getHiddenFields() {
182 $html .= Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
183 $html .= Xml
::hidden( 'title', $this->getTitle() ) . "\n";
188 function getButtons() {
193 if ( isset($this->mSubmitID
) )
194 $attribs['id'] = $this->mSubmitID
;
196 $attribs['class'] = 'mw-htmlform-submit';
198 $html .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
200 if ($this->mShowReset
) {
201 $html .= Xml
::element( 'input',
202 array( 'type' => 'reset',
203 'value' => wfMsg('htmlform-reset')
211 return $this->displaySection( $this->mFieldTree
);
214 function displayErrors( $errors ) {
215 if ( is_array( $errors ) ) {
216 $errorstr = $this->formatErrors( $errors );
221 $errorstr = Xml
::tags( 'div', array( 'class' => 'error' ), $errorstr );
224 $wgOut->addHTML( $errorstr );
227 static function formatErrors( $errors ) {
229 foreach ( $errors as $error ) {
230 if (is_array($error)) {
231 $msg = array_shift($error);
236 $errorstr .= Xml
::tags( 'li',
238 wfMsgExt( $msg, array( 'parseinline' ), $error )
242 $errorstr = Xml
::tags( 'ul', null, $errorstr );
247 function setSubmitText( $t ) {
248 $this->mSubmitText
= $t;
251 function getSubmitText() {
252 return isset($this->mSubmitText
) ?
$this->mSubmitText
: wfMsg('htmlform-submit');
255 function setSubmitID( $t ) {
256 $this->mSubmitID
= $t;
259 function setMessagePrefix( $p ) {
260 $this->mMessagePrefix
= $p;
263 function setTitle( $t ) {
267 function getTitle() {
268 return $this->mTitle
;
271 function displaySection( $fields ) {
273 $subsectionHtml = '';
275 foreach( $fields as $key => $value ) {
276 if ( is_object( $value ) ) {
277 $v = empty($value->mParams
['nodata'])
278 ?
$this->mFieldData
[$key]
279 : $value->getDefault();
280 $tableHtml .= $value->getTableRow( $v );
281 } elseif ( is_array( $value ) ) {
282 $section = $this->displaySection( $value );
283 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
284 $subsectionHtml .= Xml
::fieldset( $legend, $section ) . "\n";
288 $tableHtml = "<table><tbody>\n$tableHtml\n</tbody></table>\n";
290 return $subsectionHtml . "\n" . $tableHtml;
293 function loadData() {
296 $fieldData = array();
298 foreach( $this->mFlatFields
as $fieldname => $field ) {
299 if ( !empty($field->mParams
['nodata']) ) continue;
300 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
304 foreach( $fieldData as $name => &$value ) {
305 $field = $this->mFlatFields
[$name];
306 $value = $field->filter( $value, $this->mFlatFields
);
309 $this->mFieldData
= $fieldData;
312 function importData( $fieldData ) {
314 foreach( $fieldData as $name => &$value ) {
315 $field = $this->mFlatFields
[$name];
316 $value = $field->filter( $value, $this->mFlatFields
);
319 foreach( $this->mFlatFields
as $fieldname => $field ) {
320 if ( !isset($fieldData[$fieldname]) )
321 $fieldData[$fieldname] = $field->getDefault();
324 $this->mFieldData
= $fieldData;
327 function suppressReset( $suppressReset = true ) {
328 $this->mShowReset
= !$suppressReset;
331 function filterDataForSubmit( $data ) {
336 abstract class HTMLFormField
{
337 abstract function getInputHTML( $value );
339 function validate( $value, $alldata ) {
340 if ( isset($this->mValidationCallback
) ) {
341 return call_user_func( $this->mValidationCallback
, $value, $alldata );
347 function filter( $value, $alldata ) {
348 if( isset( $this->mFilterCallback
) ) {
349 $value = call_user_func( $this->mFilterCallback
, $value, $alldata );
355 function loadDataFromRequest( $request ) {
356 if ($request->getCheck( $this->mName
) ) {
357 return $request->getText( $this->mName
);
359 return $this->getDefault();
363 function __construct( $params ) {
364 $this->mParams
= $params;
366 if (isset( $params['label-message'] ) ) {
367 $msgInfo = $params['label-message'];
369 if ( is_array( $msgInfo ) ) {
370 $msg = array_shift( $msgInfo );
376 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
377 } elseif ( isset($params['label']) ) {
378 $this->mLabel
= $params['label'];
381 if ( isset( $params['name'] ) ) {
382 $this->mName
= 'wp'.$params['name'];
383 $this->mID
= 'mw-input-'.$params['name'];
386 if ( isset( $params['default'] ) ) {
387 $this->mDefault
= $params['default'];
390 if ( isset( $params['id'] ) ) {
391 $this->mID
= $params['id'];
394 if ( isset( $params['validation-callback'] ) ) {
395 $this->mValidationCallback
= $params['validation-callback'];
398 if ( isset( $params['filter-callback'] ) ) {
399 $this->mFilterCallback
= $params['filter-callback'];
403 function getTableRow( $value ) {
404 // Check for invalid data.
407 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
408 if ( $errors === true ||
!$wgRequest->wasPosted() ) {
411 $errors = Xml
::tags( 'span', array( 'class' => 'error' ), $errors );
416 $html .= Xml
::tags( 'td', array( 'class' => 'mw-label' ),
417 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->getLabel() )
419 $html .= Xml
::tags( 'td', array( 'class' => 'mw-input' ),
420 $this->getInputHTML( $value ) ."\n$errors" );
422 $fieldType = get_class($this);
424 $html = Xml
::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
428 if ( isset($this->mParams
['help-message']) ) {
429 $msg = $this->mParams
['help-message'];
431 $text = wfMsgExt( $msg, 'parseinline' );
433 if (!wfEmptyMsg( $msg, $text ) ) {
434 $row = Xml
::tags( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
437 $row = Xml
::tags( 'tr', null, $row );
446 function getLabel() {
447 return $this->mLabel
;
450 function getDefault() {
451 if ( isset( $this->mDefault
) ) {
452 return $this->mDefault
;
458 static function flattenOptions( $options ) {
461 foreach( $options as $key => $value ) {
462 if ( is_array( $value ) ) {
463 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
465 $flatOpts[] = $value;
473 class HTMLTextField
extends HTMLFormField
{
476 return isset($this->mParams
['size']) ?
$this->mParams
['size'] : 45;
479 function getInputHTML( $value ) {
480 $attribs = array( 'id' => $this->mID
);
482 if ( isset($this->mParams
['maxlength']) ) {
483 $attribs['maxlength'] = $this->mParams
['maxlength'];
486 return Xml
::input( $this->mName
,
494 class HTMLIntField
extends HTMLTextField
{
496 return isset($this->mParams
['size']) ?
$this->mParams
['size'] : 20;
499 function validate( $value, $alldata ) {
500 $p = parent
::validate($value, $alldata);
502 if ($p !== true) return $p;
504 if ( intval($value) != $value ) {
505 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
510 if ( isset($this->mParams
['min']) ) {
511 $min = $this->mParams
['min'];
513 return wfMsgExt( 'htmlform-int-toolow', 'parse', array($min) );
516 if ( isset($this->mParams
['max']) ) {
517 $max = $this->mParams
['max'];
519 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array($max) );
526 class HTMLCheckField
extends HTMLFormField
{
527 function getInputHTML( $value ) {
528 if ( !empty( $this->mParams
['invert'] ) )
531 return Xml
::check( $this->mName
, $value, array( 'id' => $this->mID
) ) . ' ' .
532 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->mLabel
);
535 function getLabel( ) {
536 return ' '; // In the right-hand column.
539 function loadDataFromRequest( $request ) {
541 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
545 // GetCheck won't work like we want for checks.
546 if ($request->getCheck( 'wpEditToken' ) ) {
547 // XOR has the following truth table, which is what we want
548 // INVERT VALUE | OUTPUT
551 // false false | false
553 return $request->getBool( $this->mName
) xor $invert;
555 return $this->getDefault();
560 class HTMLSelectField
extends HTMLFormField
{
562 function validate( $value, $alldata ) {
563 $p = parent
::validate( $value, $alldata );
564 if ($p !== true) return $p;
566 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
567 if ( in_array( $value, $validOptions ) )
570 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
573 function getInputHTML( $value ) {
574 $select = new XmlSelect( $this->mName
, $this->mID
, $value );
576 $select->addOptions( $this->mParams
['options'] );
578 return $select->getHTML();
582 class HTMLSelectOrOtherField
extends HTMLTextField
{
583 static $jsAdded = false;
585 function __construct( $params ) {
586 if (! array_key_exists('other', $params['options']) ) {
587 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
590 parent
::__construct( $params );
593 function getInputHTML( $value ) {
595 $valInSelect = false;
596 if ($value !== false)
597 $valInSelect = in_array( $value,
598 HTMLFormField
::flattenOptions($this->mParams
['options']) );
600 $selected = $valInSelect ?
$value : 'other';
602 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
603 $select->addOptions( $this->mParams
['options'] );
605 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
607 $select = $select->getHTML();
609 $tbAttribs = array( 'id' => $this->mID
.'-other' );
611 if ( isset($this->mParams
['maxlength']) ) {
612 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
615 $textbox = Xml
::input( $this->mName
.'-other',
617 $valInSelect ?
'' : $value,
620 return "$select<br/>\n$textbox";
623 function loadDataFromRequest( $request ) {
624 if ($request->getCheck( $this->mName
) ) {
625 $val = $request->getText( $this->mName
);
627 if ($val == 'other') {
628 $val = $request->getText( $this->mName
.'-other' );
633 return $this->getDefault();
638 class HTMLMultiSelectField
extends HTMLFormField
{
639 function validate( $value, $alldata ) {
640 $p = parent
::validate( $value, $alldata );
641 if ($p !== true) return $p;
643 if (!is_array($value)) return false;
645 // If all options are valid, array_intersect of the valid options and the provided
646 // options will return the provided options.
647 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
649 $validValues = array_intersect( $value, $validOptions );
650 if ( count( $validValues ) == count($value) )
653 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
656 function getInputHTML( $value ) {
657 $html = $this->formatOptions( $this->mParams
['options'], $value );
662 function formatOptions( $options, $value ) {
664 foreach( $options as $label => $info ) {
665 if (is_array($info)) {
666 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
667 $html .= $this->formatOptions( $info, $value );
669 $checkbox = Xml
::check( $this->mName
.'[]', in_array( $info, $value ),
670 array( 'id' => $this->mID
."-$info", 'value' => $info ) );
671 $checkbox .= ' ' . Xml
::tags( 'label', array( 'for' => $this->mID
."-$info" ), $label );
673 $html .= Xml
::tags( 'p', null, $checkbox );
680 function loadDataFromRequest( $request ) {
681 // won't work with getCheck
682 if ($request->getCheck( 'wpEditToken' ) ) {
683 $arr = $request->getArray( $this->mName
);
690 return $this->getDefault();
694 function getDefault() {
695 if ( isset( $this->mDefault
) ) {
696 return $this->mDefault
;
703 class HTMLRadioField
extends HTMLFormField
{
704 function validate( $value, $alldata ) {
705 $p = parent
::validate( $value, $alldata );
706 if ($p !== true) return $p;
708 if (!is_string($value) && !is_int($value))
711 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
713 if ( in_array( $value, $validOptions ) )
716 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
719 function getInputHTML( $value ) {
720 $html = $this->formatOptions( $this->mParams
['options'], $value );
725 function formatOptions( $options, $value ) {
727 foreach( $options as $label => $info ) {
728 if (is_array($info)) {
729 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
730 $html .= $this->formatOptions( $info, $value );
732 $html .= Xml
::radio( $this->mName
, $info, $info == $value,
733 array( 'id' => $this->mID
."-$info" ) );
735 Xml
::tags( 'label', array( 'for' => $this->mID
."-$info" ), $label );
745 class HTMLInfoField
extends HTMLFormField
{
746 function __construct( $info ) {
747 $info['nodata'] = true;
749 parent
::__construct($info);
752 function getInputHTML( $value ) {
753 return !empty($this->mParams
['raw']) ?
$value : htmlspecialchars($value);
756 function getTableRow( $value ) {
757 if ( !empty($this->mParams
['rawrow']) ) {
761 return parent
::getTableRow( $value );