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