ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilBiblLibraryTableGUI.php
Go to the documentation of this file.
1 <?php
2 
14 
33 {
38  private ilLanguage $lng;
41 
42  private DataTable $table;
43 
44  public function __construct(private readonly ilBiblAdminLibraryFacadeInterface $facade)
45  {
46  global $DIC;
47 
48  $this->access = $DIC->access();
49  $this->ctrl = $DIC->ctrl();
50  $this->data_factory = new DataFactory();
51  $this->http_request = $DIC->http()->request();
52  $this->lng = $DIC->language();
53  $this->ui_factory = $DIC->ui()->factory();
54  $this->ui_renderer = $DIC->ui()->renderer();
55 
56  $this->table = $this->buildTable();
57  }
58 
59 
60  public function getRenderedTable(): string
61  {
62  return $this->ui_renderer->render([$this->table]);
63  }
64 
65 
66  private function buildTable(): DataTable
67  {
68  return $this->ui_factory->table()->data(
69  $this->lng->txt('bibl_settings_libraries'),
70  $this->getColumns(),
71  $this
72  )->withActions(
73  $this->getActions()
74  )->withRange(
75  new Range(0, 10)
76  )->withOrder(
77  new Order('bibl_library_name', Order::ASC)
78  )->withRequest($this->http_request);
79  }
80 
81 
82  private function getColumns(): array
83  {
84  return [
85  'bibl_library_name' => $this->ui_factory->table()->column()->text($this->lng->txt('bibl_library_name')),
86  'bibl_library_url' => $this->ui_factory->table()->column()->text($this->lng->txt('bibl_library_url')),
87  'bibl_library_img' => $this->ui_factory->table()->column()->text($this->lng->txt('bibl_library_img'))
88  ];
89  }
90 
91 
92  private function getActions(): array
93  {
94  $namespace = ['lib'];
95 
96  $actions = [];
97  if ($this->access->checkAccess('write', '', $this->http_request->getQueryParams()['ref_id'])) {
98  $uri_edit = $this->data_factory->uri(
99  ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
100  ilBiblLibraryGUI::class,
102  )
103  );
109  [$url_builder_edit, $action_parameter_token_edit, $row_id_token_edit] = (
110  new URLBuilder($uri_edit)
111  )->acquireParameters(
112  $namespace,
113  'action',
114  'ids'
115  );
116 
117  $uri_delete = $this->data_factory->uri(
118  ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
119  ilBiblLibraryGUI::class,
121  )
122  );
128  [$url_builder_delete, $action_parameter_token_delete, $row_id_token_delete] = (
129  new URLBuilder($uri_delete)
130  )->acquireParameters(
131  $namespace,
132  'action',
133  'ids'
134  );
135 
136  $actions = [
137  'edit' => $this->ui_factory->table()->action()->single(
138  $this->lng->txt('edit'),
139  $url_builder_edit->withParameter($action_parameter_token_edit, 'edit'),
140  $row_id_token_edit
141  ),
142  'delete' => $this->ui_factory->table()->action()->standard(
143  $this->lng->txt('delete'),
144  $url_builder_delete->withParameter($action_parameter_token_delete, 'delete'),
145  $row_id_token_delete
146  )
147  ];
148  }
149 
150  return $actions;
151  }
152 
153 
154  public function getRows(
155  DataRowBuilder $row_builder,
156  array $visible_column_ids,
157  Range $range,
158  Order $order,
159  ?array $filter_data,
160  ?array $additional_parameters
161  ): Generator {
162  $records = $this->getRecords($range, $order);
163  foreach ($records as $record) {
164  $row_id = (string) $record['bibl_library_id'];
165  yield $row_builder->buildDataRow($row_id, $record);
166  }
167  }
168 
169 
170  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
171  {
172  return count($this->getRecords());
173  }
174 
175 
176  private function getRecords(Range $range = null, Order $order = null): array
177  {
178  $records = [];
179  $libraries = $this->facade->libraryFactory()->getAll();
180  foreach ($libraries as $library) {
181  $records[] = [
182  "bibl_library_id" => $library->getId(),
183  "bibl_library_name" => $library->getName(),
184  "bibl_library_url" => $library->getUrl(),
185  "bibl_library_img" => $library->getImg(),
186  ];
187  }
188 
189  if ($order) {
190  [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
191  usort($records, static fn($a, $b) => $a[$order_field] <=> $b[$order_field]);
192  if ($order_direction === 'DESC') {
193  $records = array_reverse($records);
194  }
195  }
196  if ($range) {
197  $records = array_slice($records, $range->getStart(), $range->getLength());
198  }
199 
200  return $records;
201  }
202 }
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e...
if($err=$client->getError()) $namespace
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getRecords(Range $range=null, Order $order=null)
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:12
buildDataRow(string $id, array $record)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:25
__construct(private readonly ilBiblAdminLibraryFacadeInterface $facade)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
URLBuilder.
Definition: URLBuilder.php:39
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28