1<?php
2
3use ILIAS\GlobalScreen\Identification\IdentificationInterface;
4use ILIAS\GlobalScreen\Scope\MainMenu\Collector\MainMenuMainCollector as Main;
5
6/**
7 * Class ilMMCustomItemFacade
8 *
9 * @author Fabian Schmid <fs@studer-raimann.ch>
10 */
11class ilMMCustomItemFacade extends ilMMAbstractItemFacade
12{
13
14    /**
15     * @var ilMMCustomItemStorage|null
16     */
17    protected $custom_item_storage;
18    /**
19     * @var string
20     */
21    protected $action = '';
22    /**
23     * @var string
24     */
25    protected $type = '';
26    /**
27     * @var bool
28     */
29    protected $top_item = false;
30
31
32    /**
33     * @inheritDoc
34     */
35    public function __construct(IdentificationInterface $identification, Main $collector)
36    {
37        parent::__construct($identification, $collector);
38        $this->custom_item_storage = $this->getCustomStorage();
39        if ($this->custom_item_storage instanceof ilMMCustomItemStorage) {
40            if ($this->custom_item_storage->getType()) {
41                $this->type = $this->custom_item_storage->getType();
42            }
43        }
44    }
45
46
47    /**
48     * @inheritDoc
49     */
50    public function update()
51    {
52        if ($this->isCustom()) {
53            $mm = $this->getCustomStorage();
54            if ($mm instanceof ilMMCustomItemStorage) {
55                $default_title = ilMMItemTranslationStorage::getDefaultTranslation($this->identification());
56                $mm->setDefaultTitle($default_title);
57                $mm->setType($this->getType());
58                $mm->update();
59            }
60        }
61        parent::update();
62    }
63
64
65    /**
66     * @inheritDoc
67     */
68    public function delete()
69    {
70        if (!$this->isDeletable()) {
71            throw new LogicException("Non Custom items can't be deleted");
72        }
73
74        $cm = $this->getCustomStorage();
75        if ($cm instanceof ilMMCustomItemStorage) {
76            $cm->delete();
77        }
78        parent::delete();
79    }
80
81
82    /**
83     * @return ilMMCustomItemStorage|null
84     */
85    private function getCustomStorage()
86    {
87        $id = $this->gs_item->getProviderIdentification()->getInternalIdentifier();
88        $mm = ilMMCustomItemStorage::find($id);
89
90        return $mm;
91    }
92
93
94    /**
95     * @inheritDoc
96     */
97    public function isCustom() : bool
98    {
99        return true;
100    }
101
102
103    /**
104     * @inheritDoc
105     */
106    public function isEditable() : bool
107    {
108        return true;
109    }
110
111
112    /**
113     * @inheritDoc
114     */
115    public function isDeletable() : bool
116    {
117        return true;
118    }
119
120
121    /**
122     * @inheritDoc
123     */
124    public function getProviderNameForPresentation() : string
125    {
126        return "Custom";
127    }
128
129
130    /**
131     * @return string
132     */
133    public function getStatus() : string
134    {
135        return "";
136    }
137
138
139    /**
140     * @inheritDoc
141     */
142    public function setAction(string $action)
143    {
144        $this->action = $action;
145    }
146
147
148    /**
149     * @inheritDoc
150     */
151    public function getType() : string
152    {
153        return $this->type;
154    }
155
156
157    /**
158     * @inheritDoc
159     */
160    public function setType(string $type)
161    {
162        $this->type = $type;
163    }
164
165
166    /**
167     * @inheritDoc
168     */
169    public function isTopItem() : bool
170    {
171        if ($this->gs_item instanceof \ILIAS\GlobalScreen\Scope\MainMenu\Factory\isItem) {
172            return parent::isTopItem();
173        }
174
175        return $this->top_item;
176    }
177
178
179    /**
180     * @inheritDoc
181     */
182    public function setIsTopItm(bool $top_item)
183    {
184        $this->top_item = $top_item;
185    }
186}
187