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 $allowedParams = array(
101 $textAttribs +
= $this->getAttributes( $allowedParams );
103 $textbox = Html
::input( $this->mName
. '-other', $value[2], 'text', $textAttribs );
105 return "$select<br />\n$textbox";
109 * @param $request WebRequest
111 * @return Array("<overall message>","<select value>","<text field value>")
113 function loadDataFromRequest( $request ) {
114 if ( $request->getCheck( $this->mName
) ) {
116 $list = $request->getText( $this->mName
);
117 $text = $request->getText( $this->mName
. '-other' );
119 if ( $list == 'other' ) {
121 } elseif ( !in_array( $list, $this->mFlatOptions
) ) {
122 # User has spoofed the select form to give an option which wasn't
123 # in the original offer. Sulk...
125 } elseif ( $text == '' ) {
128 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
131 $final = $this->getDefault();
135 foreach ( $this->mFlatOptions
as $option ) {
136 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
137 if ( strpos( $text, $match ) === 0 ) {
139 $text = substr( $text, strlen( $match ) );
145 return array( $final, $list, $text );
149 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 45;
152 function validate( $value, $alldata ) {
153 # HTMLSelectField forces $value to be one of the options in the select
154 # field, which is not useful here. But we do want the validation further up
156 $p = parent
::validate( $value[1], $alldata );
162 if ( isset( $this->mParams
['required'] )
163 && $this->mParams
['required'] !== false
166 return $this->msg( 'htmlform-required' )->parse();