3 * Double field with a dropdown list constructed from a system message in the format
6 * * New Optgroup header
7 * Plus a text field underneath for an additional reason. The 'value' of the field is
8 * "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
10 * @todo FIXME: If made 'required', only the text field should be compulsory.
12 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
26 $params[ 'options' ] = self
::parseMessage( wfMessage( $params[ 'options-message' ] )->inContentLanguage()->plain(), $params[ 'other' ] );
29 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
31 $this->mFlatOptions
= self
::flattenOptions( $params[ 'options' ] );
33 parent
::__construct( $params );
37 * Build a drop-down box from a textual list.
39 * @param string $string message text
40 * @param string $otherName name of "other reason" option
43 * TODO: this is copied from Xml::listDropDown(), deprecate/avoid duplication?
45 public static function parseMessage( $string, $otherName = null ) {
46 if ( $otherName === null ) {
47 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
51 $options = array( $otherName => 'other' );
53 foreach ( explode( "\n", $string ) as $option ) {
54 $value = trim( $option );
57 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
58 # A new group is starting...
59 $value = trim( substr( $value, 1 ) );
61 } elseif ( substr( $value, 0, 2 ) == '**' ) {
63 $opt = trim( substr( $value, 2 ) );
64 if ( $optgroup === false ) {
65 $options[ $opt ] = $opt;
67 $options[ $optgroup ][ $opt ] = $opt;
70 # groupless reason list
72 $options[ $option ] = $option;
79 function getInputHTML( $value ) {
80 $select = parent
::getInputHTML( $value[ 1 ] );
83 'id' => $this->mID
. '-other',
84 'size' => $this->getSize(),
87 if ( $this->mClass
!== '' ) {
88 $textAttribs[ 'class' ] = $this->mClass
;
91 foreach ( array( 'required', 'autofocus', 'multiple', 'disabled' ) as $param ) {
92 if ( isset( $this->mParams
[ $param ] ) ) {
93 $textAttribs[ $param ] = '';
97 $textbox = Html
::input( $this->mName
. '-other', $value[ 2 ], 'text', $textAttribs );
99 return "$select<br />\n$textbox";
103 * @param $request WebRequest
105 * @return Array("<overall message>","<select value>","<text field value>")
107 function loadDataFromRequest( $request ) {
108 if ( $request->getCheck( $this->mName
) ) {
110 $list = $request->getText( $this->mName
);
111 $text = $request->getText( $this->mName
. '-other' );
113 if ( $list == 'other' ) {
115 } elseif ( ! in_array( $list, $this->mFlatOptions
) ) {
116 # User has spoofed the select form to give an option which wasn't
117 # in the original offer. Sulk...
119 } elseif ( $text == '' ) {
122 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
126 $final = $this->getDefault();
130 foreach ( $this->mFlatOptions
as $option ) {
131 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
132 if ( strpos( $text, $match ) === 0 ) {
134 $text = substr( $text, strlen( $match ) );
139 return array( $final, $list, $text );
143 return isset( $this->mParams
[ 'size' ] ) ?
$this->mParams
[ 'size' ] : 45;
146 function validate( $value, $alldata ) {
147 # HTMLSelectField forces $value to be one of the options in the select
148 # field, which is not useful here. But we do want the validation further up
150 $p = parent
::validate( $value[ 1 ], $alldata );
156 if ( isset( $this->mParams
[ 'required' ] ) && $this->mParams
[ 'required' ] !== false && $value[ 1 ] === '' ) {
157 return $this->msg( 'htmlform-required' )->parse();