ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
EntriesRepositoryDB.php
Go to the documentation of this file.
1<?php
2
20
22use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
27
29{
30 use Hasher;
31
32 public const TABLE_NAME = 'gs_footer_items';
33
37 protected array $cache = [];
38
39 public function __construct(
40 private \ilDBInterface $db,
42 ) {
43 }
44
45 public function syncWithGlobalScreen(
46 FooterMainCollector $collector
47 ): void {
48 $collector->collectOnce();
49 $this->preload();
50
51 foreach ($collector->getRawUnfilteredItems() as $item) {
52 if (!$item instanceof canHaveParent) {
53 continue;
54 }
55 if ($this->has($item->getProviderIdentification()->serialize())) {
56 continue;
57 }
60 $new = new EntryDTO(
61 $item->getProviderIdentification()->serialize(),
62 $item->getTitle(),
63 true,
64 $item->getPosition(),
65 $item->getParentIdentification()->serialize(),
66 $item instanceof hasAction ? (string) $item->getAction() : '',
67 $item instanceof hasAction ? $item->mustOpenInNewViewport() : false,
68 true
69 );
70 $this->store($new);
71 }
72 }
73
74 public function preload(): void
75 {
76 foreach (
77 $this->db->fetchAll(
78 $this->db->query('SELECT * FROM ' . self::TABLE_NAME . ' WHERE type != 1 ORDER BY position ASC')
79 ) as $row
80 ) {
81 $entry = $this->fromDB($row);
82 $this->cache[$entry->getId()] = $entry;
83 }
84 }
85
86 private function fromDB(array $row): Entry
87 {
88 return new EntryDTO(
89 $row['id'],
90 $row['title'],
91 $row['is_active'] === 1,
92 (int) $row['position'],
93 (string) ($row['parent'] ?? ''),
94 (string) ($row['action'] ?? ''),
95 (bool) $row['external'],
96 (bool) $row['core']
97 );
98 }
99
100 public function get(string $identifier): ?Entry
101 {
102 if (isset($this->cache[$identifier]) && $this->has($identifier)) {
103 return $this->cache[$identifier];
104 }
105
106 $row = $this->db->queryF(
107 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE id = %s AND type != 1',
108 ['text'],
109 [$identifier]
110 )->fetchAssoc();
111 if ($row === null) {
112 return null;
113 }
114 return $this->fromDB($row);
115 }
116
117 public function has(string $identifier): bool
118 {
119 return $this->db->queryF(
120 'SELECT id FROM ' . self::TABLE_NAME . ' WHERE id = %s AND type != 1',
121 ['text'],
122 [$identifier]
123 )->numRows() > 0;
124 }
125
126 public function blank(): Entry
127 {
128 return new EntryDTO('', '', true, 0, '', false);
129 }
130
131 public function store(Entry $entry): Entry
132 {
133 if ($entry->getId() === '' || !$this->has($entry->getId())) {
134 return $this->create($entry);
135 }
136
137 return $this->update($entry);
138 }
139
140 private function create(Entry $entry): Entry
141 {
142 if ($this->provider === null) {
143 throw new \LogicException('No provider set');
144 }
145
146 if ($entry->getId() === '') {
147 $entry = $entry->withId($this->provider->getNewIdentification()->serialize());
148 }
149 $this->db->insert(
150 self::TABLE_NAME,
151 [
152 'id' => ['text', $entry->getId()],
153 'type' => ['integer', 2],
154 'title' => ['text', $entry->getTitle()],
155 'position' => ['integer', $entry->getPosition()],
156 'is_active' => ['integer', $entry->isActive() ? 1 : 0],
157 'parent' => ['text', $entry->getParent()],
158 'action' => ['text', $entry->getAction()],
159 'external' => ['integer', $entry->isExternal() ? 1 : 0],
160 'core' => ['integer', $entry->isCore() ? 1 : 0],
161 ]
162 );
163 return $entry;
164 }
165
166 private function update(Entry $entry): Entry
167 {
168 $this->db->update(
169 self::TABLE_NAME,
170 [
171 'title' => ['text', $entry->getTitle()],
172 'position' => ['integer', $entry->getPosition()],
173 'is_active' => ['integer', $entry->isActive() ? 1 : 0],
174 'parent' => ['text', $entry->getParent()],
175 'action' => ['text', $entry->getAction()],
176 'external' => ['integer', $entry->isExternal() ? 1 : 0],
177 'core' => ['integer', $entry->isCore() ? 1 : 0],
178 ],
179 ['id' => ['text', $entry->getId()]]
180 );
181 return $entry;
182 }
183
184 public function delete(Entry $entry): void
185 {
186 if ($entry->isCore()) {
187 return;
188 }
189
190 $this->db->manipulateF(
191 'DELETE FROM ' . self::TABLE_NAME . ' WHERE id = %s',
192 ['text'],
193 [$entry->getId()]
194 );
195 }
196
200 public function all(): \Generator
201 {
202 $this->preload();
203 yield from $this->cache;
204 }
205
206 public function allForParent(string $parent_identifier): \Generator
207 {
208 $this->preload();
209 foreach ($this->cache as $entry) {
210 if ($entry->getParent() === $parent_identifier) {
211 yield $entry;
212 }
213 }
214 }
215
216 public function updatePositionById(string $id, int $position): void
217 {
218 $this->db->update(
219 self::TABLE_NAME,
220 ['position' => ['integer', $position]],
221 ['id' => ['text', $id]]
222 );
223 }
224
225 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
226 {
227 return null;
228 }
229
230 public function reset(FooterMainCollector $collector): void
231 {
232 $this->db->manipulate('DELETE FROM ' . self::TABLE_NAME . ' WHERE type != 1');
233 $this->syncWithGlobalScreen($collector);
234 }
235
236}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
__construct(private \ilDBInterface $db, private ?\ilFooterCustomGroupsProvider $provider=null)
syncWithGlobalScreen(FooterMainCollector $collector)
Interface ilDBInterface.
$provider
Definition: ltitoken.php:80
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
has(string $class_name)