Merge "MediaWikiTestCase: Centralise insertPage() logic from SearchEngineTest"
[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 $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
42
43 foreach ( $options as $label => $info ) {
44 if ( is_array( $info ) ) {
45 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
46 $html .= $this->formatOptions( $info, $value );
47 } else {
48 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
49
50 // @todo: Make this use checkLabel for consistency purposes
51 $checkbox = Xml::check(
52 $this->mName . '[]',
53 in_array( $info, $value, true ),
54 $attribs + $thisAttribs
55 );
56 $checkbox .= '&#160;' . call_user_func( $elementFunc,
57 'label',
58 array( 'for' => "{$this->mID}-$info" ),
59 $label
60 );
61
62 $checkbox = Html::rawElement(
63 'div',
64 array( 'class' => 'mw-ui-checkbox' ),
65 $checkbox
66 );
67
68 $html .= ' ' . Html::rawElement(
69 'div',
70 array( 'class' => 'mw-htmlform-flatlist-item' ),
71 $checkbox
72 );
73 }
74 }
75
76 return $html;
77 }
78
79 /**
80 * @param WebRequest $request
81 *
82 * @return string
83 */
84 function loadDataFromRequest( $request ) {
85 if ( $this->mParent->getMethod() == 'post' ) {
86 if ( $request->wasPosted() ) {
87 # Checkboxes are just not added to the request arrays if they're not checked,
88 # so it's perfectly possible for there not to be an entry at all
89 return $request->getArray( $this->mName, array() );
90 } else {
91 # That's ok, the user has not yet submitted the form, so show the defaults
92 return $this->getDefault();
93 }
94 } else {
95 # This is the impossible case: if we look at $_GET and see no data for our
96 # field, is it because the user has not yet submitted the form, or that they
97 # have submitted it with all the options unchecked? We will have to assume the
98 # latter, which basically means that you can't specify 'positive' defaults
99 # for GET forms.
100 # @todo FIXME...
101 return $request->getArray( $this->mName, array() );
102 }
103 }
104
105 function getDefault() {
106 if ( isset( $this->mDefault ) ) {
107 return $this->mDefault;
108 } else {
109 return array();
110 }
111 }
112
113 function filterDataForSubmit( $data ) {
114 $data = HTMLFormField::forceToStringRecursive( $data );
115 $options = HTMLFormField::flattenOptions( $this->getOptions() );
116
117 $res = array();
118 foreach ( $options as $opt ) {
119 $res["$opt"] = in_array( $opt, $data, true );
120 }
121
122 return $res;
123 }
124
125 protected function needsLabel() {
126 return false;
127 }
128 }