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