ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
MainMenuMainCollector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
42use Iterator;
43use Generator;
47
56{
58 private readonly Map $map;
59
60 private bool $default_topics = false;
61
62 public function __construct(protected array $providers, MainMenuItemFactory $factory, private readonly ?ItemInformation $information = null)
63 {
64 $this->type_information_collection = new TypeInformationCollection();
65 $this->map = new Map($factory);
66 }
67
71 private function getProvidersFromList(): Iterator
72 {
73 yield from $this->providers;
74 }
75
76 public function collectStructure(): void
77 {
78 foreach ($this->getProvidersFromList() as $provider) {
79 $this->type_information_collection->append($provider->provideTypeInformation());
80 $this->map->addMultiple(...$provider->getStaticTopItems());
81 $this->map->addMultiple(...$provider->getStaticSubItems());
82 }
83 }
84
85 public function filterItemsByVisibilty(bool $async_only = false): void
86 {
87 // apply filter
88 $this->map->filter(function (isItem $item) use ($async_only): bool {
89 if ($async_only && !$item instanceof supportsAsynchronousLoading) {
90 return false;
91 }
92 if (!$item->isAvailable()) {
93 return false;
94 }
95
96 // make parent available if one child is always available
97 if ($item instanceof isParent) {
98 foreach ($item->getChildren() as $child) {
99 if ($child->isAlwaysAvailable()) {
100 return true;
101 }
102 }
103 }
104
105 // Always avaiable must be delivered when visible
106 if ($item->isAlwaysAvailable()) {
107 return $item->isVisible();
108 }
109 // All other cases
110 return $item->isAvailable() && $item->isVisible();
111 });
112
113 $this->map->walk(function (isItem &$item) {
114 if (!$this->information->isItemActive($item)) {
115 $item = $item->withAvailableCallable(fn(): bool => false)
116 ->withNonAvailableReason('-deactived_by_configuration-');
117 $this->map->add($item);
118 }
119 });
120 }
121
122 public function prepareItemsForUIRepresentation(): void
123 {
124 if ($this->default_topics) {
125 $this->map->walk(function (isItem &$item): isItem {
126 if ($item instanceof isDecorateable) {
127 $item = $item->withTopics(new Topic($item->getProviderIdentification()->getInternalIdentifier()));
128 }
129 return $item;
130 });
131 }
132
133 $this->map->walk(function (isItem &$item): isItem {
134 if (is_null($item->getTypeInformation())) {
135 $item->setTypeInformation(
136 $this->getTypeInformationForItem($item)
137 );
138 }
139
140 // Apply the TypeHandler
141 $item = $this->getTypeHandlerForItem($item)->enrichItem($item);
142 // Translate Item
143 if ($item instanceof hasTitle) {
144 $item = $this->getItemInformation()->customTranslationForUser($item);
145 }
146 // Custom Symbol
147 if ($item instanceof hasSymbol) {
148 $item = $this->getItemInformation()->customSymbol($item);
149 }
150 // Custom Position
151 $item = $this->getItemInformation()->customPosition($item);
152
153 return $item;
154 });
155
156 // Override parent from configuration
157 $this->map->walk(function (isItem $item): isItem {
158 if ($item instanceof isChild || $item instanceof isInterchangeableItem) {
159 $parent = $this->map->getSingleItemFromFilter($this->information->getParent($item));
160 if ($parent instanceof isParent) {
161 $parent->appendChild($item);
162 $item->overrideParent($parent->getProviderIdentification());
163 }
164 }
165
166 return $item;
167 });
168
169 $this->map->walk(function (isItem $item): isItem {
170 if ($item instanceof isParent) {
171 $item->calculateAmountOfChildren();
172 }
173 return $item;
174 });
175 }
176
177 public function cleanupItemsForUIRepresentation(): void
178 {
179 // Remove not visible children
180 $this->map->walk(function (isItem &$item): isItem {
181 if ($item instanceof isParent) {
182 foreach ($item->getChildren() as $child) {
183 if (!$this->map->existsInFilter($child->getProviderIdentification())) {
184 $item->removeChild($child);
185 }
186 }
187 }
188 return $item;
189 });
190
191 $this->map->walk(static function (isItem &$i): void {
192 if ($i instanceof isParent && $i->getChildren() === []) {
193 $i = $i->withAvailableCallable(static fn(): bool => false)->withVisibilityCallable(static fn(): bool => false);
194 }
195 });
196 }
197
198 public function sortItemsForUIRepresentation(): void
199 {
200 $this->map->sort();
201 }
202
209 public function getItemsForUIRepresentation(): Generator
210 {
211 foreach ($this->map->getAllFromFilter() as $item) {
212 if ($item->isTop() && $item->isAvailable() && $item->isVisible()) {
213 yield $item;
214 }
215 }
216 }
217
221 public function getRawItems(): Iterator
222 {
223 yield from $this->map->getAllFromFilter();
224 }
225
229 public function getRawUnfilteredItems(): Iterator
230 {
231 yield from $this->map->getAllFromRaw();
232 }
233
234
235 public function hasItems(): bool
236 {
237 return $this->map->has();
238 }
239
240 public function hasVisibleItems(): bool
241 {
242 if (!$this->hasItems()) {
243 return false;
244 }
245 foreach ($this->getItemsForUIRepresentation() as $item) {
246 return $item instanceof isItem;
247 }
248 return false;
249 }
250
255 {
256 return $this->map->getSingleItemFromFilter($identification);
257 }
258
262 public function getSingleItemFromRaw(IdentificationInterface $identification): isItem
263 {
264 return $this->map->getSingleItemFromRaw($identification);
265 }
266
268 {
269 $type_information = $this->getTypeInformationForItem($item);
270 if ($type_information === null) {
271 return new BaseTypeHandler();
272 }
273
274 return $type_information->getTypeHandler();
275 }
276
281 {
282 return $this->information;
283 }
284
286 {
287 return $this->getTypeInformationCollection()->get($item::class);
288 }
289
291 {
292 return $this->type_information_collection;
293 }
294}
Class MainMenuMainCollector This Collector will collect and then provide all available slates from th...
__construct(protected array $providers, MainMenuItemFactory $factory, private readonly ?ItemInformation $information=null)
getItemsForUIRepresentation()
This will return all available isTopItem (and moved isInterchangeableItem), stacked based on the conf...
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
This is just a class that marks a string as a help topic.
Definition: Topic.php:27
Interface supportsAsynchronousLoading Types, which implement this interface, can load their content a...
$provider
Definition: ltitoken.php:80
if(!file_exists('../ilias.ini.php'))