MediaWiki 1.41.2
HTMLCheckMatrix.php
Go to the documentation of this file.
1<?php
2
6
32 private static $requiredParams = [
33 // Required by underlying HTMLFormField
34 'fieldname',
35 // Required by HTMLCheckMatrix
36 'rows',
37 'columns'
38 ];
39
44 public function __construct( $params ) {
45 $missing = array_diff( self::$requiredParams, array_keys( $params ) );
46 if ( $missing ) {
47 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
48 }
49 parent::__construct( $params );
50 }
51
52 public function validate( $value, $alldata ) {
53 $rows = $this->mParams['rows'];
54 $columns = $this->mParams['columns'];
55
56 // Make sure user-defined validation callback is run
57 $p = parent::validate( $value, $alldata );
58 if ( $p !== true ) {
59 return $p;
60 }
61
62 // Make sure submitted value is an array
63 if ( !is_array( $value ) ) {
64 return false;
65 }
66
67 // If all options are valid, array_intersect of the valid options
68 // and the provided options will return the provided options.
69 $validOptions = [];
70 foreach ( $rows as $rowTag ) {
71 foreach ( $columns as $columnTag ) {
72 $validOptions[] = $columnTag . '-' . $rowTag;
73 }
74 }
75 $validValues = array_intersect( $value, $validOptions );
76 if ( count( $validValues ) == count( $value ) ) {
77 return true;
78 } else {
79 return $this->msg( 'htmlform-select-badoption' );
80 }
81 }
82
93 public function getInputHTML( $value ) {
94 $html = '';
95 $tableContents = '';
96 $rows = $this->mParams['rows'];
97 $columns = $this->mParams['columns'];
98
99 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
100
101 // Build the column headers
102 $headerContents = Html::rawElement( 'td', [], "\u{00A0}" );
103 foreach ( $columns as $columnLabel => $columnTag ) {
104 $headerContents .= Html::rawElement( 'th', [], $columnLabel );
105 }
106 $thead = Html::rawElement( 'tr', [], "\n$headerContents\n" );
107 $tableContents .= Html::rawElement( 'thead', [], "\n$thead\n" );
108
109 $tooltipClass = 'mw-icon-question';
110 if ( isset( $this->mParams['tooltip-class'] ) ) {
111 $tooltipClass = $this->mParams['tooltip-class'];
112 }
113
114 // Build the options matrix
115 foreach ( $rows as $rowLabel => $rowTag ) {
116 // Append tooltip if configured
117 if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
118 $tooltipAttribs = [
119 'class' => "mw-htmlform-tooltip $tooltipClass",
120 'title' => $this->mParams['tooltips'][$rowLabel],
121 'aria-label' => $this->mParams['tooltips'][$rowLabel]
122 ];
123 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
124 }
125 $rowContents = Html::rawElement( 'td', [], $rowLabel );
126 foreach ( $columns as $columnTag ) {
127 $thisTag = "$columnTag-$rowTag";
128 // Construct the checkbox
129 $thisAttribs = [
130 'id' => "{$this->mID}-$thisTag",
131 'value' => $thisTag,
132 ];
133 $checked = in_array( $thisTag, (array)$value, true );
134 if ( $this->isTagForcedOff( $thisTag ) ) {
135 $checked = false;
136 $thisAttribs['disabled'] = 1;
137 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
138 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
139 $checked = true;
140 $thisAttribs['disabled'] = 1;
141 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
142 }
143
144 $checkbox = $this->getOneCheckboxHTML( $checked, $attribs + $thisAttribs );
145
146 $rowContents .= Html::rawElement(
147 'td',
148 [],
149 $checkbox
150 );
151 }
152 $tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" );
153 }
154
155 // Put it all in a table
156 $html .= Html::rawElement( 'table',
157 [ 'class' => 'mw-htmlform-matrix' ],
158 Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
159
160 return $html;
161 }
162
163 public function getInputOOUI( $value ) {
164 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
165
167 [
168 'name' => $this->mName,
169 'infusable' => true,
170 'id' => $this->mID,
171 'rows' => $this->mParams['rows'],
172 'columns' => $this->mParams['columns'],
173 'tooltips' => $this->mParams['tooltips'] ?? [],
174 'forcedOff' => $this->mParams['force-options-off'] ?? [],
175 'forcedOn' => $this->mParams['force-options-on'] ?? [],
176 'values' => $value,
177 ] + OOUI\Element::configFromHtmlAttributes( $attribs )
178 );
179 }
180
181 protected function getOneCheckboxHTML( $checked, $attribs ) {
182 $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
183 if ( $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere ) ) {
184 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
185 $checkbox .
186 Html::element( 'label', [ 'for' => $attribs['id'] ] ) .
187 Html::closeElement( 'div' );
188 }
189 return $checkbox;
190 }
191
192 protected function isTagForcedOff( $tag ) {
193 return isset( $this->mParams['force-options-off'] )
194 && in_array( $tag, $this->mParams['force-options-off'] );
195 }
196
197 protected function isTagForcedOn( $tag ) {
198 return isset( $this->mParams['force-options-on'] )
199 && in_array( $tag, $this->mParams['force-options-on'] );
200 }
201
213 public function getTableRow( $value ) {
214 [ $errors, $errorClass ] = $this->getErrorsAndErrorClass( $value );
215 $inputHtml = $this->getInputHTML( $value );
216 $fieldType = $this->getClassName();
217 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
218 $cellAttributes = [ 'colspan' => 2 ];
219
220 $moreClass = '';
221 $moreAttributes = [];
222 if ( $this->mCondState ) {
223 $moreAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState );
224 $moreClass = implode( ' ', $this->mCondStateClass );
225 }
226
227 $label = $this->getLabelHtml( $cellAttributes );
228
229 $field = Html::rawElement(
230 'td',
231 [ 'class' => 'mw-input' ] + $cellAttributes,
232 $inputHtml . "\n$errors"
233 );
234
235 $html = Html::rawElement( 'tr',
236 [ 'class' => "mw-htmlform-vertical-label $moreClass" ] + $moreAttributes,
237 $label );
238 $html .= Html::rawElement( 'tr',
239 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $moreClass" ] +
240 $moreAttributes,
241 $field );
242
243 return $html . $helptext;
244 }
245
251 public function loadDataFromRequest( $request ) {
252 if ( $this->isSubmitAttempt( $request ) ) {
253 // Checkboxes are just not added to the request arrays if they're not checked,
254 // so it's perfectly possible for there not to be an entry at all
255 return $request->getArray( $this->mName, [] );
256 } else {
257 // That's ok, the user has not yet submitted the form, so show the defaults
258 return $this->getDefault();
259 }
260 }
261
262 public function getDefault() {
263 return $this->mDefault ?? [];
264 }
265
266 public function filterDataForSubmit( $data ) {
267 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
268 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
269 $res = [];
270 foreach ( $columns as $column ) {
271 foreach ( $rows as $row ) {
272 // Make sure option hasn't been forced
273 $thisTag = "$column-$row";
274 if ( $this->isTagForcedOff( $thisTag ) ) {
275 $res[$thisTag] = false;
276 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
277 $res[$thisTag] = true;
278 } else {
279 $res[$thisTag] = in_array( $thisTag, $data );
280 }
281 }
282 }
283
284 return $res;
285 }
286
287 protected function getOOUIModules() {
288 return [ 'mediawiki.widgets.CheckMatrixWidget' ];
289 }
290
291 protected function shouldInfuseOOUI() {
292 return true;
293 }
294}
A checkbox matrix Operates similarly to HTMLMultiSelectField, but instead of using an array of option...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
getOneCheckboxHTML( $checked, $attribs)
shouldInfuseOOUI()
Whether the field should be automatically infused.
getInputHTML( $value)
Build a table containing a matrix of checkbox options.
getTableRow( $value)
Get the complete table row for the input, including help text, labels, and whatever.
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
loadDataFromRequest( $request)
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
The parent class to generate form fields.
getClassName()
Gets the non namespaced class name.
getErrorsAndErrorClass( $value)
Determine form errors to display and their classes.
getHelpTextHtmlTable( $helptext)
Generate help text HTML in table format.
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getHelpText()
Determine the help text to display.
isSubmitAttempt(WebRequest $request)
Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to just v...
msg( $key,... $params)
Get a translated interface message.
getLabelHtml( $cellAttributes=[])
getAttributes(array $list)
Returns the given attributes from the parameters.
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
A class containing constants representing the names of configuration variables.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...