Merge "Skin: Make skins aware of their registered skin name"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLMultiSelectField.php
1 <?php
2
3 /**
4 * Multi-select field
5 */
6 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
7 /**
8 * @param array $params
9 * In adition to the usual HTMLFormField parameters, this can take the following fields:
10 * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
11 * can be used to filter them. This is desirable mostly for very long lists of options.
12 * This only works for users with JavaScript support and falls back to the list of checkboxes.
13 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
14 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
15 * for very short lists of concisely labelled options.
16 */
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
63 $value = HTMLFormField::forceToStringRecursive( $value );
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', $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
123 /**
124 * Get options and make them into arrays suitable for OOUI.
125 * @return array Options for inclusion in a select or whatever.
126 */
127 public function getOptionsOOUI() {
128 // Sections make this difficult. See getInputOOUI().
129 throw new MWException( 'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
130 }
131
132 /**
133 * Get the OOUI version of this field.
134 *
135 * @since 1.28
136 * @param string[] $value
137 * @return OOUI\CheckboxMultiselectInputWidget
138 */
139 public function getInputOOUI( $value ) {
140 $this->mParent->getOutput()->addModules( 'oojs-ui-widgets' );
141
142 $optionsOouiSections = [];
143 $options = $this->getOptions();
144 // If the options are supposed to be split into sections, each section becomes a separate
145 // CheckboxMultiselectInputWidget.
146 foreach ( $options as $label => $section ) {
147 if ( is_array( $section ) ) {
148 $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
149 unset( $options[$label] );
150 }
151 }
152 // If anything remains in the array, they are sectionless options. Put them in a separate widget
153 // at the beginning.
154 if ( $options ) {
155 $optionsOouiSections = array_merge(
156 [ '' => Xml::listDropDownOptionsOoui( $options ) ],
157 $optionsOouiSections
158 );
159 }
160
161 $out = '';
162 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
163 $attr = [];
164 $attr['name'] = "{$this->mName}[]";
165
166 $attr['value'] = $value;
167 $attr['options'] = $optionsOoui;
168
169 foreach ( $attr['options'] as &$option ) {
170 $option['disabled'] = in_array( $option['data'], $this->mParams['disabled-options'], true );
171 }
172 if ( $this->mOptionsLabelsNotFromMessage ) {
173 foreach ( $attr['options'] as &$option ) {
174 $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
175 }
176 }
177
178 $attr += OOUI\Element::configFromHtmlAttributes(
179 $this->getAttributes( [ 'disabled', 'tabindex' ] )
180 );
181
182 if ( $this->mClass !== '' ) {
183 $attr['classes'] = [ $this->mClass ];
184 }
185
186 $widget = new OOUI\CheckboxMultiselectInputWidget( $attr );
187 if ( $sectionLabel ) {
188 $out .= new OOUI\FieldsetLayout( [
189 'items' => [ $widget ],
190 'label' => $sectionLabel,
191 ] );
192 } else {
193 $out .= $widget;
194 }
195 }
196
197 return $out;
198 }
199
200 /**
201 * @param WebRequest $request
202 *
203 * @return string|array
204 */
205 public function loadDataFromRequest( $request ) {
206 if ( $this->isSubmitAttempt( $request ) ) {
207 // Checkboxes are just not added to the request arrays if they're not checked,
208 // so it's perfectly possible for there not to be an entry at all
209 return $request->getArray( $this->mName, [] );
210 } else {
211 // That's ok, the user has not yet submitted the form, so show the defaults
212 return $this->getDefault();
213 }
214 }
215
216 public function getDefault() {
217 if ( isset( $this->mDefault ) ) {
218 return $this->mDefault;
219 } else {
220 return [];
221 }
222 }
223
224 public function filterDataForSubmit( $data ) {
225 $data = HTMLFormField::forceToStringRecursive( $data );
226 $options = HTMLFormField::flattenOptions( $this->getOptions() );
227
228 $res = [];
229 foreach ( $options as $opt ) {
230 $res["$opt"] = in_array( $opt, $data, true );
231 }
232
233 return $res;
234 }
235
236 protected function needsLabel() {
237 return false;
238 }
239 }