| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Widget; |
| 4 |
|
| 5 |
use OOUI\CheckboxInputWidget; |
| 6 |
use OOUI\FieldLayout; |
| 7 |
use OOUI\HtmlSnippet; |
| 8 |
use OOUI\Tag; |
| 9 |
use OOUI\Widget; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class CheckMatrixWidget extends Widget { |
| 18 |
|
| 19 |
protected $name; |
| 20 |
|
| 21 |
protected $id; |
| 22 |
|
| 23 |
protected $columns; |
| 24 |
|
| 25 |
protected $rows; |
| 26 |
|
| 27 |
protected $tooltips; |
| 28 |
|
| 29 |
protected $tooltipsHtml; |
| 30 |
|
| 31 |
protected $values; |
| 32 |
|
| 33 |
protected $forcedOn; |
| 34 |
|
| 35 |
protected $forcedOff; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct( array $config = [] ) { |
| 59 |
|
| 60 |
|
| 61 |
parent::__construct( $config ); |
| 62 |
|
| 63 |
$this->name = $config['name'] ?? null; |
| 64 |
$this->id = $config['id'] ?? null; |
| 65 |
|
| 66 |
|
| 67 |
$this->rows = $config['rows'] ?? []; |
| 68 |
$this->columns = $config['columns'] ?? []; |
| 69 |
$this->tooltips = $config['tooltips'] ?? []; |
| 70 |
$this->tooltipsHtml = $config['tooltips-html'] ?? []; |
| 71 |
|
| 72 |
$this->values = $config['values'] ?? []; |
| 73 |
|
| 74 |
$this->forcedOn = $config['forcedOn'] ?? []; |
| 75 |
$this->forcedOff = $config['forcedOff'] ?? []; |
| 76 |
|
| 77 |
|
| 78 |
$table = new Tag( 'table' ); |
| 79 |
$table->addClasses( [ 'mw-htmlform-matrix mw-widget-checkMatrixWidget-matrix' ] ); |
| 80 |
$thead = new Tag( 'thead' ); |
| 81 |
$table->appendContent( $thead ); |
| 82 |
$tr = new Tag( 'tr' ); |
| 83 |
|
| 84 |
|
| 85 |
$tr->appendContent( $this->getCellTag( "\u{00A0}" ) ); |
| 86 |
foreach ( $this->columns as $columnLabel => $columnTag ) { |
| 87 |
$tr->appendContent( |
| 88 |
$this->getCellTag( new HtmlSnippet( $columnLabel ), 'th' ) |
| 89 |
); |
| 90 |
} |
| 91 |
$thead->appendContent( $tr ); |
| 92 |
|
| 93 |
|
| 94 |
$tbody = new Tag( 'tbody' ); |
| 95 |
$table->appendContent( $tbody ); |
| 96 |
foreach ( $this->rows as $rowLabel => $rowTag ) { |
| 97 |
$tbody->appendContent( |
| 98 |
$this->getTableRow( $rowLabel, $rowTag ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
$this->addClasses( [ 'mw-widget-checkMatrixWidget' ] ); |
| 104 |
$this->appendContent( $table ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private function getTableRow( $label, $tag ) { |
| 117 |
$row = new Tag( 'tr' ); |
| 118 |
$tooltip = $this->getTooltip( $label ); |
| 119 |
$labelFieldConfig = $tooltip ? [ 'help' => $tooltip ] : []; |
| 120 |
|
| 121 |
$labelField = new FieldLayout( |
| 122 |
new Widget(), |
| 123 |
[ |
| 124 |
'label' => new HtmlSnippet( $label ), |
| 125 |
'align' => 'inline', |
| 126 |
] + $labelFieldConfig |
| 127 |
); |
| 128 |
$row->appendContent( $this->getCellTag( $labelField ) ); |
| 129 |
|
| 130 |
|
| 131 |
foreach ( $this->columns as $columnTag ) { |
| 132 |
$thisTag = "$columnTag-$tag"; |
| 133 |
|
| 134 |
|
| 135 |
$checkbox = new CheckboxInputWidget( [ |
| 136 |
'value' => $thisTag, |
| 137 |
'name' => $this->name ? "{$this->name}[]" : null, |
| 138 |
'id' => $this->id ? "{$this->id}-$thisTag" : null, |
| 139 |
'selected' => $this->isTagChecked( $thisTag ), |
| 140 |
'disabled' => $this->isTagDisabled( $thisTag ), |
| 141 |
] ); |
| 142 |
|
| 143 |
$row->appendContent( $this->getCellTag( $checkbox ) ); |
| 144 |
} |
| 145 |
return $row; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
private function getCellTag( $content, $tagElement = 'td' ) { |
| 156 |
$cell = new Tag( $tagElement ); |
| 157 |
$cell->appendContent( $content ); |
| 158 |
return $cell; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function isTagChecked( $tagName ) { |
| 169 |
|
| 170 |
return in_array( $tagName, (array)$this->values, true ) || |
| 171 |
|
| 172 |
in_array( $tagName, (array)$this->forcedOn, true ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
private function isTagDisabled( $tagName ) { |
| 183 |
return ( |
| 184 |
|
| 185 |
$this->isDisabled() || |
| 186 |
|
| 187 |
in_array( $tagName, (array)$this->forcedOn, true ) || |
| 188 |
in_array( $tagName, (array)$this->forcedOff, true ) |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private function getTooltip( $label ) { |
| 200 |
if ( isset( $this->tooltipsHtml[ $label ] ) ) { |
| 201 |
return new HtmlSnippet( $this->tooltipsHtml[ $label ] ); |
| 202 |
} else { |
| 203 |
return $this->tooltips[ $label ] ?? null; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
protected function getJavaScriptClassName() { |
| 208 |
return 'mw.widgets.CheckMatrixWidget'; |
| 209 |
} |
| 210 |
|
| 211 |
public function getConfig( &$config ) { |
| 212 |
$config += [ |
| 213 |
'name' => $this->name, |
| 214 |
'id' => $this->id, |
| 215 |
'rows' => $this->rows, |
| 216 |
'columns' => $this->columns, |
| 217 |
'tooltips' => $this->tooltips, |
| 218 |
'tooltipsHtml' => $this->tooltipsHtml, |
| 219 |
'forcedOff' => $this->forcedOff, |
| 220 |
'forcedOn' => $this->forcedOn, |
| 221 |
'values' => $this->values, |
| 222 |
]; |
| 223 |
return parent::getConfig( $config ); |
| 224 |
} |
| 225 |
} |