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