ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilBadgeImageTemplate.php
Go to the documentation of this file.
1<?php
2
23
25{
26 protected ilDBInterface $db;
27 protected int $id = 0;
28 protected string $title = '';
29 protected ?string $image_rid = null;
30
32 protected ?array $types = null;
36 protected ilLogger $log;
37
38 public function __construct(?int $a_id = null)
39 {
40 global $DIC;
41
42 $this->resource_storage = $DIC->resourceStorage();
43 $this->upload_service = $DIC->upload();
44 $this->main_template = $DIC->ui()->mainTemplate();
45 $this->log = $DIC->logger()->forComponent('badge');
46 $this->db = $DIC->database();
47 if ($a_id) {
48 $this->read($a_id);
49 }
50 }
51
55 public static function getInstances(): array
56 {
57 global $DIC;
58
59 $ilDB = $DIC->database();
60
61 $res = [];
62
63 $types = [];
64 $set = $ilDB->query('SELECT * FROM badge_image_templ_type');
65 while ($row = $ilDB->fetchAssoc($set)) {
66 $types[$row['tmpl_id']][] = $row['type_id'];
67 }
68
69 $set = $ilDB->query('SELECT * FROM badge_image_template' .
70 ' ORDER BY title');
71 while ($row = $ilDB->fetchAssoc($set)) {
72 $row['types'] = (array) ($types[$row['id']] ?? null);
73
74 $obj = new self();
75 $obj->importDBRow($row);
76 $res[] = $obj;
77 }
78
79 return $res;
80 }
81
85 public static function getInstancesByType(string $a_type_unique_id): array
86 {
87 $res = [];
88
89 foreach (self::getInstances() as $tmpl) {
90 if (!count($tmpl->getTypes()) || in_array($a_type_unique_id, $tmpl->getTypes(), true)) {
91 $res[] = $tmpl;
92 }
93 }
94
95 return $res;
96 }
97
98 //
99 // setter/getter
100 //
101
102 protected function setId(int $a_id): void
103 {
104 $this->id = $a_id;
105 }
106
107 public function getId(): int
108 {
109 return $this->id;
110 }
111
112 public function setTitle(string $a_value): void
113 {
114 $this->title = trim($a_value);
115 }
116
117 public function getTitle(): string
118 {
119 return $this->title;
120 }
121
125 public function getTypes(): ?array
126 {
127 return $this->types;
128 }
129
130 public function setTypes(?array $types = null): void
131 {
132 $this->types = is_array($types)
133 ? array_unique($types)
134 : null;
135 }
136
137 public function processImageUpload(ilBadgeImageTemplate $badge): void
138 {
139 try {
140 if (!$this->upload_service->hasBeenProcessed()) {
141 $this->upload_service->process();
142 }
143 if ($this->upload_service->hasUploads()) {
144 $array_result = $this->upload_service->getResults();
145 $array_result = array_pop($array_result);
146 if ($array_result->getName() !== '') {
147 if ($badge->getImageRid()) {
148 $this->resource_storage->manage()->remove(new ResourceIdentification($badge->getImageRid()), new ilBadgeFileStakeholder());
149 }
150 $stakeholder = new ilBadgeFileStakeholder();
151 $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
152 $badge->setImageRid($identification);
153 $badge->update();
154 }
155 }
156 } catch (IllegalStateException $e) {
157 $this->main_template->setOnScreenMessage('failure', $e->getMessage(), true);
158 }
159 }
160
161 //
162 // crud
163 //
164
165 protected function read(int $a_id): void
166 {
168
169 $set = $ilDB->query('SELECT * FROM badge_image_template' .
170 ' WHERE id = ' . $ilDB->quote($a_id, 'integer'));
171 if ($ilDB->numRows($set)) {
172 $row = $ilDB->fetchAssoc($set);
173 $row['types'] = $this->readTypes($a_id);
174 $this->importDBRow($row);
175 }
176 }
177
178 protected function readTypes(int $a_id): ?array
179 {
181
182 $res = [];
183
184 $set = $ilDB->query('SELECT * FROM badge_image_templ_type WHERE tmpl_id = ' . $ilDB->quote($a_id, 'integer'));
185 while ($row = $ilDB->fetchAssoc($set)) {
186 $res[] = $row['type_id'];
187 }
188
189 if (!count($res)) {
190 $res = null;
191 }
192
193 return $res;
194 }
195
196 protected function importDBRow(array $a_row): void
197 {
198 $this->setId($a_row['id']);
199 $this->setTitle($a_row['title']);
200 if (isset($a_row['image_rid'])) {
201 $this->setImageRid($a_row['image_rid']);
202 }
203 $this->setTypes($a_row['types']);
204 }
205
206 public function create(): void
207 {
209
210 if ($this->getId()) {
211 $this->update();
212
213 return;
214 }
215
216 $id = $ilDB->nextId('badge_image_template');
217 $this->setId($id);
218
219 $fields = $this->getPropertiesForStorage();
220 $fields['id'] = ['integer', $id];
221
222 $ilDB->insert('badge_image_template', $fields);
223
224 $this->saveTypes();
225 }
226
227 public function update(): void
228 {
230
231 if (!$this->getId()) {
232 $this->create();
233
234 return;
235 }
236
237 $fields = $this->getPropertiesForStorage();
238
239 $ilDB->update(
240 'badge_image_template',
241 $fields,
242 ['id' => ['integer', $this->getId()]]
243 );
244
245 $this->saveTypes();
246 }
247
248 public function delete(): void
249 {
251
252 if (!$this->getId()) {
253 return;
254 }
255
256 $image_rid = $this->getImageRid();
257 if ($image_rid !== null) {
258 try {
259 $this->resource_storage->manage()->remove(
262 );
263 } catch (Exception $e) {
264 $this->log->warning(sprintf(
265 'There was an exception, while deleting the badge image template with id %s. Exception: %s',
266 $this->getId(),
267 $e->getMessage()
268 ));
269 }
270 }
271
272 $ilDB->manipulate('DELETE FROM badge_image_template' .
273 ' WHERE id = ' . $ilDB->quote($this->getId(), 'integer'));
274 }
275
279 protected function getPropertiesForStorage(): array
280 {
281 return [
282 'title' => ['text', $this->getTitle()],
283 'image_rid' => ['text', $this->getImageRid()]
284 ];
285 }
286
287 protected function saveTypes(): void
288 {
290
291 if ($this->getId()) {
292 $ilDB->manipulate('DELETE FROM badge_image_templ_type' .
293 ' WHERE tmpl_id = ' . $ilDB->quote($this->getId(), 'integer'));
294
295 if ($this->getTypes()) {
296 foreach ($this->getTypes() as $type) {
297 $fields = [
298 'tmpl_id' => ['integer', $this->getId()],
299 'type_id' => ['text', $type]
300 ];
301 $ilDB->insert('badge_image_templ_type', $fields);
302 }
303 }
304 }
305 }
306
307 public function getImageRid(): ?string
308 {
309 // '-' was written by the removed filesystem migration for templates without an image.
310 if ($this->image_rid === null || $this->image_rid === '' || $this->image_rid === '-') {
311 return null;
312 }
313
314 return $this->image_rid;
315 }
316
317 public function setImageRid(?string $image_rid = null): void
318 {
319 $this->image_rid = $image_rid;
320 }
321
322 public function getImageFromResourceId(): string
323 {
324 $image_rid = $this->getImageRid();
325 if ($image_rid === null) {
326 return '';
327 }
328
329 $identification = $this->resource_storage->manage()->find($image_rid);
330 if ($identification === null) {
331 return '';
332 }
333
334 return $this->resource_storage->consume()->src($identification)->getSrc();
335 }
336}
processImageUpload(ilBadgeImageTemplate $badge)
ilGlobalTemplateInterface $main_template
setImageRid(?string $image_rid=null)
static getInstancesByType(string $a_type_unique_id)
Component logger with individual log levels by component id.
Interface ilDBInterface.
foreach($_POST as $key=> $value) $res
global $DIC
Definition: shib_login.php:26