MediaWiki  REL1_24
HTMLMultiSelectField.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable {
00007     function validate( $value, $alldata ) {
00008         $p = parent::validate( $value, $alldata );
00009 
00010         if ( $p !== true ) {
00011             return $p;
00012         }
00013 
00014         if ( !is_array( $value ) ) {
00015             return false;
00016         }
00017 
00018         # If all options are valid, array_intersect of the valid options
00019         # and the provided options will return the provided options.
00020         $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
00021 
00022         $validValues = array_intersect( $value, $validOptions );
00023         if ( count( $validValues ) == count( $value ) ) {
00024             return true;
00025         } else {
00026             return $this->msg( 'htmlform-select-badoption' )->parse();
00027         }
00028     }
00029 
00030     function getInputHTML( $value ) {
00031         $value = HTMLFormField::forceToStringRecursive( $value );
00032         $html = $this->formatOptions( $this->getOptions(), $value );
00033 
00034         return $html;
00035     }
00036 
00037     function formatOptions( $options, $value ) {
00038         $html = '';
00039 
00040         $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
00041         $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ? 'rawElement' : 'element' );
00042 
00043         foreach ( $options as $label => $info ) {
00044             if ( is_array( $info ) ) {
00045                 $html .= Html::rawElement( 'h1', array(), $label ) . "\n";
00046                 $html .= $this->formatOptions( $info, $value );
00047             } else {
00048                 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
00049 
00050                 // @todo: Make this use checkLabel for consistency purposes
00051                 $checkbox = Xml::check(
00052                     $this->mName . '[]',
00053                     in_array( $info, $value, true ),
00054                     $attribs + $thisAttribs
00055                 );
00056                 $checkbox .= '&#160;' . call_user_func( $elementFunc,
00057                     'label',
00058                     array( 'for' => "{$this->mID}-$info" ),
00059                     $label
00060                 );
00061 
00062                 $html .= ' ' . Html::rawElement(
00063                     'div',
00064                     array( 'class' => 'mw-htmlform-flatlist-item' ),
00065                     $checkbox
00066                 );
00067             }
00068         }
00069 
00070         return $html;
00071     }
00072 
00078     function loadDataFromRequest( $request ) {
00079         if ( $this->mParent->getMethod() == 'post' ) {
00080             if ( $request->wasPosted() ) {
00081                 # Checkboxes are just not added to the request arrays if they're not checked,
00082                 # so it's perfectly possible for there not to be an entry at all
00083                 return $request->getArray( $this->mName, array() );
00084             } else {
00085                 # That's ok, the user has not yet submitted the form, so show the defaults
00086                 return $this->getDefault();
00087             }
00088         } else {
00089             # This is the impossible case: if we look at $_GET and see no data for our
00090             # field, is it because the user has not yet submitted the form, or that they
00091             # have submitted it with all the options unchecked? We will have to assume the
00092             # latter, which basically means that you can't specify 'positive' defaults
00093             # for GET forms.
00094             # @todo FIXME...
00095             return $request->getArray( $this->mName, array() );
00096         }
00097     }
00098 
00099     function getDefault() {
00100         if ( isset( $this->mDefault ) ) {
00101             return $this->mDefault;
00102         } else {
00103             return array();
00104         }
00105     }
00106 
00107     function filterDataForSubmit( $data ) {
00108         $data = HTMLFormField::forceToStringRecursive( $data );
00109         $options = HTMLFormField::flattenOptions( $this->getOptions() );
00110 
00111         $res = array();
00112         foreach ( $options as $opt ) {
00113             $res["$opt"] = in_array( $opt, $data, true );
00114         }
00115 
00116         return $res;
00117     }
00118 
00119     protected function needsLabel() {
00120         return false;
00121     }
00122 }