MediaWiki  REL1_23
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                 $checkbox = Xml::check(
00051                     $this->mName . '[]',
00052                     in_array( $info, $value, true ),
00053                     $attribs + $thisAttribs
00054                 );
00055                 $checkbox .= '&#160;' . call_user_func( $elementFunc,
00056                     'label',
00057                     array( 'for' => "{$this->mID}-$info" ),
00058                     $label
00059                 );
00060 
00061                 $html .= ' ' . Html::rawElement(
00062                     'div',
00063                     array( 'class' => 'mw-htmlform-flatlist-item' ),
00064                     $checkbox
00065                 );
00066             }
00067         }
00068 
00069         return $html;
00070     }
00071 
00077     function loadDataFromRequest( $request ) {
00078         if ( $this->mParent->getMethod() == 'post' ) {
00079             if ( $request->wasPosted() ) {
00080                 # Checkboxes are just not added to the request arrays if they're not checked,
00081                 # so it's perfectly possible for there not to be an entry at all
00082                 return $request->getArray( $this->mName, array() );
00083             } else {
00084                 # That's ok, the user has not yet submitted the form, so show the defaults
00085                 return $this->getDefault();
00086             }
00087         } else {
00088             # This is the impossible case: if we look at $_GET and see no data for our
00089             # field, is it because the user has not yet submitted the form, or that they
00090             # have submitted it with all the options unchecked? We will have to assume the
00091             # latter, which basically means that you can't specify 'positive' defaults
00092             # for GET forms.
00093             # @todo FIXME...
00094             return $request->getArray( $this->mName, array() );
00095         }
00096     }
00097 
00098     function getDefault() {
00099         if ( isset( $this->mDefault ) ) {
00100             return $this->mDefault;
00101         } else {
00102             return array();
00103         }
00104     }
00105 
00106     function filterDataForSubmit( $data ) {
00107         $data = HTMLFormField::forceToStringRecursive( $data );
00108         $options = HTMLFormField::flattenOptions( $this->getOptions() );
00109 
00110         $res = array();
00111         foreach ( $options as $opt ) {
00112             $res["$opt"] = in_array( $opt, $data, true );
00113         }
00114 
00115         return $res;
00116     }
00117 
00118     protected function needsLabel() {
00119         return false;
00120     }
00121 }