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