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 = [
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 public 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.
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' );
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 Array of the options that should be checked
82 public function getInputHTML( $value ) {
85 $rows = $this->mParams
['rows'];
86 $columns = $this->mParams
['columns'];
88 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
90 // Build the column headers
91 $headerContents = Html
::rawElement( 'td', [], ' ' );
92 foreach ( $columns as $columnLabel => $columnTag ) {
93 $headerContents .= Html
::rawElement( 'td', [], $columnLabel );
95 $tableContents .= Html
::rawElement( 'tr', [], "\n$headerContents\n" );
97 $tooltipClass = 'mw-icon-question';
98 if ( isset( $this->mParams
['tooltip-class'] ) ) {
99 $tooltipClass = $this->mParams
['tooltip-class'];
102 // Build the options matrix
103 foreach ( $rows as $rowLabel => $rowTag ) {
104 // Append tooltip if configured
105 if ( isset( $this->mParams
['tooltips'][$rowLabel] ) ) {
107 'class' => "mw-htmlform-tooltip $tooltipClass",
108 'title' => $this->mParams
['tooltips'][$rowLabel],
110 $rowLabel .= ' ' . Html
::element( 'span', $tooltipAttribs, '' );
112 $rowContents = Html
::rawElement( 'td', [], $rowLabel );
113 foreach ( $columns as $columnTag ) {
114 $thisTag = "$columnTag-$rowTag";
115 // Construct the checkbox
117 'id' => "{$this->mID}-$thisTag",
120 $checked = in_array( $thisTag, (array)$value, true );
121 if ( $this->isTagForcedOff( $thisTag ) ) {
123 $thisAttribs['disabled'] = 1;
124 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
125 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
127 $thisAttribs['disabled'] = 1;
128 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
131 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs );
133 $rowContents .= Html
::rawElement(
139 $tableContents .= Html
::rawElement( 'tr', [], "\n$rowContents\n" );
142 // Put it all in a table
143 $html .= Html
::rawElement( 'table',
144 [ 'class' => 'mw-htmlform-matrix' ],
145 Html
::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
150 protected function getOneCheckbox( $checked, $attribs ) {
151 if ( $this->mParent
instanceof OOUIHTMLForm
) {
152 return new OOUI\
CheckboxInputWidget( [
153 'name' => "{$this->mName}[]",
154 'selected' => $checked,
155 ] + OOUI\Element
::configFromHtmlAttributes(
159 $checkbox = Xml
::check( "{$this->mName}[]", $checked, $attribs );
160 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
161 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
163 Html
::element( 'label', [ 'for' => $attribs['id'] ] ) .
164 Html
::closeElement( 'div' );
170 protected function isTagForcedOff( $tag ) {
171 return isset( $this->mParams
['force-options-off'] )
172 && in_array( $tag, $this->mParams
['force-options-off'] );
175 protected function isTagForcedOn( $tag ) {
176 return isset( $this->mParams
['force-options-on'] )
177 && in_array( $tag, $this->mParams
['force-options-on'] );
181 * Get the complete table row for the input, including help text,
182 * labels, and whatever.
183 * We override this function since the label should always be on a separate
184 * line above the options in the case of a checkbox matrix, i.e. it's always
185 * a "vertical-label".
187 * @param string $value The value to set the input to
189 * @return string Complete HTML table row
191 public function getTableRow( $value ) {
192 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
193 $inputHtml = $this->getInputHTML( $value );
194 $fieldType = static::class;
195 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
196 $cellAttributes = [ 'colspan' => 2 ];
199 $hideAttributes = [];
200 if ( $this->mHideIf
) {
201 $hideAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
202 $hideClass = 'mw-htmlform-hide-if';
205 $label = $this->getLabelHtml( $cellAttributes );
207 $field = Html
::rawElement(
209 [ 'class' => 'mw-input' ] +
$cellAttributes,
210 $inputHtml . "\n$errors"
213 $html = Html
::rawElement( 'tr',
214 [ 'class' => "mw-htmlform-vertical-label $hideClass" ] +
$hideAttributes,
216 $html .= Html
::rawElement( 'tr',
217 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
221 return $html . $helptext;
225 * @param WebRequest $request
229 public function loadDataFromRequest( $request ) {
230 if ( $this->isSubmitAttempt( $request ) ) {
231 // Checkboxes are just not added to the request arrays if they're not checked,
232 // so it's perfectly possible for there not to be an entry at all
233 return $request->getArray( $this->mName
, [] );
235 // That's ok, the user has not yet submitted the form, so show the defaults
236 return $this->getDefault();
240 public function getDefault() {
241 if ( isset( $this->mDefault
) ) {
242 return $this->mDefault
;
248 public function filterDataForSubmit( $data ) {
249 $columns = HTMLFormField
::flattenOptions( $this->mParams
['columns'] );
250 $rows = HTMLFormField
::flattenOptions( $this->mParams
['rows'] );
252 foreach ( $columns as $column ) {
253 foreach ( $rows as $row ) {
254 // Make sure option hasn't been forced
255 $thisTag = "$column-$row";
256 if ( $this->isTagForcedOff( $thisTag ) ) {
257 $res[$thisTag] = false;
258 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
259 $res[$thisTag] = true;
261 $res[$thisTag] = in_array( $thisTag, $data );