HTMLMultiSelectField: Add 'dropdown' option for 'mw-chosen' behavior and document
[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 */
14 public function __construct( $params ) {
15 parent::__construct( $params );
16
17 // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
18 if ( isset( $params['dropdown'] ) || strpos( $this->mClass, 'mw-chosen' ) !== false ) {
19 $this->mClass .= ' mw-htmlform-dropdown';
20 }
21 }
22
23 function validate( $value, $alldata ) {
24 $p = parent::validate( $value, $alldata );
25
26 if ( $p !== true ) {
27 return $p;
28 }
29
30 if ( !is_array( $value ) ) {
31 return false;
32 }
33
34 # If all options are valid, array_intersect of the valid options
35 # and the provided options will return the provided options.
36 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
37
38 $validValues = array_intersect( $value, $validOptions );
39 if ( count( $validValues ) == count( $value ) ) {
40 return true;
41 } else {
42 return $this->msg( 'htmlform-select-badoption' )->parse();
43 }
44 }
45
46 function getInputHTML( $value ) {
47 if ( isset( $this->mParams['dropdown'] ) ) {
48 $this->mParent->getOutput()->addModules( 'jquery.chosen' );
49 }
50
51 $value = HTMLFormField::forceToStringRecursive( $value );
52 $html = $this->formatOptions( $this->getOptions(), $value );
53
54 return $html;
55 }
56
57 function formatOptions( $options, $value ) {
58 $html = '';
59
60 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
61
62 foreach ( $options as $label => $info ) {
63 if ( is_array( $info ) ) {
64 $html .= Html::rawElement( 'h1', [], $label ) . "\n";
65 $html .= $this->formatOptions( $info, $value );
66 } else {
67 $thisAttribs = [
68 'id' => "{$this->mID}-$info",
69 'value' => $info,
70 ];
71 $checked = in_array( $info, $value, true );
72
73 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
74
75 $html .= ' ' . Html::rawElement(
76 'div',
77 [ 'class' => 'mw-htmlform-flatlist-item' ],
78 $checkbox
79 );
80 }
81 }
82
83 return $html;
84 }
85
86 protected function getOneCheckbox( $checked, $attribs, $label ) {
87 if ( $this->mParent instanceof OOUIHTMLForm ) {
88 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
89 } else {
90 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
91 $checkbox =
92 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
93 '&#160;' .
94 call_user_func( $elementFunc,
95 'label',
96 [ 'for' => $attribs['id'] ],
97 $label
98 );
99 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
100 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
101 $checkbox .
102 Html::closeElement( 'div' );
103 }
104 return $checkbox;
105 }
106 }
107
108 /**
109 * Get the OOUI version of this field.
110 *
111 * @since 1.28
112 * @param string[] $value
113 * @return OOUI\CheckboxMultiselectInputWidget
114 */
115 public function getInputOOUI( $value ) {
116 $attr = $this->getTooltipAndAccessKey();
117 $attr['id'] = $this->mID;
118 $attr['name'] = "{$this->mName}[]";
119
120 $attr['value'] = $value;
121 $attr['options'] = $this->getOptionsOOUI();
122
123 if ( $this->mOptionsLabelsNotFromMessage ) {
124 foreach ( $attr['options'] as &$option ) {
125 $option['label'] = new OOUI\HtmlSnippet( $option['label'] );
126 }
127 }
128
129 $attr += OOUI\Element::configFromHtmlAttributes(
130 $this->getAttributes( [ 'disabled', 'tabindex' ] )
131 );
132
133 if ( $this->mClass !== '' ) {
134 $attr['classes'] = [ $this->mClass ];
135 }
136
137 return new OOUI\CheckboxMultiselectInputWidget( $attr );
138 }
139
140 /**
141 * @param WebRequest $request
142 *
143 * @return string
144 */
145 function loadDataFromRequest( $request ) {
146 if ( $this->isSubmitAttempt( $request ) ) {
147 // Checkboxes are just not added to the request arrays if they're not checked,
148 // so it's perfectly possible for there not to be an entry at all
149 return $request->getArray( $this->mName, [] );
150 } else {
151 // That's ok, the user has not yet submitted the form, so show the defaults
152 return $this->getDefault();
153 }
154 }
155
156 function getDefault() {
157 if ( isset( $this->mDefault ) ) {
158 return $this->mDefault;
159 } else {
160 return [];
161 }
162 }
163
164 function filterDataForSubmit( $data ) {
165 $data = HTMLFormField::forceToStringRecursive( $data );
166 $options = HTMLFormField::flattenOptions( $this->getOptions() );
167
168 $res = [];
169 foreach ( $options as $opt ) {
170 $res["$opt"] = in_array( $opt, $data, true );
171 }
172
173 return $res;
174 }
175
176 protected function needsLabel() {
177 return false;
178 }
179 }