1<?php
2
3use ILIAS\GlobalScreen\Identification\NullIdentification;
4use ILIAS\GlobalScreen\Scope\MainMenu\Collector\MainMenuMainCollector as Main;
5use ILIAS\GlobalScreen\Scope\MainMenu\Factory\isChild;
6use ILIAS\GlobalScreen\Scope\MainMenu\Factory\isItem;
7use ILIAS\GlobalScreen\Scope\MainMenu\Factory\isTopItem;
8use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Complex;
9use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\LinkList;
10use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Lost;
11use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\RepositoryLink;
12use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Separator;
13use ILIAS\GlobalScreen\Scope\MainMenu\Factory\TopItem\TopLinkItem;
14use ILIAS\GlobalScreen\Scope\MainMenu\Factory\TopItem\TopParentItem;
15use ILIAS\UI\Component\Link\Link;
16
17/**
18 * Class ilMMAbstractItemFacade
19 *
20 * @author Fabian Schmid <fs@studer-raimann.ch>
21 */
22abstract class ilMMAbstractItemFacade implements ilMMItemFacadeInterface
23{
24
25    /**
26     * @var \ILIAS\GlobalScreen\Scope\MainMenu\Collector\Information\TypeInformation
27     */
28    protected $type_information;
29    /**
30     * @var ilMMItemStorage
31     */
32    protected $mm_item;
33    /**
34     * @var isItem
35     */
36    protected $gs_item;
37    /**
38     * @var \ILIAS\GlobalScreen\Identification\IdentificationInterface
39     */
40    protected $identification;
41    /**
42     * @var string
43     */
44    protected $default_title = "-";
45
46
47    /**
48     * ilMMAbstractItemFacade constructor.
49     *
50     * @param \ILIAS\GlobalScreen\Identification\IdentificationInterface $identification
51     * @param Main                                                       $collector
52     *
53     * @throws Throwable
54     */
55    public function __construct(\ILIAS\GlobalScreen\Identification\IdentificationInterface $identification, Main $collector)
56    {
57        $this->identification = $identification;
58        $this->gs_item = $collector->getSingleItem($identification);
59        $this->type_information = $collector->getTypeInformationCollection()->get(get_class($this->gs_item));
60        $this->mm_item = ilMMItemStorage::register($this->gs_item);
61    }
62
63
64    public function getId() : string
65    {
66        return $this->identification->serialize();
67    }
68
69
70    /**
71     * @return bool
72     */
73    public function hasStorage() : bool
74    {
75        return ilMMItemStorage::find($this->getId()) !== null;
76    }
77
78
79    /**
80     * @return bool
81     */
82    public function isEmpty() : bool
83    {
84        return $this->mm_item->getIdentification() == '';
85    }
86
87
88    /**
89     * @return ilMMItemStorage
90     */
91    public function itemStorage() : ilMMItemStorage
92    {
93        return $this->mm_item;
94    }
95
96
97    /**
98     * @return \ILIAS\GlobalScreen\Identification\IdentificationInterface
99     */
100    public function identification() : \ILIAS\GlobalScreen\Identification\IdentificationInterface
101    {
102        return $this->identification;
103    }
104
105
106    /**
107     * @return isItem
108     */
109    public function item() : isItem
110    {
111        return $this->gs_item;
112    }
113
114
115    public function getAmountOfChildren() : int
116    {
117        if ($this->gs_item instanceof \ILIAS\GlobalScreen\Scope\MainMenu\Factory\isParent) {
118            return count($this->gs_item->getChildren());
119        }
120
121        return 0;
122    }
123
124
125    public function isAvailable() : bool
126    {
127        return (bool) ($this->gs_item->isAvailable() || $this->item()->isAlwaysAvailable());
128    }
129
130
131    /**
132     * @inheritDoc
133     */
134    public function isActivated() : bool
135    {
136        return (bool) $this->mm_item->isActive() || $this->item()->isAlwaysAvailable();
137    }
138
139
140    /**
141     * @inheritDoc
142     */
143    public function isAlwaysAvailable() : bool
144    {
145        return $this->item()->isAlwaysAvailable();
146    }
147
148
149    /**
150     * @return string
151     */
152    public function getProviderNameForPresentation() : string
153    {
154        return $this->identification->getProviderNameForPresentation();
155    }
156
157
158    /**
159     * @return string
160     */
161    public function getDefaultTitle() : string
162    {
163        $default_translation = ilMMItemTranslationStorage::getDefaultTranslation($this->identification);
164        if ($default_translation !== "") {
165            return $default_translation;
166        }
167        if ($this->default_title == "-" && $this->gs_item instanceof \ILIAS\GlobalScreen\Scope\MainMenu\Factory\hasTitle) {
168            $this->default_title = $this->gs_item->getTitle();
169        }
170
171        return $this->default_title;
172    }
173
174
175    /**
176     * @param string $default_title
177     */
178    public function setDefaultTitle(string $default_title)
179    {
180        $this->default_title = $default_title;
181    }
182
183
184    /**
185     * @return string
186     */
187    public function getStatus() : string
188    {
189        global $DIC;
190        if (!$this->gs_item->isAvailable() || $this->gs_item->isAlwaysAvailable()) {
191            return $DIC->ui()->renderer()->render($this->gs_item->getNonAvailableReason());
192        }
193
194        return "";
195    }
196
197
198    /**
199     * @return string
200     * @throws ReflectionException
201     */
202    public function getTypeForPresentation() : string
203    {
204        return $this->type_information->getTypeNameForPresentation();
205    }
206
207
208    public function getParentIdentificationString() : string
209    {
210        if ($this->gs_item instanceof isChild) {
211            $provider_name_for_presentation = $this->gs_item->getParent()->serialize();
212
213            return $provider_name_for_presentation;
214        }
215
216        return "";
217    }
218
219
220    /**
221     * @return bool
222     */
223    public function isCustomType() : bool
224    {
225        $known_core_types = [
226            Complex::class,
227            Link::class,
228            LinkList::class,
229            Lost::class,
230            RepositoryLink::class,
231            Separator::class,
232            TopLinkItem::class,
233            TopParentItem::class,
234        ];
235        foreach ($known_core_types as $known_core_type) {
236            if (get_class($this->gs_item) === $known_core_type) {
237                return false;
238            }
239        }
240
241        return true;
242    }
243
244
245    /**
246     * @inheritDoc
247     */
248    public function isTopItem() : bool
249    {
250        return $this->gs_item instanceof isTopItem;
251    }
252
253
254    /**
255     * @inheritDoc
256     */
257    public function isInLostItem() : bool
258    {
259        if ($this->gs_item instanceof isChild) {
260            return $this->gs_item->getParent() instanceof NullIdentification;
261        }
262
263        return false;
264    }
265
266
267    /**
268     * @inheritDoc
269     */
270    public function setIsTopItm(bool $top_item)
271    {
272        // TODO: Implement setIsTopItm() method.
273    }
274
275
276    /**
277     * FSX check if doublette
278     *
279     * @inheritDoc
280     */
281    public function getType() : string
282    {
283        return $this->type_information->getType();
284    }
285
286
287    /**
288     * @param string $parent
289     */
290    public function setParent(string $parent)
291    {
292        $this->mm_item->setParentIdentification($parent);
293    }
294
295
296    /**
297     * @inheritdoc
298     */
299    public function setPosition(int $position)
300    {
301        $this->mm_item->setPosition($position);
302    }
303
304
305    /**
306     * @param bool $status
307     */
308    public function setActiveStatus(bool $status)
309    {
310        $this->mm_item->setActive($status);
311    }
312
313
314    // CRUD
315
316    public function update()
317    {
318        ilMMItemTranslationStorage::storeDefaultTranslation($this->identification, $this->default_title);
319        $this->mm_item->update();
320    }
321
322
323    public function create()
324    {
325        ilMMItemTranslationStorage::storeDefaultTranslation($this->identification, $this->default_title);
326        $this->mm_item->create();
327        ilMMItemStorage::register($this->gs_item);
328    }
329
330    /**
331     * deletes all translations associated with the current identification.
332     * @throws Exception
333     */
334    protected function deleteAssociatedTranslations()
335    {
336        $ts = ilMMItemTranslationStorage::where([
337            'identification' => $this->identification->serialize(),
338        ], '=')->get();
339
340        if (!empty($ts)) {
341            foreach ($ts as $translation) {
342                if ($translation instanceof ilMMItemTranslationStorage) {
343                    $translation->delete();
344                }
345            }
346        }
347    }
348
349    /**
350     * @inheritDoc
351     */
352    public function delete()
353    {
354        if ($this->isDeletable()) {
355            $this->deleteAssociatedTranslations();
356            $serialize = $this->identification->serialize();
357            $gs = ilGSIdentificationStorage::find($serialize);
358            if ($gs instanceof ilGSIdentificationStorage) {
359                $gs->delete();
360            }
361            $mm = ilMMItemStorage::find($serialize);
362            if ($mm instanceof ilMMItemStorage) {
363                $mm->delete();
364            }
365        }
366    }
367}
368