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
|
|
|
/**
|
|
|
|
|
* Double field with a dropdown list constructed from a system message in the format
|
|
|
|
|
* * Optgroup header
|
|
|
|
|
* ** <option value>
|
|
|
|
|
* * New Optgroup header
|
|
|
|
|
* Plus a text field underneath for an additional reason. The 'value' of the field is
|
|
|
|
|
* "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
|
|
|
|
|
* select dropdown.
|
|
|
|
|
* @todo FIXME: If made 'required', only the text field should be compulsory.
|
|
|
|
|
*/
|
|
|
|
|
class HTMLSelectAndOtherField extends HTMLSelectField {
|
|
|
|
|
function __construct( $params ) {
|
|
|
|
|
if ( array_key_exists( 'other', $params ) ) {
|
|
|
|
|
} elseif ( array_key_exists( 'other-message', $params ) ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$params['other'] = wfMessage( $params['other-message'] )->plain();
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
2013-11-19 13:08:16 +00:00
|
|
|
$params['other'] = null;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( array_key_exists( 'options', $params ) ) {
|
|
|
|
|
# Options array already specified
|
|
|
|
|
} elseif ( array_key_exists( 'options-message', $params ) ) {
|
|
|
|
|
# Generate options array from a system message
|
2013-11-19 13:08:16 +00:00
|
|
|
$params['options'] =
|
|
|
|
|
self::parseMessage( wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
|
|
|
|
|
$params['other'] );
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
|
|
|
|
# Sulk
|
|
|
|
|
throw new MWException( 'HTMLSelectAndOtherField called without any options' );
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
$this->mFlatOptions = self::flattenOptions( $params['options'] );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
parent::__construct( $params );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Build a drop-down box from a textual list.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string message text
|
|
|
|
|
* @param string $otherName name of "other reason" option
|
|
|
|
|
*
|
|
|
|
|
* @return Array
|
2013-11-19 13:08:16 +00:00
|
|
|
* @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication?
|
2013-11-19 12:55:50 +00:00
|
|
|
*/
|
|
|
|
|
public static function parseMessage( $string, $otherName = null ) {
|
|
|
|
|
if ( $otherName === null ) {
|
|
|
|
|
$otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$optgroup = false;
|
|
|
|
|
$options = array( $otherName => 'other' );
|
|
|
|
|
|
|
|
|
|
foreach ( explode( "\n", $string ) as $option ) {
|
|
|
|
|
$value = trim( $option );
|
|
|
|
|
if ( $value == '' ) {
|
|
|
|
|
continue;
|
|
|
|
|
} elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
|
|
|
|
|
# A new group is starting...
|
|
|
|
|
$value = trim( substr( $value, 1 ) );
|
|
|
|
|
$optgroup = $value;
|
|
|
|
|
} elseif ( substr( $value, 0, 2 ) == '**' ) {
|
|
|
|
|
# groupmember
|
|
|
|
|
$opt = trim( substr( $value, 2 ) );
|
|
|
|
|
if ( $optgroup === false ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$options[$opt] = $opt;
|
2013-11-19 12:55:50 +00:00
|
|
|
} else {
|
2013-11-19 13:08:16 +00:00
|
|
|
$options[$optgroup][$opt] = $opt;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
# groupless reason list
|
|
|
|
|
$optgroup = false;
|
2013-11-19 13:08:16 +00:00
|
|
|
$options[$option] = $option;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInputHTML( $value ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$select = parent::getInputHTML( $value[1] );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
$textAttribs = array(
|
|
|
|
|
'id' => $this->mID . '-other',
|
|
|
|
|
'size' => $this->getSize(),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $this->mClass !== '' ) {
|
2013-11-19 13:08:16 +00:00
|
|
|
$textAttribs['class'] = $this->mClass;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-05-02 18:27:44 +00:00
|
|
|
$allowedParams = array(
|
|
|
|
|
'required',
|
|
|
|
|
'autofocus',
|
|
|
|
|
'multiple',
|
|
|
|
|
'disabled',
|
|
|
|
|
'tabindex'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$textAttribs += $this->getAttributes( $allowedParams );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
$textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
return "$select<br />\n$textbox";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $request WebRequest
|
|
|
|
|
*
|
|
|
|
|
* @return Array("<overall message>","<select value>","<text field value>")
|
|
|
|
|
*/
|
|
|
|
|
function loadDataFromRequest( $request ) {
|
|
|
|
|
if ( $request->getCheck( $this->mName ) ) {
|
|
|
|
|
|
|
|
|
|
$list = $request->getText( $this->mName );
|
|
|
|
|
$text = $request->getText( $this->mName . '-other' );
|
|
|
|
|
|
|
|
|
|
if ( $list == 'other' ) {
|
|
|
|
|
$final = $text;
|
2013-11-19 13:08:16 +00:00
|
|
|
} elseif ( !in_array( $list, $this->mFlatOptions ) ) {
|
2013-11-19 12:55:50 +00:00
|
|
|
# User has spoofed the select form to give an option which wasn't
|
|
|
|
|
# in the original offer. Sulk...
|
|
|
|
|
$final = $text;
|
|
|
|
|
} elseif ( $text == '' ) {
|
|
|
|
|
$final = $list;
|
|
|
|
|
} else {
|
|
|
|
|
$final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$final = $this->getDefault();
|
|
|
|
|
|
|
|
|
|
$list = 'other';
|
|
|
|
|
$text = $final;
|
|
|
|
|
foreach ( $this->mFlatOptions as $option ) {
|
|
|
|
|
$match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
|
|
|
|
|
if ( strpos( $text, $match ) === 0 ) {
|
|
|
|
|
$list = $option;
|
|
|
|
|
$text = substr( $text, strlen( $match ) );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
|
2013-11-19 12:55:50 +00:00
|
|
|
return array( $final, $list, $text );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getSize() {
|
2013-11-19 13:08:16 +00:00
|
|
|
return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
|
2013-11-19 12:55:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validate( $value, $alldata ) {
|
|
|
|
|
# HTMLSelectField forces $value to be one of the options in the select
|
|
|
|
|
# field, which is not useful here. But we do want the validation further up
|
|
|
|
|
# the chain
|
2013-11-19 13:08:16 +00:00
|
|
|
$p = parent::validate( $value[1], $alldata );
|
2013-11-19 12:55:50 +00:00
|
|
|
|
|
|
|
|
if ( $p !== true ) {
|
|
|
|
|
return $p;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 13:08:16 +00:00
|
|
|
if ( isset( $this->mParams['required'] )
|
|
|
|
|
&& $this->mParams['required'] !== false
|
|
|
|
|
&& $value[1] === ''
|
|
|
|
|
) {
|
2013-11-19 12:55:50 +00:00
|
|
|
return $this->msg( 'htmlform-required' )->parse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-11-19 13:08:16 +00:00
|
|
|
}
|