MediaWiki  master
HTMLMultiSelectField.php
Go to the documentation of this file.
1 <?php
2 
4 
11  /* @var string */
12  private $mPlaceholder;
13 
26  public function __construct( $params ) {
27  parent::__construct( $params );
28 
29  // If the disabled-options parameter is not provided, use an empty array
30  if ( !isset( $this->mParams['disabled-options'] ) ) {
31  $this->mParams['disabled-options'] = [];
32  }
33 
34  if ( isset( $params['dropdown'] ) ) {
35  $this->mClass .= ' mw-htmlform-dropdown';
36  if ( isset( $params['placeholder'] ) ) {
37  $this->mPlaceholder = $params['placeholder'];
38  } elseif ( isset( $params['placeholder-message'] ) ) {
39  $this->mPlaceholder = $this->msg( $params['placeholder-message'] )->text();
40  }
41  }
42 
43  if ( isset( $params['flatlist'] ) ) {
44  $this->mClass .= ' mw-htmlform-flatlist';
45  }
46  }
47 
52  public function validate( $value, $alldata ) {
53  $p = parent::validate( $value, $alldata );
54 
55  if ( $p !== true ) {
56  return $p;
57  }
58 
59  if ( !is_array( $value ) ) {
60  return false;
61  }
62 
63  // Reject nested arrays (T274955)
64  $value = array_filter( $value, 'is_scalar' );
65 
66  # If all options are valid, array_intersect of the valid options
67  # and the provided options will return the provided options.
68  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
69 
70  $validValues = array_intersect( $value, $validOptions );
71  if ( count( $validValues ) == count( $value ) ) {
72  return true;
73  } else {
74  return $this->msg( 'htmlform-select-badoption' );
75  }
76  }
77 
82  public function getInputHTML( $value ) {
83  if ( isset( $this->mParams['dropdown'] ) ) {
84  $this->mParent->getOutput()->addModules( 'jquery.chosen' );
85  }
86 
87  $value = HTMLFormField::forceToStringRecursive( $value );
88  $html = $this->formatOptions( $this->getOptions(), $value );
89 
90  return $html;
91  }
92 
102  public function formatOptions( $options, $value ) {
103  $html = '';
104 
105  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
106 
107  foreach ( $options as $label => $info ) {
108  if ( is_array( $info ) ) {
109  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
110  $html .= $this->formatOptions( $info, $value );
111  } else {
112  $thisAttribs = [
113  'id' => "{$this->mID}-$info",
114  'value' => $info,
115  ];
116  if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
117  $thisAttribs['disabled'] = 'disabled';
118  }
119  $checked = in_array( $info, $value, true );
120 
121  $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
122 
123  $html .= ' ' . Html::rawElement(
124  'div',
125  [ 'class' => 'mw-htmlform-flatlist-item' ],
126  $checkbox
127  );
128  }
129  }
130 
131  return $html;
132  }
133 
134  protected function getOneCheckbox( $checked, $attribs, $label ) {
135  if ( $this->mParent instanceof OOUIHTMLForm ) {
136  throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
137  } else {
138  $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
139  $checkbox =
140  Xml::check( "{$this->mName}[]", $checked, $attribs ) .
141  "\u{00A0}" .
142  call_user_func( $elementFunc,
143  'label',
144  [ 'for' => $attribs['id'] ],
145  $label
146  );
147  if ( $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere ) ) {
148  $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
149  $checkbox .
150  Html::closeElement( 'div' );
151  }
152  return $checkbox;
153  }
154  }
155 
161  public function getOptionsOOUI() {
162  // @phan-suppress-previous-line PhanPluginNeverReturnMethod
163  // Sections make this difficult. See getInputOOUI().
164  throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
165  }
166 
179  public function getInputOOUI( $value ) {
180  $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
181 
182  // Reject nested arrays (T274955)
183  $value = array_filter( $value, 'is_scalar' );
184 
185  $hasSections = false;
186  $optionsOouiSections = [];
187  $options = $this->getOptions();
188  // If the options are supposed to be split into sections, each section becomes a separate
189  // CheckboxMultiselectInputWidget.
190  foreach ( $options as $label => $section ) {
191  if ( is_array( $section ) ) {
192  $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
193  unset( $options[$label] );
194  $hasSections = true;
195  }
196  }
197  // If anything remains in the array, they are sectionless options. Put them in a separate widget
198  // at the beginning.
199  if ( $options ) {
200  $optionsOouiSections = array_merge(
201  [ '' => Xml::listDropDownOptionsOoui( $options ) ],
202  $optionsOouiSections
203  );
204  }
205  '@phan-var array[][] $optionsOouiSections';
206 
207  $out = [];
208  foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
209  $attr = [];
210  $attr['name'] = "{$this->mName}[]";
211 
212  $attr['value'] = $value;
213 
214  $options = $optionsOoui;
215  foreach ( $options as &$option ) {
216  $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
217  }
218  if ( $this->mOptionsLabelsNotFromMessage ) {
219  foreach ( $options as &$option ) {
220  // @phan-suppress-next-line SecurityCheck-XSS Labels are raw when not from message
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 ) {
296  $data = HTMLFormField::forceToStringRecursive( $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 }
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()
Stability: stableto override mixed
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:256
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:320
MediaWiki exception.
Definition: MWException.php:31
A class containing constants representing the names of configuration variables.
Compact stacked vertical format for forms, implemented using OOUI widgets.
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition: Xml.php:600
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:332