Merge "Code style fixes to ApiBase::requireAtLeastOneParameter"
[lhc/web/wiklou.git] / includes / htmlform / HTMLSelectAndOtherField.php
1 <?php
2
3 /**
4 * Double field with a dropdown list constructed from a system message in the format
5 * * Optgroup header
6 * ** <option value>
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
10 * select dropdown.
11 * @todo FIXME: If made 'required', only the text field should be compulsory.
12 */
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();
18 } else {
19 $params['other'] = null;
20 }
21
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'] =
27 self::parseMessage( wfMessage( $params['options-message'] )->inContentLanguage()->plain(),
28 $params['other'] );
29 } else {
30 # Sulk
31 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
32 }
33 $this->mFlatOptions = self::flattenOptions( $params['options'] );
34
35 parent::__construct( $params );
36 }
37
38 /**
39 * Build a drop-down box from a textual list.
40 *
41 * @param string $string message text
42 * @param string $otherName name of "other reason" option
43 *
44 * @return Array
45 * @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication?
46 */
47 public static function parseMessage( $string, $otherName = null ) {
48 if ( $otherName === null ) {
49 $otherName = wfMessage( 'htmlform-selectorother-other' )->plain();
50 }
51
52 $optgroup = false;
53 $options = array( $otherName => 'other' );
54
55 foreach ( explode( "\n", $string ) as $option ) {
56 $value = trim( $option );
57 if ( $value == '' ) {
58 continue;
59 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
60 # A new group is starting...
61 $value = trim( substr( $value, 1 ) );
62 $optgroup = $value;
63 } elseif ( substr( $value, 0, 2 ) == '**' ) {
64 # groupmember
65 $opt = trim( substr( $value, 2 ) );
66 if ( $optgroup === false ) {
67 $options[$opt] = $opt;
68 } else {
69 $options[$optgroup][$opt] = $opt;
70 }
71 } else {
72 # groupless reason list
73 $optgroup = false;
74 $options[$option] = $option;
75 }
76 }
77
78 return $options;
79 }
80
81 function getInputHTML( $value ) {
82 $select = parent::getInputHTML( $value[1] );
83
84 $textAttribs = array(
85 'id' => $this->mID . '-other',
86 'size' => $this->getSize(),
87 );
88
89 if ( $this->mClass !== '' ) {
90 $textAttribs['class'] = $this->mClass;
91 }
92
93 $allowedParams = array(
94 'required',
95 'autofocus',
96 'multiple',
97 'disabled',
98 'tabindex'
99 );
100
101 $textAttribs += $this->getAttributes( $allowedParams );
102
103 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
104
105 return "$select<br />\n$textbox";
106 }
107
108 /**
109 * @param $request WebRequest
110 *
111 * @return Array("<overall message>","<select value>","<text field value>")
112 */
113 function loadDataFromRequest( $request ) {
114 if ( $request->getCheck( $this->mName ) ) {
115
116 $list = $request->getText( $this->mName );
117 $text = $request->getText( $this->mName . '-other' );
118
119 if ( $list == 'other' ) {
120 $final = $text;
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...
124 $final = $text;
125 } elseif ( $text == '' ) {
126 $final = $list;
127 } else {
128 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
129 }
130 } else {
131 $final = $this->getDefault();
132
133 $list = 'other';
134 $text = $final;
135 foreach ( $this->mFlatOptions as $option ) {
136 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
137 if ( strpos( $text, $match ) === 0 ) {
138 $list = $option;
139 $text = substr( $text, strlen( $match ) );
140 break;
141 }
142 }
143 }
144
145 return array( $final, $list, $text );
146 }
147
148 function getSize() {
149 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
150 }
151
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
155 # the chain
156 $p = parent::validate( $value[1], $alldata );
157
158 if ( $p !== true ) {
159 return $p;
160 }
161
162 if ( isset( $this->mParams['required'] )
163 && $this->mParams['required'] !== false
164 && $value[1] === ''
165 ) {
166 return $this->msg( 'htmlform-required' )->parse();
167 }
168
169 return true;
170 }
171 }