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