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