ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilTermsOfServiceTableGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 abstract class ilTermsOfServiceTableGUI extends ilTable2GUI
25 {
27  protected array $visibleOptionalColumns = [];
28  protected array $optionalColumns = [];
29  protected array $filter = [];
30  protected array $optional_filter = [];
31 
32  public function __construct(ilTermsOfServiceControllerEnabled $gui, string $command)
33  {
34  parent::__construct($gui, $command);
35 
36  $columns = $this->getColumnDefinition();
37  $this->optionalColumns = $this->getSelectableColumns();
38  $this->visibleOptionalColumns = $this->getSelectedColumns();
39 
40  foreach ($columns as $index => $column) {
41  if ($this->isColumnVisible($index)) {
42  $this->addColumn(
43  $column['txt'],
44  isset($column['sortable']) && $column['sortable'] ? $column['field'] : '',
45  $column['width'] ?? '',
46  isset($column['is_checkbox']) ? (bool) $column['is_checkbox'] : false
47  );
48  }
49  }
50  }
51 
52  public function setProvider(ilTermsOfServiceTableDataProvider $provider): void
53  {
54  $this->provider = $provider;
55  }
56 
58  {
59  return $this->provider;
60  }
61 
66  protected function onBeforeDataFetched(array &$params, array &$filter): void
67  {
68  }
69 
74  protected function prepareRow(array &$row): void
75  {
76  }
77 
82  protected function preProcessData(array &$data): void
83  {
84  }
85 
92  protected function formatCellValue(string $column, array $row): string
93  {
94  return trim($row[$column] ?? '');
95  }
96 
97  public function getSelectableColumns(): array
98  {
99  $optionalColumns = array_filter($this->getColumnDefinition(), static function ($column): bool {
100  return isset($column['optional']) && $column['optional'];
101  });
102 
103  $columns = [];
104  foreach ($optionalColumns as $index => $column) {
105  $columns[$column['field']] = $column;
106  }
107 
108  return $columns;
109  }
110 
111  protected function isColumnVisible(int $index): bool
112  {
113  $columnDefinition = $this->getColumnDefinition();
114  if (array_key_exists($index, $columnDefinition)) {
115  $column = $columnDefinition[$index];
116  if (isset($column['optional']) && !$column['optional']) {
117  return true;
118  }
119 
120  if (
121  is_array($this->visibleOptionalColumns) &&
122  array_key_exists($column['field'], $this->visibleOptionalColumns)
123  ) {
124  return true;
125  }
126  }
127 
128  return false;
129  }
130 
131  final protected function fillRow(array $a_set): void
132  {
133  $this->prepareRow($a_set);
134 
135  foreach ($this->getColumnDefinition() as $index => $column) {
136  if (!$this->isColumnVisible($index)) {
137  continue;
138  }
139 
140  $this->tpl->setCurrentBlock('column');
141  $value = $this->formatCellValue($column['field'], $a_set);
142  if ($value === '') {
143  $this->tpl->touchBlock('column');
144  } else {
145  $this->tpl->setVariable('COLUMN_VALUE', $value);
146  }
147 
148  $this->tpl->parseCurrentBlock();
149  }
150  }
151 
152  abstract protected function getColumnDefinition(): array;
153 
154  public function populate(): void
155  {
156  if ($this->getExternalSegmentation() && $this->getExternalSorting()) {
157  $this->determineOffsetAndOrder();
158  } elseif (!$this->getExternalSegmentation() && $this->getExternalSorting()) {
159  $this->determineOffsetAndOrder(true);
160  }
161 
162  $params = [];
163  if ($this->getExternalSegmentation()) {
164  $params['limit'] = $this->getLimit();
165  $params['offset'] = $this->getOffset();
166  }
167  if ($this->getExternalSorting()) {
168  $params['order_field'] = $this->getOrderField();
169  $params['order_direction'] = $this->getOrderDirection();
170  }
171 
172  $this->determineSelectedFilters();
173  $filter = $this->filter;
174 
175  foreach ($this->optional_filter as $key => $value) {
176  if ($this->isFilterSelected($key)) {
177  $filter[$key] = $value;
178  }
179  }
180 
181  $this->onBeforeDataFetched($params, $filter);
182  $data = $this->getProvider()->getList($params, $filter);
183 
184  if (!count($data['items']) && $this->getOffset() > 0 && $this->getExternalSegmentation()) {
185  $this->resetOffset();
186  if ($this->getExternalSegmentation()) {
187  $params['limit'] = $this->getLimit();
188  $params['offset'] = $this->getOffset();
189  }
190  $data = $this->provider->getList($params, $filter);
191  }
192 
193  $this->preProcessData($data);
194 
195  $this->setData($data['items']);
196  if ($this->getExternalSegmentation()) {
197  $this->setMaxCount($data['cnt']);
198  }
199  }
200 }
setData(array $a_data)
setProvider(ilTermsOfServiceTableDataProvider $provider)
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
Interface ilTermsOfServiceControllerEnabled.
__construct(ilTermsOfServiceControllerEnabled $gui, string $command)
onBeforeDataFetched(array &$params, array &$filter)
Interface ilTermsOfServiceTableDataProvider.
$index
Definition: metadata.php:145
isFilterSelected(string $a_col)
Is given filter selected?
resetOffset(bool $a_in_determination=false)
string $key
Consumer key/client ID value.
Definition: System.php:193
__construct(Container $dic, ilPlugin $plugin)
ilTermsOfServiceTableDataProvider $provider
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
prepareRow(array &$row)
This method can be used to add some field values dynamically or manipulate existing values of the tab...
formatCellValue(string $column, array $row)
Define a final formatting for a cell value.
preProcessData(array &$data)
This method can be used to process the array of all fetched data.
determineOffsetAndOrder(bool $a_omit_offset=false)
setMaxCount(int $a_max_count)
set max.