Document what User::newSystemUser()'s "stealing" does
[mediawiki.git] / includes / htmlform / HTMLMultiSelectField.php
blob1aaa3e88fa120df443335edbf6ff275d289cd83b
1 <?php
3 /**
4 * Multi-select field
5 */
6 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
7 function validate( $value, $alldata ) {
8 $p = parent::validate( $value, $alldata );
10 if ( $p !== true ) {
11 return $p;
14 if ( !is_array( $value ) ) {
15 return false;
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() );
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();
30 function getInputHTML( $value ) {
31 $value = HTMLFormField::forceToStringRecursive( $value );
32 $html = $this->formatOptions( $this->getOptions(), $value );
34 return $html;
37 function formatOptions( $options, $value ) {
38 $html = '';
40 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
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,
51 $checked = in_array( $info, $value, true );
53 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
55 $html .= ' ' . Html::rawElement(
56 'div',
57 [ 'class' => 'mw-htmlform-flatlist-item' ],
58 $checkbox
63 return $html;
66 protected function getOneCheckbox( $checked, $attribs, $label ) {
67 if ( $this->mParent instanceof OOUIHTMLForm ) {
68 if ( $this->mOptionsLabelsNotFromMessage ) {
69 $label = new OOUI\HtmlSnippet( $label );
71 return new OOUI\FieldLayout(
72 new OOUI\CheckboxInputWidget( [
73 'name' => "{$this->mName}[]",
74 'selected' => $checked,
75 ] + OOUI\Element::configFromHtmlAttributes(
76 $attribs
77 ) ),
79 'label' => $label,
80 'align' => 'inline',
83 } else {
84 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' ];
85 $checkbox =
86 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
87 '&#160;' .
88 call_user_func( $elementFunc,
89 'label',
90 [ 'for' => $attribs['id'] ],
91 $label
93 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
94 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
95 $checkbox .
96 Html::closeElement( 'div' );
98 return $checkbox;
103 * @param WebRequest $request
105 * @return string
107 function loadDataFromRequest( $request ) {
108 if ( $this->mParent->getMethod() == 'post' ) {
109 if ( $request->wasPosted() ) {
110 # Checkboxes are just not added to the request arrays if they're not checked,
111 # so it's perfectly possible for there not to be an entry at all
112 return $request->getArray( $this->mName, [] );
113 } else {
114 # That's ok, the user has not yet submitted the form, so show the defaults
115 return $this->getDefault();
117 } else {
118 # This is the impossible case: if we look at $_GET and see no data for our
119 # field, is it because the user has not yet submitted the form, or that they
120 # have submitted it with all the options unchecked? We will have to assume the
121 # latter, which basically means that you can't specify 'positive' defaults
122 # for GET forms.
123 # @todo FIXME...
124 return $request->getArray( $this->mName, [] );
128 function getDefault() {
129 if ( isset( $this->mDefault ) ) {
130 return $this->mDefault;
131 } else {
132 return [];
136 function filterDataForSubmit( $data ) {
137 $data = HTMLFormField::forceToStringRecursive( $data );
138 $options = HTMLFormField::flattenOptions( $this->getOptions() );
140 $res = [];
141 foreach ( $options as $opt ) {
142 $res["$opt"] = in_array( $opt, $data, true );
145 return $res;
148 protected function needsLabel() {
149 return false;