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