ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilBiblLibraryTableGUI.php
Go to the documentation of this file.
1<?php
2
23use Psr\Http\Message\ServerRequestInterface as HttpRequest;
24use ILIAS\UI\Factory as UIFactory;
25use ILIAS\UI\Renderer as UIRenderer;
26use ILIAS\Data\Factory as DataFactory;
28use ILIAS\UI\Component\Table\Data as DataTable;
30
32{
35 private DataFactory $data_factory;
36 private HttpRequest $http_request;
38 private UIFactory $ui_factory;
39 private UIRenderer $ui_renderer;
40
41 private DataTable $table;
42
43 public function __construct(private readonly ilBiblAdminLibraryFacadeInterface $facade)
44 {
45 global $DIC;
46
47 $this->access = $DIC->access();
48 $this->ctrl = $DIC->ctrl();
49 $this->data_factory = new DataFactory();
50 $this->http_request = $DIC->http()->request();
51 $this->lng = $DIC->language();
52 $this->ui_factory = $DIC->ui()->factory();
53 $this->ui_renderer = $DIC->ui()->renderer();
54
55 $this->table = $this->buildTable();
56 }
57
58
59 public function getRenderedTable(): string
60 {
61 return $this->ui_renderer->render([$this->table]);
62 }
63
64
65 private function buildTable(): DataTable
66 {
67 return $this->ui_factory->table()->data(
68 $this,
69 $this->lng->txt('bibl_settings_libraries'),
70 $this->getColumns(),
71 )->withActions(
72 $this->getActions()
73 )->withRange(
74 new Range(0, 10)
75 )->withOrder(
76 new Order('bibl_library_name', Order::ASC)
77 )->withRequest($this->http_request);
78 }
79
80
81 private function getColumns(): array
82 {
83 return [
84 'bibl_library_name' => $this->ui_factory->table()->column()->text($this->lng->txt('bibl_library_name')),
85 'bibl_library_url' => $this->ui_factory->table()->column()->text($this->lng->txt('bibl_library_url')),
86 'bibl_library_img' => $this->ui_factory->table()->column()->text($this->lng->txt('bibl_library_img'))
87 ];
88 }
89
90
91 private function getActions(): array
92 {
93 $namespace = ['lib'];
94
95 $actions = [];
96 if ($this->access->checkAccess('write', '', $this->http_request->getQueryParams()['ref_id'])) {
97 $uri_edit = $this->data_factory->uri(
98 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
99 ilBiblLibraryGUI::class,
101 )
102 );
108 [$url_builder_edit, $action_parameter_token_edit, $row_id_token_edit] = (
109 new URLBuilder($uri_edit)
110 )->acquireParameters(
112 'action',
113 'ids'
114 );
115
116 $uri_delete = $this->data_factory->uri(
117 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
118 ilBiblLibraryGUI::class,
120 )
121 );
127 [$url_builder_delete, $action_parameter_token_delete, $row_id_token_delete] = (
128 new URLBuilder($uri_delete)
129 )->acquireParameters(
131 'action',
132 'ids'
133 );
134
135 $actions = [
136 'edit' => $this->ui_factory->table()->action()->single(
137 $this->lng->txt('edit'),
138 $url_builder_edit->withParameter($action_parameter_token_edit, 'edit'),
139 $row_id_token_edit
140 ),
141 'delete' => $this->ui_factory->table()->action()->standard(
142 $this->lng->txt('delete'),
143 $url_builder_delete->withParameter($action_parameter_token_delete, 'delete'),
144 $row_id_token_delete
145 )
146 ];
147 }
148
149 return $actions;
150 }
151
152
153 public function getRows(
154 DataRowBuilder $row_builder,
155 array $visible_column_ids,
157 Order $order,
158 ?array $filter_data,
159 ?array $additional_parameters
160 ): Generator {
161 $records = $this->getRecords($range, $order);
162 foreach ($records as $record) {
163 $row_id = (string) $record['bibl_library_id'];
164 yield $row_builder->buildDataRow($row_id, $record);
165 }
166 }
167
168
169 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
170 {
171 return count($this->getRecords());
172 }
173
174
175 private function getRecords(?Range $range = null, ?Order $order = null): array
176 {
177 $records = [];
178 $libraries = $this->facade->libraryFactory()->getAll();
179 foreach ($libraries as $library) {
180 $records[] = [
181 "bibl_library_id" => $library->getId(),
182 "bibl_library_name" => $library->getName(),
183 "bibl_library_url" => $library->getUrl(),
184 "bibl_library_img" => $library->getImg(),
185 ];
186 }
187
188 if ($order !== null) {
189 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value): array => [$key, $value]);
190 usort($records, static fn($a, $b): int => $a[$order_field] <=> $b[$order_field]);
191 if ($order_direction === 'DESC') {
192 $records = array_reverse($records);
193 }
194 }
195 if ($range !== null) {
196 return array_slice($records, $range->getStart(), $range->getLength());
197 }
198
199 return $records;
200 }
201}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
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....
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...
getRecords(?Range $range=null, ?Order $order=null)
__construct(private readonly ilBiblAdminLibraryFacadeInterface $facade)
language handling
if($err=$client->getError()) $namespace
buildDataRow(string $id, array $record)
This describes a Data Table.
Definition: Data.php:31
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
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...
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26