61 parent::__construct( $config );
63 $this->name = $config[
'name'] ??
null;
64 $this->
id = $config[
'id'] ??
null;
67 $this->rows = $config[
'rows'] ?? [];
68 $this->columns = $config[
'columns'] ?? [];
69 $this->tooltips = $config[
'tooltips'] ?? [];
70 $this->tooltipsHtml = $config[
'tooltips-html'] ?? [];
72 $this->values = $config[
'values'] ?? [];
74 $this->forcedOn = $config[
'forcedOn'] ?? [];
75 $this->forcedOff = $config[
'forcedOff'] ?? [];
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' );
85 $tr->appendContent( $this->getCellTag(
"\u{00A0}" ) );
86 foreach ( $this->columns as $columnLabel => $columnTag ) {
88 $this->getCellTag(
new HtmlSnippet( $columnLabel ),
'th' )
91 $thead->appendContent( $tr );
94 $tbody =
new Tag(
'tbody' );
95 $table->appendContent( $tbody );
96 foreach ( $this->rows as $rowLabel => $rowTag ) {
97 $tbody->appendContent(
98 $this->getTableRow( $rowLabel, $rowTag )
103 $this->addClasses( [
'mw-widget-checkMatrixWidget' ] );
104 $this->appendContent( $table );
116 private function getTableRow( $label, $tag ) {
117 $row =
new Tag(
'tr' );
118 $tooltip = $this->getTooltip( $label );
119 $labelFieldConfig = $tooltip ? [
'help' => $tooltip ] : [];
121 $labelField =
new FieldLayout(
124 'label' =>
new HtmlSnippet( $label ),
126 ] + $labelFieldConfig
128 $row->appendContent( $this->getCellTag( $labelField ) );
131 foreach ( $this->columns as $columnTag ) {
132 $thisTag =
"$columnTag-$tag";
135 $checkbox =
new CheckboxInputWidget( [
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 ),
143 $row->appendContent( $this->getCellTag( $checkbox ) );
155 private function getCellTag( $content, $tagElement =
'td' ) {
156 $cell =
new Tag( $tagElement );
157 $cell->appendContent( $content );
168 private function isTagChecked( $tagName ) {
170 return in_array( $tagName, (array)$this->values,
true ) ||
172 in_array( $tagName, (array)$this->forcedOn,
true );
182 private function isTagDisabled( $tagName ) {
185 $this->isDisabled() ||
187 in_array( $tagName, (array)$this->forcedOn,
true ) ||
188 in_array( $tagName, (array)$this->forcedOff,
true )
199 private function getTooltip( $label ) {
200 if ( isset( $this->tooltipsHtml[ $label ] ) ) {
201 return new HtmlSnippet( $this->tooltipsHtml[ $label ] );
203 return $this->tooltips[ $label ] ??
null;
208 return 'mw.widgets.CheckMatrixWidget';
223 return parent::getConfig( $config );