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