MediaWiki REL1_33
HTMLCheckMatrix.php
Go to the documentation of this file.
1<?php
2
26 private static $requiredParams = [
27 // Required by underlying HTMLFormField
28 'fieldname',
29 // Required by HTMLCheckMatrix
30 'rows',
31 'columns'
32 ];
33
34 public function __construct( $params ) {
35 $missing = array_diff( self::$requiredParams, array_keys( $params ) );
36 if ( $missing ) {
37 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
38 }
39 parent::__construct( $params );
40 }
41
42 public function validate( $value, $alldata ) {
43 $rows = $this->mParams['rows'];
44 $columns = $this->mParams['columns'];
45
46 // Make sure user-defined validation callback is run
47 $p = parent::validate( $value, $alldata );
48 if ( $p !== true ) {
49 return $p;
50 }
51
52 // Make sure submitted value is an array
53 if ( !is_array( $value ) ) {
54 return false;
55 }
56
57 // If all options are valid, array_intersect of the valid options
58 // and the provided options will return the provided options.
59 $validOptions = [];
60 foreach ( $rows as $rowTag ) {
61 foreach ( $columns as $columnTag ) {
62 $validOptions[] = $columnTag . '-' . $rowTag;
63 }
64 }
65 $validValues = array_intersect( $value, $validOptions );
66 if ( count( $validValues ) == count( $value ) ) {
67 return true;
68 } else {
69 return $this->msg( 'htmlform-select-badoption' );
70 }
71 }
72
83 public function getInputHTML( $value ) {
84 $html = '';
85 $tableContents = '';
86 $rows = $this->mParams['rows'];
87 $columns = $this->mParams['columns'];
88
89 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
90
91 // Build the column headers
92 $headerContents = Html::rawElement( 'td', [], "\u{00A0}" );
93 foreach ( $columns as $columnLabel => $columnTag ) {
94 $headerContents .= Html::rawElement( 'th', [], $columnLabel );
95 }
96 $thead = Html::rawElement( 'tr', [], "\n$headerContents\n" );
97 $tableContents .= Html::rawElement( 'thead', [], "\n$thead\n" );
98
99 $tooltipClass = 'mw-icon-question';
100 if ( isset( $this->mParams['tooltip-class'] ) ) {
101 $tooltipClass = $this->mParams['tooltip-class'];
102 }
103
104 // Build the options matrix
105 foreach ( $rows as $rowLabel => $rowTag ) {
106 // Append tooltip if configured
107 if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
108 $tooltipAttribs = [
109 'class' => "mw-htmlform-tooltip $tooltipClass",
110 'title' => $this->mParams['tooltips'][$rowLabel],
111 'aria-label' => $this->mParams['tooltips'][$rowLabel]
112 ];
113 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
114 }
115 $rowContents = Html::rawElement( 'td', [], $rowLabel );
116 foreach ( $columns as $columnTag ) {
117 $thisTag = "$columnTag-$rowTag";
118 // Construct the checkbox
119 $thisAttribs = [
120 'id' => "{$this->mID}-$thisTag",
121 'value' => $thisTag,
122 ];
123 $checked = in_array( $thisTag, (array)$value, true );
124 if ( $this->isTagForcedOff( $thisTag ) ) {
125 $checked = false;
126 $thisAttribs['disabled'] = 1;
127 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
128 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
129 $checked = true;
130 $thisAttribs['disabled'] = 1;
131 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
132 }
133
134 $checkbox = $this->getOneCheckboxHTML( $checked, $attribs + $thisAttribs );
135
136 $rowContents .= Html::rawElement(
137 'td',
138 [],
139 $checkbox
140 );
141 }
142 $tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" );
143 }
144
145 // Put it all in a table
146 $html .= Html::rawElement( 'table',
147 [ 'class' => 'mw-htmlform-matrix' ],
148 Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
149
150 return $html;
151 }
152
153 public function getInputOOUI( $value ) {
154 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
155
157 [
158 'name' => $this->mName,
159 'infusable' => true,
160 'id' => $this->mID,
161 'rows' => $this->mParams['rows'],
162 'columns' => $this->mParams['columns'],
163 'tooltips' => $this->mParams['tooltips'] ?? [],
164 'forcedOff' => $this->mParams['force-options-off'] ?? [],
165 'forcedOn' => $this->mParams['force-options-on'] ?? [],
166 'values' => $value,
167 ] + OOUI\Element::configFromHtmlAttributes( $attribs )
168 );
169 }
170
171 protected function getOneCheckboxHTML( $checked, $attribs ) {
172 $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
173 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
174 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
175 $checkbox .
176 Html::element( 'label', [ 'for' => $attribs['id'] ] ) .
177 Html::closeElement( 'div' );
178 }
179 return $checkbox;
180 }
181
182 protected function isTagForcedOff( $tag ) {
183 return isset( $this->mParams['force-options-off'] )
184 && in_array( $tag, $this->mParams['force-options-off'] );
185 }
186
187 protected function isTagForcedOn( $tag ) {
188 return isset( $this->mParams['force-options-on'] )
189 && in_array( $tag, $this->mParams['force-options-on'] );
190 }
191
203 public function getTableRow( $value ) {
204 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
205 $inputHtml = $this->getInputHTML( $value );
206 $fieldType = static::class;
207 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
208 $cellAttributes = [ 'colspan' => 2 ];
209
210 $hideClass = '';
211 $hideAttributes = [];
212 if ( $this->mHideIf ) {
213 $hideAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
214 $hideClass = 'mw-htmlform-hide-if';
215 }
216
217 $label = $this->getLabelHtml( $cellAttributes );
218
219 $field = Html::rawElement(
220 'td',
221 [ 'class' => 'mw-input' ] + $cellAttributes,
222 $inputHtml . "\n$errors"
223 );
224
225 $html = Html::rawElement( 'tr',
226 [ 'class' => "mw-htmlform-vertical-label $hideClass" ] + $hideAttributes,
227 $label );
228 $html .= Html::rawElement( 'tr',
229 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
230 $hideAttributes,
231 $field );
232
233 return $html . $helptext;
234 }
235
241 public function loadDataFromRequest( $request ) {
242 if ( $this->isSubmitAttempt( $request ) ) {
243 // Checkboxes are just not added to the request arrays if they're not checked,
244 // so it's perfectly possible for there not to be an entry at all
245 return $request->getArray( $this->mName, [] );
246 } else {
247 // That's ok, the user has not yet submitted the form, so show the defaults
248 return $this->getDefault();
249 }
250 }
251
252 public function getDefault() {
253 return $this->mDefault ?? [];
254 }
255
256 public function filterDataForSubmit( $data ) {
257 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
258 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
259 $res = [];
260 foreach ( $columns as $column ) {
261 foreach ( $rows as $row ) {
262 // Make sure option hasn't been forced
263 $thisTag = "$column-$row";
264 if ( $this->isTagForcedOff( $thisTag ) ) {
265 $res[$thisTag] = false;
266 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
267 $res[$thisTag] = true;
268 } else {
269 $res[$thisTag] = in_array( $thisTag, $data );
270 }
271 }
272 }
273
274 return $res;
275 }
276
277 protected function getOOUIModules() {
278 return [ 'mediawiki.widgets.CheckMatrixWidget' ];
279 }
280
281 protected function shouldInfuseOOUI() {
282 return true;
283 }
284}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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.
__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.
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...
getLabelHtml( $cellAttributes=[])
getAttributes(array $list)
Returns the given attributes from the parameters.
msg()
Get a translated interface message.
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2818
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition hooks.txt:2843
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2012
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$params