Debian Codegraph @

includes/htmlform/fields/HTMLMultiSelectField.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\HTMLForm\Field;
4
5 use MediaWiki\Html\Html;
6 use MediaWiki\HTMLForm\HTMLFormField;
7 use MediaWiki\HTMLForm\HTMLNestedFilterable;
8 use MediaWiki\HTMLForm\OOUIHTMLForm;
9 use MediaWiki\Request\WebRequest;
10 use MediaWiki\Xml\Xml;
11 use RuntimeException;
12
13 /**
14 * Multi-select field
15 *
16 * @stable to extend
17 */
18 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
19 /** @var string */
20 private $mPlaceholder;
21
22 /**
23 * @stable to call
24 *
25 * @param array $params
26 * In adition to the usual HTMLFormField parameters, this can take the following fields:
27 * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
28 * can be used to filter them. This is desirable mostly for very long lists of options.
29 * This only works for users with JavaScript support and falls back to the list of checkboxes.
30 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
31 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
32 * for very short lists of concisely labelled options.
33 */
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
56 /**
57 * @inheritDoc
58 * @stable to override
59 */
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
86 /**
87 * @inheritDoc
88 * @stable to override
89 */
90 public function getInputHTML( $value ) {
91 $value = HTMLFormField::forceToStringRecursive( $value );
92 $html = $this->formatOptions( $this->getOptions(), $value );
93
94 return $html;
95 }
96
97 /**
98 * @stable to override
99 *
100 * @param array $options
101 * @param mixed $value
102 *
103 * @return string
104 */
105 public function formatOptions( $options, $value ) {
106 $html = '';
107
108 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
109
110 foreach ( $options as $label => $info ) {
111 if ( is_array( $info ) ) {
112 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
113 $html .= $this->formatOptions( $info, $value );
114 } else {
115 $thisAttribs = [
116 'id' => "{$this->mID}-$info",
117 'value' => $info,
118 ];
119 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
120 $thisAttribs['disabled'] = 'disabled';
121 }
122 $checked = in_array( $info, $value, true );
123
124 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
125
126 $html .= ' ' . Html::rawElement(
127 'div',
128 [ 'class' => 'mw-htmlform-flatlist-item' ],
129 $checkbox
130 );
131 }
132 }
133
134 return $html;
135 }
136
137 protected function getOneCheckbox( $checked, $attribs, $label ) {
138 if ( $this->mParent instanceof OOUIHTMLForm ) {
139 throw new RuntimeException( __METHOD__ . ' is not supported' );
140 } else {
141 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
142 $checkbox =
143 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
144 "\u{00A0}" .
145 call_user_func( $elementFunc,
146 'label',
147 [ 'for' => $attribs['id'] ],
148 $label
149 );
150 return $checkbox;
151 }
152 }
153
154 /**
155 * Get options and make them into arrays suitable for OOUI.
156 * @stable to override
157 */
158 public function getOptionsOOUI() {
159 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
160 // Sections make this difficult. See getInputOOUI().
161 throw new RuntimeException( __METHOD__ . ' is not supported' );
162 }
163
164 /**
165 * Get the OOUI version of this field.
166 *
167 * Returns OOUI\CheckboxMultiselectInputWidget for fields that only have one section,
168 * string otherwise.
169 *
170 * @stable to override
171 * @since 1.28
172 * @param string[] $value
173 * @return string|\OOUI\CheckboxMultiselectInputWidget
174 * @suppress PhanParamSignatureMismatch
175 */
176 public function getInputOOUI( $value ) {
177 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
178
179 // Reject nested arrays (T274955)
180 $value = array_filter( $value, 'is_scalar' );
181
182 $hasSections = false;
183 $optionsOouiSections = [];
184 $options = $this->getOptions();
185 // If the options are supposed to be split into sections, each section becomes a separate
186 // CheckboxMultiselectInputWidget.
187 foreach ( $options as $label => $section ) {
188 if ( is_array( $section ) ) {
189 $optionsOouiSections[ $label ] = Html::listDropdownOptionsOoui( $section );
190 unset( $options[$label] );
191 $hasSections = true;
192 }
193 }
194 // If anything remains in the array, they are sectionless options. Put them in a separate widget
195 // at the beginning.
196 if ( $options ) {
197 $optionsOouiSections = array_merge(
198 [ '' => Html::listDropdownOptionsOoui( $options ) ],
199 $optionsOouiSections
200 );
201 }
202 '@phan-var array[][] $optionsOouiSections';
203
204 $out = [];
205 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
206 $attr = [];
207 $attr['name'] = "{$this->mName}[]";
208
209 $attr['value'] = $value;
210
211 $options = $optionsOoui;
212 foreach ( $options as &$option ) {
213 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
214 }
215 if ( $this->mOptionsLabelsNotFromMessage ) {
216 foreach ( $options as &$option ) {
217 $option['label'] = new \OOUI\HtmlSnippet( $option['label'] );
218 }
219 }
220 unset( $option );
221 $attr['options'] = $options;
222
223 $attr += \OOUI\Element::configFromHtmlAttributes(
224 $this->getAttributes( [ 'disabled', 'tabindex' ] )
225 );
226
227 if ( $this->mClass !== '' ) {
228 $attr['classes'] = [ $this->mClass ];
229 }
230
231 $widget = new \OOUI\CheckboxMultiselectInputWidget( $attr );
232 if ( $sectionLabel ) {
233 if ( $this->mOptionsLabelsNotFromMessage ) {
234 // @phan-suppress-next-line SecurityCheck-XSS Can't track conditional escaping via a property
235 $sectionLabel = new \OOUI\HtmlSnippet( $sectionLabel );
236 }
237 $out[] = new \OOUI\FieldsetLayout( [
238 'items' => [ $widget ],
239 'label' => $sectionLabel,
240 ] );
241 } else {
242 $out[] = $widget;
243 }
244 }
245
246 if ( !$hasSections && $out ) {
247 if ( $this->mPlaceholder ) {
248 $out[0]->setData( ( $out[0]->getData() ?: [] ) + [
249 'placeholder' => $this->mPlaceholder,
250 ] );
251 }
252 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
253 // This allows it to be made infusable and later tweaked by JS code.
254 return $out[0];
255 }
256
257 return implode( '', $out );
258 }
259
260 /**
261 * @stable to override
262 * @param WebRequest $request
263 *
264 * @return string|array
265 */
266 public function loadDataFromRequest( $request ) {
267 $fromRequest = $request->getArray( $this->mName, [] );
268 // Fetch the value in either one of the two following case:
269 // - we have a valid submit attempt (form was just submitted)
270 // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
271 if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
272 // Checkboxes are just not added to the request arrays if they're not checked,
273 // so it's perfectly possible for there not to be an entry at all
274 // @phan-suppress-next-line PhanTypeMismatchReturnNullable getArray does not return null
275 return $fromRequest;
276 } else {
277 // That's ok, the user has not yet submitted the form, so show the defaults
278 return $this->getDefault();
279 }
280 }
281
282 /**
283 * @inheritDoc
284 * @stable to override
285 */
286 public function getDefault() {
287 return $this->mDefault ?? [];
288 }
289
290 /**
291 * @inheritDoc
292 * @stable to override
293 */
294 public function filterDataForSubmit( $data ) {
295 $data = HTMLFormField::forceToStringRecursive( $data );
296 $options = HTMLFormField::flattenOptions( $this->getOptions() );
297 $forcedOn = array_intersect( $this->mParams['disabled-options'], $this->getDefault() );
298
299 $res = [];
300 foreach ( $options as $opt ) {
301 $res["$opt"] = in_array( $opt, $forcedOn, true ) || in_array( $opt, $data, true );
302 }
303
304 return $res;
305 }
306
307 /**
308 * @inheritDoc
309 * @stable to override
310 */
311 protected function needsLabel() {
312 return false;
313 }
314 }
315
316 /** @deprecated class alias since 1.42 */
317 class_alias( HTMLMultiSelectField::class, 'HTMLMultiSelectField' );