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