ILIAS  release_7 Revision v7.30-3-g800a261c036
MainMenuMainCollector.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
21
42use Iterator;
43use Throwable;
44use Generator;
45
54{
62 private $information;
66 protected $providers;
70 private $map;
71
79 {
80 $this->information = $information;
81 $this->providers = $providers;
82 $this->type_information_collection = new TypeInformationCollection();
83 $this->map = new Map($factory);
84 }
85
89 private function getProvidersFromList() : Iterator
90 {
91 yield from $this->providers;
92 }
93
94 public function collectStructure() : void
95 {
96 foreach ($this->getProvidersFromList() as $provider) {
97 $this->type_information_collection->append($provider->provideTypeInformation());
98 $this->map->addMultiple(...$provider->getStaticTopItems());
99 $this->map->addMultiple(...$provider->getStaticSubItems());
100 }
101 }
102
103 public function filterItemsByVisibilty(bool $async_only = false) : void
104 {
105 // apply filter
106 $this->map->filter(function (isItem $item) use ($async_only) : bool {
107 if ($async_only && !$item instanceof supportsAsynchronousLoading) {
108 return false;
109 }
110 if (!$item->isAvailable()) {
111 return false;
112 }
113
114 // make parent available if one child is always available
115 if ($item instanceof isParent) {
116 foreach ($item->getChildren() as $child) {
117 if ($child->isAlwaysAvailable()) {
118 return true;
119 }
120 }
121 }
122
123 // Always avaiable must be delivered when visible
124 if ($item->isAlwaysAvailable()) {
125 return $item->isVisible();
126 }
127 // All other cases
128 return $item->isAvailable() && $item->isVisible() && $this->information->isItemActive($item);
129 });
130 }
131
132 public function prepareItemsForUIRepresentation() : void
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) {
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
171 public function cleanupItemsForUIRepresentation() : void
172 {
173 // Remove not visible children
174 $this->map->walk(function (isItem &$item) : isItem {
175 if ($item instanceof isParent) {
176 foreach ($item->getChildren() as $child) {
177 if (!$this->map->existsInFilter($child->getProviderIdentification())) {
178 $item->removeChild($child);
179 }
180 }
181 }
182 return $item;
183 });
184
185 $this->map->walk(static function (isItem &$i) : void {
186 if ($i instanceof isParent && count($i->getChildren()) === 0) {
187 $i = $i->withAvailableCallable(static function () {
188 return false;
189 })->withVisibilityCallable(static function () {
190 return false;
191 });
192 }
193 });
194
195 // filter empty slates
196 $this->map->filter(static function (isItem $i) : bool {
197 if ($i instanceof isParent) {
198 return count($i->getChildren()) > 0;
199 }
200
201 return true;
202 });
203 }
204
205 public function sortItemsForUIRepresentation() : void
206 {
207 $this->map->sort();
208 }
209
216 public function getItemsForUIRepresentation() : Generator
217 {
218 foreach ($this->map->getAllFromFilter() as $item) {
219 if ($item->isTop()) {
220 yield $item;
221 }
222 }
223 }
224
228 public function getRawItems() : Iterator
229 {
230 yield from $this->map->getAllFromFilter();
231 }
232
236 public function getRawUnfilteredItems() : Iterator
237 {
238 yield from $this->map->getAllFromRaw();
239 }
240
241
242 public function hasItems() : bool
243 {
244 return $this->map->has();
245 }
246
247 public function hasVisibleItems() : bool
248 {
249 if (!$this->hasItems()) {
250 return false;
251 }
252 foreach ($this->getItemsForUIRepresentation() as $item) {
253 return $item instanceof isItem;
254 }
255 return false;
256 }
257
263 public function getSingleItemFromFilter(IdentificationInterface $identification) : isItem
264 {
265 return $this->map->getSingleItemFromFilter($identification);
266 }
267
273 public function getSingleItemFromRaw(IdentificationInterface $identification) : isItem
274 {
275 return $this->map->getSingleItemFromRaw($identification);
276 }
277
282 public function getTypeHandlerForItem(isItem $item) : TypeHandler
283 {
284 $type_information = $this->getTypeInformationForItem($item);
285 if ($type_information === null) {
286 return new BaseTypeHandler();
287 }
288
289 return $type_information->getTypeHandler();
290 }
291
296 {
297 return $this->information;
298 }
299
305 {
306 return $this->getTypeInformationCollection()->get(get_class($item));
307 }
308
313 {
314 return $this->type_information_collection;
315 }
316}
An exception for terminatinating execution or to throw for unit testing.
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...
__construct(array $providers, MainMenuItemFactory $factory, ItemInformation $information=null)
MainMenuMainCollector constructor.
Class MainMenuItemFactory This factory provides you all available types for MainMenu GlobalScreen Ite...
if(!file_exists(getcwd() . '/ilias.ini.php'))
registration confirmation script for ilias
Definition: confirmReg.php:12
Interface hasSymbol Methods for Entries with Symbols.
Definition: hasSymbol.php:33
Interface supportsAsynchronousLoading Types, which implement this interface, can load their content a...
$factory
Definition: metadata.php:58
$i
Definition: metadata.php:24