ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilMMItemRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
31 
37 {
38 
42  private $db;
46  private $cache;
50  private $services;
54  private $main_collector;
55 
60  public function __construct()
61  {
62  global $DIC;
64  $this->db = $DIC->database();
65  $this->main_collector = $DIC->globalScreen()->collector()->mainmenu();
66  $this->main_collector->collectOnce();
67  $this->services = $DIC->globalScreen();
68 
69  foreach ($this->main_collector->getRawUnfilteredItems() as $top_item) {
70  ilMMItemStorage::register($top_item);
71  }
72  }
73 
74  public function clearCache() : void
75  {
76  $this->cache->flush();
77  }
78 
83  public function getSingleItem(IdentificationInterface $identification) : isItem
84  {
85  return $this->main_collector->getSingleItemFromRaw($identification);
86  }
87 
88  public function getSingleItemFromFilter(IdentificationInterface $identification) : isItem
89  {
90  return $this->main_collector->getSingleItemFromFilter($identification);
91  }
92 
93  public function resolveIdentificationFromString(string $identification_string) : IdentificationInterface
94  {
95  return $this->services->identification()->fromSerializedIdentification($identification_string);
96  }
97 
101  public function repository() : ilMMItemRepository
102  {
103  return $this;
104  }
105 
110  public function getTopItems() : array
111  {
112  return ilMMItemStorage::where(" parent_identification = '' OR parent_identification IS NULL ")->orderBy('position')->getArray();
113  }
114 
118  public function getSubItemsForTable() : array
119  {
120  $r = $this->db->query(
121  "SELECT sub_items.*, top_items.position AS parent_position
122 FROM il_mm_items AS sub_items
123 LEFT JOIN il_mm_items AS top_items ON top_items.identification = sub_items.parent_identification
124 WHERE sub_items.parent_identification != '' ORDER BY top_items.position, parent_identification, sub_items.position ASC"
125  );
126  $return = [];
127  while ($data = $this->db->fetchAssoc($r)) {
128  $return[] = $data;
129  }
130 
131  return $return;
132  }
133 
134  public function flushLostItems()
135  {
136  foreach ($this->getTopItems() as $item) {
137  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
138  if (Lost::class === $item_facade->getType()) {
139  $item_facade->delete();
140  }
141  }
142 
143  foreach ($this->getSubItemsForTable() as $item) {
144  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
145  if (Lost::class === $item_facade->getType()) {
146  $item_facade->delete();
147  }
148  }
149  }
150 
151  public function hasLostItems() : bool
152  {
153  foreach ($this->getTopItems() as $item) {
154  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
155  if (Lost::class === $item_facade->getType()) {
156  return true;
157  }
158  }
159 
160  foreach ($this->getSubItemsForTable() as $item) {
161  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
162  if (Lost::class === $item_facade->getType()) {
163  return true;
164  }
165  }
166  return false;
167  }
168 
174  public function getItemFacade(IdentificationInterface $identification = null) : ilMMItemFacadeInterface
175  {
176  if ($identification === null || $identification instanceof NullIdentification || $identification instanceof NullPluginIdentification) {
177  return new ilMMNullItemFacade($identification ? $identification : new NullIdentification(), $this->main_collector);
178  }
179  if ($identification->getClassName() === CustomMainBarProvider::class) {
180  return new ilMMCustomItemFacade($identification, $this->main_collector);
181  }
182 
183  return new ilMMItemFacade($identification, $this->main_collector);
184  }
185 
191  public function getItemFacadeForIdentificationString(string $identification) : ilMMItemFacadeInterface
192  {
193  $id = $this->services->identification()->fromSerializedIdentification($identification);
194 
195  return $this->getItemFacade($id);
196  }
197 
198  public function getPossibleParentsForFormAndTable() : array
199  {
200  static $parents;
201  if ($parents === null) {
202  $parents = [];
203  foreach ($this->getTopItems() as $top_item_identification => $data) {
204  $identification = $this->services->identification()->fromSerializedIdentification($top_item_identification);
205  $item = $this->getSingleItem($identification);
206  if ($item instanceof TopParentItem) {
207  $parents[$top_item_identification] = $this->getItemFacade($identification)
208  ->getDefaultTitle();
209  }
210  }
211  }
212 
213  return $parents;
214  }
215 
219  public function getPossibleSubItemTypesWithInformation() : array
220  {
221  $types = [];
222  foreach ($this->main_collector->getTypeInformationCollection()->getAll() as $information) {
223  if ($information->isCreationPrevented()) {
224  continue;
225  }
226  if ($information->isChild()) {
227  if ($information->getType() === TopLinkItem::class) { // since these two types are identical (more or less), we truncate one
228  continue;
229  }
230  $types[$information->getType()] = $information;
231  }
232  }
233 
234  return $types;
235  }
236 
240  public function getPossibleTopItemTypesWithInformation(bool $new): array
241  {
242  $types = [];
243  foreach ($this->main_collector->getTypeInformationCollection()->getAll() as $information) {
244  if (!$new || $information->isTop()) {
245  $types[$information->getType()] = $information;
246  }
247  }
248 
249  return $types;
250  }
251 
257  public function getTypeHandlerForType(string $type) : TypeHandler
258  {
259  $item = $this->services->mainBar()->custom($type, new NullIdentification());
260 
261  return $this->main_collector->getTypeHandlerForItem($item);
262  }
263 
267  public function updateItem(ilMMItemFacadeInterface $item_facade) : void
268  {
269  if ($item_facade->isEditable()) {
270  $item_facade->update();
271  $this->clearCache();
272  }
273  }
274 
278  public function createItem(ilMMItemFacadeInterface $item_facade) : void
279  {
280  $item_facade->create();
281  $this->clearCache();
282  }
283 
287  public function deleteItem(ilMMItemFacadeInterface $item_facade) : void
288  {
289  if ($item_facade->isDeletable()) {
290  $item_facade->delete();
291  $this->clearCache();
292  }
293  }
294 }
resolveIdentificationFromString(string $identification_string)
getItemFacadeForIdentificationString(string $identification)
$data
Definition: storeScorm.php:23
getPossibleTopItemTypesWithInformation(bool $new)
deleteItem(ilMMItemFacadeInterface $item_facade)
Class ilMMNullItemFacade.
$type
static getInstance($component)
static where($where, $operator=null)
Class ilMMItemRepository.
Class ilMMItemFacade.
updateItem(ilMMItemFacadeInterface $item_facade)
static register(isItem $item)
global $DIC
Definition: goto.php:24
__construct()
ilMMItemRepository constructor.
getSingleItemFromFilter(IdentificationInterface $identification)
Class ilMMCustomItemFacade.
createItem(ilMMItemFacadeInterface $item_facade)
getItemFacade(IdentificationInterface $identification=null)
Interface ilMMItemFacadeInterface.
getSingleItem(IdentificationInterface $identification)