| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\Html\Html; |
| 6 |
use MediaWiki\HTMLForm\HTMLFormField; |
| 7 |
use MediaWiki\HTMLForm\HTMLFormFieldRequiredOptionsException; |
| 8 |
use MediaWiki\HTMLForm\HTMLNestedFilterable; |
| 9 |
use MediaWiki\Json\FormatJson; |
| 10 |
use MediaWiki\Request\WebRequest; |
| 11 |
use MediaWiki\Xml\Xml; |
| 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 |
class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable { |
| 42 |
private const REQUIRED_PARAMS = [ |
| 43 |
|
| 44 |
'fieldname', |
| 45 |
|
| 46 |
'rows', |
| 47 |
'columns' |
| 48 |
]; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct( $params ) { |
| 55 |
$missing = array_diff( self::REQUIRED_PARAMS, array_keys( $params ) ); |
| 56 |
if ( $missing ) { |
| 57 |
throw new HTMLFormFieldRequiredOptionsException( $this, $missing ); |
| 58 |
} |
| 59 |
parent::__construct( $params ); |
| 60 |
} |
| 61 |
|
| 62 |
public function validate( $value, $alldata ) { |
| 63 |
$rows = $this->mParams['rows']; |
| 64 |
$columns = $this->mParams['columns']; |
| 65 |
|
| 66 |
|
| 67 |
$p = parent::validate( $value, $alldata ); |
| 68 |
if ( $p !== true ) { |
| 69 |
return $p; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
if ( !is_array( $value ) ) { |
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$validOptions = []; |
| 80 |
foreach ( $rows as $rowTag ) { |
| 81 |
foreach ( $columns as $columnTag ) { |
| 82 |
$validOptions[] = $columnTag . '-' . $rowTag; |
| 83 |
} |
| 84 |
} |
| 85 |
$validValues = array_intersect( $value, $validOptions ); |
| 86 |
if ( count( $validValues ) == count( $value ) ) { |
| 87 |
return true; |
| 88 |
} else { |
| 89 |
return $this->msg( 'htmlform-select-badoption' ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function getInputHTML( $value ) { |
| 104 |
$html = ''; |
| 105 |
$tableContents = ''; |
| 106 |
$rows = $this->mParams['rows']; |
| 107 |
$columns = $this->mParams['columns']; |
| 108 |
|
| 109 |
$attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 110 |
|
| 111 |
|
| 112 |
$headerContents = Html::rawElement( 'td', [], "\u{00A0}" ); |
| 113 |
foreach ( $columns as $columnLabel => $columnTag ) { |
| 114 |
$headerContents .= Html::rawElement( 'th', [], $columnLabel ); |
| 115 |
} |
| 116 |
$thead = Html::rawElement( 'tr', [], "\n$headerContents\n" ); |
| 117 |
$tableContents .= Html::rawElement( 'thead', [], "\n$thead\n" ); |
| 118 |
|
| 119 |
$tooltipClass = 'mw-icon-question'; |
| 120 |
if ( isset( $this->mParams['tooltip-class'] ) ) { |
| 121 |
$tooltipClass = $this->mParams['tooltip-class']; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
foreach ( $rows as $rowLabel => $rowTag ) { |
| 126 |
|
| 127 |
if ( isset( $this->mParams['tooltips'][$rowLabel] ) ) { |
| 128 |
$tooltipAttribs = [ |
| 129 |
'class' => "mw-htmlform-tooltip $tooltipClass", |
| 130 |
'title' => $this->mParams['tooltips'][$rowLabel], |
| 131 |
'aria-label' => $this->mParams['tooltips'][$rowLabel] |
| 132 |
]; |
| 133 |
$rowLabel .= ' ' . Html::element( 'span', $tooltipAttribs, '' ); |
| 134 |
} |
| 135 |
$rowContents = Html::rawElement( 'td', [], $rowLabel ); |
| 136 |
foreach ( $columns as $columnTag ) { |
| 137 |
$thisTag = "$columnTag-$rowTag"; |
| 138 |
|
| 139 |
$thisAttribs = [ |
| 140 |
'id' => "{$this->mID}-$thisTag", |
| 141 |
'value' => $thisTag, |
| 142 |
]; |
| 143 |
$checked = in_array( $thisTag, (array)$value, true ); |
| 144 |
if ( $this->isTagForcedOff( $thisTag ) ) { |
| 145 |
$checked = false; |
| 146 |
$thisAttribs['disabled'] = 1; |
| 147 |
$thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-off'; |
| 148 |
} elseif ( $this->isTagForcedOn( $thisTag ) ) { |
| 149 |
$checked = true; |
| 150 |
$thisAttribs['disabled'] = 1; |
| 151 |
$thisAttribs['class'] = 'checkmatrix-forced checkmatrix-forced-on'; |
| 152 |
} |
| 153 |
|
| 154 |
$checkbox = $this->getOneCheckboxHTML( $checked, $attribs + $thisAttribs ); |
| 155 |
|
| 156 |
$rowContents .= Html::rawElement( |
| 157 |
'td', |
| 158 |
[], |
| 159 |
$checkbox |
| 160 |
); |
| 161 |
} |
| 162 |
$tableContents .= Html::rawElement( 'tr', [], "\n$rowContents\n" ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
$html .= Html::rawElement( 'table', |
| 167 |
[ 'class' => 'mw-htmlform-matrix' ], |
| 168 |
Html::rawElement( 'tbody', [], "\n$tableContents\n" ) ) . "\n"; |
| 169 |
|
| 170 |
return $html; |
| 171 |
} |
| 172 |
|
| 173 |
public function getInputOOUI( $value ) { |
| 174 |
$attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] ); |
| 175 |
|
| 176 |
return new \MediaWiki\Widget\CheckMatrixWidget( |
| 177 |
[ |
| 178 |
'name' => $this->mName, |
| 179 |
'infusable' => true, |
| 180 |
'id' => $this->mID, |
| 181 |
'rows' => $this->mParams['rows'], |
| 182 |
'columns' => $this->mParams['columns'], |
| 183 |
'tooltips' => $this->mParams['tooltips'] ?? [], |
| 184 |
'tooltips-html' => $this->mParams['tooltips-html'] ?? [], |
| 185 |
'forcedOff' => $this->mParams['force-options-off'] ?? [], |
| 186 |
'forcedOn' => $this->mParams['force-options-on'] ?? [], |
| 187 |
'values' => $value, |
| 188 |
] + \OOUI\Element::configFromHtmlAttributes( $attribs ) |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
protected function getOneCheckboxHTML( $checked, $attribs ) { |
| 193 |
return Xml::check( "{$this->mName}[]", $checked, $attribs ); |
| 194 |
} |
| 195 |
|
| 196 |
protected function isTagForcedOff( $tag ) { |
| 197 |
return isset( $this->mParams['force-options-off'] ) |
| 198 |
&& in_array( $tag, $this->mParams['force-options-off'] ); |
| 199 |
} |
| 200 |
|
| 201 |
protected function isTagForcedOn( $tag ) { |
| 202 |
return isset( $this->mParams['force-options-on'] ) |
| 203 |
&& in_array( $tag, $this->mParams['force-options-on'] ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function getTableRow( $value ) { |
| 218 |
[ $errors, $errorClass ] = $this->getErrorsAndErrorClass( $value ); |
| 219 |
$inputHtml = $this->getInputHTML( $value ); |
| 220 |
$fieldType = $this->getClassName(); |
| 221 |
$helptext = $this->getHelpTextHtmlTable( $this->getHelpText() ); |
| 222 |
$cellAttributes = [ 'colspan' => 2 ]; |
| 223 |
|
| 224 |
$moreClass = ''; |
| 225 |
$moreAttributes = []; |
| 226 |
if ( $this->mCondState ) { |
| 227 |
$moreAttributes['data-cond-state'] = FormatJson::encode( $this->mCondState ); |
| 228 |
$moreClass = implode( ' ', $this->mCondStateClass ); |
| 229 |
} |
| 230 |
|
| 231 |
$label = $this->getLabelHtml( $cellAttributes ); |
| 232 |
|
| 233 |
$field = Html::rawElement( |
| 234 |
'td', |
| 235 |
[ 'class' => 'mw-input' ] + $cellAttributes, |
| 236 |
$inputHtml . "\n$errors" |
| 237 |
); |
| 238 |
|
| 239 |
$html = Html::rawElement( 'tr', |
| 240 |
[ 'class' => "mw-htmlform-vertical-label $moreClass" ] + $moreAttributes, |
| 241 |
$label ); |
| 242 |
$html .= Html::rawElement( 'tr', |
| 243 |
[ 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $moreClass" ] + |
| 244 |
$moreAttributes, |
| 245 |
$field ); |
| 246 |
|
| 247 |
return $html . $helptext; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function loadDataFromRequest( $request ) { |
| 256 |
if ( $this->isSubmitAttempt( $request ) ) { |
| 257 |
|
| 258 |
|
| 259 |
return $request->getArray( $this->mName, [] ); |
| 260 |
} else { |
| 261 |
|
| 262 |
return $this->getDefault(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
public function getDefault() { |
| 267 |
return $this->mDefault ?? []; |
| 268 |
} |
| 269 |
|
| 270 |
public function filterDataForSubmit( $data ) { |
| 271 |
$columns = HTMLFormField::flattenOptions( $this->mParams['columns'] ); |
| 272 |
$rows = HTMLFormField::flattenOptions( $this->mParams['rows'] ); |
| 273 |
$res = []; |
| 274 |
foreach ( $columns as $column ) { |
| 275 |
foreach ( $rows as $row ) { |
| 276 |
|
| 277 |
$thisTag = "$column-$row"; |
| 278 |
if ( $this->isTagForcedOff( $thisTag ) ) { |
| 279 |
$res[$thisTag] = false; |
| 280 |
} elseif ( $this->isTagForcedOn( $thisTag ) ) { |
| 281 |
$res[$thisTag] = true; |
| 282 |
} else { |
| 283 |
$res[$thisTag] = in_array( $thisTag, $data ); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
return $res; |
| 289 |
} |
| 290 |
|
| 291 |
protected function getOOUIModules() { |
| 292 |
return [ 'mediawiki.widgets.CheckMatrixWidget' ]; |
| 293 |
} |
| 294 |
|
| 295 |
protected function shouldInfuseOOUI() { |
| 296 |
return true; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
class_alias( HTMLCheckMatrix::class, 'HTMLCheckMatrix' ); |