ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
MainMenuMainCollector.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
21 
42 use Iterator;
43 use Throwable;
44 use Generator;
45 
54 {
60  protected array $providers;
61  private Map $map;
62 
69  public function __construct(array $providers, MainMenuItemFactory $factory, ItemInformation $information = null)
70  {
71  $this->information = $information;
72  $this->providers = $providers;
73  $this->type_information_collection = new TypeInformationCollection();
74  $this->map = new Map($factory);
75  }
76 
80  private function getProvidersFromList(): Iterator
81  {
82  yield from $this->providers;
83  }
84 
85  public function collectStructure(): void
86  {
87  foreach ($this->getProvidersFromList() as $provider) {
88  $this->type_information_collection->append($provider->provideTypeInformation());
89  $this->map->addMultiple(...$provider->getStaticTopItems());
90  $this->map->addMultiple(...$provider->getStaticSubItems());
91  }
92  }
93 
94  public function filterItemsByVisibilty(bool $async_only = false): void
95  {
96  // apply filter
97  $this->map->filter(function (isItem $item) use ($async_only): bool {
98  if ($async_only && !$item instanceof supportsAsynchronousLoading) {
99  return false;
100  }
101  if (!$item->isAvailable()) {
102  return false;
103  }
104 
105  // make parent available if one child is always available
106  if ($item instanceof isParent) {
107  foreach ($item->getChildren() as $child) {
108  if ($child->isAlwaysAvailable()) {
109  return true;
110  }
111  }
112  }
113 
114  // Always avaiable must be delivered when visible
115  if ($item->isAlwaysAvailable()) {
116  return $item->isVisible();
117  }
118  // All other cases
119  return $item->isAvailable() && $item->isVisible() && $this->information->isItemActive($item);
120  });
121  }
122 
123  public function prepareItemsForUIRepresentation(): void
124  {
125  $this->map->walk(function (isItem &$item): isItem {
126  if (is_null($item->getTypeInformation())) {
127  $item->setTypeInformation(
128  $this->getTypeInformationForItem($item)
129  );
130  }
131 
132  // Apply the TypeHandler
133  $item = $this->getTypeHandlerForItem($item)->enrichItem($item);
134  // Translate Item
135  if ($item instanceof hasTitle) {
136  $item = $this->getItemInformation()->customTranslationForUser($item);
137  }
138  // Custom Symbol
139  if ($item instanceof hasSymbol) {
140  $item = $this->getItemInformation()->customSymbol($item);
141  }
142  // Custom Position
143  $item = $this->getItemInformation()->customPosition($item);
144 
145  return $item;
146  });
147 
148  // Override parent from configuration
149  $this->map->walk(function (isItem $item) {
150  if ($item instanceof isChild || $item instanceof isInterchangeableItem) {
151  $parent = $this->map->getSingleItemFromFilter($this->information->getParent($item));
152  if ($parent instanceof isParent) {
153  $parent->appendChild($item);
154  $item->overrideParent($parent->getProviderIdentification());
155  }
156  }
157 
158  return $item;
159  });
160  }
161 
162  public function cleanupItemsForUIRepresentation(): void
163  {
164  // Remove not visible children
165  $this->map->walk(function (isItem &$item): isItem {
166  if ($item instanceof isParent) {
167  foreach ($item->getChildren() as $child) {
168  if (!$this->map->existsInFilter($child->getProviderIdentification())) {
169  $item->removeChild($child);
170  }
171  }
172  }
173  return $item;
174  });
175 
176  $this->map->walk(static function (isItem &$i): void {
177  if ($i instanceof isParent && count($i->getChildren()) === 0) {
178  $i = $i->withAvailableCallable(static function () {
179  return false;
180  })->withVisibilityCallable(static function () {
181  return false;
182  });
183  }
184  });
185 
186  // filter empty slates
187  $this->map->filter(static function (isItem $i): bool {
188  if ($i instanceof isParent) {
189  return count($i->getChildren()) > 0;
190  }
191 
192  return true;
193  });
194  }
195 
196  public function sortItemsForUIRepresentation(): void
197  {
198  $this->map->sort();
199  }
200 
208  {
209  foreach ($this->map->getAllFromFilter() as $item) {
210  if ($item->isTop()) {
211  yield $item;
212  }
213  }
214  }
215 
219  public function getRawItems(): Iterator
220  {
221  yield from $this->map->getAllFromFilter();
222  }
223 
227  public function getRawUnfilteredItems(): Iterator
228  {
229  yield from $this->map->getAllFromRaw();
230  }
231 
232 
233  public function hasItems(): bool
234  {
235  return $this->map->has();
236  }
237 
238  public function hasVisibleItems(): bool
239  {
240  if (!$this->hasItems()) {
241  return false;
242  }
243  foreach ($this->getItemsForUIRepresentation() as $item) {
244  return $item instanceof isItem;
245  }
246  return false;
247  }
248 
254  public function getSingleItemFromFilter(IdentificationInterface $identification): isItem
255  {
256  return $this->map->getSingleItemFromFilter($identification);
257  }
258 
264  public function getSingleItemFromRaw(IdentificationInterface $identification): isItem
265  {
266  return $this->map->getSingleItemFromRaw($identification);
267  }
268 
273  public function getTypeHandlerForItem(isItem $item): TypeHandler
274  {
275  $type_information = $this->getTypeInformationForItem($item);
276  if ($type_information === null) {
277  return new BaseTypeHandler();
278  }
279 
280  return $type_information->getTypeHandler();
281  }
282 
287  {
288  return $this->information;
289  }
290 
296  {
297  return $this->getTypeInformationCollection()->get(get_class($item));
298  }
299 
304  {
306  }
307 }
Class MainMenuMainCollector This Collector will collect and then provide all available slates from th...
getItemsForUIRepresentation()
This will return all available isTopItem (and moved isInterchangeableItem), stacked based on the conf...
setTypeInformation(TypeInformation $information)
$provider
Definition: ltitoken.php:83
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
withAvailableCallable(callable $is_available)
Pass a callable which can decide whether your element is available in general, e.g.
__construct(array $providers, MainMenuItemFactory $factory, ItemInformation $information=null)
MainMenuMainCollector constructor.
Interface supportsAsynchronousLoading Types, which implement this interface, can load their content a...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface hasSymbol Methods for Entries with Symbols.
Definition: hasSymbol.php:32
$factory
Definition: metadata.php:75
$i
Definition: metadata.php:41