MediaWiki  REL1_23
HTMLCheckMatrix.php
Go to the documentation of this file.
00001 <?php
00002 
00024 class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
00025     static private $requiredParams = array(
00026         // Required by underlying HTMLFormField
00027         'fieldname',
00028         // Required by HTMLCheckMatrix
00029         'rows',
00030         'columns'
00031     );
00032 
00033     public function __construct( $params ) {
00034         $missing = array_diff( self::$requiredParams, array_keys( $params ) );
00035         if ( $missing ) {
00036             throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
00037         }
00038         parent::__construct( $params );
00039     }
00040 
00041     function validate( $value, $alldata ) {
00042         $rows = $this->mParams['rows'];
00043         $columns = $this->mParams['columns'];
00044 
00045         // Make sure user-defined validation callback is run
00046         $p = parent::validate( $value, $alldata );
00047         if ( $p !== true ) {
00048             return $p;
00049         }
00050 
00051         // Make sure submitted value is an array
00052         if ( !is_array( $value ) ) {
00053             return false;
00054         }
00055 
00056         // If all options are valid, array_intersect of the valid options
00057         // and the provided options will return the provided options.
00058         $validOptions = array();
00059         foreach ( $rows as $rowTag ) {
00060             foreach ( $columns as $columnTag ) {
00061                 $validOptions[] = $columnTag . '-' . $rowTag;
00062             }
00063         }
00064         $validValues = array_intersect( $value, $validOptions );
00065         if ( count( $validValues ) == count( $value ) ) {
00066             return true;
00067         } else {
00068             return $this->msg( 'htmlform-select-badoption' )->parse();
00069         }
00070     }
00071 
00082     function getInputHTML( $value ) {
00083         $html = '';
00084         $tableContents = '';
00085         $rows = $this->mParams['rows'];
00086         $columns = $this->mParams['columns'];
00087 
00088         $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ) );
00089 
00090         // Build the column headers
00091         $headerContents = Html::rawElement( 'td', array(), '&#160;' );
00092         foreach ( $columns as $columnLabel => $columnTag ) {
00093             $headerContents .= Html::rawElement( 'td', array(), $columnLabel );
00094         }
00095         $tableContents .= Html::rawElement( 'tr', array(), "\n$headerContents\n" );
00096 
00097         $tooltipClass = 'mw-icon-question';
00098         if ( isset( $this->mParams['tooltip-class'] ) ) {
00099             $tooltipClass = $this->mParams['tooltip-class'];
00100         }
00101 
00102         // Build the options matrix
00103         foreach ( $rows as $rowLabel => $rowTag ) {
00104             // Append tooltip if configured
00105             if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
00106                 $tooltipAttribs = array(
00107                     'class' => "mw-htmlform-tooltip $tooltipClass",
00108                     'title' => $this->mParams['tooltips'][$rowLabel],
00109                 );
00110                 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
00111             }
00112             $rowContents = Html::rawElement( 'td', array(), $rowLabel );
00113             foreach ( $columns as $columnTag ) {
00114                 $thisTag = "$columnTag-$rowTag";
00115                 // Construct the checkbox
00116                 $thisAttribs = array(
00117                     'id' => "{$this->mID}-$thisTag",
00118                     'value' => $thisTag,
00119                 );
00120                 $checked = in_array( $thisTag, (array)$value, true );
00121                 if ( $this->isTagForcedOff( $thisTag ) ) {
00122                     $checked = false;
00123                     $thisAttribs['disabled'] = 1;
00124                 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
00125                     $checked = true;
00126                     $thisAttribs['disabled'] = 1;
00127                 }
00128                 $rowContents .= Html::rawElement(
00129                     'td',
00130                     array(),
00131                     Xml::check( "{$this->mName}[]", $checked, $attribs + $thisAttribs )
00132                 );
00133             }
00134             $tableContents .= Html::rawElement( 'tr', array(), "\n$rowContents\n" );
00135         }
00136 
00137         // Put it all in a table
00138         $html .= Html::rawElement( 'table',
00139                 array( 'class' => 'mw-htmlform-matrix' ),
00140                 Html::rawElement( 'tbody', array(), "\n$tableContents\n" ) ) . "\n";
00141 
00142         return $html;
00143     }
00144 
00145     protected function isTagForcedOff( $tag ) {
00146         return isset( $this->mParams['force-options-off'] )
00147             && in_array( $tag, $this->mParams['force-options-off'] );
00148     }
00149 
00150     protected function isTagForcedOn( $tag ) {
00151         return isset( $this->mParams['force-options-on'] )
00152             && in_array( $tag, $this->mParams['force-options-on'] );
00153     }
00154 
00166     function getTableRow( $value ) {
00167         list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
00168         $inputHtml = $this->getInputHTML( $value );
00169         $fieldType = get_class( $this );
00170         $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
00171         $cellAttributes = array( 'colspan' => 2 );
00172 
00173         $label = $this->getLabelHtml( $cellAttributes );
00174 
00175         $field = Html::rawElement(
00176             'td',
00177             array( 'class' => 'mw-input' ) + $cellAttributes,
00178             $inputHtml . "\n$errors"
00179         );
00180 
00181         $html = Html::rawElement( 'tr', array( 'class' => 'mw-htmlform-vertical-label' ), $label );
00182         $html .= Html::rawElement( 'tr',
00183             array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
00184             $field );
00185 
00186         return $html . $helptext;
00187     }
00188 
00194     function loadDataFromRequest( $request ) {
00195         if ( $this->mParent->getMethod() == 'post' ) {
00196             if ( $request->wasPosted() ) {
00197                 // Checkboxes are not added to the request arrays if they're not checked,
00198                 // so it's perfectly possible for there not to be an entry at all
00199                 return $request->getArray( $this->mName, array() );
00200             } else {
00201                 // That's ok, the user has not yet submitted the form, so show the defaults
00202                 return $this->getDefault();
00203             }
00204         } else {
00205             // This is the impossible case: if we look at $_GET and see no data for our
00206             // field, is it because the user has not yet submitted the form, or that they
00207             // have submitted it with all the options unchecked. We will have to assume the
00208             // latter, which basically means that you can't specify 'positive' defaults
00209             // for GET forms.
00210             return $request->getArray( $this->mName, array() );
00211         }
00212     }
00213 
00214     function getDefault() {
00215         if ( isset( $this->mDefault ) ) {
00216             return $this->mDefault;
00217         } else {
00218             return array();
00219         }
00220     }
00221 
00222     function filterDataForSubmit( $data ) {
00223         $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
00224         $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
00225         $res = array();
00226         foreach ( $columns as $column ) {
00227             foreach ( $rows as $row ) {
00228                 // Make sure option hasn't been forced
00229                 $thisTag = "$column-$row";
00230                 if ( $this->isTagForcedOff( $thisTag ) ) {
00231                     $res[$thisTag] = false;
00232                 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
00233                     $res[$thisTag] = true;
00234                 } else {
00235                     $res[$thisTag] = in_array( $thisTag, $data );
00236                 }
00237             }
00238         }
00239 
00240         return $res;
00241     }
00242 }