ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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 
40  private readonly HttpServices $http;
41  private readonly ilLanguage $lng;
42  private readonly UIFactory $ui_factory;
43  private readonly Renderer $ui_renderer;
44  private readonly ilCtrlInterface $ctrl;
45  private readonly Factory $refinery;
46  private int $current_page = 0;
47  private int $entries_per_page = 10;
48  private readonly ilUIService $ui_service;
51 
53  protected array $filter_objects = [];
54 
55  public function __construct(protected ilObjBibliographicGUI $a_parent_obj, protected ilBiblFactoryFacade $facade, protected UIServices $ui)
56  {
57  global $DIC;
58  $this->ctrl = $DIC->ctrl();
59  $this->http = $DIC->http();
60  $this->lng = $DIC->language();
61  $this->ui_factory = $DIC->ui()->factory();
62  $this->ui_renderer = $DIC->ui()->renderer();
63  $this->ui_service = $DIC->uiService();
64  $this->refinery = $DIC->refinery();
65  $this->ctrl->saveParameterByClass(ilObjBibliographicGUI::class, self::P_PAGE);
66  $this->ctrl->saveParameterByClass(ilObjBibliographicGUI::class, self::P_SORTATION);
67 
68  $this->filter = $this->buildFilter();
69  $this->table = $this->buildTable();
70  }
71 
72  public function getRenderedTableAndExistingFilters(): string
73  {
74  if ($this->filter !== null) {
76  }
78  return $this->ui_renderer->render($components);
79  }
80 
81  protected function buildFilter(): ?StandardFilter
82  {
83  $filter_objects = $this->facade->filterFactory()->getAllForObjectId($this->facade->iliasObjId());
84  if (empty($filter_objects)) {
85  return null;
86  }
87 
88  $available_field_ids_for_object = array_map(static fn(ilBiblField $field): ?int => $field->getId(), $this->facade->fieldFactory()->getAvailableFieldsForObjId($this->facade->iliasObjId()));
89 
90  $filter_inputs = [];
91  $filter_active_states = [];
92  foreach ($filter_objects as $filter_object) {
93  if (in_array($filter_object->getFieldId(), $available_field_ids_for_object, true)) {
94  $filter_presentation = new ilBiblFieldFilterPresentationGUI($filter_object, $this->facade);
95  $field = $this->facade->fieldFactory()->findById($filter_object->getFieldId());
96  $post_var = $field->getIdentifier();
97  $filter_input = $filter_presentation->getFilterInput();
98  $filter_inputs[$post_var] = $filter_input;
99  $filter_active_states[] = true;
100  $this->filter_objects[$post_var] = $filter_object;
101  }
102  }
103 
104  return $this->ui_service->filter()->standard(
105  'bibl_entry_filter',
106  $this->ctrl->getLinkTargetByClass(
107  ilObjBibliographicGUI::class,
109  "",
110  true
111  ),
112  $filter_inputs,
113  $filter_active_states,
114  true,
115  true
116  );
117  }
118 
119  private function renderLibraryButtons(int $entry_id): array
120  {
121  $entry = $this->facade->entryFactory()->findByIdAndTypeString($entry_id, $this->facade->type()->getStringRepresentation());
122 
123  $settings = $this->facade->libraryFactory()->getAll();
124  if ($settings === []) {
125  return [];
126  }
127 
128  $buttons = [];
129 
130  foreach ($settings as $set) {
131  $presentation = new ilBiblLibraryPresentationGUI($set, $this->facade, $this->ctrl, $this->lng, $this->ui);
132  $buttons[$set->getName()] = $presentation->getButton($this->facade, $entry);
133  }
134 
135  return $buttons;
136  }
137 
138  protected function buildTable(): PresentationTable
139  {
140  $records = $this->getData();
141  $sorted_records = $this->getSortedRecords($records);
142  $this->current_page = $this->determinePage();
143  $records_current_page = $this->getRecordsOfCurrentPage($sorted_records);
144  $view_controls = [];
145  $sortations = [];
146  foreach (array_keys($this->getSortationsMapping()) as $sort_id) {
147  $sortations[$sort_id] = $this->lng->txt('sorting_' . $sort_id);
148  }
149  $view_controls[] = $this->ui_factory->viewControl()->sortation($sortations)
150  ->withTargetURL(
151  $this->ctrl->getLinkTargetByClass(ilObjBibliographicGUI::class, ilObjBibliographicGUI::CMD_SHOW_CONTENT),
152  self::P_SORTATION
153  )->withLabel($this->lng->txt('sorting_' . $this->determineSortation()));
154  $view_controls[] = $this->ui_factory->viewControl()->pagination()
155  ->withTargetURL($this->http->request()->getRequestTarget(), self::P_PAGE)
156  ->withTotalEntries(count($records))
157  ->withPageSize($this->entries_per_page)
158  ->withCurrentPage($this->current_page);
159 
160  return $this->ui_factory->table()->presentation(
161  "",
162  $view_controls,
163  function (
164  PresentationRow $row,
165  array $record,
166  UIFactory $ui_factory
167  ): PresentationRow {
168  // Create row with fields and actions
169  $record['author'] = empty($record['author']) ? null : $record['author'];
170  $record['title'] = empty($record['title']) ? null : $record['title'];
171  $record['year'] = empty($record['year']) ? null : $record['year'];
172 
173  $author = $record['autor'] = $record['author'] ?? $record['AU'] ?? '';
174  $title = ($record['title'] ??= $record['TI'] ?? '');
175  $year = ($record['year'] ??= $record['PY'] ?? '');
176  $entry_id = $record['entry_id'];
177 
178  unset($record['author'], $record['title'], $record['AU'], $record['TI'], $record['entry_id']);
179  $translated_record = $this->getRecordWithTranslatedKeys($record);
180 
181  return $row
182  ->withHeadline($title)
183  ->withSubheadline($author)
184  ->withImportantFields([$year])
185  ->withFurtherFields($this->renderLibraryButtons($entry_id))
186  ->withContent($ui_factory->listing()->descriptive($translated_record));
187  }
188  )->withData($records_current_page);
189  }
190 
191 
192  protected function getData(): array
193  {
194  $query = new ilBiblTableQueryInfo();
195 
196  $filter_data = ($this->filter !== null) ? ($this->ui_service->filter()->getData($this->filter) ?? []) : [];
197  foreach ($filter_data as $field_name => $field_value) {
198  if (empty($field_value)) {
199  continue;
200  }
201  if (is_array($field_value) && $field_value === []) {
202  continue;
203  }
204  $filter = $this->filter_objects[$field_name];
205  $filter_info = new ilBiblTableQueryFilter();
206  $filter_info->setFieldName($field_name);
207  switch ($filter->getFilterType()) {
209  $filter_info->setFieldValue($field_value);
210  $filter_info->setOperator("IN");
211  break;
213  $filter_info->setFieldValue($field_value);
214  $filter_info->setOperator("=");
215  break;
217  $filter_info->setFieldValue("%$field_value%");
218  $filter_info->setOperator("LIKE");
219  break;
220  }
221 
222  $query->addFilter($filter_info);
223  }
224 
225  $bibl_data = [];
226  $object_id = $this->facade->iliasObjId();
227  $entries = $this->facade->entryFactory()->filterEntryIdsForTableAsArray($object_id, $query);
228 
229  foreach ($entries as $entry) {
231  $bibl_entry = $this->facade->entryFactory()->findByIdAndTypeString($entry['entry_id'], $entry['entry_type']);
232  $entry_attributes = $this->facade->attributeFactory()->getAttributesForEntry($bibl_entry);
233  $sorted_attributes = $this->facade->attributeFactory()->sortAttributes($entry_attributes);
234  $entry_data = [];
235  $entry_data['entry_id'] = $entry['entry_id'];
236  foreach ($sorted_attributes as $sorted_attribute) {
237  $entry_data[$sorted_attribute->getName()] = $sorted_attribute->getValue();
238  }
239  if (!array_key_exists('author', $entry_data)) {
240  $entry_data['author'] = '';
241  }
242  if (!array_key_exists('title', $entry_data)) {
243  $entry_data['title'] = '';
244  }
245  if (!array_key_exists('year', $entry_data)) {
246  $entry_data['year'] = '';
247  }
248  $bibl_data[] = $entry_data;
249  }
250 
251  return $bibl_data;
252  }
253 
254  protected function getSortedRecords(array $records): array
255  {
256  $sortation = $this->determineSortation();
257  $sortation_mapping = $this->getSortationsMapping();
258  $sortation_string = $sortation_mapping[$sortation];
259  $sortation_parts = explode(' ', (string) $sortation_string);
260  $sortation_field = array_column($records, $sortation_parts[0]);
261  $sortation_direction = ($sortation_parts[1] === 'ASC') ? SORT_ASC : SORT_DESC;
262  array_multisort($sortation_field, $sortation_direction, $records);
263  return $records;
264  }
265 
266 
267  protected function getRecordsOfCurrentPage(array $records): array
268  {
269  $offset = array_search($this->current_page * $this->entries_per_page, array_keys($records), true);
270  $length = $this->entries_per_page;
271  return array_slice($records, $offset, $length);
272  }
273 
274 
275  protected function getRecordWithTranslatedKeys(array $record): array
276  {
277  $translated_record = [];
278  foreach ($record as $key => $value) {
280  $field = ilBiblField::where(['identifier' => $key])->first();
281  $translated_key = $key;
282  if ($field !== null) {
283  $translated_key = $this->facade->translationFactory()->translate($field);
284  }
285  $translated_record[$translated_key] = $value;
286  }
287  return $translated_record;
288  }
289 
290 
291  private function determinePage(): int
292  {
293  return $this->http->wrapper()->query()->has(self::P_PAGE)
294  ? $this->http->wrapper()->query()->retrieve(self::P_PAGE, $this->refinery->kindlyTo()->int())
295  : 0;
296  }
297 
298  private function determineSortation(): int
299  {
300  return $this->http->wrapper()->query()->has(self::P_SORTATION)
301  ? $this->http->wrapper()->query()->retrieve(self::P_SORTATION, $this->refinery->kindlyTo()->int())
302  : array_keys($this->getSortationsMapping())[0] ?? self::SORTATION_BY_TITLE_ASC;
303  }
304 
305  public function getSortationsMapping(): array
306  {
307  return [
308  self::SORTATION_BY_TITLE_ASC => 'title ASC',
309  self::SORTATION_BY_TITLE_DESC => 'title DESC',
310  self::SORTATION_BY_AUTHOR_ASC => 'author ASC',
311  self::SORTATION_BY_AUTHOR_DESC => 'author DESC',
312  self::SORTATION_BY_YEAR_ASC => 'year ASC',
313  self::SORTATION_BY_YEAR_DESC => 'year DESC'
314  ];
315  }
316 }
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:22
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.