Merge "Make sure images that don't have an explicit alignment get aligned right"
[mediawiki.git] / includes / htmlform / HTMLMultiSelectField.php
blob576f5cd66a1e411d9bc30aa94e31965fdfa0986c
1 <?php
3 /**
4 * Multi-select field
5 */
6 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
7 function validate( $value, $alldata ) {
8 $p = parent::validate( $value, $alldata );
10 if ( $p !== true ) {
11 return $p;
14 if ( !is_array( $value ) ) {
15 return false;
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() );
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();
30 function getInputHTML( $value ) {
31 $value = HTMLFormField::forceToStringRecursive( $value );
32 $html = $this->formatOptions( $this->getOptions(), $value );
34 return $html;
37 function formatOptions( $options, $value ) {
38 $html = '';
40 $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
41 $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
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 );
50 $checkbox = Xml::check(
51 $this->mName . '[]',
52 in_array( $info, $value, true ),
53 $attribs + $thisAttribs
55 $checkbox .= '&#160;' . call_user_func( $elementFunc,
56 'label',
57 array( 'for' => "{$this->mID}-$info" ),
58 $label
61 $html .= ' ' . Html::rawElement(
62 'div',
63 array( 'class' => 'mw-htmlform-flatlist-item' ),
64 $checkbox
69 return $html;
72 /**
73 * @param WebRequest $request
75 * @return string
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();
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() );
98 function getDefault() {
99 if ( isset( $this->mDefault ) ) {
100 return $this->mDefault;
101 } else {
102 return array();
106 function filterDataForSubmit( $data ) {
107 $data = HTMLFormField::forceToStringRecursive( $data );
108 $options = HTMLFormField::flattenOptions( $this->getOptions() );
110 $res = array();
111 foreach ( $options as $opt ) {
112 $res["$opt"] = in_array( $opt, $data, true );
115 return $res;
118 protected function needsLabel() {
119 return false;