MediaWiki master
HTMLMultiSelectField.php
Go to the documentation of this file.
1<?php
2
4
11use RuntimeException;
12
20 private $mPlaceholder;
21
36 public function __construct( $params ) {
37 parent::__construct( $params );
38
39 // If the disabled-options parameter is not provided, use an empty array
40 if ( !isset( $this->mParams['disabled-options'] ) ) {
41 $this->mParams['disabled-options'] = [];
42 }
43
44 if ( isset( $params['dropdown'] ) ) {
45 $this->mClass .= ' mw-htmlform-dropdown';
46 if ( isset( $params['placeholder'] ) ) {
47 $this->mPlaceholder = $params['placeholder'];
48 } elseif ( isset( $params['placeholder-message'] ) ) {
49 $this->mPlaceholder = $this->msg( $params['placeholder-message'] )->text();
50 }
51 }
52
53 if ( isset( $params['flatlist'] ) ) {
54 $this->mClass .= ' mw-htmlform-flatlist';
55 }
56 }
57
62 public function validate( $value, $alldata ) {
63 $p = parent::validate( $value, $alldata );
64
65 if ( $p !== true ) {
66 return $p;
67 }
68
69 if ( !is_array( $value ) ) {
70 return false;
71 }
72
73 // Reject nested arrays (T274955)
74 $value = array_filter( $value, 'is_scalar' );
75
76 if ( isset( $this->mParams['max'] ) && ( count( $value ) > $this->mParams['max'] ) ) {
77 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
78 }
79
80 # If all options are valid, array_intersect of the valid options
81 # and the provided options will return the provided options.
82 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
83
84 $validValues = array_intersect( $value, $validOptions );
85 if ( count( $validValues ) == count( $value ) ) {
86 return true;
87 } else {
88 return $this->msg( 'htmlform-select-badoption' );
89 }
90 }
91
96 public function getInputHTML( $value ) {
98 $html = $this->formatOptions( $this->getOptions(), $value );
99
100 return $html;
101 }
102
111 public function formatOptions( $options, $value ) {
112 $html = '';
113
114 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
115
116 foreach ( $options as $label => $info ) {
117 if ( is_array( $info ) ) {
118 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
119 $html .= $this->formatOptions( $info, $value );
120 } else {
121 $thisAttribs = [
122 'id' => "{$this->mID}-$info",
123 'value' => $info,
124 ];
125 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
126 $thisAttribs['disabled'] = 'disabled';
127 }
128 $checked = in_array( $info, $value, true );
129
130 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
131
132 $html .= ' ' . Html::rawElement(
133 'div',
134 [ 'class' => 'mw-htmlform-flatlist-item' ],
135 $checkbox
136 );
137 }
138 }
139
140 return $html;
141 }
142
143 protected function getOneCheckbox( $checked, $attribs, $label ) {
144 if ( $this->mParent instanceof OOUIHTMLForm ) {
145 throw new RuntimeException( __METHOD__ . ' is not supported' );
146 } else {
147 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
148 $checkbox =
149 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
150 "\u{00A0}" .
151 call_user_func( $elementFunc,
152 'label',
153 [ 'for' => $attribs['id'] ],
154 $label
155 );
156 return $checkbox;
157 }
158 }
159
164 public function getOptionsOOUI() {
165 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
166 // Sections make this difficult. See getInputOOUI().
167 throw new RuntimeException( __METHOD__ . ' is not supported' );
168 }
169
182 public function getInputOOUI( $value ) {
183 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
184
185 // Reject nested arrays (T274955)
186 $value = array_filter( $value, 'is_scalar' );
187
188 $hasSections = false;
189 $optionsOouiSections = [];
190 $options = $this->getOptions();
191 // If the options are supposed to be split into sections, each section becomes a separate
192 // CheckboxMultiselectInputWidget.
193 foreach ( $options as $label => $section ) {
194 if ( is_array( $section ) ) {
195 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
196 unset( $options[$label] );
197 $hasSections = true;
198 }
199 }
200 // If anything remains in the array, they are sectionless options. Put them in a separate widget
201 // at the beginning.
202 if ( $options ) {
203 $optionsOouiSections = array_merge(
204 [ '' => Html::listDropdownOptionsOoui( $options ) ],
205 $optionsOouiSections
206 );
207 }
208 '@phan-var array[][] $optionsOouiSections';
209
210 $out = [];
211 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
212 $attr = [];
213 $attr['name'] = "{$this->mName}[]";
214
215 $attr['value'] = $value;
216
217 $options = $optionsOoui;
218 foreach ( $options as &$option ) {
219 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
220 }
221 if ( $this->mOptionsLabelsNotFromMessage ) {
222 foreach ( $options as &$option ) {
223 $option['label'] = new \OOUI\HtmlSnippet( $option['label'] );
224 }
225 }
226 unset( $option );
227 $attr['options'] = $options;
228
229 $attr += \OOUI\Element::configFromHtmlAttributes(
230 $this->getAttributes( [ 'disabled', 'tabindex' ] )
231 );
232
233 if ( $this->mClass !== '' ) {
234 $attr['classes'] = [ $this->mClass ];
235 }
236
237 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
238 if ( $sectionLabel ) {
239 $out[] = new \OOUI\FieldsetLayout( [
240 'items' => [ $widget ],
241 // @phan-suppress-next-line SecurityCheck-XSS Key is html, taint cannot track that
242 'label' => new \OOUI\HtmlSnippet( $sectionLabel ),
243 ] );
244 } else {
245 $out[] = $widget;
246 }
247 }
248
249 if ( !$hasSections && $out ) {
250 $firstFieldData = $out[0]->getData() ?: [];
251 if ( $this->mPlaceholder ) {
252 $firstFieldData['placeholder'] = $this->mPlaceholder;
253 }
254 if ( isset( $this->mParams['max'] ) ) {
255 $firstFieldData['tagLimit'] = $this->mParams['max'];
256 }
257 $out[0]->setData( $firstFieldData );
258 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
259 // This allows it to be made infusable and later tweaked by JS code.
260 return $out[0];
261 }
262
263 return implode( '', $out );
264 }
265
272 public function loadDataFromRequest( $request ) {
273 $fromRequest = $request->getArray( $this->mName, [] );
274 // Fetch the value in either one of the two following case:
275 // - we have a valid submit attempt (form was just submitted)
276 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
277 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
278 // Checkboxes are just not added to the request arrays if they're not checked,
279 // so it's perfectly possible for there not to be an entry at all
280 // @phan-suppress-next-line PhanTypeMismatchReturnNullable getArray does not return null
281 return $fromRequest;
282 } else {
283 // That's ok, the user has not yet submitted the form, so show the defaults
284 return $this->getDefault();
285 }
286 }
287
292 public function getDefault() {
293 return $this->mDefault ?? [];
294 }
295
300 public function filterDataForSubmit( $data ) {
302 $options = HTMLFormField::flattenOptions( $this->getOptions() );
303 $forcedOn = array_intersect( $this->mParams['disabled-options'], $this->getDefault() );
304
305 $res = [];
306 foreach ( $options as $opt ) {
307 $res["$opt"] = in_array( $opt, $forcedOn, true ) || in_array( $opt, $data, true );
308 }
309
310 return $res;
311 }
312
317 protected function needsLabel() {
318 return false;
319 }
320}
321
323class_alias( HTMLMultiSelectField::class, 'HTMLMultiSelectField' );
array $params
The job parameters.
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputOOUI( $value)
Get the OOUI version of this field.
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
The parent class to generate form fields.
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
getOptions()
Fetch the array of options from the field's parameters.
getAttributes(array $list)
Returns the given attributes from the parameters.
msg( $key,... $params)
Get a translated interface message.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
Compact stacked vertical format for forms, implemented using OOUI widgets.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
static check( $name, $checked=false, array $attribs=[])
Convenience function to produce a checkbox (input element with type=checkbox)
Definition Html.php:672
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Module of static functions for generating XML.
Definition Xml.php:37