f1b058d18ed3225778198f39c73a79ce4698048e
[lhc/web/wiklou.git] / includes / htmlform / HTMLMultiSelectField.php
1 <?php
2 /**
3 * Multi-select field
4 */
5 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
6
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->mParams[ 'options' ] );
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->mParams[ 'options' ], $value );
32
33 return $html;
34 }
35
36 function formatOptions( $options, $value ) {
37 $html = '';
38
39 $attribs = array();
40
41 if ( ! empty( $this->mParams[ 'disabled' ] ) ) {
42 $attribs[ 'disabled' ] = 'disabled';
43 }
44
45 foreach ( $options as $label => $info ) {
46 if ( is_array( $info ) ) {
47 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
48 $html .= $this->formatOptions( $info, $value );
49 } else {
50 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
51
52 $checkbox = Xml::check( $this->mName . '[]', in_array( $info, $value, true ), $attribs + $thisAttribs );
53 $checkbox .= '&#160;' . Html::rawElement( 'label', array( 'for' => "{$this->mID}-$info" ), $label );
54
55 $html .= ' ' . Html::rawElement( 'div', array( 'class' => 'mw-htmlform-flatlist-item' ), $checkbox );
56 }
57 }
58
59 return $html;
60 }
61
62 /**
63 * @param $request WebRequest
64 *
65 * @return String
66 */
67 function loadDataFromRequest( $request ) {
68 if ( $this->mParent->getMethod() == 'post' ) {
69 if ( $request->wasPosted() ) {
70 # Checkboxes are just not added to the request arrays if they're not checked,
71 # so it's perfectly possible for there not to be an entry at all
72 return $request->getArray( $this->mName, array() );
73 } else {
74 # That's ok, the user has not yet submitted the form, so show the defaults
75 return $this->getDefault();
76 }
77 } else {
78 # This is the impossible case: if we look at $_GET and see no data for our
79 # field, is it because the user has not yet submitted the form, or that they
80 # have submitted it with all the options unchecked? We will have to assume the
81 # latter, which basically means that you can't specify 'positive' defaults
82 # for GET forms.
83 # @todo FIXME...
84 return $request->getArray( $this->mName, array() );
85 }
86 }
87
88 function getDefault() {
89 if ( isset( $this->mDefault ) ) {
90 return $this->mDefault;
91 } else {
92 return array();
93 }
94 }
95
96 function filterDataForSubmit( $data ) {
97 $options = HTMLFormField::flattenOptions( $this->mParams[ 'options' ] );
98
99 $res = array();
100 foreach ( $options as $opt ) {
101 $res[ "$opt" ] = in_array( $opt, $data );
102 }
103
104 return $res;
105 }
106
107 protected function needsLabel() {
108 return false;
109 }
110 }