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