Added a separate error message for mkdir failures
[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( array( 'disabled', 'tabindex' ) );
41
42 foreach ( $options as $label => $info ) {
43 if ( is_array( $info ) ) {
44 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
45 $html .= $this->formatOptions( $info, $value );
46 } else {
47 $thisAttribs = array(
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 array( '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 if ( $this->mOptionsLabelsNotFromMessage ) {
69 $label = new OOUI\HtmlSnippet( $label );
70 }
71 return new OOUI\FieldLayout(
72 new OOUI\CheckboxInputWidget( array(
73 'name' => "{$this->mName}[]",
74 'selected' => $checked,
75 'value' => $attribs['value'],
76 ) + $attribs ),
77 array(
78 'label' => $label,
79 'align' => 'inline',
80 )
81 );
82 } else {
83 $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
84 $checkbox =
85 Xml::check( "{$this->mName}[]", $checked, $attribs ) .
86 '&#160;' .
87 call_user_func( $elementFunc,
88 'label',
89 array( 'for' => $attribs['id'] ),
90 $label
91 );
92 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
93 $checkbox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
94 $checkbox .
95 Html::closeElement( 'div' );
96 }
97 return $checkbox;
98 }
99 }
100
101 /**
102 * @param WebRequest $request
103 *
104 * @return string
105 */
106 function loadDataFromRequest( $request ) {
107 if ( $this->mParent->getMethod() == 'post' ) {
108 if ( $request->wasPosted() ) {
109 # Checkboxes are just not added to the request arrays if they're not checked,
110 # so it's perfectly possible for there not to be an entry at all
111 return $request->getArray( $this->mName, array() );
112 } else {
113 # That's ok, the user has not yet submitted the form, so show the defaults
114 return $this->getDefault();
115 }
116 } else {
117 # This is the impossible case: if we look at $_GET and see no data for our
118 # field, is it because the user has not yet submitted the form, or that they
119 # have submitted it with all the options unchecked? We will have to assume the
120 # latter, which basically means that you can't specify 'positive' defaults
121 # for GET forms.
122 # @todo FIXME...
123 return $request->getArray( $this->mName, array() );
124 }
125 }
126
127 function getDefault() {
128 if ( isset( $this->mDefault ) ) {
129 return $this->mDefault;
130 } else {
131 return array();
132 }
133 }
134
135 function filterDataForSubmit( $data ) {
136 $data = HTMLFormField::forceToStringRecursive( $data );
137 $options = HTMLFormField::flattenOptions( $this->getOptions() );
138
139 $res = array();
140 foreach ( $options as $opt ) {
141 $res["$opt"] = in_array( $opt, $data, true );
142 }
143
144 return $res;
145 }
146
147 protected function needsLabel() {
148 return false;
149 }
150 }