MediaWiki  1.34.0
CheckMatrixWidget.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Widget;
4 
11 class CheckMatrixWidget extends \OOUI\Widget {
13  protected $name;
15  protected $id;
17  protected $columns;
19  protected $rows;
21  protected $tooltips;
23  protected $values;
25  protected $forcedOn;
27  protected $forcedOff;
28 
47  public function __construct( array $config = [] ) {
48  // Configuration initialization
49 
50  parent::__construct( $config );
51 
52  $this->name = $config['name'] ?? null;
53  $this->id = $config['id'] ?? null;
54 
55  // Properties
56  $this->rows = $config['rows'] ?? [];
57  $this->columns = $config['columns'] ?? [];
58  $this->tooltips = $config['tooltips'] ?? [];
59 
60  $this->values = $config['values'] ?? [];
61 
62  $this->forcedOn = $config['forcedOn'] ?? [];
63  $this->forcedOff = $config['forcedOff'] ?? [];
64 
65  // Build the table
66  $table = new \OOUI\Tag( 'table' );
67  $table->addClasses( [ 'mw-htmlform-matrix mw-widget-checkMatrixWidget-matrix' ] );
68  $thead = new \OOUI\Tag( 'thead' );
69  $table->appendContent( $thead );
70  $tr = new \OOUI\Tag( 'tr' );
71 
72  // Build the header
73  $tr->appendContent( $this->getCellTag( "\u{00A0}" ) );
74  foreach ( $this->columns as $columnLabel => $columnTag ) {
75  $tr->appendContent(
76  $this->getCellTag( new \OOUI\HtmlSnippet( $columnLabel ), 'th' )
77  );
78  }
79  $thead->appendContent( $tr );
80 
81  // Build the options matrix
82  $tbody = new \OOUI\Tag( 'tbody' );
83  $table->appendContent( $tbody );
84  foreach ( $this->rows as $rowLabel => $rowTag ) {
85  $tbody->appendContent(
86  $this->getTableRow( $rowLabel, $rowTag )
87  );
88  }
89 
90  // Initialization
91  $this->addClasses( [ 'mw-widget-checkMatrixWidget' ] );
92  $this->appendContent( $table );
93  }
94 
103  private function getTableRow( $label, $tag ) {
104  $row = new \OOUI\Tag( 'tr' );
105  $tooltip = $this->getTooltip( $label );
106  $labelFieldConfig = $tooltip ? [ 'help' => $tooltip ] : [];
107  // Build label cell
108  $labelField = new \OOUI\FieldLayout(
109  new \OOUI\Widget(), // Empty widget, since we don't have the checkboxes here
110  [
111  'label' => new \OOUI\HtmlSnippet( $label ),
112  'align' => 'inline',
113  ] + $labelFieldConfig
114  );
115  $row->appendContent( $this->getCellTag( $labelField ) );
116 
117  // Build checkbox column cells
118  foreach ( $this->columns as $columnTag ) {
119  $thisTag = "$columnTag-$tag";
120 
121  // Construct a checkbox
122  $checkbox = new \OOUI\CheckboxInputWidget( [
123  'value' => $thisTag,
124  'name' => $this->name ? "{$this->name}[]" : null,
125  'id' => $this->id ? "{$this->id}-$thisTag" : null,
126  'selected' => $this->isTagChecked( $thisTag ),
127  'disabled' => $this->isTagDisabled( $thisTag ),
128  ] );
129 
130  $row->appendContent( $this->getCellTag( $checkbox ) );
131  }
132  return $row;
133  }
134 
141  private function getCellTag( $content, $tagElement = 'td' ) {
142  $cell = new \OOUI\Tag( $tagElement );
143  $cell->appendContent( $content );
144  return $cell;
145  }
146 
154  private function isTagChecked( $tagName ) {
155  // If the tag is in the value list
156  return in_array( $tagName, (array)$this->values, true ) ||
157  // Or if the tag is forced on
158  in_array( $tagName, (array)$this->forcedOn, true );
159  }
160 
168  private function isTagDisabled( $tagName ) {
169  return (
170  // If the entire widget is disabled
171  $this->isDisabled() ||
172  // If the tag is 'forced on' or 'forced off'
173  in_array( $tagName, (array)$this->forcedOn, true ) ||
174  in_array( $tagName, (array)$this->forcedOff, true )
175  );
176  }
177 
184  private function getTooltip( $label ) {
185  return $this->tooltips[ $label ] ?? null;
186  }
187 
188  protected function getJavaScriptClassName() {
189  return 'mw.widgets.CheckMatrixWidget';
190  }
191 
192  public function getConfig( &$config ) {
193  $config += [
194  'name' => $this->name,
195  'id' => $this->id,
196  'rows' => $this->rows,
197  'columns' => $this->columns,
198  'tooltips' => $this->tooltips,
199  'forcedOff' => $this->forcedOff,
200  'forcedOn' => $this->forcedOn,
201  'values' => $this->values,
202  ];
203  return parent::getConfig( $config );
204  }
205 }
MediaWiki\Widget\CheckMatrixWidget\getTooltip
getTooltip( $label)
Get the tooltip help associated with this row.
Definition: CheckMatrixWidget.php:184
MediaWiki\Widget\CheckMatrixWidget
Check matrix widget.
Definition: CheckMatrixWidget.php:11
MediaWiki\Widget\CheckMatrixWidget\$id
string null $id
Definition: CheckMatrixWidget.php:15
MediaWiki\Widget\CheckMatrixWidget\getTableRow
getTableRow( $label, $tag)
Get a formatted table row for the option, with a checkbox widget.
Definition: CheckMatrixWidget.php:103
MediaWiki\Widget\CheckMatrixWidget\$values
array $values
Definition: CheckMatrixWidget.php:23
MediaWiki\Widget\CheckMatrixWidget\$forcedOn
array $forcedOn
Definition: CheckMatrixWidget.php:25
MediaWiki\Widget\CheckMatrixWidget\__construct
__construct(array $config=[])
Operates similarly to MultiSelectWidget, but instead of using an array of options,...
Definition: CheckMatrixWidget.php:47
MediaWiki\Widget\CheckMatrixWidget\$forcedOff
array $forcedOff
Definition: CheckMatrixWidget.php:27
MediaWiki\Widget\CheckMatrixWidget\$columns
array $columns
Definition: CheckMatrixWidget.php:17
MediaWiki\Widget\CheckMatrixWidget\getJavaScriptClassName
getJavaScriptClassName()
Definition: CheckMatrixWidget.php:188
MediaWiki\Widget\CheckMatrixWidget\$name
string null $name
Definition: CheckMatrixWidget.php:13
$content
$content
Definition: router.php:78
MediaWiki\Widget\CheckMatrixWidget\isTagDisabled
isTagDisabled( $tagName)
Check whether the given tag's checkbox should be disabled.
Definition: CheckMatrixWidget.php:168
MediaWiki\Widget
Definition: CheckMatrixWidget.php:3
MediaWiki\Widget\CheckMatrixWidget\isTagChecked
isTagChecked( $tagName)
Check whether the given tag's checkbox should be checked.
Definition: CheckMatrixWidget.php:154
MediaWiki\$config
Config $config
Definition: MediaWiki.php:43
MediaWiki\Widget\CheckMatrixWidget\$tooltips
array $tooltips
Definition: CheckMatrixWidget.php:21
MediaWiki\Widget\CheckMatrixWidget\getConfig
getConfig(&$config)
Definition: CheckMatrixWidget.php:192
MediaWiki\Widget\CheckMatrixWidget\$rows
array $rows
Definition: CheckMatrixWidget.php:19
MediaWiki\Widget\CheckMatrixWidget\getCellTag
getCellTag( $content, $tagElement='td')
Get an individual cell tag with requested content.
Definition: CheckMatrixWidget.php:141