5 * Operates similarly to HTMLMultiSelectField, but instead of using an array of
6 * options, uses an array of rows and an array of columns to dynamically
7 * construct a matrix of options. The tags used to identify a particular cell
8 * are of the form "columnName-rowName"
12 * - Required list of columns in the matrix.
14 * - Required list of rows in the matrix.
16 * - Accepts array of column-row tags to be displayed as enabled but unavailable to change
18 * - Accepts array of column-row tags to be displayed as disabled but unavailable to change.
20 * - Optional array mapping row label to tooltip content
22 * - Optional CSS class used on tooltip container span. Defaults to mw-icon-question.
24 class HTMLCheckMatrix
extends HTMLFormField
implements HTMLNestedFilterable
{
25 static private $requiredParams = array(
26 // Required by underlying HTMLFormField
28 // Required by HTMLCheckMatrix
33 public function __construct( $params ) {
34 $missing = array_diff( self
::$requiredParams, array_keys( $params ) );
36 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
38 parent
::__construct( $params );
41 function validate( $value, $alldata ) {
42 $rows = $this->mParams
['rows'];
43 $columns = $this->mParams
['columns'];
45 // Make sure user-defined validation callback is run
46 $p = parent
::validate( $value, $alldata );
51 // Make sure submitted value is an array
52 if ( !is_array( $value ) ) {
56 // If all options are valid, array_intersect of the valid options
57 // and the provided options will return the provided options.
58 $validOptions = array();
59 foreach ( $rows as $rowTag ) {
60 foreach ( $columns as $columnTag ) {
61 $validOptions[] = $columnTag . '-' . $rowTag;
64 $validValues = array_intersect( $value, $validOptions );
65 if ( count( $validValues ) == count( $value ) ) {
68 return $this->msg( 'htmlform-select-badoption' )->parse();
73 * Build a table containing a matrix of checkbox options.
74 * The value of each option is a combination of the row tag and column tag.
75 * mParams['rows'] is an array with row labels as keys and row tags as values.
76 * mParams['columns'] is an array with column labels as keys and column tags as values.
78 * @param array $value of the options that should be checked
82 function getInputHTML( $value ) {
86 $rows = $this->mParams
['rows'];
87 $columns = $this->mParams
['columns'];
89 // If the disabled param is set, disable all the options
90 if ( !empty( $this->mParams
['disabled'] ) ) {
91 $attribs['disabled'] = 'disabled';
94 // Build the column headers
95 $headerContents = Html
::rawElement( 'td', array(), ' ' );
96 foreach ( $columns as $columnLabel => $columnTag ) {
97 $headerContents .= Html
::rawElement( 'td', array(), $columnLabel );
99 $tableContents .= Html
::rawElement( 'tr', array(), "\n$headerContents\n" );
101 $tooltipClass = 'mw-icon-question';
102 if ( isset( $this->mParams
['tooltip-class'] ) ) {
103 $tooltipClass = $this->mParams
['tooltip-class'];
106 // Build the options matrix
107 foreach ( $rows as $rowLabel => $rowTag ) {
108 // Append tooltip if configured
109 if ( isset( $this->mParams
['tooltips'][$rowLabel] ) ) {
110 $tooltipAttribs = array(
111 'class' => "mw-htmlform-tooltip $tooltipClass",
112 'title' => $this->mParams
['tooltips'][$rowLabel],
114 $rowLabel .= ' ' . Html
::element( 'span', $tooltipAttribs, '' );
116 $rowContents = Html
::rawElement( 'td', array(), $rowLabel );
117 foreach ( $columns as $columnTag ) {
118 $thisTag = "$columnTag-$rowTag";
119 // Construct the checkbox
120 $thisAttribs = array(
121 'id' => "{$this->mID}-$thisTag",
124 $checked = in_array( $thisTag, (array)$value, true );
125 if ( $this->isTagForcedOff( $thisTag ) ) {
127 $thisAttribs['disabled'] = 1;
128 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
130 $thisAttribs['disabled'] = 1;
132 $rowContents .= Html
::rawElement(
135 Xml
::check( "{$this->mName}[]", $checked, $attribs +
$thisAttribs )
138 $tableContents .= Html
::rawElement( 'tr', array(), "\n$rowContents\n" );
141 // Put it all in a table
142 $html .= Html
::rawElement( 'table',
143 array( 'class' => 'mw-htmlform-matrix' ),
144 Html
::rawElement( 'tbody', array(), "\n$tableContents\n" ) ) . "\n";
149 protected function isTagForcedOff( $tag ) {
150 return isset( $this->mParams
['force-options-off'] )
151 && in_array( $tag, $this->mParams
['force-options-off'] );
154 protected function isTagForcedOn( $tag ) {
155 return isset( $this->mParams
['force-options-on'] )
156 && in_array( $tag, $this->mParams
['force-options-on'] );
160 * Get the complete table row for the input, including help text,
161 * labels, and whatever.
162 * We override this function since the label should always be on a separate
163 * line above the options in the case of a checkbox matrix, i.e. it's always
164 * a "vertical-label".
166 * @param string $value the value to set the input to
168 * @return String complete HTML table row
170 function getTableRow( $value ) {
171 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
172 $inputHtml = $this->getInputHTML( $value );
173 $fieldType = get_class( $this );
174 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
175 $cellAttributes = array( 'colspan' => 2 );
177 $label = $this->getLabelHtml( $cellAttributes );
179 $field = Html
::rawElement(
181 array( 'class' => 'mw-input' ) +
$cellAttributes,
182 $inputHtml . "\n$errors"
185 $html = Html
::rawElement( 'tr', array( 'class' => 'mw-htmlform-vertical-label' ), $label );
186 $html .= Html
::rawElement( 'tr',
187 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass" ),
190 return $html . $helptext;
194 * @param $request WebRequest
198 function loadDataFromRequest( $request ) {
199 if ( $this->mParent
->getMethod() == 'post' ) {
200 if ( $request->wasPosted() ) {
201 // Checkboxes are not added to the request arrays if they're not checked,
202 // so it's perfectly possible for there not to be an entry at all
203 return $request->getArray( $this->mName
, array() );
205 // That's ok, the user has not yet submitted the form, so show the defaults
206 return $this->getDefault();
209 // This is the impossible case: if we look at $_GET and see no data for our
210 // field, is it because the user has not yet submitted the form, or that they
211 // have submitted it with all the options unchecked. We will have to assume the
212 // latter, which basically means that you can't specify 'positive' defaults
214 return $request->getArray( $this->mName
, array() );
218 function getDefault() {
219 if ( isset( $this->mDefault
) ) {
220 return $this->mDefault
;
226 function filterDataForSubmit( $data ) {
227 $columns = HTMLFormField
::flattenOptions( $this->mParams
['columns'] );
228 $rows = HTMLFormField
::flattenOptions( $this->mParams
['rows'] );
230 foreach ( $columns as $column ) {
231 foreach ( $rows as $row ) {
232 // Make sure option hasn't been forced
233 $thisTag = "$column-$row";
234 if ( $this->isTagForcedOff( $thisTag ) ) {
235 $res[$thisTag] = false;
236 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
237 $res[$thisTag] = true;
239 $res[$thisTag] = in_array( $thisTag, $data );