ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBiblEntryTableGUI.php
Go to the documentation of this file.
1<?php
2
19use ILIAS\UI\Factory as UIFactory;
20use ILIAS\HTTP\Services as HttpServices;
26use ILIAS\UI\Component\Table\Presentation as PresentationTable;
27
29{
30 public const P_PAGE = 'page';
31 public const P_SORTATION = 'sortation';
32 public const SORTATION_BY_TITLE_ASC = 1;
33 public const SORTATION_BY_TITLE_DESC = 2;
34 public const SORTATION_BY_AUTHOR_ASC = 3;
35 public const SORTATION_BY_AUTHOR_DESC = 4;
36 public const SORTATION_BY_YEAR_ASC = 5;
37 public const SORTATION_BY_YEAR_DESC = 6;
38
39 private readonly HttpServices $http;
40 private readonly ilLanguage $lng;
41 private readonly UIFactory $ui_factory;
42 private readonly Renderer $ui_renderer;
43 private readonly ilCtrlInterface $ctrl;
44 private readonly Factory $refinery;
45 private int $current_page = 0;
46 private int $entries_per_page = 10;
47 private readonly ilUIService $ui_service;
48 private ?StandardFilter $filter;
49 private PresentationTable $table;
51
53 protected array $filter_objects = [];
54
55 public function __construct(
56 protected ilObjBibliographicGUI $a_parent_obj,
57 protected ilBiblFactoryFacade $facade,
58 protected UIServices $ui
59 ) {
60 global $DIC;
61 $this->ctrl = $DIC->ctrl();
62 $this->http = $DIC->http();
63 $this->lng = $DIC->language();
64 $this->ui_factory = $DIC->ui()->factory();
65 $this->ui_renderer = $DIC->ui()->renderer();
66 $this->ui_service = $DIC->uiService();
67 $this->refinery = $DIC->refinery();
68 $this->ctrl->saveParameterByClass(ilObjBibliographicGUI::class, self::P_PAGE);
69 $this->ctrl->saveParameterByClass(ilObjBibliographicGUI::class, self::P_SORTATION);
70
71 $this->filter = $this->buildFilter();
72 $this->table = $this->buildTable();
73 $this->string_transform = $DIC->refinery()->string();
74 }
75
76 public function getRenderedTableAndExistingFilters(): string
77 {
78 if ($this->filter !== null) {
80 }
82 return $this->ui_renderer->render($components);
83 }
84
85 protected function buildFilter(): ?StandardFilter
86 {
87 $filter_objects = $this->facade->filterFactory()->getAllForObjectId($this->facade->iliasObjId());
88 if (empty($filter_objects)) {
89 return null;
90 }
91
92 $available_field_ids_for_object = array_map(
93 static fn(ilBiblField $field): ?int => $field->getId(),
94 $this->facade->fieldFactory()->getAvailableFieldsForObjId($this->facade->iliasObjId())
95 );
96
97 $filter_inputs = [];
98 $filter_active_states = [];
99 foreach ($filter_objects as $filter_object) {
100 if (in_array($filter_object->getFieldId(), $available_field_ids_for_object, true)) {
101 $filter_presentation = new ilBiblFieldFilterPresentationGUI($filter_object, $this->facade);
102 $field = $this->facade->fieldFactory()->findById($filter_object->getFieldId());
103 $post_var = $field->getIdentifier();
104 $filter_input = $filter_presentation->getFilterInput();
105 $filter_inputs[$post_var] = $filter_input;
106 $filter_active_states[] = true;
107 $this->filter_objects[$post_var] = $filter_object;
108 }
109 }
110
111 return $this->ui_service->filter()->standard(
112 'bibl_entry_filter',
113 $this->ctrl->getLinkTargetByClass(
114 ilObjBibliographicGUI::class,
116 "",
117 true
118 ),
119 $filter_inputs,
120 $filter_active_states,
121 true,
122 true
123 );
124 }
125
126 private function renderLibraryButtons(int $entry_id): array
127 {
128 $entry = $this->facade->entryFactory()->findByIdAndTypeString(
129 $entry_id,
130 $this->facade->type()->getStringRepresentation()
131 );
132
133 $settings = $this->facade->libraryFactory()->getAll();
134 if ($settings === []) {
135 return [];
136 }
137
138 $buttons = [];
139
140 foreach ($settings as $set) {
141 $presentation = new ilBiblLibraryPresentationGUI($set, $this->facade, $this->ctrl, $this->lng, $this->ui);
142 $buttons[$set->getName()] = $presentation->getButton($this->facade, $entry);
143 }
144
145 return $buttons;
146 }
147
148 protected function buildTable(): PresentationTable
149 {
150 $records = $this->getData();
151 $sorted_records = $this->getSortedRecords($records);
152 $this->current_page = $this->determinePage();
153 $records_current_page = $this->getRecordsOfCurrentPage($sorted_records);
154 $view_controls = [];
155 $sortations = [];
156 foreach (array_keys($this->getSortationsMapping()) as $sort_id) {
157 $sortations[$sort_id] = $this->lng->txt('sorting_' . $sort_id);
158 }
159 if ($sortations !== []) {
160 $view_controls[] = $this->ui_factory->viewControl()->sortation($sortations, array_key_first($sortations))
161 ->withTargetURL(
162 $this->ctrl->getLinkTargetByClass(
163 ilObjBibliographicGUI::class,
165 ),
166 self::P_SORTATION
167 );
168 }
169
170 $view_controls[] = $this->ui_factory->viewControl()->pagination()
171 ->withTargetURL($this->http->request()->getRequestTarget(), self::P_PAGE)
172 ->withTotalEntries(count($records))
173 ->withPageSize($this->entries_per_page)
174 ->withCurrentPage($this->current_page);
175
176 return $this->ui_factory->table()->presentation(
177 "",
178 $view_controls,
179 function (
180 PresentationRow $row,
181 array $record,
182 UIFactory $ui_factory
183 ): PresentationRow {
184 // Create row with fields and actions
185 $record['author'] = empty($record['author']) ? null : $record['author'];
186 $record['title'] = empty($record['title']) ? null : $record['title'];
187 $record['year'] = empty($record['year']) ? null : $record['year'];
188
189 $author = $record['autor'] = $record['author'] ?? $record['AU'] ?? '';
190 $title = ($record['title'] ??= $record['TI'] ?? '');
191 $year = ($record['year'] ??= $record['PY'] ?? '');
192 $entry_id = $record['entry_id'];
193
194 unset($record['author'], $record['title'], $record['AU'], $record['TI'], $record['entry_id']);
195
196 $translated_record = $this->getRecordWithTranslatedKeys($record);
197 $clickable_translated_record = array_map(
198 fn($value) => $this->string_transform->makeClickable()->transform($value),
199 $translated_record
200 );
201
202 $this->ctrl->setParameterByClass(ilObjBibliographicGUI::class, 'entry_id', $entry_id);
203
204 return $row
205 ->withHeadline($title)
206 ->withAction(
207 $this->ui_factory->button()->shy(
208 $this->lng->txt('detail_view'),
209 $this->ctrl->getLinkTargetByClass(
210 ilObjBibliographicGUI::class,
211 'showDetails'
212 )
213 )
214 )
215 ->withSubheadline($author)
216 ->withImportantFields([$year])
217 ->withFurtherFields($this->renderLibraryButtons($entry_id))
218 ->withContent($ui_factory->listing()->descriptive($clickable_translated_record));
219 }
220 )->withData($records_current_page);
221 }
222
223 protected function getData(): array
224 {
225 $query = new ilBiblTableQueryInfo();
226
227 $filter_data = ($this->filter !== null) ? ($this->ui_service->filter()->getData($this->filter) ?? []) : [];
228 foreach ($filter_data as $field_name => $field_value) {
229 if (empty($field_value)) {
230 continue;
231 }
232 if (is_array($field_value) && $field_value === []) {
233 continue;
234 }
235 $filter = $this->filter_objects[$field_name];
236 $filter_info = new ilBiblTableQueryFilter();
237 $filter_info->setFieldName($field_name);
238 switch ($filter->getFilterType()) {
240 $filter_info->setFieldValue($field_value);
241 $filter_info->setOperator("IN");
242 break;
244 $filter_info->setFieldValue($field_value);
245 $filter_info->setOperator("=");
246 break;
248 $filter_info->setFieldValue("%$field_value%");
249 $filter_info->setOperator("LIKE");
250 break;
251 }
252
253 $query->addFilter($filter_info);
254 }
255
256 $bibl_data = [];
257 $object_id = $this->facade->iliasObjId();
258 $entries = $this->facade->entryFactory()->filterEntryIdsForTableAsArray($object_id, $query);
259
260 foreach ($entries as $entry) {
262 $bibl_entry = $this->facade->entryFactory()->findByIdAndTypeString(
263 $entry['entry_id'],
264 $entry['entry_type']
265 );
266 $entry_attributes = $this->facade->attributeFactory()->getAttributesForEntry($bibl_entry);
267 $sorted_attributes = $this->facade->attributeFactory()->sortAttributes($entry_attributes);
268 $entry_data = [];
269 $entry_data['entry_id'] = $entry['entry_id'];
270 foreach ($sorted_attributes as $sorted_attribute) {
271 $entry_data[$sorted_attribute->getName()] = $sorted_attribute->getValue();
272 }
273 $entry_data['author'] ??= $entry_data['AU'] ?? $entry_data['A1'] ?? $entry_data['A2'] ?? '';
274 $entry_data['title'] ??= $entry_data['T1'] ?? $entry_data['T2'] ?? '';
275 $entry_data['year'] ??= $entry_data['PY'] ?? $entry_data['Y1'] ?? '';
276 $bibl_data[] = $entry_data;
277 }
278
279 return $bibl_data;
280 }
281
282 protected function getSortedRecords(array $records): array
283 {
284 $sortation = $this->determineSortation();
285 $sortation_mapping = $this->getSortationsMapping();
286 $sortation_string = $sortation_mapping[$sortation];
287 $sortation_parts = explode(' ', (string) $sortation_string);
288 $sortation_field = array_column($records, $sortation_parts[0]);
289 $sortation_direction = ($sortation_parts[1] === 'ASC') ? SORT_ASC : SORT_DESC;
290 array_multisort($sortation_field, $sortation_direction, $records);
291 return $records;
292 }
293
294 protected function getRecordsOfCurrentPage(array $records): array
295 {
296 $offset = array_search($this->current_page * $this->entries_per_page, array_keys($records), true);
297 $length = $this->entries_per_page;
298 return array_slice($records, $offset, $length);
299 }
300
301 protected function getRecordWithTranslatedKeys(array $record): array
302 {
303 $record = $this->facade->attributeFactory()->sortAttributesArray($record);
304
305 $translated_record = [];
306 foreach ($record as $key => $value) {
308 $field = ilBiblField::where(['identifier' => $key])->first();
309 $translated_key = $key;
310 if ($field !== null) {
311 $translated_key = $this->facade->translationFactory()->translate($field);
312 }
313 $translated_record[$translated_key] = $value;
314 }
315 return $translated_record;
316 }
317
318 private function determinePage(): int
319 {
320 return $this->http->wrapper()->query()->has(self::P_PAGE)
321 ? $this->http->wrapper()->query()->retrieve(self::P_PAGE, $this->refinery->kindlyTo()->int())
322 : 0;
323 }
324
325 private function determineSortation(): int
326 {
327 return $this->http->wrapper()->query()->has(self::P_SORTATION)
328 ? $this->http->wrapper()->query()->retrieve(self::P_SORTATION, $this->refinery->kindlyTo()->int())
329 : array_keys($this->getSortationsMapping())[0] ?? self::SORTATION_BY_TITLE_ASC;
330 }
331
332 public function getSortationsMapping(): array
333 {
334 return [
335 self::SORTATION_BY_TITLE_ASC => 'title ASC',
336 self::SORTATION_BY_TITLE_DESC => 'title DESC',
337 self::SORTATION_BY_AUTHOR_ASC => 'author ASC',
338 self::SORTATION_BY_AUTHOR_DESC => 'author DESC',
339 self::SORTATION_BY_YEAR_ASC => 'year ASC',
340 self::SORTATION_BY_YEAR_DESC => 'year DESC'
341 ];
342 }
343}
$settings
Definition: index.php:44
$components
static where($where, $operator=null)
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:43
readonly ilUIService $ui_service
__construct(protected ilObjBibliographicGUI $a_parent_obj, protected ilBiblFactoryFacade $facade, protected UIServices $ui)
readonly HttpServices $http
getRecordsOfCurrentPage(array $records)
ILIAS Refinery String Group $string_transform
readonly UIFactory $ui_factory
readonly ilCtrlInterface $ctrl
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...
Class ilBiblLibraryPresentationGUI.
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...
language handling
Class ilObjBibliographicGUI.
Filter service.
This describes a standard filter.
Definition: Standard.php:27
This describes a Presentation Table.
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
filter(string $filter_id, array $class_path, string $cmd, bool $activated=true, bool $expanded=true)
global $DIC
Definition: shib_login.php:26