MediaWiki REL1_37
HTMLMultiSelectField.php
Go to the documentation of this file.
1<?php
2
21 public function __construct( $params ) {
22 parent::__construct( $params );
23
24 // If the disabled-options parameter is not provided, use an empty array
25 if ( !isset( $this->mParams['disabled-options'] ) ) {
26 $this->mParams['disabled-options'] = [];
27 }
28
29 if ( isset( $params['dropdown'] ) ) {
30 $this->mClass .= ' mw-htmlform-dropdown';
31 }
32
33 if ( isset( $params['flatlist'] ) ) {
34 $this->mClass .= ' mw-htmlform-flatlist';
35 }
36 }
37
42 public function validate( $value, $alldata ) {
43 $p = parent::validate( $value, $alldata );
44
45 if ( $p !== true ) {
46 return $p;
47 }
48
49 if ( !is_array( $value ) ) {
50 return false;
51 }
52
53 // Reject nested arrays (T274955)
54 $value = array_filter( $value, 'is_scalar' );
55
56 # If all options are valid, array_intersect of the valid options
57 # and the provided options will return the provided options.
58 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
59
60 $validValues = array_intersect( $value, $validOptions );
61 if ( count( $validValues ) == count( $value ) ) {
62 return true;
63 } else {
64 return $this->msg( 'htmlform-select-badoption' );
65 }
66 }
67
72 public function getInputHTML( $value ) {
73 if ( isset( $this->mParams['dropdown'] ) ) {
74 $this->mParent->getOutput()->addModules( 'jquery.chosen' );
75 }
76
78 $html = $this->formatOptions( $this->getOptions(), $value );
79
80 return $html;
81 }
82
92 public function formatOptions( $options, $value ) {
93 $html = '';
94
95 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
96
97 foreach ( $options as $label => $info ) {
98 if ( is_array( $info ) ) {
99 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
100 $html .= $this->formatOptions( $info, $value );
101 } else {
102 $thisAttribs = [
103 'id' => "{$this->mID}-$info",
104 'value' => $info,
105 ];
106 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
107 $thisAttribs['disabled'] = 'disabled';
108 }
109 $checked = in_array( $info, $value, true );
110
111 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
112
113 $html .= ' ' . Html::rawElement(
114 'div',
115 [ 'class' => 'mw-htmlform-flatlist-item' ],
116 $checkbox
117 );
118 }
119 }
120
121 return $html;
122 }
123
124 protected function getOneCheckbox( $checked, $attribs, $label ) {
125 if ( $this->mParent instanceof OOUIHTMLForm ) {
126 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
127 } else {
128 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
129 $checkbox =
130 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
131 "\u{00A0}" .
132 call_user_func( $elementFunc,
133 'label',
134 [ 'for' => $attribs['id'] ],
135 $label
136 );
137 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
138 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
139 $checkbox .
140 Html::closeElement( 'div' );
141 }
142 return $checkbox;
143 }
144 }
145
151 public function getOptionsOOUI() {
152 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
153 // Sections make this difficult. See getInputOOUI().
154 throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
155 }
156
169 public function getInputOOUI( $value ) {
170 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
171
172 // Reject nested arrays (T274955)
173 $value = array_filter( $value, 'is_scalar' );
174
175 $hasSections = false;
176 $optionsOouiSections = [];
177 $options = $this->getOptions();
178 // If the options are supposed to be split into sections, each section becomes a separate
179 // CheckboxMultiselectInputWidget.
180 foreach ( $options as $label => $section ) {
181 if ( is_array( $section ) ) {
182 $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
183 unset( $options[$label] );
184 $hasSections = true;
185 }
186 }
187 // If anything remains in the array, they are sectionless options. Put them in a separate widget
188 // at the beginning.
189 if ( $options ) {
190 $optionsOouiSections = array_merge(
191 [ '' => Xml::listDropDownOptionsOoui( $options ) ],
192 $optionsOouiSections
193 );
194 }
195 '@phan-var array[][] $optionsOouiSections';
196
197 $out = [];
198 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
199 $attr = [];
200 $attr['name'] = "{$this->mName}[]";
201
202 $attr['value'] = $value;
203
204 $options = $optionsOoui;
205 foreach ( $options as &$option ) {
206 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
207 }
208 if ( $this->mOptionsLabelsNotFromMessage ) {
209 foreach ( $options as &$option ) {
210 // @phan-suppress-next-line SecurityCheck-XSS Labels are raw when not from message
211 $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
212 }
213 }
214 unset( $option );
215 $attr['options'] = $options;
216
217 $attr += OOUI\Element::configFromHtmlAttributes(
218 $this->getAttributes( [ 'disabled', 'tabindex' ] )
219 );
220
221 if ( $this->mClass !== '' ) {
222 $attr['classes'] = [ $this->mClass ];
223 }
224
225 $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
226 if ( $sectionLabel ) {
227 $out[] = new OOUI\FieldsetLayout( [
228 'items' => [ $widget ],
229 // @phan-suppress-next-line SecurityCheck-XSS Key is html, taint cannot track that
230 'label' => new OOUI\HtmlSnippet( $sectionLabel ),
231 ] );
232 } else {
233 $out[] = $widget;
234 }
235 }
236
237 if ( !$hasSections && $out ) {
238 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
239 // This allows it to be made infusable and later tweaked by JS code.
240 return $out[ 0 ];
241 }
242
243 return implode( '', $out );
244 }
245
252 public function loadDataFromRequest( $request ) {
253 $fromRequest = $request->getArray( $this->mName, [] );
254 // Fetch the value in either one of the two following case:
255 // - we have a valid submit attempt (form was just submitted)
256 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
257 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
258 // Checkboxes are just not added to the request arrays if they're not checked,
259 // so it's perfectly possible for there not to be an entry at all
260 return $fromRequest;
261 } else {
262 // That's ok, the user has not yet submitted the form, so show the defaults
263 return $this->getDefault();
264 }
265 }
266
271 public function getDefault() {
272 return $this->mDefault ?? [];
273 }
274
279 public function filterDataForSubmit( $data ) {
281 $options = HTMLFormField::flattenOptions( $this->getOptions() );
282
283 $res = [];
284 foreach ( $options as $opt ) {
285 $res["$opt"] = in_array( $opt, $data, true );
286 }
287
288 return $res;
289 }
290
295 protected function needsLabel() {
296 return false;
297 }
298}
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>...
getOptions()
Fetch the array of options from the field's parameters.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
msg( $key,... $params)
Get a translated interface message.
getAttributes(array $list)
Returns the given attributes from the parameters.
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getOneCheckbox( $checked, $attribs, $label)
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
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.
formatOptions( $options, $value)
getDefault()
to override mixed
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
MediaWiki exception.
Compact stacked vertical format for forms, implemented using OOUI widgets.