Merge "Pass message arguments as array to ErrorPageError"
[lhc/web/wiklou.git] / includes / htmlform / fields / 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 public function __construct( $params ) {
15 if ( array_key_exists( 'other', $params ) ) {
16 // Do nothing
17 } elseif ( array_key_exists( 'other-message', $params ) ) {
18 $params['other'] = $this->getMessage( $params['other-message'] )->plain();
19 } else {
20 $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
21 }
22
23 parent::__construct( $params );
24
25 if ( $this->getOptions() === null ) {
26 // Sulk
27 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28 }
29 if ( !in_array( 'other', $this->mOptions, true ) ) {
30 // Have 'other' always as first element
31 $this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
32 }
33 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
34 }
35
36 public function getInputHTML( $value ) {
37 $select = parent::getInputHTML( $value[1] );
38
39 $textAttribs = [
40 'id' => $this->mID . '-other',
41 'size' => $this->getSize(),
42 'class' => [ 'mw-htmlform-select-and-other-field' ],
43 'data-id-select' => $this->mID,
44 ];
45
46 if ( $this->mClass !== '' ) {
47 $textAttribs['class'][] = $this->mClass;
48 }
49
50 if ( isset( $this->mParams['maxlength-unit'] ) ) {
51 $textAttribs['data-mw-maxlength-unit'] = $this->mParams['maxlength-unit'];
52 }
53
54 $allowedParams = [
55 'required',
56 'autofocus',
57 'multiple',
58 'disabled',
59 'tabindex',
60 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
61 'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
62 ];
63
64 $textAttribs += $this->getAttributes( $allowedParams );
65
66 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
67
68 return "$select<br />\n$textbox";
69 }
70
71 protected function getOOUIModules() {
72 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
73 }
74
75 public function getInputOOUI( $value ) {
76 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
77
78 # TextInput
79 $textAttribs = [
80 'name' => $this->mName . '-other',
81 'value' => $value[2],
82 ];
83
84 $allowedParams = [
85 'required',
86 'autofocus',
87 'multiple',
88 'disabled',
89 'tabindex',
90 'maxlength',
91 ];
92
93 $textAttribs += OOUI\Element::configFromHtmlAttributes(
94 $this->getAttributes( $allowedParams )
95 );
96
97 if ( $this->mClass !== '' ) {
98 $textAttribs['classes'] = [ $this->mClass ];
99 }
100
101 # DropdownInput
102 $dropdownInputAttribs = [
103 'name' => $this->mName,
104 'id' => $this->mID . '-select',
105 'options' => $this->getOptionsOOUI(),
106 'value' => $value[1],
107 ];
108
109 $allowedParams = [
110 'tabindex',
111 'disabled',
112 ];
113
114 $dropdownInputAttribs += OOUI\Element::configFromHtmlAttributes(
115 $this->getAttributes( $allowedParams )
116 );
117
118 if ( $this->mClass !== '' ) {
119 $dropdownInputAttribs['classes'] = [ $this->mClass ];
120 }
121
122 $disabled = false;
123 if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
124 $disabled = true;
125 }
126
127 return $this->getInputWidget( [
128 'id' => $this->mID,
129 'disabled' => $disabled,
130 'textinput' => $textAttribs,
131 'dropdowninput' => $dropdownInputAttribs,
132 'or' => false,
133 'required' => $this->mParams[ 'required' ] ?? false,
134 'classes' => [ 'mw-htmlform-select-and-other-field' ],
135 'data' => [
136 'maxlengthUnit' => $this->mParams['maxlength-unit'] ?? 'bytes'
137 ],
138 ] );
139 }
140
141 public function getInputWidget( $params ) {
142 return new MediaWiki\Widget\SelectWithInputWidget( $params );
143 }
144
145 /**
146 * @inheritDoc
147 */
148 public function getDefault() {
149 $default = parent::getDefault();
150
151 // Default values of empty form
152 $final = '';
153 $list = 'other';
154 $text = '';
155
156 if ( $default !== null ) {
157 $final = $default;
158 // Assume the default is a text value, with the 'other' option selected.
159 // Then check if that assumption is correct, and update $list and $text if not.
160 $text = $final;
161 foreach ( $this->mFlatOptions as $option ) {
162 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
163 if ( strpos( $final, $match ) === 0 ) {
164 $list = $option;
165 $text = substr( $final, strlen( $match ) );
166 break;
167 }
168 }
169 }
170
171 return [ $final, $list, $text ];
172 }
173
174 /**
175 * @param WebRequest $request
176 *
177 * @return array ["<overall message>","<select value>","<text field value>"]
178 */
179 public function loadDataFromRequest( $request ) {
180 if ( $request->getCheck( $this->mName ) ) {
181 $list = $request->getText( $this->mName );
182 $text = $request->getText( $this->mName . '-other' );
183
184 // Should be built the same as in mediawiki.htmlform.js
185 if ( $list == 'other' ) {
186 $final = $text;
187 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
188 # User has spoofed the select form to give an option which wasn't
189 # in the original offer. Sulk...
190 $final = $text;
191 } elseif ( $text == '' ) {
192 $final = $list;
193 } else {
194 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
195 }
196 return [ $final, $list, $text ];
197 }
198 return $this->getDefault();
199 }
200
201 public function getSize() {
202 return $this->mParams['size'] ?? 45;
203 }
204
205 public function validate( $value, $alldata ) {
206 # HTMLSelectField forces $value to be one of the options in the select
207 # field, which is not useful here. But we do want the validation further up
208 # the chain
209 $p = parent::validate( $value[1], $alldata );
210
211 if ( $p !== true ) {
212 return $p;
213 }
214
215 if ( isset( $this->mParams['required'] )
216 && $this->mParams['required'] !== false
217 && $value[0] === ''
218 ) {
219 return $this->msg( 'htmlform-required' );
220 }
221
222 return true;
223 }
224 }