MediaWiki REL1_31
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 // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
26 if ( isset( $params['dropdown'] ) || strpos( $this->mClass, 'mw-chosen' ) !== false ) {
27 $this->mClass .= ' mw-htmlform-dropdown';
28 }
29
30 if ( isset( $params['flatlist'] ) ) {
31 $this->mClass .= ' mw-htmlform-flatlist';
32 }
33 }
34
35 public function validate( $value, $alldata ) {
36 $p = parent::validate( $value, $alldata );
37
38 if ( $p !== true ) {
39 return $p;
40 }
41
42 if ( !is_array( $value ) ) {
43 return false;
44 }
45
46 # If all options are valid, array_intersect of the valid options
47 # and the provided options will return the provided options.
48 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
49
50 $validValues = array_intersect( $value, $validOptions );
51 if ( count( $validValues ) == count( $value ) ) {
52 return true;
53 } else {
54 return $this->msg( 'htmlform-select-badoption' );
55 }
56 }
57
58 public function getInputHTML( $value ) {
59 if ( isset( $this->mParams['dropdown'] ) ) {
60 $this->mParent->getOutput()->addModules( 'jquery.chosen' );
61 }
62
64 $html = $this->formatOptions( $this->getOptions(), $value );
65
66 return $html;
67 }
68
69 public function formatOptions( $options, $value ) {
70 $html = '';
71
72 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
73
74 foreach ( $options as $label => $info ) {
75 if ( is_array( $info ) ) {
76 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
77 $html .= $this->formatOptions( $info, $value );
78 } else {
79 $thisAttribs = [
80 'id' => "{$this->mID}-$info",
81 'value' => $info,
82 ];
83 if ( in_array( $info, $this->mParams['disabled-options'], true ) ) {
84 $thisAttribs['disabled'] = 'disabled';
85 }
86 $checked = in_array( $info, $value, true );
87
88 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
89
90 $html .= ' ' . Html::rawElement(
91 'div',
92 [ 'class' => 'mw-htmlform-flatlist-item' ],
93 $checkbox
94 );
95 }
96 }
97
98 return $html;
99 }
100
101 protected function getOneCheckbox( $checked, $attribs, $label ) {
102 if ( $this->mParent instanceof OOUIHTMLForm ) {
103 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
104 } else {
105 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
106 $checkbox =
107 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
108 '&#160;' .
109 call_user_func( $elementFunc,
110 'label',
111 [ 'for' => $attribs['id'] ],
112 $label
113 );
114 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
115 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
116 $checkbox .
117 Html::closeElement( 'div' );
118 }
119 return $checkbox;
120 }
121 }
122
127 public function getOptionsOOUI() {
128 // Sections make this difficult. See getInputOOUI().
129 throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
130 }
131
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
166 $out = [];
167 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
168 $attr = [];
169 $attr['name'] = "{$this->mName}[]";
170
171 $attr['value'] = $value;
172 $attr['options'] = $optionsOoui;
173
174 foreach ( $attr['options'] as &$option ) {
175 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
176 }
177 if ( $this->mOptionsLabelsNotFromMessage ) {
178 foreach ( $attr['options'] as &$option ) {
179 $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
180 }
181 }
182
183 $attr += OOUI\Element::configFromHtmlAttributes(
184 $this->getAttributes( [ 'disabled', 'tabindex' ] )
185 );
186
187 if ( $this->mClass !== '' ) {
188 $attr['classes'] = [ $this->mClass ];
189 }
190
191 $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
192 if ( $sectionLabel ) {
193 $out[] = new OOUI\FieldsetLayout( [
194 'items' => [ $widget ],
195 'label' => new OOUI\HtmlSnippet( $sectionLabel ),
196 ] );
197 } else {
198 $out[] = $widget;
199 }
200 }
201
202 if ( !$hasSections ) {
203 // Directly return the only OOUI\CheckboxMultiselectInputWidget.
204 // This allows it to be made infusable and later tweaked by JS code.
205 return $out[ 0 ];
206 }
207
208 return implode( '', $out );
209 }
210
216 public function loadDataFromRequest( $request ) {
217 if ( $this->isSubmitAttempt( $request ) ) {
218 // Checkboxes are just not added to the request arrays if they're not checked,
219 // so it's perfectly possible for there not to be an entry at all
220 return $request->getArray( $this->mName, [] );
221 } else {
222 // That's ok, the user has not yet submitted the form, so show the defaults
223 return $this->getDefault();
224 }
225 }
226
227 public function getDefault() {
228 if ( isset( $this->mDefault ) ) {
229 return $this->mDefault;
230 } else {
231 return [];
232 }
233 }
234
235 public function filterDataForSubmit( $data ) {
238
239 $res = [];
240 foreach ( $options as $opt ) {
241 $res["$opt"] = in_array( $opt, $data, true );
242 }
243
244 return $res;
245 }
246
247 protected function needsLabel() {
248 return false;
249 }
250}
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...
getAttributes(array $list)
Returns the given attributes from the parameters.
msg()
Get a translated interface message.
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.
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 seperating 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)
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
MediaWiki exception.
Compact stacked vertical format for forms, implemented using OOUI widgets.
$res
Definition database.txt:21
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2806
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:864
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2013
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2014
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template $section
Definition hooks.txt:3022
$params