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