ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
ResourceListingUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
33 
38 {
39  public const P_RESOURCE_ID = 'irss:resource_id';
40  public const P_PAGE = 'irss_page';
41  public const P_SORTATION = 'irss_sort';
42 
43  private \ilUIFilterService $filter_service;
44  private \ilCtrlInterface $ctrl;
46  private \ILIAS\Refinery\Factory $refinery;
48  private \ilLanguage $language;
49  private array $components = [];
51  private Services $irss;
52 
53  public function __construct(
54  private ViewDefinition $view_definition,
55  private TableDataSource $data_source,
56  ActionGenerator $action_generator = null
57  ) {
58  global $DIC;
59  $this->action_generator = $action_generator ?? new NullActionGenerator();
60  $this->ctrl = $DIC->ctrl();
61  $this->storeRequestParameters();
62  $this->language = $DIC->language();
63  $this->ui_factory = $DIC->ui()->factory();
64  $this->filter_service = $DIC->uiService()->filter();
65  $this->refinery = $DIC->refinery();
66  $this->irss = $DIC->resourceStorage();
67  $this->query = $DIC->http()->wrapper()->query();
68  // Setup DataSource
69  $this->data_source->setOffsetAndLimit(
70  $this->determinePage() * $this->view_definition->getItemsPerPage(),
71  $this->view_definition->getItemsPerPage()
72  );
73  $this->data_source->setSortDirection($this->determineSortation());
74  $this->initFilters();
75  $data_source->process();
76  $this->initUpload();
77  $this->initTable();
78  }
79 
80  private function initFilters(): void
81  {
82  // Filters
83  $filters = $this->data_source->getFilterItems($this->ui_factory, $this->language);
84  if ($filters !== []) {
85  $embedding_gui = $this->view_definition->getEmbeddingGui();
86  $this->components[] = $filter = $this->filter_service->standard(
87  $embedding_gui,
88  $this->ctrl->getLinkTargetByClass($embedding_gui, $this->view_definition->getEmbeddingCmd()),
89  $filters,
90  array_map(
91  fn($filter): bool => true,
92  $filters
93  ),
94  true,
95  true
96  );
97  $this->data_source->applyFilterValues($this->filter_service->getData($filter));
98  }
99  }
100 
101  private function initUpload(): void
102  {
103  // Currently no direct upload possible here
104  }
105 
106  private function initTable(): void
107  {
108  // Table
109  $this->components[] = $this->ui_factory->table()->presentation(
110  '',
111  [],
112  $this->getRowMapping()
113  )->withData(
114  $this->data_source->getResourceIdentifications()
115  )->withViewControls($this->getViewControls());
116 
117  $this->components = array_merge($this->components, $this->action_generator->getCollectedModals());
118  }
119 
120  public function getRowMapping(): \Closure
121  {
122  return function (
123  PresentationRow $row,
124  ResourceIdentification $resource_identification
125  ): PresentationRow {
126  $resource = $this->irss->manage()->getResource($resource_identification);
127  $resource_to_component = new ResourceToComponent(
128  $resource,
129  $this->action_generator
130  );
131  return $resource_to_component->getAsRowMapping()($row, $resource_identification);
132  };
133  }
134 
135  public function getComponents(): array
136  {
137  return $this->components;
138  }
139 
140  protected function getViewControls(): array
141  {
142  $view_controls = [];
143  // Sortation
144  $sortations = [];
145  foreach (array_keys($this->data_source->getSortationsMapping()) as $sort_id) {
146  $sortations[$sort_id] = $this->language->txt('sorting_' . $sort_id);
147  }
148 
149  $view_controls[] = $this->ui_factory->viewControl()->sortation($sortations, (string) $this->determineSortation())
150  ->withTargetURL(
151  $this->ctrl->getLinkTargetByClass(
152  $this->view_definition->getEmbeddingGui(),
153  $this->view_definition->getEmbeddingCmd()
154  ),
155  self::P_SORTATION
156  );
157 
158  // Pagination
159  $count = $this->data_source->getFilteredAmountOfItems();
160  if ($count > $this->view_definition->getItemsPerPage()) {
161  $view_controls[] = $this->ui_factory->viewControl()->pagination()
162  ->withTargetURL(
163  $this->ctrl->getLinkTargetByClass(
164  $this->view_definition->getEmbeddingGui(),
165  $this->view_definition->getEmbeddingCmd()
166  ),
167  self::P_PAGE
168  )
169  ->withCurrentPage($this->determinePage())
170  ->withPageSize($this->view_definition->getItemsPerPage())
171  ->withTotalEntries($count)
172  ->withMaxPaginationButtons(5);
173  }
174 
175  return $view_controls;
176  }
177 
178  private function determinePage(): int
179  {
180  if ($this->query->has('cmdFilter')) { // Reset Page if Filter is applied, reset, ...
181  return 0;
182  }
183 
184  return $this->query->has(self::P_PAGE)
185  ? $this->query->retrieve(self::P_PAGE, $this->refinery->kindlyTo()->int())
186  : 0;
187  }
188 
189  private function determineSortation(): int
190  {
191  return $this->query->has(self::P_SORTATION)
192  ? $this->query->retrieve(self::P_SORTATION, $this->refinery->kindlyTo()->int())
193  : array_keys($this->data_source->getSortationsMapping())[0] ?? SortDirection::BY_SIZE_DESC;
194  }
195 
196 
197  protected function storeRequestParameters(): void
198  {
199  $this->ctrl->saveParameterByClass($this->view_definition->getEmbeddingGui(), self::P_SORTATION);
200  $this->ctrl->saveParameterByClass($this->view_definition->getEmbeddingGui(), self::P_PAGE);
201  }
202 }
__construct(private ViewDefinition $view_definition, private TableDataSource $data_source, ActionGenerator $action_generator=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is how the factory for UI elements looks.
Definition: Factory.php:37
global $DIC
Definition: shib_login.php:25
This describes a Row used in Presentation Table.
language()
description: > Example for rendring a language glyph.
Definition: language.php:25
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...