MediaWiki master
HTMLMultiSelectField.php
Go to the documentation of this file.
1<?php
2
4
10use RuntimeException;
11use Xml;
12
19 /* @var string */
20 private $mPlaceholder;
21
34 public function __construct( $params ) {
35 parent::__construct( $params );
36
37 // If the disabled-options parameter is not provided, use an empty array
38 if ( !isset( $this->mParams['disabled-options'] ) ) {
39 $this->mParams['disabled-options'] = [];
40 }
41
42 if ( isset( $params['dropdown'] ) ) {
43 $this->mClass .= ' mw-htmlform-dropdown';
44 if ( isset( $params['placeholder'] ) ) {
45 $this->mPlaceholder = $params['placeholder'];
46 } elseif ( isset( $params['placeholder-message'] ) ) {
47 $this->mPlaceholder = $this->msg( $params['placeholder-message'] )->text();
48 }
49 }
50
51 if ( isset( $params['flatlist'] ) ) {
52 $this->mClass .= ' mw-htmlform-flatlist';
53 }
54 }
55
60 public function validate( $value, $alldata ) {
61 $p = parent::validate( $value, $alldata );
62
63 if ( $p !== true ) {
64 return $p;
65 }
66
67 if ( !is_array( $value ) ) {
68 return false;
69 }
70
71 // Reject nested arrays (T274955)
72 $value = array_filter( $value, 'is_scalar' );
73
74 # If all options are valid, array_intersect of the valid options
75 # and the provided options will return the provided options.
76 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
77
78 $validValues = array_intersect( $value, $validOptions );
79 if ( count( $validValues ) == count( $value ) ) {
80 return true;
81 } else {
82 return $this->msg( 'htmlform-select-badoption' );
83 }
84 }
85
90 public function getInputHTML( $value ) {
91 if ( isset( $this->mParams['dropdown'] ) ) {
92 $this->mParent->getOutput()->addModules( 'jquery.chosen' );
93 }
94
96 $html = $this->formatOptions( $this->getOptions(), $value );
97
98 return $html;
99 }
100
109 public function formatOptions( $options, $value ) {
110 $html = '';
111
112 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
113
114 foreach ( $options as $label => $info ) {
115 if ( is_array( $info ) ) {
116 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
117 $html .= $this->formatOptions( $info, $value );
118 } else {
119 $thisAttribs = [
120 'id' => "{$this->mID}-$info",
121 'value' => $info,
122 ];
123 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
124 $thisAttribs['disabled'] = 'disabled';
125 }
126 $checked = in_array( $info, $value, true );
127
128 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
129
130 $html .= ' ' . Html::rawElement(
131 'div',
132 [ 'class' => 'mw-htmlform-flatlist-item' ],
133 $checkbox
134 );
135 }
136 }
137
138 return $html;
139 }
140
141 protected function getOneCheckbox( $checked, $attribs, $label ) {
142 if ( $this->mParent instanceof OOUIHTMLForm ) {
143 throw new RuntimeException( __METHOD__ . ' is not supported' );
144 } else {
145 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
146 $checkbox =
147 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
148 "\u{00A0}" .
149 call_user_func( $elementFunc,
150 'label',
151 [ 'for' => $attribs['id'] ],
152 $label
153 );
154 return $checkbox;
155 }
156 }
157
162 public function getOptionsOOUI() {
163 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
164 // Sections make this difficult. See getInputOOUI().
165 throw new RuntimeException( __METHOD__ . ' is not supported' );
166 }
167
180 public function getInputOOUI( $value ) {
181 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
182
183 // Reject nested arrays (T274955)
184 $value = array_filter( $value, 'is_scalar' );
185
186 $hasSections = false;
187 $optionsOouiSections = [];
188 $options = $this->getOptions();
189 // If the options are supposed to be split into sections, each section becomes a separate
190 // CheckboxMultiselectInputWidget.
191 foreach ( $options as $label => $section ) {
192 if ( is_array( $section ) ) {
193 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
194 unset( $options[$label] );
195 $hasSections = true;
196 }
197 }
198 // If anything remains in the array, they are sectionless options. Put them in a separate widget
199 // at the beginning.
200 if ( $options ) {
201 $optionsOouiSections = array_merge(
202 [ '' => Html::listDropdownOptionsOoui( $options ) ],
203 $optionsOouiSections
204 );
205 }
206 '@phan-var array[][] $optionsOouiSections';
207
208 $out = [];
209 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
210 $attr = [];
211 $attr['name'] = "{$this->mName}[]";
212
213 $attr['value'] = $value;
214
215 $options = $optionsOoui;
216 foreach ( $options as &$option ) {
217 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
218 }
219 if ( $this->mOptionsLabelsNotFromMessage ) {
220 foreach ( $options as &$option ) {
221 $option['label'] = new \OOUI\HtmlSnippet( $option['label'] );
222 }
223 }
224 unset( $option );
225 $attr['options'] = $options;
226
227 $attr += \OOUI\Element::configFromHtmlAttributes(
228 $this->getAttributes( [ 'disabled', 'tabindex' ] )
229 );
230
231 if ( $this->mClass !== '' ) {
232 $attr['classes'] = [ $this->mClass ];
233 }
234
235 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
236 if ( $sectionLabel ) {
237 $out[] = new \OOUI\FieldsetLayout( [
238 'items' => [ $widget ],
239 // @phan-suppress-next-line SecurityCheck-XSS Key is html, taint cannot track that
240 'label' => new \OOUI\HtmlSnippet( $sectionLabel ),
241 ] );
242 } else {
243 $out[] = $widget;
244 }
245 }
246
247 if ( !$hasSections && $out ) {
248 if ( $this->mPlaceholder ) {
249 $out[0]->setData( ( $out[0]->getData() ?: [] ) + [
250 'placeholder' => $this->mPlaceholder,
251 ] );
252 }
253 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
254 // This allows it to be made infusable and later tweaked by JS code.
255 return $out[0];
256 }
257
258 return implode( '', $out );
259 }
260
267 public function loadDataFromRequest( $request ) {
268 $fromRequest = $request->getArray( $this->mName, [] );
269 // Fetch the value in either one of the two following case:
270 // - we have a valid submit attempt (form was just submitted)
271 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
272 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
273 // Checkboxes are just not added to the request arrays if they're not checked,
274 // so it's perfectly possible for there not to be an entry at all
275 // @phan-suppress-next-line PhanTypeMismatchReturnNullable getArray does not return null
276 return $fromRequest;
277 } else {
278 // That's ok, the user has not yet submitted the form, so show the defaults
279 return $this->getDefault();
280 }
281 }
282
287 public function getDefault() {
288 return $this->mDefault ?? [];
289 }
290
295 public function filterDataForSubmit( $data ) {
297 $options = HTMLFormField::flattenOptions( $this->getOptions() );
298 $forcedOn = array_intersect( $this->mParams['disabled-options'], $this->getDefault() );
299
300 $res = [];
301 foreach ( $options as $opt ) {
302 $res["$opt"] = in_array( $opt, $forcedOn, true ) || in_array( $opt, $data, true );
303 }
304
305 return $res;
306 }
307
312 protected function needsLabel() {
313 return false;
314 }
315}
316
318class_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
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Module of static functions for generating XML.
Definition Xml.php:33