MediaWiki  master
HTMLMultiSelectField.php
Go to the documentation of this file.
1 <?php
2 
17  public function __construct( $params ) {
18  parent::__construct( $params );
19 
20  // If the disabled-options parameter is not provided, use an empty array
21  if ( isset( $this->mParams['disabled-options'] ) === false ) {
22  $this->mParams['disabled-options'] = [];
23  }
24 
25  if ( isset( $params['dropdown'] ) ) {
26  $this->mClass .= ' mw-htmlform-dropdown';
27  }
28 
29  if ( isset( $params['flatlist'] ) ) {
30  $this->mClass .= ' mw-htmlform-flatlist';
31  }
32  }
33 
34  public function validate( $value, $alldata ) {
35  $p = parent::validate( $value, $alldata );
36 
37  if ( $p !== true ) {
38  return $p;
39  }
40 
41  if ( !is_array( $value ) ) {
42  return false;
43  }
44 
45  # If all options are valid, array_intersect of the valid options
46  # and the provided options will return the provided options.
47  $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
48 
49  $validValues = array_intersect( $value, $validOptions );
50  if ( count( $validValues ) == count( $value ) ) {
51  return true;
52  } else {
53  return $this->msg( 'htmlform-select-badoption' );
54  }
55  }
56 
57  public function getInputHTML( $value ) {
58  if ( isset( $this->mParams['dropdown'] ) ) {
59  $this->mParent->getOutput()->addModules( 'jquery.chosen' );
60  }
61 
62  $value = HTMLFormField::forceToStringRecursive( $value );
63  $html = $this->formatOptions( $this->getOptions(), $value );
64 
65  return $html;
66  }
67 
68  public function formatOptions( $options, $value ) {
69  $html = '';
70 
71  $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
72 
73  foreach ( $options as $label => $info ) {
74  if ( is_array( $info ) ) {
75  $html .= Html::rawElement( 'h1', [], $label ) . "\n";
76  $html .= $this->formatOptions( $info, $value );
77  } else {
78  $thisAttribs = [
79  'id' => "{$this->mID}-$info",
80  'value' => $info,
81  ];
82  if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
83  $thisAttribs['disabled'] = 'disabled';
84  }
85  $checked = in_array( $info, $value, true );
86 
87  $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
88 
89  $html .= ' ' . Html::rawElement(
90  'div',
91  [ 'class' => 'mw-htmlform-flatlist-item' ],
92  $checkbox
93  );
94  }
95  }
96 
97  return $html;
98  }
99 
100  protected function getOneCheckbox( $checked, $attribs, $label ) {
101  if ( $this->mParent instanceof OOUIHTMLForm ) {
102  throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
103  } else {
104  $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
105  $checkbox =
106  Xml::check( "{$this->mName}[]", $checked, $attribs ) .
107  "\u{00A0}" .
108  call_user_func( $elementFunc,
109  'label',
110  [ 'for' => $attribs['id'] ],
111  $label
112  );
113  if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
114  $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
115  $checkbox .
116  Html::closeElement( 'div' );
117  }
118  return $checkbox;
119  }
120  }
121 
126  public function getOptionsOOUI() {
127  // Sections make this difficult. See getInputOOUI().
128  throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
129  }
130 
142  public function getInputOOUI( $value ) {
143  $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
144 
145  $hasSections = false;
146  $optionsOouiSections = [];
147  $options = $this->getOptions();
148  // If the options are supposed to be split into sections, each section becomes a separate
149  // CheckboxMultiselectInputWidget.
150  foreach ( $options as $label => $section ) {
151  if ( is_array( $section ) ) {
152  $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
153  unset( $options[$label] );
154  $hasSections = true;
155  }
156  }
157  // If anything remains in the array, they are sectionless options. Put them in a separate widget
158  // at the beginning.
159  if ( $options ) {
160  $optionsOouiSections = array_merge(
161  [ '' => Xml::listDropDownOptionsOoui( $options ) ],
162  $optionsOouiSections
163  );
164  }
165  '@phan-var array[][] $optionsOouiSections';
166 
167  $out = [];
168  foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
169  $attr = [];
170  $attr['name'] = "{$this->mName}[]";
171 
172  $attr['value'] = $value;
173 
174  $options = $optionsOoui;
175  foreach ( $options as &$option ) {
176  $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
177  }
178  if ( $this->mOptionsLabelsNotFromMessage ) {
179  foreach ( $options as &$option ) {
180  $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
181  }
182  }
183  unset( $option );
184  $attr['options'] = $options;
185 
186  $attr += OOUI\Element::configFromHtmlAttributes(
187  $this->getAttributes( [ 'disabled', 'tabindex' ] )
188  );
189 
190  if ( $this->mClass !== '' ) {
191  $attr['classes'] = [ $this->mClass ];
192  }
193 
194  $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
195  if ( $sectionLabel ) {
196  $out[] = new OOUI\FieldsetLayout( [
197  'items' => [ $widget ],
198  'label' => new OOUI\HtmlSnippet( $sectionLabel ),
199  ] );
200  } else {
201  $out[] = $widget;
202  }
203  }
204 
205  if ( !$hasSections ) {
206  // Directly return the only OOUI\CheckboxMultiselectInputWidget.
207  // This allows it to be made infusable and later tweaked by JS code.
208  return $out[ 0 ];
209  }
210 
211  return implode( '', $out );
212  }
213 
219  public function loadDataFromRequest( $request ) {
220  $fromRequest = $request->getArray( $this->mName, [] );
221  // Fetch the value in either one of the two following case:
222  // - we have a valid submit attempt (form was just submitted)
223  // - we have a value (an URL manually built by the user, or GET form with no wpFormIdentifier)
224  if ( $this->isSubmitAttempt( $request ) || $fromRequest ) {
225  // Checkboxes are just not added to the request arrays if they're not checked,
226  // so it's perfectly possible for there not to be an entry at all
227  return $fromRequest;
228  } else {
229  // That's ok, the user has not yet submitted the form, so show the defaults
230  return $this->getDefault();
231  }
232  }
233 
234  public function getDefault() {
235  return $this->mDefault ?? [];
236  }
237 
238  public function filterDataForSubmit( $data ) {
239  $data = HTMLFormField::forceToStringRecursive( $data );
240  $options = HTMLFormField::flattenOptions( $this->getOptions() );
241 
242  $res = [];
243  foreach ( $options as $opt ) {
244  $res["$opt"] = in_array( $opt, $data, true );
245  }
246 
247  return $res;
248  }
249 
250  protected function needsLabel() {
251  return false;
252  }
253 }
HTMLFormField\getOptions
getOptions()
Fetch the array of options from the field's parameters.
Definition: HTMLFormField.php:1055
HTMLMultiSelectField\loadDataFromRequest
loadDataFromRequest( $request)
Definition: HTMLMultiSelectField.php:219
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:250
HTMLMultiSelectField\getDefault
getDefault()
Definition: HTMLMultiSelectField.php:234
HTMLMultiSelectField\getInputOOUI
getInputOOUI( $value)
Get the OOUI version of this field.
Definition: HTMLMultiSelectField.php:142
Xml\listDropDownOptionsOoui
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
Definition: Xml.php:585
$res
$res
Definition: testCompression.php:57
HTMLFormField\$mClass
$mClass
Definition: HTMLFormField.php:17
HTMLMultiSelectField\filterDataForSubmit
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
Definition: HTMLMultiSelectField.php:238
OOUIHTMLForm
Compact stacked vertical format for forms, implemented using OOUI widgets.
Definition: OOUIHTMLForm.php:27
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
HTMLMultiSelectField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLMultiSelectField.php:34
MWException
MediaWiki exception.
Definition: MWException.php:26
Xml\check
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.
Definition: Xml.php:327
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
HTMLMultiSelectField\getOneCheckbox
getOneCheckbox( $checked, $attribs, $label)
Definition: HTMLMultiSelectField.php:100
HTMLMultiSelectField\getInputHTML
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself.
Definition: HTMLMultiSelectField.php:57
HTMLNestedFilterable
Definition: HTMLNestedFilterable.php:3
HTMLMultiSelectField
Multi-select field.
Definition: HTMLMultiSelectField.php:6
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:251
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
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:1041
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:362
HTMLMultiSelectField\formatOptions
formatOptions( $options, $value)
Definition: HTMLMultiSelectField.php:68
HTMLMultiSelectField\__construct
__construct( $params)
Definition: HTMLMultiSelectField.php:17
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:1094
HTMLFormField\msg
msg( $key,... $params)
Get a translated interface message.
Definition: HTMLFormField.php:83
HTMLMultiSelectField\getOptionsOOUI
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
Definition: HTMLMultiSelectField.php:126
HTMLFormField\getAttributes
getAttributes(array $list)
Returns the given attributes from the parameters.
Definition: HTMLFormField.php:999