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