Merge "Rewrite pref cleanup script"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLCheckMatrix.php
1 <?php
2
3 /**
4 * A checkbox matrix
5 * Operates similarly to HTMLMultiSelectField, but instead of using an array of
6 * options, uses an array of rows and an array of columns to dynamically
7 * construct a matrix of options. The tags used to identify a particular cell
8 * are of the form "columnName-rowName"
9 *
10 * Options:
11 * - columns
12 * - Required list of columns in the matrix.
13 * - rows
14 * - Required list of rows in the matrix.
15 * - force-options-on
16 * - Accepts array of column-row tags to be displayed as enabled but unavailable to change
17 * - force-options-off
18 * - Accepts array of column-row tags to be displayed as disabled but unavailable to change.
19 * - tooltips
20 * - Optional array mapping row label to tooltip content
21 * - tooltip-class
22 * - Optional CSS class used on tooltip container span. Defaults to mw-icon-question.
23 */
24 class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
25 static private $requiredParams = [
26 // Required by underlying HTMLFormField
27 'fieldname',
28 // Required by HTMLCheckMatrix
29 'rows',
30 'columns'
31 ];
32
33 public function __construct( $params ) {
34 $missing = array_diff( self::$requiredParams, array_keys( $params ) );
35 if ( $missing ) {
36 throw new HTMLFormFieldRequiredOptionsException( $this, $missing );
37 }
38 parent::__construct( $params );
39 }
40
41 public function validate( $value, $alldata ) {
42 $rows = $this->mParams['rows'];
43 $columns = $this->mParams['columns'];
44
45 // Make sure user-defined validation callback is run
46 $p = parent::validate( $value, $alldata );
47 if ( $p !== true ) {
48 return $p;
49 }
50
51 // Make sure submitted value is an array
52 if ( !is_array( $value ) ) {
53 return false;
54 }
55
56 // If all options are valid, array_intersect of the valid options
57 // and the provided options will return the provided options.
58 $validOptions = [];
59 foreach ( $rows as $rowTag ) {
60 foreach ( $columns as $columnTag ) {
61 $validOptions[] = $columnTag . '-' . $rowTag;
62 }
63 }
64 $validValues = array_intersect( $value, $validOptions );
65 if ( count( $validValues ) == count( $value ) ) {
66 return true;
67 } else {
68 return $this->msg( 'htmlform-select-badoption' );
69 }
70 }
71
72 /**
73 * Build a table containing a matrix of checkbox options.
74 * The value of each option is a combination of the row tag and column tag.
75 * mParams['rows'] is an array with row labels as keys and row tags as values.
76 * mParams['columns'] is an array with column labels as keys and column tags as values.
77 *
78 * @param array $value Array of the options that should be checked
79 *
80 * @return string
81 */
82 public function getInputHTML( $value ) {
83 $html = '';
84 $tableContents = '';
85 $rows = $this->mParams['rows'];
86 $columns = $this->mParams['columns'];
87
88 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
89
90 // Build the column headers
91 $headerContents = Html::rawElement( 'td', [], '&#160;' );
92 foreach ( $columns as $columnLabel => $columnTag ) {
93 $headerContents .= Html::rawElement( 'td', [], $columnLabel );
94 }
95 $tableContents .= Html::rawElement( 'tr', [], "\n$headerContents\n" );
96
97 $tooltipClass = 'mw-icon-question';
98 if ( isset( $this->mParams['tooltip-class'] ) ) {
99 $tooltipClass = $this->mParams['tooltip-class'];
100 }
101
102 // Build the options matrix
103 foreach ( $rows as $rowLabel => $rowTag ) {
104 // Append tooltip if configured
105 if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
106 $tooltipAttribs = [
107 'class' => "mw-htmlform-tooltip $tooltipClass",
108 'title' => $this->mParams['tooltips'][$rowLabel],
109 'aria-label' => $this->mParams['tooltips'][$rowLabel]
110 ];
111 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
112 }
113 $rowContents = Html::rawElement( 'td', [], $rowLabel );
114 foreach ( $columns as $columnTag ) {
115 $thisTag = "$columnTag-$rowTag";
116 // Construct the checkbox
117 $thisAttribs = [
118 'id' => "{$this->mID}-$thisTag",
119 'value' => $thisTag,
120 ];
121 $checked = in_array( $thisTag, (array)$value, true );
122 if ( $this->isTagForcedOff( $thisTag ) ) {
123 $checked = false;
124 $thisAttribs['disabled'] = 1;
125 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off';
126 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
127 $checked = true;
128 $thisAttribs['disabled'] = 1;
129 $thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on';
130 }
131
132 $checkbox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs );
133
134 $rowContents .= Html::rawElement(
135 'td',
136 [],
137 $checkbox
138 );
139 }
140 $tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" );
141 }
142
143 // Put it all in a table
144 $html .= Html::rawElement( 'table',
145 [ 'class' => 'mw-htmlform-matrix' ],
146 Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n";
147
148 return $html;
149 }
150
151 protected function getOneCheckbox( $checked, $attribs ) {
152 if ( $this->mParent instanceof OOUIHTMLForm ) {
153 return new OOUI\CheckboxInputWidget( [
154 'name' => "{$this->mName}[]",
155 'selected' => $checked,
156 ] + OOUI\Element::configFromHtmlAttributes(
157 $attribs
158 ) );
159 } else {
160 $checkbox = Xml::check( "{$this->mName}[]", $checked, $attribs );
161 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
162 $checkbox = Html::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
163 $checkbox .
164 Html::element( 'label', [ 'for' => $attribs['id'] ] ) .
165 Html::closeElement( 'div' );
166 }
167 return $checkbox;
168 }
169 }
170
171 protected function isTagForcedOff( $tag ) {
172 return isset( $this->mParams['force-options-off'] )
173 && in_array( $tag, $this->mParams['force-options-off'] );
174 }
175
176 protected function isTagForcedOn( $tag ) {
177 return isset( $this->mParams['force-options-on'] )
178 && in_array( $tag, $this->mParams['force-options-on'] );
179 }
180
181 /**
182 * Get the complete table row for the input, including help text,
183 * labels, and whatever.
184 * We override this function since the label should always be on a separate
185 * line above the options in the case of a checkbox matrix, i.e. it's always
186 * a "vertical-label".
187 *
188 * @param string $value The value to set the input to
189 *
190 * @return string Complete HTML table row
191 */
192 public function getTableRow( $value ) {
193 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
194 $inputHtml = $this->getInputHTML( $value );
195 $fieldType = static::class;
196 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
197 $cellAttributes = [ 'colspan' => 2 ];
198
199 $hideClass = '';
200 $hideAttributes = [];
201 if ( $this->mHideIf ) {
202 $hideAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
203 $hideClass = 'mw-htmlform-hide-if';
204 }
205
206 $label = $this->getLabelHtml( $cellAttributes );
207
208 $field = Html::rawElement(
209 'td',
210 [ 'class' => 'mw-input' ] + $cellAttributes,
211 $inputHtml . "\n$errors"
212 );
213
214 $html = Html::rawElement( 'tr',
215 [ 'class' => "mw-htmlform-vertical-label $hideClass" ] + $hideAttributes,
216 $label );
217 $html .= Html::rawElement( 'tr',
218 [ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ] +
219 $hideAttributes,
220 $field );
221
222 return $html . $helptext;
223 }
224
225 /**
226 * @param WebRequest $request
227 *
228 * @return array
229 */
230 public function loadDataFromRequest( $request ) {
231 if ( $this->isSubmitAttempt( $request ) ) {
232 // Checkboxes are just not added to the request arrays if they're not checked,
233 // so it's perfectly possible for there not to be an entry at all
234 return $request->getArray( $this->mName, [] );
235 } else {
236 // That's ok, the user has not yet submitted the form, so show the defaults
237 return $this->getDefault();
238 }
239 }
240
241 public function getDefault() {
242 if ( isset( $this->mDefault ) ) {
243 return $this->mDefault;
244 } else {
245 return [];
246 }
247 }
248
249 public function filterDataForSubmit( $data ) {
250 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
251 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
252 $res = [];
253 foreach ( $columns as $column ) {
254 foreach ( $rows as $row ) {
255 // Make sure option hasn't been forced
256 $thisTag = "$column-$row";
257 if ( $this->isTagForcedOff( $thisTag ) ) {
258 $res[$thisTag] = false;
259 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
260 $res[$thisTag] = true;
261 } else {
262 $res[$thisTag] = in_array( $thisTag, $data );
263 }
264 }
265 }
266
267 return $res;
268 }
269 }