MediaWiki
REL1_24
|
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(), ' ' ); 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 $thisId = "{$this->mID}-$thisTag"; 00117 $thisAttribs = array( 00118 'id' => $thisId, 00119 'value' => $thisTag, 00120 ); 00121 $checked = in_array( $thisTag, (array)$value, true ); 00122 if ( $this->isTagForcedOff( $thisTag ) ) { 00123 $checked = false; 00124 $thisAttribs['disabled'] = 1; 00125 } elseif ( $this->isTagForcedOn( $thisTag ) ) { 00126 $checked = true; 00127 $thisAttribs['disabled'] = 1; 00128 } 00129 $chkBox = Xml::check( "{$this->mName}[]", $checked, $attribs + $thisAttribs ); 00130 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) { 00131 $chkBox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) . 00132 $chkBox . 00133 Html::element( 'label', array( 'for' => $thisId ) ) . 00134 Html::closeElement( 'div' ); 00135 } 00136 $rowContents .= Html::rawElement( 00137 'td', 00138 array(), 00139 $chkBox 00140 ); 00141 } 00142 $tableContents .= Html::rawElement( 'tr', array(), "\n$rowContents\n" ); 00143 } 00144 00145 // Put it all in a table 00146 $html .= Html::rawElement( 'table', 00147 array( 'class' => 'mw-htmlform-matrix' ), 00148 Html::rawElement( 'tbody', array(), "\n$tableContents\n" ) ) . "\n"; 00149 00150 return $html; 00151 } 00152 00153 protected function isTagForcedOff( $tag ) { 00154 return isset( $this->mParams['force-options-off'] ) 00155 && in_array( $tag, $this->mParams['force-options-off'] ); 00156 } 00157 00158 protected function isTagForcedOn( $tag ) { 00159 return isset( $this->mParams['force-options-on'] ) 00160 && in_array( $tag, $this->mParams['force-options-on'] ); 00161 } 00162 00174 function getTableRow( $value ) { 00175 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value ); 00176 $inputHtml = $this->getInputHTML( $value ); 00177 $fieldType = get_class( $this ); 00178 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() ); 00179 $cellAttributes = array( 'colspan' => 2 ); 00180 00181 $label = $this->getLabelHtml( $cellAttributes ); 00182 00183 $field = Html::rawElement( 00184 'td', 00185 array( 'class' => 'mw-input' ) + $cellAttributes, 00186 $inputHtml . "\n$errors" 00187 ); 00188 00189 $html = Html::rawElement( 'tr', array( 'class' => 'mw-htmlform-vertical-label' ), $label ); 00190 $html .= Html::rawElement( 'tr', 00191 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ), 00192 $field ); 00193 00194 return $html . $helptext; 00195 } 00196 00202 function loadDataFromRequest( $request ) { 00203 if ( $this->mParent->getMethod() == 'post' ) { 00204 if ( $request->wasPosted() ) { 00205 // Checkboxes are not added to the request arrays if they're not checked, 00206 // so it's perfectly possible for there not to be an entry at all 00207 return $request->getArray( $this->mName, array() ); 00208 } else { 00209 // That's ok, the user has not yet submitted the form, so show the defaults 00210 return $this->getDefault(); 00211 } 00212 } else { 00213 // This is the impossible case: if we look at $_GET and see no data for our 00214 // field, is it because the user has not yet submitted the form, or that they 00215 // have submitted it with all the options unchecked. We will have to assume the 00216 // latter, which basically means that you can't specify 'positive' defaults 00217 // for GET forms. 00218 return $request->getArray( $this->mName, array() ); 00219 } 00220 } 00221 00222 function getDefault() { 00223 if ( isset( $this->mDefault ) ) { 00224 return $this->mDefault; 00225 } else { 00226 return array(); 00227 } 00228 } 00229 00230 function filterDataForSubmit( $data ) { 00231 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] ); 00232 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] ); 00233 $res = array(); 00234 foreach ( $columns as $column ) { 00235 foreach ( $rows as $row ) { 00236 // Make sure option hasn't been forced 00237 $thisTag = "$column-$row"; 00238 if ( $this->isTagForcedOff( $thisTag ) ) { 00239 $res[$thisTag] = false; 00240 } elseif ( $this->isTagForcedOn( $thisTag ) ) { 00241 $res[$thisTag] = true; 00242 } else { 00243 $res[$thisTag] = in_array( $thisTag, $data ); 00244 } 00245 } 00246 } 00247 00248 return $res; 00249 } 00250 }