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 associative array mapping column labels (as HTML) to their tags.
14 * - Required associative array mapping row labels (as HTML) to their tags.
16 * - Array of column-row tags to be displayed as enabled but unavailable to change.
18 * - Array of column-row tags to be displayed as disabled but unavailable to change.
20 * - Optional associative array mapping row labels to tooltips (as text, will be escaped).
22 * - Optional CSS class used on tooltip container span. Defaults to mw-icon-question.
23 * Not used by OOUI form fields.
25 class HTMLCheckMatrix
extends HTMLFormField
implements HTMLNestedFilterable
{
26 static private $requiredParams = [
27 // Required by underlying HTMLFormField
29 // Required by HTMLCheckMatrix
34 public function __construct( $params ) {
35 $missing = array_diff( self
::$requiredParams, array_keys( $params ) );
37 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
39 parent
::__construct( $params );
42 public function validate( $value, $alldata ) {
43 $rows = $this->mParams
['rows'];
44 $columns = $this->mParams
['columns'];
46 // Make sure user-defined validation callback is run
47 $p = parent
::validate( $value, $alldata );
52 // Make sure submitted value is an array
53 if ( !is_array( $value ) ) {
57 // If all options are valid, array_intersect of the valid options
58 // and the provided options will return the provided options.
60 foreach ( $rows as $rowTag ) {
61 foreach ( $columns as $columnTag ) {
62 $validOptions[] = $columnTag . '-' . $rowTag;
65 $validValues = array_intersect( $value, $validOptions );
66 if ( count( $validValues ) == count( $value ) ) {
69 return $this->msg( 'htmlform-select-badoption' );
74 * Build a table containing a matrix of checkbox options.
75 * The value of each option is a combination of the row tag and column tag.
76 * mParams['rows'] is an array with row labels as keys and row tags as values.
77 * mParams['columns'] is an array with column labels as keys and column tags as values.
79 * @param array $value Array of the options that should be checked
83 public function getInputHTML( $value ) {
86 $rows = $this->mParams
['rows'];
87 $columns = $this->mParams
['columns'];
89 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
91 // Build the column headers
92 $headerContents = Html
::rawElement( 'td', [], "\u{00A0}" );
93 foreach ( $columns as $columnLabel => $columnTag ) {
94 $headerContents .= Html
::rawElement( 'td', [], $columnLabel );
96 $tableContents .= Html
::rawElement( 'tr', [], "\n$headerContents\n" );
98 $tooltipClass = 'mw-icon-question';
99 if ( isset( $this->mParams
['tooltip-class'] ) ) {
100 $tooltipClass = $this->mParams
['tooltip-class'];
103 // Build the options matrix
104 foreach ( $rows as $rowLabel => $rowTag ) {
105 // Append tooltip if configured
106 if ( isset( $this->mParams
['tooltips'][$rowLabel] ) ) {
108 'class' => "mw-htmlform-tooltip $tooltipClass",
109 'title' => $this->mParams
['tooltips'][$rowLabel],
110 'aria-label' => $this->mParams
['tooltips'][$rowLabel]
112 $rowLabel .= ' ' . Html
::element( 'span', $tooltipAttribs, '' );
114 $rowContents = Html
::rawElement( 'td', [], $rowLabel );
115 foreach ( $columns as $columnTag ) {
116 $thisTag = "$columnTag-$rowTag";
117 // Construct the checkbox
119 'id' => "{$this->mID}-$thisTag",
122 $checked = in_array( $thisTag, (array)$value, true );
123 if ( $this->isTagForcedOff( $thisTag ) ) {
125 $thisAttribs['disabled'] = 1;
126 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
127 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
129 $thisAttribs['disabled'] = 1;
130 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
133 $checkbox = $this->getOneCheckboxHTML( $checked, $attribs +
$thisAttribs );
135 $rowContents .= Html
::rawElement(
141 $tableContents .= Html
::rawElement( 'tr', [], "\n$rowContents\n" );
144 // Put it all in a table
145 $html .= Html
::rawElement( 'table',
146 [ 'class' => 'mw-htmlform-matrix' ],
147 Html
::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
152 public function getInputOOUI( $value ) {
153 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
155 return new MediaWiki\Widget\
CheckMatrixWidget(
157 'name' => $this->mName
,
160 'rows' => $this->mParams
['rows'],
161 'columns' => $this->mParams
['columns'],
162 'tooltips' => $this->mParams
['tooltips'] ??
[],
163 'forcedOff' => $this->mParams
['force-options-off'] ??
[],
164 'forcedOn' => $this->mParams
['force-options-on'] ??
[],
166 ] + OOUI\Element
::configFromHtmlAttributes( $attribs )
170 protected function getOneCheckboxHTML( $checked, $attribs ) {
171 $checkbox = Xml
::check( "{$this->mName}[]", $checked, $attribs );
172 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
173 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
175 Html
::element( 'label', [ 'for' => $attribs['id'] ] ) .
176 Html
::closeElement( 'div' );
181 protected function isTagForcedOff( $tag ) {
182 return isset( $this->mParams
['force-options-off'] )
183 && in_array( $tag, $this->mParams
['force-options-off'] );
186 protected function isTagForcedOn( $tag ) {
187 return isset( $this->mParams
['force-options-on'] )
188 && in_array( $tag, $this->mParams
['force-options-on'] );
192 * Get the complete table row for the input, including help text,
193 * labels, and whatever.
194 * We override this function since the label should always be on a separate
195 * line above the options in the case of a checkbox matrix, i.e. it's always
196 * a "vertical-label".
198 * @param string $value The value to set the input to
200 * @return string Complete HTML table row
202 public function getTableRow( $value ) {
203 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
204 $inputHtml = $this->getInputHTML( $value );
205 $fieldType = static::class;
206 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
207 $cellAttributes = [ 'colspan' => 2 ];
210 $hideAttributes = [];
211 if ( $this->mHideIf
) {
212 $hideAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
213 $hideClass = 'mw-htmlform-hide-if';
216 $label = $this->getLabelHtml( $cellAttributes );
218 $field = Html
::rawElement(
220 [ 'class' => 'mw-input' ] +
$cellAttributes,
221 $inputHtml . "\n$errors"
224 $html = Html
::rawElement( 'tr',
225 [ 'class' => "mw-htmlform-vertical-label $hideClass" ] +
$hideAttributes,
227 $html .= Html
::rawElement( 'tr',
228 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
232 return $html . $helptext;
236 * @param WebRequest $request
240 public function loadDataFromRequest( $request ) {
241 if ( $this->isSubmitAttempt( $request ) ) {
242 // Checkboxes are just not added to the request arrays if they're not checked,
243 // so it's perfectly possible for there not to be an entry at all
244 return $request->getArray( $this->mName
, [] );
246 // That's ok, the user has not yet submitted the form, so show the defaults
247 return $this->getDefault();
251 public function getDefault() {
252 return $this->mDefault ??
[];
255 public function filterDataForSubmit( $data ) {
256 $columns = HTMLFormField
::flattenOptions( $this->mParams
['columns'] );
257 $rows = HTMLFormField
::flattenOptions( $this->mParams
['rows'] );
259 foreach ( $columns as $column ) {
260 foreach ( $rows as $row ) {
261 // Make sure option hasn't been forced
262 $thisTag = "$column-$row";
263 if ( $this->isTagForcedOff( $thisTag ) ) {
264 $res[$thisTag] = false;
265 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
266 $res[$thisTag] = true;
268 $res[$thisTag] = in_array( $thisTag, $data );
276 protected function getOOUIModules() {
277 return [ 'mediawiki.widgets.CheckMatrixWidget' ];
280 protected function shouldInfuseOOUI() {