2009-04-24 01:31:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class HTMLForm {
|
|
|
|
|
|
|
|
|
|
static $jsAdded = false;
|
|
|
|
|
|
|
|
|
|
/* The descriptor is an array of arrays.
|
|
|
|
|
i.e. array(
|
2009-06-21 18:26:29 +00:00
|
|
|
'fieldname' => array( 'section' => 'section/subsection',
|
|
|
|
|
properties... ),
|
|
|
|
|
...
|
|
|
|
|
)
|
2009-04-24 01:31:17 +00:00
|
|
|
*/
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
static $typeMappings = array(
|
|
|
|
|
'text' => 'HTMLTextField',
|
|
|
|
|
'select' => 'HTMLSelectField',
|
|
|
|
|
'radio' => 'HTMLRadioField',
|
|
|
|
|
'multiselect' => 'HTMLMultiSelectField',
|
|
|
|
|
'check' => 'HTMLCheckField',
|
|
|
|
|
'toggle' => 'HTMLCheckField',
|
|
|
|
|
'int' => 'HTMLIntField',
|
2009-07-17 22:19:02 +00:00
|
|
|
'float' => 'HTMLFloatField',
|
2009-06-21 18:26:29 +00:00
|
|
|
'info' => 'HTMLInfoField',
|
|
|
|
|
'selectorother' => 'HTMLSelectOrOtherField',
|
|
|
|
|
);
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function __construct( $descriptor, $messagePrefix ) {
|
|
|
|
|
$this->mMessagePrefix = $messagePrefix;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Expand out into a tree.
|
|
|
|
|
$loadedDescriptor = array();
|
|
|
|
|
$this->mFlatFields = array();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $descriptor as $fieldname => $info ) {
|
|
|
|
|
$section = '';
|
|
|
|
|
if ( isset( $info['section'] ) )
|
|
|
|
|
$section = $info['section'];
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$info['name'] = $fieldname;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$field = $this->loadInputFromParameters( $info );
|
|
|
|
|
$field->mParent = $this;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$setSection =& $loadedDescriptor;
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $section ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$sectionParts = explode( '/', $section );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
while( count( $sectionParts ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$newName = array_shift( $sectionParts );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if ( !isset( $setSection[$newName] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$setSection[$newName] = array();
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$setSection =& $setSection[$newName];
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$setSection[$fieldname] = $field;
|
|
|
|
|
$this->mFlatFields[$fieldname] = $field;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mFieldTree = $loadedDescriptor;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mShowReset = true;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
static function addJS() {
|
2009-06-21 18:26:29 +00:00
|
|
|
if( self::$jsAdded ) return;
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
global $wgOut, $wgStylePath;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$wgOut->addScriptFile( "$wgStylePath/common/htmlform.js" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function loadInputFromParameters( $descriptor ) {
|
|
|
|
|
if ( isset( $descriptor['class'] ) ) {
|
|
|
|
|
$class = $descriptor['class'];
|
|
|
|
|
} elseif ( isset( $descriptor['type'] ) ) {
|
|
|
|
|
$class = self::$typeMappings[$descriptor['type']];
|
|
|
|
|
$descriptor['class'] = $class;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( !$class ) {
|
|
|
|
|
throw new MWException( "Descriptor with no class: " . print_r( $descriptor, true ) );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$obj = new $class( $descriptor );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function show() {
|
|
|
|
|
$html = '';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
self::addJS();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Load data from the request.
|
|
|
|
|
$this->loadData();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Try a submission
|
|
|
|
|
global $wgUser, $wgRequest;
|
|
|
|
|
$editToken = $wgRequest->getVal( 'wpEditToken' );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$result = false;
|
|
|
|
|
if ( $wgUser->matchEditToken( $editToken ) )
|
|
|
|
|
$result = $this->trySubmit();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( $result === true )
|
2009-04-24 01:31:17 +00:00
|
|
|
return $result;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Display form.
|
|
|
|
|
$this->displayForm( $result );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
/** Return values:
|
|
|
|
|
* TRUE == Successful submission
|
|
|
|
|
* FALSE == No submission attempted
|
|
|
|
|
* Anything else == Error to display.
|
|
|
|
|
*/
|
|
|
|
|
function trySubmit() {
|
|
|
|
|
// Check for validation
|
|
|
|
|
foreach( $this->mFlatFields as $fieldname => $field ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( !empty( $field->mParams['nodata'] ) ) continue;
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( $field->validate( $this->mFieldData[$fieldname],
|
|
|
|
|
$this->mFieldData ) !== true ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
return isset( $this->mValidationErrorMessage ) ?
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mValidationErrorMessage : array( 'htmlform-invalid-input' );
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$callback = $this->mSubmitCallback;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$data = $this->filterDataForSubmit( $this->mFieldData );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$res = call_user_func( $callback, $data );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $res;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function setSubmitCallback( $cb ) {
|
|
|
|
|
$this->mSubmitCallback = $cb;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function setValidationErrorMessage( $msg ) {
|
|
|
|
|
$this->mValidationErrorMessage = $msg;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-05-27 16:11:23 +00:00
|
|
|
function setIntro( $msg ) {
|
|
|
|
|
$this->mIntro = $msg;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function displayForm( $submitResult ) {
|
|
|
|
|
global $wgOut;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( $submitResult !== false ) {
|
|
|
|
|
$this->displayErrors( $submitResult );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if ( isset( $this->mIntro ) ) {
|
2009-05-27 16:11:23 +00:00
|
|
|
$wgOut->addHTML( $this->mIntro );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html = $this->getBody();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Hidden fields
|
|
|
|
|
$html .= $this->getHiddenFields();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Buttons
|
|
|
|
|
$html .= $this->getButtons();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html = $this->wrapForm( $html );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$wgOut->addHTML( $html );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function wrapForm( $html ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
return Xml::tags(
|
|
|
|
|
'form',
|
|
|
|
|
array(
|
|
|
|
|
'action' => $this->getTitle()->getFullURL(),
|
|
|
|
|
'method' => 'post',
|
|
|
|
|
),
|
|
|
|
|
$html
|
|
|
|
|
);
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getHiddenFields() {
|
|
|
|
|
global $wgUser;
|
|
|
|
|
$html = '';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= Xml::hidden( 'wpEditToken', $wgUser->editToken() ) . "\n";
|
|
|
|
|
$html .= Xml::hidden( 'title', $this->getTitle() ) . "\n";
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getButtons() {
|
|
|
|
|
$html = '';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$attribs = array();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if ( isset( $this->mSubmitID ) )
|
2009-04-24 01:31:17 +00:00
|
|
|
$attribs['id'] = $this->mSubmitID;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$attribs['class'] = 'mw-htmlform-submit';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( $this->mShowReset ) {
|
|
|
|
|
$html .= Xml::element(
|
|
|
|
|
'input',
|
|
|
|
|
array(
|
|
|
|
|
'type' => 'reset',
|
|
|
|
|
'value' => wfMsg( 'htmlform-reset' )
|
|
|
|
|
)
|
|
|
|
|
) . "\n";
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getBody() {
|
|
|
|
|
return $this->displaySection( $this->mFieldTree );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function displayErrors( $errors ) {
|
|
|
|
|
if ( is_array( $errors ) ) {
|
|
|
|
|
$errorstr = $this->formatErrors( $errors );
|
|
|
|
|
} else {
|
|
|
|
|
$errorstr = $errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$errorstr = Xml::tags( 'div', array( 'class' => 'error' ), $errorstr );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->addHTML( $errorstr );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
static function formatErrors( $errors ) {
|
|
|
|
|
$errorstr = '';
|
|
|
|
|
foreach ( $errors as $error ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if( is_array( $error ) ) {
|
|
|
|
|
$msg = array_shift( $error );
|
2009-04-24 01:31:17 +00:00
|
|
|
} else {
|
|
|
|
|
$msg = $error;
|
|
|
|
|
$error = array();
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
$errorstr .= Xml::tags(
|
|
|
|
|
'li',
|
|
|
|
|
null,
|
|
|
|
|
wfMsgExt( $msg, array( 'parseinline' ), $error )
|
|
|
|
|
);
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$errorstr = Xml::tags( 'ul', null, $errorstr );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $errorstr;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function setSubmitText( $t ) {
|
|
|
|
|
$this->mSubmitText = $t;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getSubmitText() {
|
2009-06-21 18:26:29 +00:00
|
|
|
return isset( $this->mSubmitText ) ? $this->mSubmitText : wfMsg( 'htmlform-submit' );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function setSubmitID( $t ) {
|
|
|
|
|
$this->mSubmitID = $t;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function setMessagePrefix( $p ) {
|
|
|
|
|
$this->mMessagePrefix = $p;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function setTitle( $t ) {
|
|
|
|
|
$this->mTitle = $t;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getTitle() {
|
|
|
|
|
return $this->mTitle;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function displaySection( $fields ) {
|
|
|
|
|
$tableHtml = '';
|
|
|
|
|
$subsectionHtml = '';
|
2009-04-27 14:18:40 +00:00
|
|
|
$hasLeftColumn = false;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $fields as $key => $value ) {
|
|
|
|
|
if ( is_object( $value ) ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
$v = empty( $value->mParams['nodata'] )
|
2009-04-24 01:31:17 +00:00
|
|
|
? $this->mFieldData[$key]
|
|
|
|
|
: $value->getDefault();
|
|
|
|
|
$tableHtml .= $value->getTableRow( $v );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( $value->getLabel() != ' ' )
|
2009-04-27 14:18:40 +00:00
|
|
|
$hasLeftColumn = true;
|
2009-04-24 01:31:17 +00:00
|
|
|
} elseif ( is_array( $value ) ) {
|
|
|
|
|
$section = $this->displaySection( $value );
|
|
|
|
|
$legend = wfMsg( "{$this->mMessagePrefix}-$key" );
|
|
|
|
|
$subsectionHtml .= Xml::fieldset( $legend, $section ) . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-27 14:18:40 +00:00
|
|
|
$classes = array();
|
2009-06-21 18:26:29 +00:00
|
|
|
if( !$hasLeftColumn ) // Avoid strange spacing when no labels exist
|
2009-04-27 14:18:40 +00:00
|
|
|
$classes[] = 'mw-htmlform-nolabel';
|
|
|
|
|
$classes = implode( ' ', $classes );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-27 14:18:40 +00:00
|
|
|
$tableHtml = "<table class='$classes'><tbody>\n$tableHtml\n</tbody></table>\n";
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $subsectionHtml . "\n" . $tableHtml;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function loadData() {
|
|
|
|
|
global $wgRequest;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$fieldData = array();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $this->mFlatFields as $fieldname => $field ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( !empty( $field->mParams['nodata'] ) ) continue;
|
|
|
|
|
if ( !empty( $field->mParams['disabled'] ) ) {
|
2009-04-28 01:00:35 +00:00
|
|
|
$fieldData[$fieldname] = $field->getDefault();
|
|
|
|
|
} else {
|
|
|
|
|
$fieldData[$fieldname] = $field->loadDataFromRequest( $wgRequest );
|
|
|
|
|
}
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// Filter data.
|
|
|
|
|
foreach( $fieldData as $name => &$value ) {
|
|
|
|
|
$field = $this->mFlatFields[$name];
|
|
|
|
|
$value = $field->filter( $value, $this->mFlatFields );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mFieldData = $fieldData;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function importData( $fieldData ) {
|
|
|
|
|
// Filter data.
|
|
|
|
|
foreach( $fieldData as $name => &$value ) {
|
|
|
|
|
$field = $this->mFlatFields[$name];
|
|
|
|
|
$value = $field->filter( $value, $this->mFlatFields );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $this->mFlatFields as $fieldname => $field ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( !isset( $fieldData[$fieldname] ) )
|
2009-04-24 01:31:17 +00:00
|
|
|
$fieldData[$fieldname] = $field->getDefault();
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mFieldData = $fieldData;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function suppressReset( $suppressReset = true ) {
|
|
|
|
|
$this->mShowReset = !$suppressReset;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function filterDataForSubmit( $data ) {
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
abstract class HTMLFormField {
|
|
|
|
|
abstract function getInputHTML( $value );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function validate( $value, $alldata ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( isset( $this->mValidationCallback ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
return call_user_func( $this->mValidationCallback, $value, $alldata );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function filter( $value, $alldata ) {
|
|
|
|
|
if( isset( $this->mFilterCallback ) ) {
|
|
|
|
|
$value = call_user_func( $this->mFilterCallback, $value, $alldata );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $value;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function loadDataFromRequest( $request ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $request->getCheck( $this->mName ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
return $request->getText( $this->mName );
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function __construct( $params ) {
|
|
|
|
|
$this->mParams = $params;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( isset( $params['label-message'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$msgInfo = $params['label-message'];
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( is_array( $msgInfo ) ) {
|
|
|
|
|
$msg = array_shift( $msgInfo );
|
|
|
|
|
} else {
|
|
|
|
|
$msg = $msgInfo;
|
|
|
|
|
$msgInfo = array();
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mLabel = wfMsgExt( $msg, 'parseinline', $msgInfo );
|
2009-06-21 18:26:29 +00:00
|
|
|
} elseif ( isset( $params['label'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->mLabel = $params['label'];
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( isset( $params['name'] ) ) {
|
2009-07-19 16:49:58 +00:00
|
|
|
$name = $params['name'];
|
|
|
|
|
$validName = Sanitizer::escapeId( $name );
|
|
|
|
|
if( $name != $validName ) {
|
|
|
|
|
throw new MWException("Invalid name '$name' passed to " . __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
$this->mName = 'wp'.$name;
|
|
|
|
|
$this->mID = 'mw-input-'.$name;
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( isset( $params['default'] ) ) {
|
|
|
|
|
$this->mDefault = $params['default'];
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( isset( $params['id'] ) ) {
|
2009-07-19 16:49:58 +00:00
|
|
|
$id = $params['id'];
|
|
|
|
|
$validId = Sanitizer::escapeId( $id );
|
|
|
|
|
if( $id != $validId ) {
|
|
|
|
|
throw new MWException("Invalid id '$id' passed to " . __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
$this->mID = $id;
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( isset( $params['validation-callback'] ) ) {
|
|
|
|
|
$this->mValidationCallback = $params['validation-callback'];
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( isset( $params['filter-callback'] ) ) {
|
|
|
|
|
$this->mFilterCallback = $params['filter-callback'];
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getTableRow( $value ) {
|
|
|
|
|
// Check for invalid data.
|
|
|
|
|
global $wgRequest;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$errors = $this->validate( $value, $this->mParent->mFieldData );
|
|
|
|
|
if ( $errors === true || !$wgRequest->wasPosted() ) {
|
|
|
|
|
$errors = '';
|
|
|
|
|
} else {
|
|
|
|
|
$errors = Xml::tags( 'span', array( 'class' => 'error' ), $errors );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html = '';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= Xml::tags( 'td', array( 'class' => 'mw-label' ),
|
|
|
|
|
Xml::tags( 'label', array( 'for' => $this->mID ), $this->getLabel() )
|
|
|
|
|
);
|
|
|
|
|
$html .= Xml::tags( 'td', array( 'class' => 'mw-input' ),
|
|
|
|
|
$this->getInputHTML( $value ) ."\n$errors" );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
$fieldType = get_class( $this );
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html = Xml::tags( 'tr', array( 'class' => "mw-htmlform-field-$fieldType" ),
|
|
|
|
|
$html ) . "\n";
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-07-24 01:22:06 +00:00
|
|
|
$helptext = null;
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( isset( $this->mParams['help-message'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$msg = $this->mParams['help-message'];
|
2009-07-24 01:22:06 +00:00
|
|
|
$helptext = wfMsgExt( $msg, 'parseinline' );
|
|
|
|
|
if ( wfEmptyMsg( $msg, $helptext ) ) {
|
|
|
|
|
# Never mind
|
|
|
|
|
$helptext = null;
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-07-24 01:22:06 +00:00
|
|
|
} elseif ( isset( $this->mParams['help'] ) ) {
|
|
|
|
|
$helptext = $this->mParams['help'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_null( $helptext ) ) {
|
|
|
|
|
$row = Xml::tags( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ),
|
|
|
|
|
$helptext );
|
|
|
|
|
$row = Xml::tags( 'tr', null, $row );
|
|
|
|
|
$html .= "$row\n";
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getLabel() {
|
|
|
|
|
return $this->mLabel;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getDefault() {
|
|
|
|
|
if ( isset( $this->mDefault ) ) {
|
|
|
|
|
return $this->mDefault;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
static function flattenOptions( $options ) {
|
|
|
|
|
$flatOpts = array();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $options as $key => $value ) {
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
$flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
|
|
|
|
|
} else {
|
|
|
|
|
$flatOpts[] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $flatOpts;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTMLTextField extends HTMLFormField {
|
|
|
|
|
|
|
|
|
|
function getSize() {
|
2009-06-21 18:26:29 +00:00
|
|
|
return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInputHTML( $value ) {
|
|
|
|
|
$attribs = array( 'id' => $this->mID );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if ( isset( $this->mParams['maxlength'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$attribs['maxlength'] = $this->mParams['maxlength'];
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-21 18:26:29 +00:00
|
|
|
if( !empty( $this->mParams['disabled'] ) ) {
|
2009-04-28 01:00:35 +00:00
|
|
|
$attribs['disabled'] = 'disabled';
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
return Xml::input(
|
|
|
|
|
$this->mName,
|
|
|
|
|
$this->getSize(),
|
|
|
|
|
$value,
|
|
|
|
|
$attribs
|
|
|
|
|
);
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-17 22:19:02 +00:00
|
|
|
class HTMLFloatField extends HTMLTextField {
|
2009-04-24 01:31:17 +00:00
|
|
|
function getSize() {
|
2009-06-21 18:26:29 +00:00
|
|
|
return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 20;
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function validate( $value, $alldata ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
$p = parent::validate( $value, $alldata );
|
|
|
|
|
|
2009-07-17 22:19:02 +00:00
|
|
|
if ( $p !== true ) return $p;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-07-17 22:19:02 +00:00
|
|
|
if ( floatval( $value ) != $value ) {
|
|
|
|
|
return wfMsgExt( 'htmlform-float-invalid', 'parse' );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$in_range = true;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-07-17 22:19:02 +00:00
|
|
|
# The "int" part of these message names is rather confusing. They make
|
|
|
|
|
# equal sense for all numbers.
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( isset( $this->mParams['min'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$min = $this->mParams['min'];
|
|
|
|
|
if ( $min > $value )
|
2009-06-21 18:26:29 +00:00
|
|
|
return wfMsgExt( 'htmlform-int-toolow', 'parse', array( $min ) );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if ( isset( $this->mParams['max'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$max = $this->mParams['max'];
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $max < $value )
|
|
|
|
|
return wfMsgExt( 'htmlform-int-toohigh', 'parse', array( $max ) );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-17 22:19:02 +00:00
|
|
|
class HTMLIntField extends HTMLFloatField {
|
|
|
|
|
function validate( $value, $alldata ) {
|
|
|
|
|
$p = parent::validate( $value, $alldata );
|
|
|
|
|
|
|
|
|
|
if ( $p !== true ) return $p;
|
|
|
|
|
|
|
|
|
|
if ( intval( $value ) != $value ) {
|
|
|
|
|
return wfMsgExt( 'htmlform-int-invalid', 'parse' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
class HTMLCheckField extends HTMLFormField {
|
|
|
|
|
function getInputHTML( $value ) {
|
2009-04-27 00:55:23 +00:00
|
|
|
if ( !empty( $this->mParams['invert'] ) )
|
|
|
|
|
$value = !$value;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-28 01:00:35 +00:00
|
|
|
$attr = array( 'id' => $this->mID );
|
2009-06-21 18:26:29 +00:00
|
|
|
if( !empty( $this->mParams['disabled'] ) ) {
|
2009-04-28 01:00:35 +00:00
|
|
|
$attr['disabled'] = 'disabled';
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-28 01:00:35 +00:00
|
|
|
return Xml::check( $this->mName, $value, $attr ) . ' ' .
|
2009-04-24 01:31:17 +00:00
|
|
|
Xml::tags( 'label', array( 'for' => $this->mID ), $this->mLabel );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
function getLabel() {
|
2009-04-24 01:31:17 +00:00
|
|
|
return ' '; // In the right-hand column.
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function loadDataFromRequest( $request ) {
|
|
|
|
|
$invert = false;
|
|
|
|
|
if ( isset( $this->mParams['invert'] ) && $this->mParams['invert'] ) {
|
|
|
|
|
$invert = true;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// GetCheck won't work like we want for checks.
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $request->getCheck( 'wpEditToken' ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
// XOR has the following truth table, which is what we want
|
|
|
|
|
// INVERT VALUE | OUTPUT
|
|
|
|
|
// true true | false
|
|
|
|
|
// false true | true
|
|
|
|
|
// false false | false
|
|
|
|
|
// true false | true
|
|
|
|
|
return $request->getBool( $this->mName ) xor $invert;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTMLSelectField extends HTMLFormField {
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function validate( $value, $alldata ) {
|
|
|
|
|
$p = parent::validate( $value, $alldata );
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $p !== true ) return $p;
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
|
|
|
|
|
if ( in_array( $value, $validOptions ) )
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
function getInputHTML( $value ) {
|
|
|
|
|
$select = new XmlSelect( $this->mName, $this->mID, strval( $value ) );
|
|
|
|
|
|
2009-06-17 19:08:14 +00:00
|
|
|
// If one of the options' 'name' is int(0), it is automatically selected.
|
|
|
|
|
// because PHP sucks and things int(0) == 'some string'.
|
|
|
|
|
// Working around this by forcing all of them to strings.
|
|
|
|
|
$options = array_map( 'strval', $this->mParams['options'] );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( !empty( $this->mParams['disabled'] ) ) {
|
2009-04-28 01:00:35 +00:00
|
|
|
$select->setAttribute( 'disabled', 'disabled' );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-06-17 19:08:14 +00:00
|
|
|
$select->addOptions( $options );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $select->getHTML();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTMLSelectOrOtherField extends HTMLTextField {
|
|
|
|
|
static $jsAdded = false;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function __construct( $params ) {
|
2009-07-03 14:06:22 +00:00
|
|
|
if( !in_array( 'other', $params['options'] ) ) {
|
2009-04-24 09:02:21 +00:00
|
|
|
$params['options'][wfMsg( 'htmlform-selectorother-other' )] = 'other';
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
parent::__construct( $params );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getInputHTML( $value ) {
|
|
|
|
|
$valInSelect = false;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( $value !== false )
|
2009-04-24 08:58:19 +00:00
|
|
|
$valInSelect = in_array( $value,
|
2009-06-21 18:26:29 +00:00
|
|
|
HTMLFormField::flattenOptions( $this->mParams['options'] ) );
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$selected = $valInSelect ? $value : 'other';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$select = new XmlSelect( $this->mName, $this->mID, $selected );
|
2009-07-15 16:36:04 +00:00
|
|
|
$select->addOptions( array_map( 'strval', $this->mParams['options'] ) );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
$tbAttribs = array( 'id' => $this->mID . '-other' );
|
|
|
|
|
if( !empty( $this->mParams['disabled'] ) ) {
|
2009-04-28 01:00:35 +00:00
|
|
|
$select->setAttribute( 'disabled', 'disabled' );
|
|
|
|
|
$tbAttribs['disabled'] = 'disabled';
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-28 01:00:35 +00:00
|
|
|
$select = $select->getHTML();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if ( isset( $this->mParams['maxlength'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$tbAttribs['maxlength'] = $this->mParams['maxlength'];
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
$textbox = Xml::input( $this->mName . '-other',
|
2009-04-24 01:31:17 +00:00
|
|
|
$this->getSize(),
|
|
|
|
|
$valInSelect ? '' : $value,
|
|
|
|
|
$tbAttribs );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return "$select<br/>\n$textbox";
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function loadDataFromRequest( $request ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $request->getCheck( $this->mName ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$val = $request->getText( $this->mName );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( $val == 'other' ) {
|
|
|
|
|
$val = $request->getText( $this->mName . '-other' );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $val;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTMLMultiSelectField extends HTMLFormField {
|
|
|
|
|
function validate( $value, $alldata ) {
|
|
|
|
|
$p = parent::validate( $value, $alldata );
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $p !== true ) return $p;
|
|
|
|
|
|
|
|
|
|
if( !is_array( $value ) ) return false;
|
|
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
// If all options are valid, array_intersect of the valid options and the provided
|
|
|
|
|
// options will return the provided options.
|
|
|
|
|
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$validValues = array_intersect( $value, $validOptions );
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( count( $validValues ) == count( $value ) )
|
2009-04-24 01:31:17 +00:00
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getInputHTML( $value ) {
|
|
|
|
|
$html = $this->formatOptions( $this->mParams['options'], $value );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function formatOptions( $options, $value ) {
|
|
|
|
|
$html = '';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-28 01:00:35 +00:00
|
|
|
$attribs = array();
|
|
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) {
|
|
|
|
|
$attribs['disabled'] = 'disabled';
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $options as $label => $info ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if( is_array( $info ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= Xml::tags( 'h1', null, $label ) . "\n";
|
|
|
|
|
$html .= $this->formatOptions( $info, $value );
|
|
|
|
|
} else {
|
2009-06-21 18:26:29 +00:00
|
|
|
$thisAttribs = array( 'id' => $this->mID . "-$info", 'value' => $info );
|
2009-04-28 01:00:35 +00:00
|
|
|
|
2009-06-21 18:26:29 +00:00
|
|
|
$checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value ),
|
2009-04-28 01:00:35 +00:00
|
|
|
$attribs + $thisAttribs );
|
2009-06-21 18:26:29 +00:00
|
|
|
$checkbox .= ' ' . Xml::tags( 'label', array( 'for' => $this->mID . "-$info" ), $label );
|
|
|
|
|
|
2009-04-27 11:45:45 +00:00
|
|
|
$html .= $checkbox . '<br />';
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function loadDataFromRequest( $request ) {
|
|
|
|
|
// won't work with getCheck
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $request->getCheck( 'wpEditToken' ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$arr = $request->getArray( $this->mName );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
if( !$arr )
|
2009-04-24 01:31:17 +00:00
|
|
|
$arr = array();
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $arr;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getDefault() {
|
|
|
|
|
if ( isset( $this->mDefault ) ) {
|
|
|
|
|
return $this->mDefault;
|
|
|
|
|
} else {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTMLRadioField extends HTMLFormField {
|
|
|
|
|
function validate( $value, $alldata ) {
|
|
|
|
|
$p = parent::validate( $value, $alldata );
|
2009-06-21 18:26:29 +00:00
|
|
|
if( $p !== true ) return $p;
|
|
|
|
|
|
|
|
|
|
if( !is_string( $value ) && !is_int( $value ) )
|
2009-04-24 01:31:17 +00:00
|
|
|
return false;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$validOptions = HTMLFormField::flattenOptions( $this->mParams['options'] );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
if ( in_array( $value, $validOptions ) )
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return wfMsgExt( 'htmlform-select-badoption', 'parseinline' );
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getInputHTML( $value ) {
|
|
|
|
|
$html = $this->formatOptions( $this->mParams['options'], $value );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function formatOptions( $options, $value ) {
|
|
|
|
|
$html = '';
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-28 01:00:35 +00:00
|
|
|
$attribs = array();
|
|
|
|
|
if ( !empty( $this->mParams['disabled'] ) ) {
|
|
|
|
|
$attribs['disabled'] = 'disabled';
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
foreach( $options as $label => $info ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if( is_array( $info ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= Xml::tags( 'h1', null, $label ) . "\n";
|
|
|
|
|
$html .= $this->formatOptions( $info, $value );
|
|
|
|
|
} else {
|
2009-07-19 16:49:58 +00:00
|
|
|
$id = Sanitizer::escapeId( $this->mID . "-$info" );
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= Xml::radio( $this->mName, $info, $info == $value,
|
2009-07-19 16:49:58 +00:00
|
|
|
$attribs + array( 'id' => $id ) );
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= ' ' .
|
2009-07-19 16:49:58 +00:00
|
|
|
Xml::tags( 'label', array( 'for' => $id ), $label );
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
$html .= "<br/>\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class HTMLInfoField extends HTMLFormField {
|
|
|
|
|
function __construct( $info ) {
|
|
|
|
|
$info['nodata'] = true;
|
2009-06-21 18:26:29 +00:00
|
|
|
|
|
|
|
|
parent::__construct( $info );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getInputHTML( $value ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
return !empty( $this->mParams['raw'] ) ? $value : htmlspecialchars( $value );
|
2009-04-24 01:31:17 +00:00
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
function getTableRow( $value ) {
|
2009-06-21 18:26:29 +00:00
|
|
|
if ( !empty( $this->mParams['rawrow'] ) ) {
|
2009-04-24 01:31:17 +00:00
|
|
|
return $value;
|
|
|
|
|
}
|
2009-06-21 18:26:29 +00:00
|
|
|
|
2009-04-24 01:31:17 +00:00
|
|
|
return parent::getTableRow( $value );
|
|
|
|
|
}
|
|
|
|
|
}
|