4 * Double field with a dropdown list constructed from a system message in the format
7 * * New Optgroup header
8 * Plus a text field underneath for an additional reason. The 'value' of the field is
9 * "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
11 * @todo FIXME: If made 'required', only the text field should be compulsory.
13 class HTMLSelectAndOtherField
extends HTMLSelectField
{
14 function __construct( $params ) {
15 if ( array_key_exists( 'other', $params ) ) {
16 } elseif ( array_key_exists( 'other-message', $params ) ) {
17 $params['other'] = wfMessage( $params['other-message'] )->plain();
19 $params['other'] = null;
22 if ( array_key_exists( 'options', $params ) ) {
23 # Options array already specified
24 } elseif ( array_key_exists( 'options-message', $params ) ) {
25 # Generate options array from a system message
27 self
::parseMessage( wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
31 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
33 $this->mFlatOptions
= self
::flattenOptions( $params['options'] );
35 parent
::__construct( $params );
39 * Build a drop-down box from a textual list.
41 * @param string $string message text
42 * @param string $otherName name of "other reason" option
45 * @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication?
47 public static function parseMessage( $string, $otherName = null ) {
48 if ( $otherName === null ) {
49 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
53 $options = array( $otherName => 'other' );
55 foreach ( explode( "\n", $string ) as $option ) {
56 $value = trim( $option );
59 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
60 # A new group is starting...
61 $value = trim( substr( $value, 1 ) );
63 } elseif ( substr( $value, 0, 2 ) == '**' ) {
65 $opt = trim( substr( $value, 2 ) );
66 if ( $optgroup === false ) {
67 $options[$opt] = $opt;
69 $options[$optgroup][$opt] = $opt;
72 # groupless reason list
74 $options[$option] = $option;
81 function getInputHTML( $value ) {
82 $select = parent
::getInputHTML( $value[1] );
85 'id' => $this->mID
. '-other',
86 'size' => $this->getSize(),
89 if ( $this->mClass
!== '' ) {
90 $textAttribs['class'] = $this->mClass
;
93 foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
94 if ( isset( $this->mParams
[$param] ) ) {
95 $textAttribs[$param] = '';
99 $textbox = Html
::input( $this->mName
. '-other', $value[2], 'text', $textAttribs );
101 return "$select<br />\n$textbox";
105 * @param $request WebRequest
107 * @return Array("<overall message>","<select value>","<text field value>")
109 function loadDataFromRequest( $request ) {
110 if ( $request->getCheck( $this->mName
) ) {
112 $list = $request->getText( $this->mName
);
113 $text = $request->getText( $this->mName
. '-other' );
115 if ( $list == 'other' ) {
117 } elseif ( !in_array( $list, $this->mFlatOptions
) ) {
118 # User has spoofed the select form to give an option which wasn't
119 # in the original offer. Sulk...
121 } elseif ( $text == '' ) {
124 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
127 $final = $this->getDefault();
131 foreach ( $this->mFlatOptions
as $option ) {
132 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
133 if ( strpos( $text, $match ) === 0 ) {
135 $text = substr( $text, strlen( $match ) );
141 return array( $final, $list, $text );
145 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 45;
148 function validate( $value, $alldata ) {
149 # HTMLSelectField forces $value to be one of the options in the select
150 # field, which is not useful here. But we do want the validation further up
152 $p = parent
::validate( $value[1], $alldata );
158 if ( isset( $this->mParams
['required'] )
159 && $this->mParams
['required'] !== false
162 return $this->msg( 'htmlform-required' )->parse();