1<?php
2
3use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Information\TypeInformation;
4use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Information\TypeInformationCollection;
5use ILIAS\GlobalScreen\Scope\MainMenu\Factory\hasAction;
6use ILIAS\GlobalScreen\Scope\MainMenu\Factory\hasTitle;
7use ILIAS\GlobalScreen\Scope\MainMenu\Factory\isChild;
8use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Complex;
9use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Link;
10use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\LinkList;
11use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Lost;
12use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\RepositoryLink;
13use ILIAS\GlobalScreen\Scope\MainMenu\Factory\Item\Separator;
14use ILIAS\GlobalScreen\Scope\MainMenu\Factory\TopItem\TopLinkItem;
15use ILIAS\GlobalScreen\Scope\MainMenu\Factory\TopItem\TopParentItem;
16use ILIAS\GlobalScreen\Scope\MainMenu\Provider\AbstractStaticMainMenuProvider;
17use ILIAS\GlobalScreen\Scope\MainMenu\Provider\StaticMainMenuProvider;
18
19/**
20 * Class ilMMCustomProvider
21 *
22 * @author Fabian Schmid <fs@studer-raimann.ch>
23 */
24class ilMMCustomProvider extends AbstractStaticMainMenuProvider implements StaticMainMenuProvider
25{
26
27    /**
28     * @var \ILIAS\DI\Container
29     */
30    protected $dic;
31
32
33    /**
34     * @return TopParentItem[]
35     */
36    public function getStaticTopItems() : array
37    {
38        /**
39         * @var $item ilMMCustomItemStorage
40         */
41        $top_items = [];
42        foreach (ilMMCustomItemStorage::where(['top_item' => true])->get() as $item) {
43            $top_items[] = $this->getSingleCustomItem($item, false);
44        }
45
46        return $top_items;
47    }
48
49
50    /**
51     * @return \ILIAS\GlobalScreen\Scope\MainMenu\Factory\isItem[]
52     */
53    public function getStaticSubItems() : array
54    {
55        /**
56         * @var $item ilMMCustomItemStorage
57         */
58        $items = [];
59        foreach (ilMMCustomItemStorage::where(['top_item' => false])->get() as $item) {
60            $items[] = $this->getSingleCustomItem($item, false);
61        }
62
63        return $items;
64    }
65
66
67    /**
68     * @param ilMMCustomItemStorage $storage
69     * @param bool                  $register
70     *
71     * @return \ILIAS\GlobalScreen\Scope\MainMenu\Factory\isItem
72     */
73    public function getSingleCustomItem(ilMMCustomItemStorage $storage, $register = false) : \ILIAS\GlobalScreen\Scope\MainMenu\Factory\isItem
74    {
75        $identification = $this->globalScreen()->identification()->core($this)->identifier($storage->getIdentifier());
76
77        if ($register) {
78            ilGSIdentificationStorage::registerIdentification($identification, $this);
79        }
80
81        $item = $this->globalScreen()->mainmenu()->custom($storage->getType(), $identification);
82
83        if ($item instanceof hasTitle && $storage->getDefaultTitle() !== '') {
84            $item = $item->withTitle($storage->getDefaultTitle());
85        }
86        if ($item instanceof hasAction) {
87            $item = $item->withAction("#");
88        }
89        if ($item instanceof isChild) {
90            $mm_item = ilMMItemStorage::find($identification->serialize());
91            $parent_identification = "";
92            if ($mm_item instanceof ilMMItemStorage) {
93                $parent_identification = $mm_item->getParentIdentification();
94            }
95
96            if ($parent_identification) {
97                $item = $item->withParent(
98                    $this->globalScreen()
99                        ->identification()
100                        ->fromSerializedIdentification($parent_identification)
101                );
102            }
103        }
104
105        if ($register) {
106            ilMMItemStorage::register($item);
107        }
108
109        return $item;
110    }
111
112
113    /**
114     * @inheritDoc
115     */
116    public function provideTypeInformation() : TypeInformationCollection
117    {
118        $c = new TypeInformationCollection();
119        $c->add(new TypeInformation(TopParentItem::class, $this->translateType(TopParentItem::class), new ilMMTopParentItemRenderer()));
120        $c->add(new TypeInformation(TopLinkItem::class, $this->translateType(TopLinkItem::class), new ilMMTopLinkItemRenderer(), new ilMMTypeHandlerTopLink()));
121        $c->add(new TypeInformation(Link::class, $this->translateType(Link::class), null, new ilMMTypeHandlerLink()));
122        $link_list = new TypeInformation(LinkList::class, $this->translateType(LinkList::class));
123        $link_list->setCreationPrevented(true);
124        $c->add($link_list);
125        $c->add(new TypeInformation(Separator::class, $this->translateType(Separator::class), null, new ilMMTypeHandlerSeparator(), $this->translateByline(Separator::class)));
126        $c->add(new TypeInformation(RepositoryLink::class, $this->translateType(RepositoryLink::class), null, new ilMMTypeHandlerRepositoryLink()));
127        $complex = new TypeInformation(Complex::class, $this->translateType(Complex::class));
128        $complex->setCreationPrevented(true);
129        $c->add($complex);
130        $lost = new TypeInformation(Lost::class, $this->translateType(Lost::class), new ilMMLostItemRenderer());
131        $lost->setCreationPrevented(true);
132        $c->add($lost);
133
134        return $c;
135    }
136
137
138    /**
139     * @param string $type
140     *
141     * @return string
142     */
143    private function translateType(string $type) : string
144    {
145        $last_part = substr(strrchr($type, "\\"), 1);
146        $last_part = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $last_part));
147
148        return $this->dic->language()->txt("type_" . strtolower($last_part));
149    }
150
151
152    /**
153     * @param string $type
154     *
155     * @return string
156     */
157    private function translateByline(string $type) : string
158    {
159        $last_part = substr(strrchr($type, "\\"), 1);
160        $last_part = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $last_part));
161
162        return $this->dic->language()->txt("type_" . strtolower($last_part) . "_info");
163    }
164}
165