ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMMItemRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
34 
39 class ilMMItemRepository implements Request
40 {
41  private ilDBInterface $db;
42 
44 
46 
47  private \ILIAS\Cache\Container\Container $cache;
48 
53  public function __construct()
54  {
55  global $DIC;
56  $this->cache = $DIC->globalCache()->get($this);
57  $this->db = $DIC->database();
58  $this->main_collector = $DIC->globalScreen()->collector()->mainmenu();
59  $this->main_collector->collectOnce();
60  $this->services = $DIC->globalScreen();
61 
62  foreach ($this->main_collector->getRawUnfilteredItems() as $top_item) {
63  ilMMItemStorage::register($top_item);
64  }
65  }
66 
67  public function getContainerKey(): string
68  {
69  return 'mm_items';
70  }
71 
72 
73  public function isForced(): bool
74  {
75  return false;
76  }
77 
78  public function clearCache(): void
79  {
80  $this->cache->flush();
81  }
82 
87  public function getSingleItem(IdentificationInterface $identification): isItem
88  {
89  return $this->main_collector->getSingleItemFromRaw($identification);
90  }
91 
92  public function getSingleItemFromFilter(IdentificationInterface $identification): isItem
93  {
94  return $this->main_collector->getSingleItemFromFilter($identification);
95  }
96 
97  public function resolveIdentificationFromString(string $identification_string): IdentificationInterface
98  {
99  return $this->services->identification()->fromSerializedIdentification($identification_string);
100  }
101 
105  public function repository(): ilMMItemRepository
106  {
107  return $this;
108  }
109 
114  public function getTopItems(): array
115  {
116  return ilMMItemStorage::where(" parent_identification = '' OR parent_identification IS NULL ")->orderBy('position')->getArray();
117  }
118 
122  public function getSubItemsForTable(): array
123  {
124  $r = $this->db->query(
125  "SELECT sub_items.*, top_items.position AS parent_position
126 FROM il_mm_items AS sub_items
127 LEFT JOIN il_mm_items AS top_items ON top_items.identification = sub_items.parent_identification
128 WHERE sub_items.parent_identification != '' ORDER BY top_items.position, parent_identification, sub_items.position ASC"
129  );
130  $return = [];
131  while ($data = $this->db->fetchAssoc($r)) {
132  $return[] = $data;
133  }
134 
135  return $return;
136  }
137 
138  public function flushLostItems(): void
139  {
140  foreach ($this->getTopItems() as $item) {
141  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
142  if (Lost::class === $item_facade->getType()) {
143  $item_facade->delete();
144  }
145  }
146 
147  foreach ($this->getSubItemsForTable() as $item) {
148  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
149  if (Lost::class === $item_facade->getType()) {
150  $item_facade->delete();
151  }
152  }
153  }
154 
155  public function hasLostItems(): bool
156  {
157  foreach ($this->getTopItems() as $item) {
158  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
159  if (Lost::class === $item_facade->getType()) {
160  return true;
161  }
162  }
163 
164  foreach ($this->getSubItemsForTable() as $item) {
165  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
166  if (Lost::class === $item_facade->getType()) {
167  return true;
168  }
169  }
170  return false;
171  }
172 
178  public function getItemFacade(IdentificationInterface $identification = null): ilMMItemFacadeInterface
179  {
180  if ($identification === null || $identification instanceof NullIdentification || $identification instanceof NullPluginIdentification) {
181  return new ilMMNullItemFacade($identification ?: new NullIdentification(), $this->main_collector);
182  }
183  if ($identification->getClassName() === CustomMainBarProvider::class) {
184  return new ilMMCustomItemFacade($identification, $this->main_collector);
185  }
186 
187  return new ilMMItemFacade($identification, $this->main_collector);
188  }
189 
195  public function getItemFacadeForIdentificationString(string $identification): ilMMItemFacadeInterface
196  {
197  $id = $this->services->identification()->fromSerializedIdentification($identification);
198 
199  return $this->getItemFacade($id);
200  }
201 
202  public function getPossibleParentsForFormAndTable(): array
203  {
204  static $parents;
205  if ($parents === null) {
206  $parents = [];
207  foreach ($this->getTopItems() as $top_item_identification => $data) {
208  $identification = $this->services->identification()->fromSerializedIdentification($top_item_identification);
209  $item = $this->getSingleItem($identification);
210  if ($item instanceof TopParentItem) {
211  $parents[$top_item_identification] = $this->getItemFacade($identification)
212  ->getDefaultTitle();
213  }
214  }
215  }
216 
217  return $parents;
218  }
219 
223  public function getPossibleSubItemTypesWithInformation(): array
224  {
225  $types = [];
226  foreach ($this->main_collector->getTypeInformationCollection()->getAll() as $information) {
227  if ($information->isCreationPrevented()) {
228  continue;
229  }
230  if ($information->isChild()) {
231  if ($information->getType() === TopLinkItem::class) { // since these two types are identical (more or less), we truncate one
232  continue;
233  }
234  $types[$information->getType()] = $information;
235  }
236  }
237 
238  return $types;
239  }
240 
244  public function getPossibleTopItemTypesWithInformation(bool $new): array
245  {
246  $types = [];
247  foreach ($this->main_collector->getTypeInformationCollection()->getAll() as $information) {
248  if (!$new || $information->isTop()) {
249  $types[$information->getType()] = $information;
250  }
251  }
252 
253  return $types;
254  }
255 
261  public function getTypeHandlerForType(string $type): TypeHandler
262  {
263  $item = $this->services->mainBar()->custom($type, new NullIdentification());
264 
265  return $this->main_collector->getTypeHandlerForItem($item);
266  }
267 
271  public function updateItem(ilMMItemFacadeInterface $item_facade): void
272  {
273  if ($item_facade->isEditable()) {
274  $item_facade->update();
275  $this->clearCache();
276  }
277  }
278 
282  public function createItem(ilMMItemFacadeInterface $item_facade): void
283  {
284  $item_facade->create();
285  $this->clearCache();
286  }
287 
291  public function deleteItem(ilMMItemFacadeInterface $item_facade): void
292  {
293  if ($item_facade->isDeletable()) {
294  $item_facade->delete();
295  $this->clearCache();
296  }
297  }
298 }
resolveIdentificationFromString(string $identification_string)
getItemFacadeForIdentificationString(string $identification)
Class MainMenuMainCollector This Collector will collect and then provide all available slates from th...
getPossibleTopItemTypesWithInformation(bool $new)
deleteItem(ilMMItemFacadeInterface $item_facade)
Class ilMMNullItemFacade.
static where($where, $operator=null)
Class ilMMItemRepository.
Class ilMMItemFacade.
global $DIC
Definition: feed.php:28
updateItem(ilMMItemFacadeInterface $item_facade)
static register(isItem $item)
__construct()
ilMMItemRepository constructor.
getSingleItemFromFilter(IdentificationInterface $identification)
Class ilMMCustomItemFacade.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createItem(ilMMItemFacadeInterface $item_facade)
getItemFacade(IdentificationInterface $identification=null)
ILIAS Cache Container Container $cache
Interface ilMMItemFacadeInterface.
MainMenuMainCollector $main_collector
getSingleItem(IdentificationInterface $identification)
$r