ellapsed => elapsed
[lhc/web/wiklou.git] / includes / htmlform / 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 = array(
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 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 = array();
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' )->parse();
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 function getInputHTML( $value ) {
83 $html = '';
84 $tableContents = '';
85 $rows = $this->mParams['rows'];
86 $columns = $this->mParams['columns'];
87
88 $mappings = array();
89
90 if ( $this->mParent instanceof OOUIHTMLForm ) {
91 $mappings['tabindex'] = 'tabIndex';
92 }
93
94 $attribs = $this->getAttributes( array( 'disabled', 'tabindex' ), $mappings );
95
96 // Build the column headers
97 $headerContents = Html::rawElement( 'td', array(), '&#160;' );
98 foreach ( $columns as $columnLabel => $columnTag ) {
99 $headerContents .= Html::rawElement( 'td', array(), $columnLabel );
100 }
101 $tableContents .= Html::rawElement( 'tr', array(), "\n$headerContents\n" );
102
103 $tooltipClass = 'mw-icon-question';
104 if ( isset( $this->mParams['tooltip-class'] ) ) {
105 $tooltipClass = $this->mParams['tooltip-class'];
106 }
107
108 // Build the options matrix
109 foreach ( $rows as $rowLabel => $rowTag ) {
110 // Append tooltip if configured
111 if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) {
112 $tooltipAttribs = array(
113 'class' => "mw-htmlform-tooltip $tooltipClass",
114 'title' => $this->mParams['tooltips'][$rowLabel],
115 );
116 $rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' );
117 }
118 $rowContents = Html::rawElement( 'td', array(), $rowLabel );
119 foreach ( $columns as $columnTag ) {
120 $thisTag = "$columnTag-$rowTag";
121 // Construct the checkbox
122 $thisId = "{$this->mID}-$thisTag";
123 $thisAttribs = array(
124 'id' => $thisId,
125 'value' => $thisTag,
126 );
127 $checked = in_array( $thisTag, (array)$value, true );
128 if ( $this->isTagForcedOff( $thisTag ) ) {
129 $checked = false;
130 $thisAttribs['disabled'] = 1;
131 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
132 $checked = true;
133 $thisAttribs['disabled'] = 1;
134 }
135 $chkBox = $this->getOneCheckbox( $checked, $attribs + $thisAttribs );
136
137 if ( $this->mParent->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
138 $chkBox = Html::openElement( 'div', array( 'class' => 'mw-ui-checkbox' ) ) .
139 $chkBox .
140 Html::element( 'label', array( 'for' => $thisId ) ) .
141 Html::closeElement( 'div' );
142 }
143 $rowContents .= Html::rawElement(
144 'td',
145 array(),
146 $chkBox
147 );
148 }
149 $tableContents .= Html::rawElement( 'tr', array(), "\n$rowContents\n" );
150 }
151
152 // Put it all in a table
153 $html .= Html::rawElement( 'table',
154 array( 'class' => 'mw-htmlform-matrix' ),
155 Html::rawElement( 'tbody', array(), "\n$tableContents\n" ) ) . "\n";
156
157 return $html;
158 }
159
160 protected function getOneCheckbox( $checked, $attribs ) {
161 if ( $this->mParent instanceof OOUIHTMLForm ) {
162 return new OOUI\CheckboxInputWidget( array(
163 'name' => "{$this->mName}[]",
164 'selected' => $checked,
165 'value' => '1',
166 ) + $attribs );
167 }
168
169 return Xml::check( "{$this->mName}[]", $checked, $attribs );
170 }
171
172 protected function isTagForcedOff( $tag ) {
173 return isset( $this->mParams['force-options-off'] )
174 && in_array( $tag, $this->mParams['force-options-off'] );
175 }
176
177 protected function isTagForcedOn( $tag ) {
178 return isset( $this->mParams['force-options-on'] )
179 && in_array( $tag, $this->mParams['force-options-on'] );
180 }
181
182 /**
183 * Get the complete table row for the input, including help text,
184 * labels, and whatever.
185 * We override this function since the label should always be on a separate
186 * line above the options in the case of a checkbox matrix, i.e. it's always
187 * a "vertical-label".
188 *
189 * @param string $value The value to set the input to
190 *
191 * @return string Complete HTML table row
192 */
193 function getTableRow( $value ) {
194 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
195 $inputHtml = $this->getInputHTML( $value );
196 $fieldType = get_class( $this );
197 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
198 $cellAttributes = array( 'colspan' => 2 );
199
200 $hideClass = '';
201 $hideAttributes = array();
202 if ( $this->mHideIf ) {
203 $hideAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
204 $hideClass = 'mw-htmlform-hide-if';
205 }
206
207 $label = $this->getLabelHtml( $cellAttributes );
208
209 $field = Html::rawElement(
210 'td',
211 array( 'class' => 'mw-input' ) + $cellAttributes,
212 $inputHtml . "\n$errors"
213 );
214
215 $html = Html::rawElement( 'tr',
216 array( 'class' => "mw-htmlform-vertical-label $hideClass" ) + $hideAttributes,
217 $label );
218 $html .= Html::rawElement( 'tr',
219 array( 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $hideClass" ) +
220 $hideAttributes,
221 $field );
222
223 return $html . $helptext;
224 }
225
226 /**
227 * @param WebRequest $request
228 *
229 * @return array
230 */
231 function loadDataFromRequest( $request ) {
232 if ( $this->mParent->getMethod() == 'post' ) {
233 if ( $request->wasPosted() ) {
234 // Checkboxes are not added to the request arrays if they're not checked,
235 // so it's perfectly possible for there not to be an entry at all
236 return $request->getArray( $this->mName, array() );
237 } else {
238 // That's ok, the user has not yet submitted the form, so show the defaults
239 return $this->getDefault();
240 }
241 } else {
242 // This is the impossible case: if we look at $_GET and see no data for our
243 // field, is it because the user has not yet submitted the form, or that they
244 // have submitted it with all the options unchecked. We will have to assume the
245 // latter, which basically means that you can't specify 'positive' defaults
246 // for GET forms.
247 return $request->getArray( $this->mName, array() );
248 }
249 }
250
251 function getDefault() {
252 if ( isset( $this->mDefault ) ) {
253 return $this->mDefault;
254 } else {
255 return array();
256 }
257 }
258
259 function filterDataForSubmit( $data ) {
260 $columns = HTMLFormField::flattenOptions( $this->mParams['columns'] );
261 $rows = HTMLFormField::flattenOptions( $this->mParams['rows'] );
262 $res = array();
263 foreach ( $columns as $column ) {
264 foreach ( $rows as $row ) {
265 // Make sure option hasn't been forced
266 $thisTag = "$column-$row";
267 if ( $this->isTagForcedOff( $thisTag ) ) {
268 $res[$thisTag] = false;
269 } elseif ( $this->isTagForcedOn( $thisTag ) ) {
270 $res[$thisTag] = true;
271 } else {
272 $res[$thisTag] = in_array( $thisTag, $data );
273 }
274 }
275 }
276
277 return $res;
278 }
279 }