ILIAS  trunk Revision v12.0_alpha-1221-g4e438232683
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 = "";
30 protected ?string $image_rid = "";
32 protected ?array $types = null;
36
37 public function __construct(?int $a_id = null)
38 {
39 global $DIC;
40
41 $this->resource_storage = $DIC->resourceStorage();
42 $this->upload_service = $DIC->upload();
43 $this->main_template = $DIC->ui()->mainTemplate();
44 $this->db = $DIC->database();
45 if ($a_id) {
46 $this->read($a_id);
47 }
48 }
49
53 public static function getInstances(): array
54 {
55 global $DIC;
56
57 $ilDB = $DIC->database();
58
59 $res = array();
60
61 $types = array();
62 $set = $ilDB->query("SELECT * FROM badge_image_templ_type");
63 while ($row = $ilDB->fetchAssoc($set)) {
64 $types[$row["tmpl_id"]][] = $row["type_id"];
65 }
66
67 $set = $ilDB->query("SELECT * FROM badge_image_template" .
68 " ORDER BY title");
69 while ($row = $ilDB->fetchAssoc($set)) {
70 $row["types"] = (array) ($types[$row["id"]] ?? null);
71
72 $obj = new self();
73 $obj->importDBRow($row);
74 $res[] = $obj;
75 }
76
77 return $res;
78 }
79
83 public static function getInstancesByType(string $a_type_unique_id): array
84 {
85 $res = [];
86
87 foreach (self::getInstances() as $tmpl) {
88 if (!count($tmpl->getTypes()) || in_array($a_type_unique_id, $tmpl->getTypes(), true)) {
89 $res[] = $tmpl;
90 }
91 }
92
93 return $res;
94 }
95
96
97 //
98 // setter/getter
99 //
100
101 protected function setId(int $a_id): void
102 {
103 $this->id = $a_id;
104 }
105
106 public function getId(): int
107 {
108 return $this->id;
109 }
110
111 public function setTitle(string $a_value): void
112 {
113 $this->title = trim($a_value);
114 }
115
116 public function getTitle(): string
117 {
118 return $this->title;
119 }
120
121 protected function setImage(?string $a_value): void
122 {
123 if ($a_value !== null) {
124 $this->image = trim($a_value);
125 }
126 }
127
131 public function getTypes(): ?array
132 {
133 return $this->types;
134 }
135
136 public function setTypes(?array $types = null): void
137 {
138 $this->types = is_array($types)
139 ? array_unique($types)
140 : null;
141 }
142
143 public function getImage(): string
144 {
145 return $this->image;
146 }
147
152 public function uploadImage(array $a_upload_meta): void
153 {
154 if ($this->getId() &&
155 $a_upload_meta["tmp_name"]) {
156 $path = $this->getFilePath($this->getId());
157
158 $filename = ilFileUtils::getValidFilename($a_upload_meta["name"]);
159
160 $exp = explode(".", $filename);
161 $suffix = strtolower(array_pop($exp));
162 $tgt = $path . "img" . $this->getId() . "." . $suffix;
163
164 if (ilFileUtils::moveUploadedFile($a_upload_meta["tmp_name"], "img" . $this->getId() . "." . $suffix, $tgt)) {
165 $this->setImage($filename);
166 $this->update();
167 }
168 }
169 }
170
171 public function processImageUpload(ilBadgeImageTemplate $badge): void
172 {
173 try {
174 if (!$this->upload_service->hasBeenProcessed()) {
175 $this->upload_service->process();
176 }
177 if ($this->upload_service->hasUploads()) {
178 $array_result = $this->upload_service->getResults();
179 $array_result = array_pop($array_result);
180 if ($array_result->getName() !== '') {
181 if ($badge->getImageRid()) {
182 $this->resource_storage->manage()->remove(new ResourceIdentification($badge->getImageRid()), new ilBadgeFileStakeholder());
183 }
184 $stakeholder = new ilBadgeFileStakeholder();
185 $identification = $this->resource_storage->manage()->upload($array_result, $stakeholder);
186 $badge->setImageRid($identification);
187 $badge->update();
188 }
189 }
190 } catch (IllegalStateException $e) {
191 $this->main_template->setOnScreenMessage('failure', $e->getMessage(), true);
192 }
193 }
194
195 public function getImagePath(): string
196 {
197 if ($this->getId()) {
198 if (is_file($this->getFilePath($this->getId()) . "img" . $this->getId())) { // formerly (early 5.2 versino), images have been uploaded with no suffix
199 return $this->getFilePath($this->getId()) . "img" . $this->getId();
200 }
201
202 $exp = explode(".", $this->getImage());
203 $suffix = strtolower(array_pop($exp));
204 return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
205 }
206 return "";
207 }
208
212 protected function getFilePath(
213 int $a_id,
214 ?string $a_subdir = null
215 ): string {
216 $storage = new ilFSStorageBadgeImageTemplate($a_id);
217 $storage->create();
218
219 $path = $storage->getAbsolutePath() . "/";
220
221 if ($a_subdir) {
222 $path .= $a_subdir . "/";
223
224 if (!is_dir($path)) {
225 mkdir($path);
226 }
227 }
228
229 return $path;
230 }
231
232
233 //
234 // crud
235 //
236
237 protected function read(int $a_id): void
238 {
239 $ilDB = $this->db;
240
241 $set = $ilDB->query("SELECT * FROM badge_image_template" .
242 " WHERE id = " . $ilDB->quote($a_id, "integer"));
243 if ($ilDB->numRows($set)) {
244 $row = $ilDB->fetchAssoc($set);
245 $row["types"] = $this->readTypes($a_id);
246 $this->importDBRow($row);
247 }
248 }
249
250 protected function readTypes(int $a_id): ?array
251 {
252 $ilDB = $this->db;
253
254 $res = array();
255
256 $set = $ilDB->query("SELECT * FROM badge_image_templ_type WHERE tmpl_id = " . $ilDB->quote($a_id, "integer"));
257 while ($row = $ilDB->fetchAssoc($set)) {
258 $res[] = $row["type_id"];
259 }
260
261 if (!count($res)) {
262 $res = null;
263 }
264
265 return $res;
266 }
267
268 protected function importDBRow(array $a_row): void
269 {
270 $this->setId($a_row["id"]);
271 $this->setTitle($a_row["title"]);
272 if (isset($a_row["image"])) {
273 $this->setImage($a_row["image"]);
274 }
275 if (isset($a_row["image_rid"])) {
276 $this->setImageRid($a_row["image_rid"]);
277 }
278 $this->setTypes($a_row["types"]);
279 }
280
281 public function create(): void
282 {
283 $ilDB = $this->db;
284
285 if ($this->getId()) {
286 $this->update();
287 return;
288 }
289
290 $id = $ilDB->nextId("badge_image_template");
291 $this->setId($id);
292
293 $fields = $this->getPropertiesForStorage();
294 $fields["id"] = array("integer", $id);
295
296 $ilDB->insert("badge_image_template", $fields);
297
298 $this->saveTypes();
299 }
300
301 public function update(): void
302 {
303 $ilDB = $this->db;
304
305 if (!$this->getId()) {
306 $this->create();
307 return;
308 }
309
310 $fields = $this->getPropertiesForStorage();
311
312 $ilDB->update(
313 "badge_image_template",
314 $fields,
315 array("id" => array("integer", $this->getId()))
316 );
317
318 $this->saveTypes();
319 }
320
321 public function delete(): void
322 {
323 $ilDB = $this->db;
324
325 if (!$this->getId()) {
326 return;
327 }
328
329 $path = $this->getFilePath($this->getId());
331
332 $ilDB->manipulate("DELETE FROM badge_image_template" .
333 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
334 }
335
339 protected function getPropertiesForStorage(): array
340 {
341 return [
342 "title" => ["text", $this->getTitle()],
343 "image" => ["text", $this->getImage()],
344 "image_rid" => ["text", $this->getImageRid()]
345 ];
346 }
347
348 protected function saveTypes(): void
349 {
350 $ilDB = $this->db;
351
352 if ($this->getId()) {
353 $ilDB->manipulate("DELETE FROM badge_image_templ_type" .
354 " WHERE tmpl_id = " . $ilDB->quote($this->getId(), "integer"));
355
356 if ($this->getTypes()) {
357 foreach ($this->getTypes() as $type) {
358 $fields = array(
359 "tmpl_id" => array("integer", $this->getId()),
360 "type_id" => array("text", $type)
361 );
362 $ilDB->insert("badge_image_templ_type", $fields);
363 }
364 }
365 }
366 }
367
368 public function getImageRid(): ?string
369 {
370 return $this->image_rid;
371 }
372
373 public function setImageRid(?string $image_rid = null): void
374 {
375 $this->image_rid = $image_rid;
376 }
377
378 public function getImageFromResourceId(
379 ): string {
380 $image_src = '';
381
382 if ($this->getImageRid()) {
383 $identification = $this->resource_storage->manage()->find($this->getImageRid());
384 if ($identification !== null) {
385 $image_src = $this->resource_storage->consume()->src($identification)->getSrc();
386 }
387 } elseif ($this->getImage()) {
388 $image_src = ilWACSignedPath::signFile($this->getImagePath());
389 }
390
391 return $image_src;
392 }
393}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$filename
Definition: buildRTE.php:78
processImageUpload(ilBadgeImageTemplate $badge)
uploadImage(array $a_upload_meta)
getFilePath(int $a_id, ?string $a_subdir=null)
Init file system storage.
ilGlobalTemplateInterface $main_template
setImageRid(?string $image_rid=null)
static getInstancesByType(string $a_type_unique_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static getValidFilename(string $a_filename)
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
static signFile(string $path_to_file)
Interface ilDBInterface.
$path
Definition: ltiservices.php:30
$res
Definition: ltiservices.php:69
global $DIC
Definition: shib_login.php:26