MediaWiki master
CheckMatrixWidget.php
Go to the documentation of this file.
1<?php
2
4
5use OOUI\CheckboxInputWidget;
6use OOUI\FieldLayout;
7use OOUI\HtmlSnippet;
8use OOUI\Tag;
9use OOUI\Widget;
10
17class CheckMatrixWidget extends Widget {
19 protected $name;
21 protected $id;
23 protected $columns;
25 protected $rows;
27 protected $tooltips;
29 protected $tooltipsHtml;
31 protected $values;
33 protected $forcedOn;
35 protected $forcedOff;
36
58 public function __construct( array $config = [] ) {
59 // Configuration initialization
60
61 parent::__construct( $config );
62
63 $this->name = $config['name'] ?? null;
64 $this->id = $config['id'] ?? null;
65
66 // Properties
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 // Build the table
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 // Build the header
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 // Build the options matrix
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 // Initialization
103 $this->addClasses( [ 'mw-widget-checkMatrixWidget' ] );
104 $this->appendContent( $table );
105 }
106
116 private function getTableRow( $label, $tag ) {
117 $row = new Tag( 'tr' );
118 $tooltip = $this->getTooltip( $label );
119 $labelFieldConfig = $tooltip ? [ 'help' => $tooltip ] : [];
120 // Build label cell
121 $labelField = new FieldLayout(
122 new Widget(), // Empty widget, since we don't have the checkboxes here
123 [
124 'label' => new HtmlSnippet( $label ),
125 'align' => 'inline',
126 ] + $labelFieldConfig
127 );
128 $row->appendContent( $this->getCellTag( $labelField ) );
129
130 // Build checkbox column cells
131 foreach ( $this->columns as $columnTag ) {
132 $thisTag = "$columnTag-$tag";
133
134 // Construct a checkbox
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
155 private function getCellTag( $content, $tagElement = 'td' ) {
156 $cell = new Tag( $tagElement );
157 $cell->appendContent( $content );
158 return $cell;
159 }
160
168 private function isTagChecked( $tagName ) {
169 // If the tag is in the value list
170 return in_array( $tagName, (array)$this->values, true ) ||
171 // Or if the tag is forced on
172 in_array( $tagName, (array)$this->forcedOn, true );
173 }
174
182 private function isTagDisabled( $tagName ) {
183 return (
184 // If the entire widget is disabled
185 $this->isDisabled() ||
186 // If the tag is 'forced on' or 'forced off'
187 in_array( $tagName, (array)$this->forcedOn, true ) ||
188 in_array( $tagName, (array)$this->forcedOff, true )
189 );
190 }
191
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}
__construct(array $config=[])
Operates similarly to MultiSelectWidget, but instead of using an array of options,...