MediaWiki REL1_35
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'] ) === false ) {
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 # If all options are valid, array_intersect of the valid options
54 # and the provided options will return the provided options.
55 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
56
57 $validValues = array_intersect( $value, $validOptions );
58 if ( count( $validValues ) == count( $value ) ) {
59 return true;
60 } else {
61 return $this->msg( 'htmlform-select-badoption' );
62 }
63 }
64
69 public function getInputHTML( $value ) {
70 if ( isset( $this->mParams['dropdown'] ) ) {
71 $this->mParent->getOutput()->addModules( 'jquery.chosen' );
72 }
73
75 $html = $this->formatOptions( $this->getOptions(), $value );
76
77 return $html;
78 }
79
89 public function formatOptions( $options, $value ) {
90 $html = '';
91
92 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
93
94 foreach ( $options as $label => $info ) {
95 if ( is_array( $info ) ) {
96 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
97 $html .= $this->formatOptions( $info, $value );
98 } else {
99 $thisAttribs = [
100 'id' => "{$this->mID}-$info",
101 'value' => $info,
102 ];
103 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
104 $thisAttribs['disabled'] = 'disabled';
105 }
106 $checked = in_array( $info, $value, true );
107
108 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
109
110 $html .= ' ' . Html::rawElement(
111 'div',
112 [ 'class' => 'mw-htmlform-flatlist-item' ],
113 $checkbox
114 );
115 }
116 }
117
118 return $html;
119 }
120
121 protected function getOneCheckbox( $checked, $attribs, $label ) {
122 if ( $this->mParent instanceof OOUIHTMLForm ) {
123 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
124 } else {
125 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
126 $checkbox =
127 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
128 "\u{00A0}" .
129 call_user_func( $elementFunc,
130 'label',
131 [ 'for' => $attribs['id'] ],
132 $label
133 );
134 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
135 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
136 $checkbox .
137 Html::closeElement( 'div' );
138 }
139 return $checkbox;
140 }
141 }
142
148 public function getOptionsOOUI() {
149 // Sections make this difficult. See getInputOOUI().
150 throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
151 }
152
165 public function getInputOOUI( $value ) {
166 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
167
168 $hasSections = false;
169 $optionsOouiSections = [];
170 $options = $this->getOptions();
171 // If the options are supposed to be split into sections, each section becomes a separate
172 // CheckboxMultiselectInputWidget.
173 foreach ( $options as $label => $section ) {
174 if ( is_array( $section ) ) {
175 $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
176 unset( $options[$label] );
177 $hasSections = true;
178 }
179 }
180 // If anything remains in the array, they are sectionless options. Put them in a separate widget
181 // at the beginning.
182 if ( $options ) {
183 $optionsOouiSections = array_merge(
184 [ '' => Xml::listDropDownOptionsOoui( $options ) ],
185 $optionsOouiSections
186 );
187 }
188 '@phan-var array[][] $optionsOouiSections';
189
190 $out = [];
191 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
192 $attr = [];
193 $attr['name'] = "{$this->mName}[]";
194
195 $attr['value'] = $value;
196
197 $options = $optionsOoui;
198 foreach ( $options as &$option ) {
199 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
200 }
201 if ( $this->mOptionsLabelsNotFromMessage ) {
202 foreach ( $options as &$option ) {
203 $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
204 }
205 }
206 unset( $option );
207 $attr['options'] = $options;
208
209 $attr += OOUI\Element::configFromHtmlAttributes(
210 $this->getAttributes( [ 'disabled', 'tabindex' ] )
211 );
212
213 if ( $this->mClass !== '' ) {
214 $attr['classes'] = [ $this->mClass ];
215 }
216
217 $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
218 if ( $sectionLabel ) {
219 $out[] = new OOUI\FieldsetLayout( [
220 'items' => [ $widget ],
221 'label' => new OOUI\HtmlSnippet( $sectionLabel ),
222 ] );
223 } else {
224 $out[] = $widget;
225 }
226 }
227
228 if ( !$hasSections && $out ) {
229 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
230 // This allows it to be made infusable and later tweaked by JS code.
231 return $out[ 0 ];
232 }
233
234 return implode( '', $out );
235 }
236
243 public function loadDataFromRequest( $request ) {
244 $fromRequest = $request->getArray( $this->mName, [] );
245 // Fetch the value in either one of the two following case:
246 // - we have a valid submit attempt (form was just submitted)
247 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
248 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
249 // Checkboxes are just not added to the request arrays if they're not checked,
250 // so it's perfectly possible for there not to be an entry at all
251 return $fromRequest;
252 } else {
253 // That's ok, the user has not yet submitted the form, so show the defaults
254 return $this->getDefault();
255 }
256 }
257
262 public function getDefault() {
263 return $this->mDefault ?? [];
264 }
265
270 public function filterDataForSubmit( $data ) {
272 $options = HTMLFormField::flattenOptions( $this->getOptions() );
273
274 $res = [];
275 foreach ( $options as $opt ) {
276 $res["$opt"] = in_array( $opt, $data, true );
277 }
278
279 return $res;
280 }
281
286 protected function needsLabel() {
287 return false;
288 }
289}
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 Stable to override.
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...
__construct( $params)
Stable to call.
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)
Stable to override.
getDefault()
Stable to override.mixed Stable to override
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
loadDataFromRequest( $request)
Stable to override.
MediaWiki exception.
Compact stacked vertical format for forms, implemented using OOUI widgets.