2013-11-19 12:55:50 +00:00
|
|
|
<?php
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
/**
|
|
|
|
|
* The parent class to generate form fields. Any field type should
|
|
|
|
|
* be a subclass of this.
|
|
|
|
|
*/
|
|
|
|
|
abstract class HTMLFormField {
|
2013-11-19 13:08:16 +00:00
|
|
|
public $mParams;
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
protected $mValidationCallback;
|
|
|
|
|
protected $mFilterCallback;
|
|
|
|
|
protected $mName;
|
|
|
|
|
protected $mLabel; # String label. Set on construction
|
|
|
|
|
protected $mID;
|
|
|
|
|
protected $mClass = '';
|
|
|
|
|
protected $mDefault;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var bool If true will generate an empty div element with no label
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*/
|
|
|
|
|
protected $mShowEmptyLabels = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var HTMLForm
|
|
|
|
|
*/
|
|
|
|
|
public $mParent;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This function must be implemented to return the HTML to generate
|
|
|
|
|
* the input object itself. It should not implement the surrounding
|
|
|
|
|
* table cells/rows, or labels/help messages.
|
|
|
|
|
*
|
|
|
|
|
* @param string $value the value to set the input to; eg a default
|
|
|
|
|
* text for a text input.
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @return string Valid HTML.
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
abstract function getInputHTML( $value );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a translated interface message
|
|
|
|
|
*
|
|
|
|
|
* This is a wrapper around $this->mParent->msg() if $this->mParent is set
|
|
|
|
|
* and wfMessage() otherwise.
|
|
|
|
|
*
|
|
|
|
|
* Parameters are the same as wfMessage().
|
|
|
|
|
*
|
|
|
|
|
* @return Message object
|
|
|
|
|
*/
|
|
|
|
|
function msg() {
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
|
|
if ( $this->mParent ) {
|
|
|
|
|
$callback = array( $this->mParent, 'msg' );
|
|
|
|
|
} else {
|
|
|
|
|
$callback = 'wfMessage';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return call_user_func_array( $callback, $args );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Override this function to add specific validation checks on the
|
|
|
|
|
* field input. Don't forget to call parent::validate() to ensure
|
|
|
|
|
* that the user-defined callback mValidationCallback is still run
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string $value The value the field was submitted with
|
|
|
|
|
* @param array $alldata The data collected from the form
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
|
|
|
|
* @return Mixed Bool true on success, or String error to display.
|
|
|
|
|
*/
|
|
|
|
|
function validate( $value, $alldata ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['required'] )
|
|
|
|
|
&& $this->mParams['required'] !== false
|
|
|
|
|
&& $value === ''
|
|
|
|
|
) {
|
2013-11-19 12:55:50 +00:00
|
|
|
return $this->msg( 'htmlform-required' )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $this->mValidationCallback ) ) {
|
|
|
|
|
return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function filter( $value, $alldata ) {
|
|
|
|
|
if ( isset( $this->mFilterCallback ) ) {
|
|
|
|
|
$value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Should this field have a label, or is there no input element with the
|
|
|
|
|
* appropriate id for the label to point to?
|
|
|
|
|
*
|
|
|
|
|
* @return bool True to output a label, false to suppress
|
|
|
|
|
*/
|
|
|
|
|
protected function needsLabel() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tell the field whether to generate a separate label element if its label
|
|
|
|
|
* is blank.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*
|
|
|
|
|
* @param bool $show Set to false to not generate a label.
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function setShowEmptyLabel( $show ) {
|
|
|
|
|
$this->mShowEmptyLabels = $show;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the value that this input has been set to from a posted form,
|
|
|
|
|
* or the input's default value if it has not been set.
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param WebRequest $request
|
2013-11-19 12:55:50 +00:00
|
|
|
* @return String the value
|
|
|
|
|
*/
|
|
|
|
|
function loadDataFromRequest( $request ) {
|
|
|
|
|
if ( $request->getCheck( $this->mName ) ) {
|
|
|
|
|
return $request->getText( $this->mName );
|
|
|
|
|
} else {
|
|
|
|
|
return $this->getDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialise the object
|
|
|
|
|
*
|
|
|
|
|
* @param array $params Associative Array. See HTMLForm doc for syntax.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
function __construct( $params ) {
|
|
|
|
|
$this->mParams = $params;
|
|
|
|
|
|
|
|
|
|
# Generate the label from a message, if possible
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['label-message'] ) ) {
|
|
|
|
|
$msgInfo = $params['label-message'];
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
if ( is_array( $msgInfo ) ) {
|
|
|
|
|
$msg = array_shift( $msgInfo );
|
|
|
|
|
} else {
|
|
|
|
|
$msg = $msgInfo;
|
|
|
|
|
$msgInfo = array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mLabel = wfMessage( $msg, $msgInfo )->parse();
|
2013-11-19 13:08:16 +00:00
|
|
|
} elseif ( isset( $params['label'] ) ) {
|
|
|
|
|
if ( $params['label'] === ' ' ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
// Apparently some things set   directly and in an odd format
|
|
|
|
|
$this->mLabel = ' ';
|
|
|
|
|
} else {
|
2013-11-19 13:08:16 +00:00
|
|
|
$this->mLabel = htmlspecialchars( $params['label'] );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
} elseif ( isset( $params['label-raw'] ) ) {
|
|
|
|
|
$this->mLabel = $params['label-raw'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mName = "wp{$params['fieldname']}";
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['name'] ) ) {
|
|
|
|
|
$this->mName = $params['name'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$validName = Sanitizer::escapeId( $this->mName );
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mID = "mw-input-{$this->mName}";
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['default'] ) ) {
|
|
|
|
|
$this->mDefault = $params['default'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['id'] ) ) {
|
|
|
|
|
$id = $params['id'];
|
2013-11-19 12:55:50 +00:00
|
|
|
$validId = Sanitizer::escapeId( $id );
|
|
|
|
|
|
|
|
|
|
if ( $id != $validId ) {
|
|
|
|
|
throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->mID = $id;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['cssclass'] ) ) {
|
|
|
|
|
$this->mClass = $params['cssclass'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['validation-callback'] ) ) {
|
|
|
|
|
$this->mValidationCallback = $params['validation-callback'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['filter-callback'] ) ) {
|
|
|
|
|
$this->mFilterCallback = $params['filter-callback'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['flatlist'] ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$this->mClass .= ' mw-htmlform-flatlist';
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $params['hidelabel'] ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$this->mShowEmptyLabels = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the complete table row for the input, including help text,
|
|
|
|
|
* labels, and whatever.
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string $value The value to set the input to.
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @return string Complete HTML table row.
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
function getTableRow( $value ) {
|
|
|
|
|
list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
|
|
|
|
|
$inputHtml = $this->getInputHTML( $value );
|
|
|
|
|
$fieldType = get_class( $this );
|
|
|
|
|
$helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
|
|
|
|
|
$cellAttributes = array();
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( !empty( $this->mParams['vertical-label'] ) ) {
|
|
|
|
|
$cellAttributes['colspan'] = 2;
|
2013-11-19 12:55:50 +00:00
|
|
|
$verticalLabel = true;
|
|
|
|
|
} else {
|
|
|
|
|
$verticalLabel = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$label = $this->getLabelHtml( $cellAttributes );
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
$field = Html::rawElement(
|
|
|
|
|
'td',
|
|
|
|
|
array( 'class' => 'mw-input' ) + $cellAttributes,
|
|
|
|
|
$inputHtml . "\n$errors"
|
|
|
|
|
);
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
if ( $verticalLabel ) {
|
|
|
|
|
$html = Html::rawElement( 'tr', array( 'class' => 'mw-htmlform-vertical-label' ), $label );
|
2013-11-19 13:08:16 +00:00
|
|
|
$html .= Html::rawElement( 'tr',
|
|
|
|
|
array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
|
|
|
|
|
$field );
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
2013-11-19 13:08:16 +00:00
|
|
|
$html =
|
|
|
|
|
Html::rawElement( 'tr',
|
|
|
|
|
array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
|
|
|
|
|
$label . $field );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html . $helptext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the complete div for the input, including help text,
|
|
|
|
|
* labels, and whatever.
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string $value The value to set the input to.
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @return string Complete HTML table row.
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getDiv( $value ) {
|
|
|
|
|
list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
|
|
|
|
|
$inputHtml = $this->getInputHTML( $value );
|
|
|
|
|
$fieldType = get_class( $this );
|
|
|
|
|
$helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
|
|
|
|
|
$cellAttributes = array();
|
|
|
|
|
$label = $this->getLabelHtml( $cellAttributes );
|
|
|
|
|
|
|
|
|
|
$outerDivClass = array(
|
|
|
|
|
'mw-input',
|
|
|
|
|
'mw-htmlform-nolabel' => ( $label === '' )
|
|
|
|
|
);
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
$field = Html::rawElement(
|
|
|
|
|
'div',
|
|
|
|
|
array( 'class' => $outerDivClass ) + $cellAttributes,
|
|
|
|
|
$inputHtml . "\n$errors"
|
|
|
|
|
);
|
2013-11-19 12:55:50 +00:00
|
|
|
$divCssClasses = array( "mw-htmlform-field-$fieldType", $this->mClass, $errorClass );
|
|
|
|
|
if ( $this->mParent->isVForm() ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$divCssClasses[] = 'mw-ui-vform-div';
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
$html = Html::rawElement( 'div', array( 'class' => $divCssClasses ), $label . $field );
|
|
|
|
|
$html .= $helptext;
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the complete raw fields for the input, including help text,
|
|
|
|
|
* labels, and whatever.
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string $value The value to set the input to.
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @return string Complete HTML table row.
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getRaw( $value ) {
|
|
|
|
|
list( $errors, ) = $this->getErrorsAndErrorClass( $value );
|
|
|
|
|
$inputHtml = $this->getInputHTML( $value );
|
|
|
|
|
$helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
|
|
|
|
|
$cellAttributes = array();
|
|
|
|
|
$label = $this->getLabelHtml( $cellAttributes );
|
|
|
|
|
|
|
|
|
|
$html = "\n$errors";
|
|
|
|
|
$html .= $label;
|
|
|
|
|
$html .= $inputHtml;
|
|
|
|
|
$html .= $helptext;
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate help text HTML in table format
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string|null $helptext
|
|
|
|
|
* @return string
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getHelpTextHtmlTable( $helptext ) {
|
|
|
|
|
if ( is_null( $helptext ) ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = Html::rawElement( 'td', array( 'colspan' => 2, 'class' => 'htmlform-tip' ), $helptext );
|
|
|
|
|
$row = Html::rawElement( 'tr', array(), $row );
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return $row;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate help text HTML in div format
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string|null $helptext
|
2013-11-19 12:55:50 +00:00
|
|
|
*
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function getHelpTextHtmlDiv( $helptext ) {
|
|
|
|
|
if ( is_null( $helptext ) ) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$div = Html::rawElement( 'div', array( 'class' => 'htmlform-tip' ), $helptext );
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return $div;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate help text HTML formatted for raw output
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string|null $helptext
|
2013-11-19 12:55:50 +00:00
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
public function getHelpTextHtmlRaw( $helptext ) {
|
|
|
|
|
return $this->getHelpTextHtmlDiv( $helptext );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine the help text to display
|
|
|
|
|
* @since 1.20
|
2013-11-19 13:08:16 +00:00
|
|
|
* @return string
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getHelpText() {
|
|
|
|
|
$helptext = null;
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['help-message'] ) ) {
|
|
|
|
|
$this->mParams['help-messages'] = array( $this->mParams['help-message'] );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['help-messages'] ) ) {
|
|
|
|
|
foreach ( $this->mParams['help-messages'] as $name ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$helpMessage = (array)$name;
|
|
|
|
|
$msg = $this->msg( array_shift( $helpMessage ), $helpMessage );
|
|
|
|
|
|
|
|
|
|
if ( $msg->exists() ) {
|
|
|
|
|
if ( is_null( $helptext ) ) {
|
|
|
|
|
$helptext = '';
|
|
|
|
|
} else {
|
|
|
|
|
$helptext .= $this->msg( 'word-separator' )->escaped(); // some space
|
|
|
|
|
}
|
|
|
|
|
$helptext .= $msg->parse(); // Append message
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
} elseif ( isset( $this->mParams['help'] ) ) {
|
|
|
|
|
$helptext = $this->mParams['help'];
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return $helptext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine form errors to display and their classes
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string $value The value of the input
|
|
|
|
|
* @return array
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getErrorsAndErrorClass( $value ) {
|
|
|
|
|
$errors = $this->validate( $value, $this->mParent->mFieldData );
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( $errors === true ||
|
|
|
|
|
( !$this->mParent->getRequest()->wasPosted() && $this->mParent->getMethod() === 'post' )
|
|
|
|
|
) {
|
2013-11-19 12:55:50 +00:00
|
|
|
$errors = '';
|
|
|
|
|
$errorClass = '';
|
|
|
|
|
} else {
|
|
|
|
|
$errors = self::formatErrors( $errors );
|
|
|
|
|
$errorClass = 'mw-htmlform-invalid-input';
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return array( $errors, $errorClass );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLabel() {
|
|
|
|
|
return is_null( $this->mLabel ) ? '' : $this->mLabel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLabelHtml( $cellAttributes = array() ) {
|
|
|
|
|
# Don't output a for= attribute for labels with no associated input.
|
|
|
|
|
# Kind of hacky here, possibly we don't want these to be <label>s at all.
|
|
|
|
|
$for = array();
|
|
|
|
|
|
|
|
|
|
if ( $this->needsLabel() ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$for['for'] = $this->mID;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$labelValue = trim( $this->getLabel() );
|
|
|
|
|
$hasLabel = false;
|
|
|
|
|
if ( $labelValue !== ' ' && $labelValue !== '' ) {
|
|
|
|
|
$hasLabel = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$displayFormat = $this->mParent->getDisplayFormat();
|
|
|
|
|
$html = '';
|
|
|
|
|
|
|
|
|
|
if ( $displayFormat === 'table' ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$html =
|
|
|
|
|
Html::rawElement( 'td',
|
|
|
|
|
array( 'class' => 'mw-label' ) + $cellAttributes,
|
|
|
|
|
Html::rawElement( 'label', $for, $labelValue ) );
|
2013-11-19 12:55:50 +00:00
|
|
|
} elseif ( $hasLabel || $this->mShowEmptyLabels ) {
|
|
|
|
|
if ( $displayFormat === 'div' ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$html =
|
|
|
|
|
Html::rawElement( 'div',
|
|
|
|
|
array( 'class' => 'mw-label' ) + $cellAttributes,
|
|
|
|
|
Html::rawElement( 'label', $for, $labelValue ) );
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
|
|
|
|
$html = Html::rawElement( 'label', $for, $labelValue );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDefault() {
|
|
|
|
|
if ( isset( $this->mDefault ) ) {
|
|
|
|
|
return $this->mDefault;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the attributes required for the tooltip and accesskey.
|
|
|
|
|
*
|
|
|
|
|
* @return array Attributes
|
|
|
|
|
*/
|
|
|
|
|
public function getTooltipAndAccessKey() {
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( empty( $this->mParams['tooltip'] ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
return array();
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
|
|
|
|
|
return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* flatten an array of options to a single array, for instance,
|
|
|
|
|
* a set of "<options>" inside "<optgroups>".
|
|
|
|
|
*
|
|
|
|
|
* @param array $options Associative Array with values either Strings
|
|
|
|
|
* or Arrays
|
2013-11-19 13:08:16 +00:00
|
|
|
* @return array Flattened input
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public static function flattenOptions( $options ) {
|
|
|
|
|
$flatOpts = array();
|
|
|
|
|
|
|
|
|
|
foreach ( $options as $value ) {
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
$flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
|
|
|
|
|
} else {
|
2013-11-19 13:08:16 +00:00
|
|
|
$flatOpts[] = $value;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $flatOpts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Formats one or more errors as accepted by field validation-callback.
|
|
|
|
|
*
|
2013-11-19 13:08:16 +00:00
|
|
|
* @param string|Message|array $errors String|Message|Array of strings or Message instances
|
|
|
|
|
* @return string HTML
|
2013-11-19 12:55:50 +00:00
|
|
|
* @since 1.18
|
|
|
|
|
*/
|
|
|
|
|
protected static function formatErrors( $errors ) {
|
|
|
|
|
if ( is_array( $errors ) && count( $errors ) === 1 ) {
|
|
|
|
|
$errors = array_shift( $errors );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_array( $errors ) ) {
|
|
|
|
|
$lines = array();
|
|
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
|
if ( $error instanceof Message ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$lines[] = Html::rawElement( 'li', array(), $error->parse() );
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
2013-11-19 13:08:16 +00:00
|
|
|
$lines[] = Html::rawElement( 'li', array(), $error );
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return Html::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
|
|
|
|
|
} else {
|
|
|
|
|
if ( $errors instanceof Message ) {
|
|
|
|
|
$errors = $errors->parse();
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return Html::rawElement( 'span', array( 'class' => 'error' ), $errors );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|