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 = '';
274 $hasLeftColumn = false;
276 foreach( $fields as $key => $value ) {
277 if ( is_object( $value ) ) {
278 $v = empty($value->mParams
['nodata'])
279 ?
$this->mFieldData
[$key]
280 : $value->getDefault();
281 $tableHtml .= $value->getTableRow( $v );
283 if ($value->getLabel() != ' ')
284 $hasLeftColumn = true;
285 } elseif ( is_array( $value ) ) {
286 $section = $this->displaySection( $value );
287 $legend = wfMsg( "{$this->mMessagePrefix}-$key" );
288 $subsectionHtml .= Xml
::fieldset( $legend, $section ) . "\n";
293 if (!$hasLeftColumn) // Avoid strange spacing when no labels exist
294 $classes[] = 'mw-htmlform-nolabel';
295 $classes = implode( ' ', $classes );
297 $tableHtml = "<table class='$classes'><tbody>\n$tableHtml\n</tbody></table>\n";
299 return $subsectionHtml . "\n" . $tableHtml;
302 function loadData() {
305 $fieldData = array();
307 foreach( $this->mFlatFields
as $fieldname => $field ) {
308 if ( !empty($field->mParams
['nodata']) ) continue;
309 $fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
313 foreach( $fieldData as $name => &$value ) {
314 $field = $this->mFlatFields
[$name];
315 $value = $field->filter( $value, $this->mFlatFields
);
318 $this->mFieldData
= $fieldData;
321 function importData( $fieldData ) {
323 foreach( $fieldData as $name => &$value ) {
324 $field = $this->mFlatFields
[$name];
325 $value = $field->filter( $value, $this->mFlatFields
);
328 foreach( $this->mFlatFields
as $fieldname => $field ) {
329 if ( !isset($fieldData[$fieldname]) )
330 $fieldData[$fieldname] = $field->getDefault();
333 $this->mFieldData
= $fieldData;
336 function suppressReset( $suppressReset = true ) {
337 $this->mShowReset
= !$suppressReset;
340 function filterDataForSubmit( $data ) {
345 abstract class HTMLFormField
{
346 abstract function getInputHTML( $value );
348 function validate( $value, $alldata ) {
349 if ( isset($this->mValidationCallback
) ) {
350 return call_user_func( $this->mValidationCallback
, $value, $alldata );
356 function filter( $value, $alldata ) {
357 if( isset( $this->mFilterCallback
) ) {
358 $value = call_user_func( $this->mFilterCallback
, $value, $alldata );
364 function loadDataFromRequest( $request ) {
365 if ($request->getCheck( $this->mName
) ) {
366 return $request->getText( $this->mName
);
368 return $this->getDefault();
372 function __construct( $params ) {
373 $this->mParams
= $params;
375 if (isset( $params['label-message'] ) ) {
376 $msgInfo = $params['label-message'];
378 if ( is_array( $msgInfo ) ) {
379 $msg = array_shift( $msgInfo );
385 $this->mLabel
= wfMsgExt( $msg, 'parseinline', $msgInfo );
386 } elseif ( isset($params['label']) ) {
387 $this->mLabel
= $params['label'];
390 if ( isset( $params['name'] ) ) {
391 $this->mName
= 'wp'.$params['name'];
392 $this->mID
= 'mw-input-'.$params['name'];
395 if ( isset( $params['default'] ) ) {
396 $this->mDefault
= $params['default'];
399 if ( isset( $params['id'] ) ) {
400 $this->mID
= $params['id'];
403 if ( isset( $params['validation-callback'] ) ) {
404 $this->mValidationCallback
= $params['validation-callback'];
407 if ( isset( $params['filter-callback'] ) ) {
408 $this->mFilterCallback
= $params['filter-callback'];
412 function getTableRow( $value ) {
413 // Check for invalid data.
416 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
417 if ( $errors === true ||
!$wgRequest->wasPosted() ) {
420 $errors = Xml
::tags( 'span', array( 'class' => 'error' ), $errors );
425 $html .= Xml
::tags( 'td', array( 'class' => 'mw-label' ),
426 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->getLabel() )
428 $html .= Xml
::tags( 'td', array( 'class' => 'mw-input' ),
429 $this->getInputHTML( $value ) ."\n$errors" );
431 $fieldType = get_class($this);
433 $html = Xml
::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
437 if ( isset($this->mParams
['help-message']) ) {
438 $msg = $this->mParams
['help-message'];
440 $text = wfMsgExt( $msg, 'parseinline' );
442 if (!wfEmptyMsg( $msg, $text ) ) {
443 $row = Xml
::tags( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
446 $row = Xml
::tags( 'tr', null, $row );
455 function getLabel() {
456 return $this->mLabel
;
459 function getDefault() {
460 if ( isset( $this->mDefault
) ) {
461 return $this->mDefault
;
467 static function flattenOptions( $options ) {
470 foreach( $options as $key => $value ) {
471 if ( is_array( $value ) ) {
472 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
474 $flatOpts[] = $value;
482 class HTMLTextField
extends HTMLFormField
{
485 return isset($this->mParams
['size']) ?
$this->mParams
['size'] : 45;
488 function getInputHTML( $value ) {
489 $attribs = array( 'id' => $this->mID
);
491 if ( isset($this->mParams
['maxlength']) ) {
492 $attribs['maxlength'] = $this->mParams
['maxlength'];
495 return Xml
::input( $this->mName
,
503 class HTMLIntField
extends HTMLTextField
{
505 return isset($this->mParams
['size']) ?
$this->mParams
['size'] : 20;
508 function validate( $value, $alldata ) {
509 $p = parent
::validate($value, $alldata);
511 if ($p !== true) return $p;
513 if ( intval($value) != $value ) {
514 return wfMsgExt( 'htmlform-int-invalid', 'parse' );
519 if ( isset($this->mParams
['min']) ) {
520 $min = $this->mParams
['min'];
522 return wfMsgExt( 'htmlform-int-toolow', 'parse', array($min) );
525 if ( isset($this->mParams
['max']) ) {
526 $max = $this->mParams
['max'];
528 return wfMsgExt( 'htmlform-int-toohigh', 'parse', array($max) );
535 class HTMLCheckField
extends HTMLFormField
{
536 function getInputHTML( $value ) {
537 if ( !empty( $this->mParams
['invert'] ) )
540 return Xml
::check( $this->mName
, $value, array( 'id' => $this->mID
) ) . ' ' .
541 Xml
::tags( 'label', array( 'for' => $this->mID
), $this->mLabel
);
544 function getLabel( ) {
545 return ' '; // In the right-hand column.
548 function loadDataFromRequest( $request ) {
550 if ( isset( $this->mParams
['invert'] ) && $this->mParams
['invert'] ) {
554 // GetCheck won't work like we want for checks.
555 if ($request->getCheck( 'wpEditToken' ) ) {
556 // XOR has the following truth table, which is what we want
557 // INVERT VALUE | OUTPUT
560 // false false | false
562 return $request->getBool( $this->mName
) xor $invert;
564 return $this->getDefault();
569 class HTMLSelectField
extends HTMLFormField
{
571 function validate( $value, $alldata ) {
572 $p = parent
::validate( $value, $alldata );
573 if ($p !== true) return $p;
575 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
576 if ( in_array( $value, $validOptions ) )
579 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
582 function getInputHTML( $value ) {
583 $select = new XmlSelect( $this->mName
, $this->mID
, $value );
585 $select->addOptions( $this->mParams
['options'] );
587 return $select->getHTML();
591 class HTMLSelectOrOtherField
extends HTMLTextField
{
592 static $jsAdded = false;
594 function __construct( $params ) {
595 if (! array_key_exists('other', $params['options']) ) {
596 $params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
599 parent
::__construct( $params );
602 function getInputHTML( $value ) {
604 $valInSelect = false;
605 if ($value !== false)
606 $valInSelect = in_array( $value,
607 HTMLFormField
::flattenOptions($this->mParams
['options']) );
609 $selected = $valInSelect ?
$value : 'other';
611 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
612 $select->addOptions( $this->mParams
['options'] );
614 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
616 $select = $select->getHTML();
618 $tbAttribs = array( 'id' => $this->mID
.'-other' );
620 if ( isset($this->mParams
['maxlength']) ) {
621 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
624 $textbox = Xml
::input( $this->mName
.'-other',
626 $valInSelect ?
'' : $value,
629 return "$select<br/>\n$textbox";
632 function loadDataFromRequest( $request ) {
633 if ($request->getCheck( $this->mName
) ) {
634 $val = $request->getText( $this->mName
);
636 if ($val == 'other') {
637 $val = $request->getText( $this->mName
.'-other' );
642 return $this->getDefault();
647 class HTMLMultiSelectField
extends HTMLFormField
{
648 function validate( $value, $alldata ) {
649 $p = parent
::validate( $value, $alldata );
650 if ($p !== true) return $p;
652 if (!is_array($value)) return false;
654 // If all options are valid, array_intersect of the valid options and the provided
655 // options will return the provided options.
656 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
658 $validValues = array_intersect( $value, $validOptions );
659 if ( count( $validValues ) == count($value) )
662 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
665 function getInputHTML( $value ) {
666 $html = $this->formatOptions( $this->mParams
['options'], $value );
671 function formatOptions( $options, $value ) {
673 foreach( $options as $label => $info ) {
674 if (is_array($info)) {
675 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
676 $html .= $this->formatOptions( $info, $value );
678 $checkbox = Xml
::check( $this->mName
.'[]', in_array( $info, $value ),
679 array( 'id' => $this->mID
."-$info", 'value' => $info ) );
680 $checkbox .= ' ' . Xml
::tags( 'label', array( 'for' => $this->mID
."-$info" ), $label );
682 $html .= $checkbox . '<br />';
689 function loadDataFromRequest( $request ) {
690 // won't work with getCheck
691 if ($request->getCheck( 'wpEditToken' ) ) {
692 $arr = $request->getArray( $this->mName
);
699 return $this->getDefault();
703 function getDefault() {
704 if ( isset( $this->mDefault
) ) {
705 return $this->mDefault
;
712 class HTMLRadioField
extends HTMLFormField
{
713 function validate( $value, $alldata ) {
714 $p = parent
::validate( $value, $alldata );
715 if ($p !== true) return $p;
717 if (!is_string($value) && !is_int($value))
720 $validOptions = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
722 if ( in_array( $value, $validOptions ) )
725 return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
728 function getInputHTML( $value ) {
729 $html = $this->formatOptions( $this->mParams
['options'], $value );
734 function formatOptions( $options, $value ) {
736 foreach( $options as $label => $info ) {
737 if (is_array($info)) {
738 $html .= Xml
::tags( 'h1', null, $label ) . "\n";
739 $html .= $this->formatOptions( $info, $value );
741 $html .= Xml
::radio( $this->mName
, $info, $info == $value,
742 array( 'id' => $this->mID
."-$info" ) );
744 Xml
::tags( 'label', array( 'for' => $this->mID
."-$info" ), $label );
754 class HTMLInfoField
extends HTMLFormField
{
755 function __construct( $info ) {
756 $info['nodata'] = true;
758 parent
::__construct($info);
761 function getInputHTML( $value ) {
762 return !empty($this->mParams
['raw']) ?
$value : htmlspecialchars($value);
765 function getTableRow( $value ) {
766 if ( !empty($this->mParams
['rawrow']) ) {
770 return parent
::getTableRow( $value );