MediaWiki  master
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  // 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 
77  $value = HTMLFormField::forceToStringRecursive( $value );
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  // Sections make this difficult. See getInputOOUI().
153  throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
154  }
155 
168  public function getInputOOUI( $value ) {
169  $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
170 
171  // Reject nested arrays (T274955)
172  $value = array_filter( $value, 'is_scalar' );
173 
174  $hasSections = false;
175  $optionsOouiSections = [];
176  $options = $this->getOptions();
177  // If the options are supposed to be split into sections, each section becomes a separate
178  // CheckboxMultiselectInputWidget.
179  foreach ( $options as $label => $section ) {
180  if ( is_array( $section ) ) {
181  $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
182  unset( $options[$label] );
183  $hasSections = true;
184  }
185  }
186  // If anything remains in the array, they are sectionless options. Put them in a separate widget
187  // at the beginning.
188  if ( $options ) {
189  $optionsOouiSections = array_merge(
190  [ '' => Xml::listDropDownOptionsOoui( $options ) ],
191  $optionsOouiSections
192  );
193  }
194  '@phan-var array[][] $optionsOouiSections';
195 
196  $out = [];
197  foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
198  $attr = [];
199  $attr['name'] = "{$this->mName}[]";
200 
201  $attr['value'] = $value;
202 
203  $options = $optionsOoui;
204  foreach ( $options as &$option ) {
205  $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
206  }
207  if ( $this->mOptionsLabelsNotFromMessage ) {
208  foreach ( $options as &$option ) {
209  // @phan-suppress-next-line SecurityCheck-XSS Labels are raw when not from message
210  $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
211  }
212  }
213  unset( $option );
214  $attr['options'] = $options;
215 
216  $attr += OOUI\Element::configFromHtmlAttributes(
217  $this->getAttributes( [ 'disabled', 'tabindex' ] )
218  );
219 
220  if ( $this->mClass !== '' ) {
221  $attr['classes'] = [ $this->mClass ];
222  }
223 
224  $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
225  if ( $sectionLabel ) {
226  $out[] = new OOUI\FieldsetLayout( [
227  'items' => [ $widget ],
228  // @phan-suppress-next-line SecurityCheck-XSS Key is html, taint cannot track that
229  'label' => new OOUI\HtmlSnippet( $sectionLabel ),
230  ] );
231  } else {
232  $out[] = $widget;
233  }
234  }
235 
236  if ( !$hasSections && $out ) {
237  // Directly return the only OOUI\CheckboxMultiselectInputWidget.
238  // This allows it to be made infusable and later tweaked by JS code.
239  return $out[ 0 ];
240  }
241 
242  return implode( '', $out );
243  }
244 
251  public function loadDataFromRequest( $request ) {
252  $fromRequest = $request->getArray( $this->mName, [] );
253  // Fetch the value in either one of the two following case:
254  // - we have a valid submit attempt (form was just submitted)
255  // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
256  if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
257  // Checkboxes are just not added to the request arrays if they're not checked,
258  // so it's perfectly possible for there not to be an entry at all
259  return $fromRequest;
260  } else {
261  // That's ok, the user has not yet submitted the form, so show the defaults
262  return $this->getDefault();
263  }
264  }
265 
270  public function getDefault() {
271  return $this->mDefault ?? [];
272  }
273 
278  public function filterDataForSubmit( $data ) {
279  $data = HTMLFormField::forceToStringRecursive( $data );
280  $options = HTMLFormField::flattenOptions( $this->getOptions() );
281 
282  $res = [];
283  foreach ( $options as $opt ) {
284  $res["$opt"] = in_array( $opt, $data, true );
285  }
286 
287  return $res;
288  }
289 
294  protected function needsLabel() {
295  return false;
296  }
297 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1095
HTMLMultiSelectField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLMultiSelectField.php:251
HTMLMultiSelectField\needsLabel
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
Definition: HTMLMultiSelectField.php:294
HTMLMultiSelectField\getDefault
getDefault()
Stability: stableto override mixed
Definition: HTMLMultiSelectField.php:270
HTMLMultiSelectField\getInputOOUI
getInputOOUI( $value)
Get the OOUI version of this field.
Definition: HTMLMultiSelectField.php:168
Xml\listDropDownOptionsOoui
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition: Xml.php:594
$res
$res
Definition: testCompression.php:57
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:19
HTMLMultiSelectField\filterDataForSubmit
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
Definition: HTMLMultiSelectField.php:278
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:29
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:318
HTMLMultiSelectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
Definition: HTMLMultiSelectField.php:42
MWException
MediaWiki exception.
Definition: MWException.php:29
Xml\check
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:329
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:9
HTMLMultiSelectField\getOneCheckbox
getOneCheckbox( $checked, $attribs, $label)
Definition: HTMLMultiSelectField.php:124
HTMLMultiSelectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
Definition: HTMLMultiSelectField.php:72
HTMLNestedFilterable
Definition: HTMLNestedFilterable.php:3
HTMLMultiSelectField
Multi-select field.
Definition: HTMLMultiSelectField.php:8
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:254
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
HTMLFormField\forceToStringRecursive
static forceToStringRecursive( $array)
Recursively forces values in an array to strings, because issues arise with integer 0 as a value.
Definition: HTMLFormField.php:1081
HTMLFormField\isSubmitAttempt
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
Definition: HTMLFormField.php:378
HTMLMultiSelectField\formatOptions
formatOptions( $options, $value)
Definition: HTMLMultiSelectField.php:92
HTMLMultiSelectField\__construct
__construct( $params)
Definition: HTMLMultiSelectField.php:21
HTMLFormField\flattenOptions
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
Definition: HTMLFormField.php:1135
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:87
HTMLMultiSelectField\getOptionsOOUI
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Definition: HTMLMultiSelectField.php:151
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:1039