ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilMMItemRepository.php
Go to the documentation of this file.
1 <?php
2 
13 
19 {
20 
24  private $db;
28  private $cache;
32  private $services;
36  private $main_collector;
37 
42  public function __construct()
43  {
44  global $DIC;
46  $this->db = $DIC->database();
47  $this->main_collector = $DIC->globalScreen()->collector()->mainmenu();
48  $this->main_collector->collectOnce();
49  $this->services = $DIC->globalScreen();
50 
51  foreach ($this->main_collector->getRawItems() as $top_item) {
52  ilMMItemStorage::register($top_item);
53  }
54  }
55 
56  public function clearCache() : void
57  {
58  $this->cache->flush();
59  }
60 
65  public function getSingleItem(IdentificationInterface $identification) : isItem
66  {
67  return $this->main_collector->getSingleItemFromRaw($identification);
68  }
69 
70  public function getSingleItemFromFilter(IdentificationInterface $identification) : isItem
71  {
72  return $this->main_collector->getSingleItemFromFilter($identification);
73  }
74 
75  public function resolveIdentificationFromString(string $identification_string) : IdentificationInterface
76  {
77  return $this->services->identification()->fromSerializedIdentification($identification_string);
78  }
79 
83  public function repository() : ilMMItemRepository
84  {
85  return $this;
86  }
87 
92  public function getTopItems() : array
93  {
94  return ilMMItemStorage::where(" parent_identification = '' OR parent_identification IS NULL ")->orderBy('position')->getArray();
95  }
96 
100  public function getSubItemsForTable() : array
101  {
102  $r = $this->db->query(
103  "SELECT sub_items.*, top_items.position AS parent_position
104 FROM il_mm_items AS sub_items
105 LEFT JOIN il_mm_items AS top_items ON top_items.identification = sub_items.parent_identification
106 WHERE sub_items.parent_identification != '' ORDER BY top_items.position, parent_identification, sub_items.position ASC"
107  );
108  $return = [];
109  while ($data = $this->db->fetchAssoc($r)) {
110  $return[] = $data;
111  }
112 
113  return $return;
114  }
115 
116  public function flushLostItems()
117  {
118  foreach ($this->getTopItems() as $item) {
119  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
120  if (Lost::class === $item_facade->getType()) {
121  $item_facade->delete();
122  }
123  }
124 
125  foreach ($this->getSubItemsForTable() as $item) {
126  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
127  if (Lost::class === $item_facade->getType()) {
128  $item_facade->delete();
129  }
130  }
131  }
132 
133  public function hasLostItems() : bool
134  {
135  foreach ($this->getTopItems() as $item) {
136  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
137  if (Lost::class === $item_facade->getType()) {
138  return true;
139  }
140  }
141 
142  foreach ($this->getSubItemsForTable() as $item) {
143  $item_facade = $this->getItemFacade($this->services->identification()->fromSerializedIdentification($item['identification']));
144  if (Lost::class === $item_facade->getType()) {
145  return true;
146  }
147  }
148  return false;
149  }
150 
156  public function getItemFacade(IdentificationInterface $identification = null) : ilMMItemFacadeInterface
157  {
158  if ($identification === null || $identification instanceof NullIdentification || $identification instanceof NullPluginIdentification) {
159  return new ilMMNullItemFacade($identification ? $identification : new NullIdentification(), $this->main_collector);
160  }
161  if ($identification->getClassName() === CustomMainBarProvider::class) {
162  return new ilMMCustomItemFacade($identification, $this->main_collector);
163  }
164 
165  return new ilMMItemFacade($identification, $this->main_collector);
166  }
167 
173  public function getItemFacadeForIdentificationString(string $identification) : ilMMItemFacadeInterface
174  {
175  $id = $this->services->identification()->fromSerializedIdentification($identification);
176 
177  return $this->getItemFacade($id);
178  }
179 
180  public function getPossibleParentsForFormAndTable() : array
181  {
182  static $parents;
183  if ($parents === null) {
184  $parents = [];
185  foreach ($this->getTopItems() as $top_item_identification => $data) {
186  $identification = $this->services->identification()->fromSerializedIdentification($top_item_identification);
187  $item = $this->getSingleItem($identification);
188  if ($item instanceof TopParentItem) {
189  $parents[$top_item_identification] = $this->getItemFacade($identification)
190  ->getDefaultTitle();
191  }
192  }
193  }
194 
195  return $parents;
196  }
197 
201  public function getPossibleSubItemTypesWithInformation() : array
202  {
203  $types = [];
204  foreach ($this->main_collector->getTypeInformationCollection()->getAll() as $information) {
205  if ($information->isCreationPrevented()) {
206  continue;
207  }
208  if ($information->isChild()) {
209  if ($information->getType() === TopLinkItem::class) { // since these two types are identical (more or less), we truncate one
210  continue;
211  }
212  $types[$information->getType()] = $information;
213  }
214  }
215 
216  return $types;
217  }
218 
222  public function getPossibleTopItemTypesWithInformation() : array
223  {
224  $types = [];
225  foreach ($this->main_collector->getTypeInformationCollection()->getAll() as $information) {
226  if ($information->isTop()) {
227  $types[$information->getType()] = $information;
228  }
229  }
230 
231  return $types;
232  }
233 
239  public function getTypeHandlerForType(string $type) : TypeHandler
240  {
241  $item = $this->services->mainBar()->custom($type, new NullIdentification());
242 
243  return $this->main_collector->getTypeHandlerForItem($item);
244  }
245 
249  public function updateItem(ilMMItemFacadeInterface $item_facade) : void
250  {
251  if ($item_facade->isEditable()) {
252  $item_facade->update();
253  $this->clearCache();
254  }
255  }
256 
260  public function createItem(ilMMItemFacadeInterface $item_facade) : void
261  {
262  $item_facade->create();
263  $this->clearCache();
264  }
265 
269  public function deleteItem(ilMMItemFacadeInterface $item_facade) : void
270  {
271  if ($item_facade->isDeletable()) {
272  $item_facade->delete();
273  $this->clearCache();
274  }
275  }
276 }
resolveIdentificationFromString(string $identification_string)
getItemFacadeForIdentificationString(string $identification)
$data
Definition: storeScorm.php:23
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)
__construct()
ilMMItemRepository constructor.
getSingleItemFromFilter(IdentificationInterface $identification)
Class ilMMCustomItemFacade.
$DIC
Definition: xapitoken.php:46
createItem(ilMMItemFacadeInterface $item_facade)
getItemFacade(IdentificationInterface $identification=null)
Interface ilMMItemFacadeInterface.
getSingleItem(IdentificationInterface $identification)