MediaWiki REL1_35
HTMLCheckMatrix.php
Go to the documentation of this file.
1<?php
2
28 private static $requiredParams = [
29 // Required by underlying HTMLFormField
30 'fieldname',
31 // Required by HTMLCheckMatrix
32 'rows',
33 'columns'
34 ];
35
36 /*
37 * @stable to call
38 */
39 public function __construct( $params ) {
40 $missing = array_diff( self::$requiredParams, array_keys( $params ) );
41 if ( $missing ) {
42 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
43 }
44 parent::__construct( $params );
45 }
46
47 public function validate( $value, $alldata ) {
48 $rows = $this->mParams['rows'];
49 $columns = $this->mParams['columns'];
50
51 // Make sure user-defined validation callback is run
52 $p = parent::validate( $value, $alldata );
53 if ( $p !== true ) {
54 return $p;
55 }
56
57 // Make sure submitted value is an array
58 if ( !is_array( $value ) ) {
59 return false;
60 }
61
62 // If all options are valid, array_intersect of the valid options
63 // and the provided options will return the provided options.
64 $validOptions = [];
65 foreach ( $rows as $rowTag ) {
66 foreach ( $columns as $columnTag ) {
67 $validOptions[] = $columnTag . '-' . $rowTag;
68 }
69 }
70 $validValues = array_intersect( $value, $validOptions );
71 if ( count( $validValues ) == count( $value ) ) {
72 return true;
73 } else {
74 return $this->msg( 'htmlform-select-badoption' );
75 }
76 }
77
88 public function getInputHTML( $value ) {
89 $html = '';
90 $tableContents = '';
91 $rows = $this->mParams['rows'];
92 $columns = $this->mParams['columns'];
93
94 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
95
96 // Build the column headers
97 $headerContents = Html::rawElement( 'td', [], "\u{00A0}" );
98 foreach ( $columns as $columnLabel => $columnTag ) {
99 $headerContents .= Html::rawElement( 'th', [], $columnLabel );
100 }
101 $thead = Html::rawElement( 'tr', [], "\n$headerContents\n" );
102 $tableContents .= Html::rawElement( 'thead', [], "\n$thead\n" );
103
104 $tooltipClass = 'mw-icon-question';
105 if ( isset( $this->mParams['tooltip-class'] ) ) {
106 $tooltipClass = $this->mParams['tooltip-class'];
107 }
108
109 // Build the options matrix
110 foreach ( $rows as $rowLabel => $rowTag ) {
111 // Append tooltip if configured
112 if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
113 $tooltipAttribs = [
114 'class' => "mw-htmlform-tooltip $tooltipClass",
115 'title' => $this->mParams['tooltips'][$rowLabel],
116 'aria-label' => $this->mParams['tooltips'][$rowLabel]
117 ];
118 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
119 }
120 $rowContents = Html::rawElement( 'td', [], $rowLabel );
121 foreach ( $columns as $columnTag ) {
122 $thisTag = "$columnTag-$rowTag";
123 // Construct the checkbox
124 $thisAttribs = [
125 'id' => "{$this->mID}-$thisTag",
126 'value' => $thisTag,
127 ];
128 $checked = in_array( $thisTag, (array)$value, true );
129 if ( $this->isTagForcedOff( $thisTag ) ) {
130 $checked = false;
131 $thisAttribs['disabled'] = 1;
132 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
133 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
134 $checked = true;
135 $thisAttribs['disabled'] = 1;
136 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
137 }
138
139 $checkbox = $this->getOneCheckboxHTML( $checked, $attribs + $thisAttribs );
140
141 $rowContents .= Html::rawElement(
142 'td',
143 [],
144 $checkbox
145 );
146 }
147 $tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" );
148 }
149
150 // Put it all in a table
151 $html .= Html::rawElement( 'table',
152 [ 'class' => 'mw-htmlform-matrix' ],
153 Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
154
155 return $html;
156 }
157
158 public function getInputOOUI( $value ) {
159 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
160
162 [
163 'name' => $this->mName,
164 'infusable' => true,
165 'id' => $this->mID,
166 'rows' => $this->mParams['rows'],
167 'columns' => $this->mParams['columns'],
168 'tooltips' => $this->mParams['tooltips'] ?? [],
169 'forcedOff' => $this->mParams['force-options-off'] ?? [],
170 'forcedOn' => $this->mParams['force-options-on'] ?? [],
171 'values' => $value,
172 ] + OOUI\Element::configFromHtmlAttributes( $attribs )
173 );
174 }
175
176 protected function getOneCheckboxHTML( $checked, $attribs ) {
177 $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
178 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
179 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
180 $checkbox .
181 Html::element( 'label', [ 'for' => $attribs['id'] ] ) .
182 Html::closeElement( 'div' );
183 }
184 return $checkbox;
185 }
186
187 protected function isTagForcedOff( $tag ) {
188 return isset( $this->mParams['force-options-off'] )
189 && in_array( $tag, $this->mParams['force-options-off'] );
190 }
191
192 protected function isTagForcedOn( $tag ) {
193 return isset( $this->mParams['force-options-on'] )
194 && in_array( $tag, $this->mParams['force-options-on'] );
195 }
196
208 public function getTableRow( $value ) {
209 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
210 $inputHtml = $this->getInputHTML( $value );
211 $fieldType = $this->getClassName();
212 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
213 $cellAttributes = [ 'colspan' => 2 ];
214
215 $hideClass = '';
216 $hideAttributes = [];
217 if ( $this->mHideIf ) {
218 $hideAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
219 $hideClass = 'mw-htmlform-hide-if';
220 }
221
222 $label = $this->getLabelHtml( $cellAttributes );
223
224 $field = Html::rawElement(
225 'td',
226 [ 'class' => 'mw-input' ] + $cellAttributes,
227 $inputHtml . "\n$errors"
228 );
229
230 $html = Html::rawElement( 'tr',
231 [ 'class' => "mw-htmlform-vertical-label $hideClass" ] + $hideAttributes,
232 $label );
233 $html .= Html::rawElement( 'tr',
234 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
235 $hideAttributes,
236 $field );
237
238 return $html . $helptext;
239 }
240
246 public function loadDataFromRequest( $request ) {
247 if ( $this->isSubmitAttempt( $request ) ) {
248 // Checkboxes are just not added to the request arrays if they're not checked,
249 // so it's perfectly possible for there not to be an entry at all
250 return $request->getArray( $this->mName, [] );
251 } else {
252 // That's ok, the user has not yet submitted the form, so show the defaults
253 return $this->getDefault();
254 }
255 }
256
257 public function getDefault() {
258 return $this->mDefault ?? [];
259 }
260
261 public function filterDataForSubmit( $data ) {
262 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
263 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
264 $res = [];
265 foreach ( $columns as $column ) {
266 foreach ( $rows as $row ) {
267 // Make sure option hasn't been forced
268 $thisTag = "$column-$row";
269 if ( $this->isTagForcedOff( $thisTag ) ) {
270 $res[$thisTag] = false;
271 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
272 $res[$thisTag] = true;
273 } else {
274 $res[$thisTag] = in_array( $thisTag, $data );
275 }
276 }
277 }
278
279 return $res;
280 }
281
282 protected function getOOUIModules() {
283 return [ 'mediawiki.widgets.CheckMatrixWidget' ];
284 }
285
286 protected function shouldInfuseOOUI() {
287 return true;
288 }
289}
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.
getDefault()
Stable to override.
__construct( $params)
Initialise the object.
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 Stable to override.
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=[])
Stable to override.
getAttributes(array $list)
Returns the given attributes from the parameters Stable to override.