ILIAS  release_8 Revision v8.24
class.ilBadgeImageTemplate.php
Go to the documentation of this file.
1<?php
2
25{
26 protected ilDBInterface $db;
27 protected int $id = 0;
28 protected string $title = "";
29 protected string $image = "";
31 protected ?array $types = null;
32
33 public function __construct(int $a_id = null)
34 {
35 global $DIC;
36
37 $this->db = $DIC->database();
38 if ($a_id) {
39 $this->read($a_id);
40 }
41 }
42
46 public static function getInstances(): array
47 {
48 global $DIC;
49
50 $ilDB = $DIC->database();
51
52 $res = array();
53
54 $types = array();
55 $set = $ilDB->query("SELECT * FROM badge_image_templ_type");
56 while ($row = $ilDB->fetchAssoc($set)) {
57 $types[$row["tmpl_id"]][] = $row["type_id"];
58 }
59
60 $set = $ilDB->query("SELECT * FROM badge_image_template" .
61 " ORDER BY title");
62 while ($row = $ilDB->fetchAssoc($set)) {
63 $row["types"] = (array) ($types[$row["id"]] ?? null);
64
65 $obj = new self();
66 $obj->importDBRow($row);
67 $res[] = $obj;
68 }
69
70 return $res;
71 }
72
77 public static function getInstancesByType(string $a_type_unique_id): array
78 {
79 $res = [];
80
81 foreach (self::getInstances() as $tmpl) {
82 if (!count($tmpl->getTypes()) || in_array($a_type_unique_id, $tmpl->getTypes(), true)) {
83 $res[] = $tmpl;
84 }
85 }
86
87 return $res;
88 }
89
90
91 //
92 // setter/getter
93 //
94
95 protected function setId(int $a_id): void
96 {
97 $this->id = $a_id;
98 }
99
100 public function getId(): int
101 {
102 return $this->id;
103 }
104
105 public function setTitle(string $a_value): void
106 {
107 $this->title = trim($a_value);
108 }
109
110 public function getTitle(): string
111 {
112 return $this->title;
113 }
114
115 protected function setImage(string $a_value): void
116 {
117 $this->image = trim($a_value);
118 }
119
123 public function getTypes(): ?array
124 {
125 return $this->types;
126 }
127
128 public function setTypes(array $types = null): void
129 {
130 $this->types = is_array($types)
131 ? array_unique($types)
132 : null;
133 }
134
135 public function getImage(): string
136 {
137 return $this->image;
138 }
139
144 public function uploadImage(array $a_upload_meta): void
145 {
146 if ($this->getId() &&
147 $a_upload_meta["tmp_name"]) {
148 $path = $this->getFilePath($this->getId());
149
150
151 $filename = ilFileUtils::getValidFilename($a_upload_meta["name"]);
152
153 $exp = explode(".", $filename);
154 $suffix = strtolower(array_pop($exp));
155 $tgt = $path . "img" . $this->getId() . "." . $suffix;
156
157 if (ilFileUtils::moveUploadedFile($a_upload_meta["tmp_name"], "img" . $this->getId() . "." . $suffix, $tgt)) {
158 $this->setImage($filename);
159 $this->update();
160 }
161 }
162 }
163
164 public function getImagePath(): string
165 {
166 if ($this->getId()) {
167 if (is_file($this->getFilePath($this->getId()) . "img" . $this->getId())) { // formerly (early 5.2 versino), images have been uploaded with no suffix
168 return $this->getFilePath($this->getId()) . "img" . $this->getId();
169 }
170
171 $exp = explode(".", $this->getImage());
172 $suffix = strtolower(array_pop($exp));
173 return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
174 }
175 return "";
176 }
177
181 protected function getFilePath(
182 int $a_id,
183 string $a_subdir = null
184 ): string {
185 $storage = new ilFSStorageBadgeImageTemplate($a_id);
186 $storage->create();
187
188 $path = $storage->getAbsolutePath() . "/";
189
190 if ($a_subdir) {
191 $path .= $a_subdir . "/";
192
193 if (!is_dir($path)) {
194 mkdir($path);
195 }
196 }
197
198 return $path;
199 }
200
201
202 //
203 // crud
204 //
205
206 protected function read(int $a_id): void
207 {
208 $ilDB = $this->db;
209
210 $set = $ilDB->query("SELECT * FROM badge_image_template" .
211 " WHERE id = " . $ilDB->quote($a_id, "integer"));
212 if ($ilDB->numRows($set)) {
213 $row = $ilDB->fetchAssoc($set);
214 $row["types"] = $this->readTypes($a_id);
215 $this->importDBRow($row);
216 }
217 }
218
219 protected function readTypes(int $a_id): ?array
220 {
221 $ilDB = $this->db;
222
223 $res = array();
224
225 $set = $ilDB->query("SELECT * FROM badge_image_templ_type WHERE tmpl_id = " . $ilDB->quote($a_id, "integer"));
226 while ($row = $ilDB->fetchAssoc($set)) {
227 $res[] = $row["type_id"];
228 }
229
230 if (!count($res)) {
231 $res = null;
232 }
233
234 return $res;
235 }
236
237 protected function importDBRow(array $a_row): void
238 {
239 $this->setId($a_row["id"]);
240 $this->setTitle($a_row["title"]);
241 $this->setImage($a_row["image"]);
242 $this->setTypes($a_row["types"]);
243 }
244
245 public function create(): void
246 {
247 $ilDB = $this->db;
248
249 if ($this->getId()) {
250 $this->update();
251 return;
252 }
253
254 $id = $ilDB->nextId("badge_image_template");
255 $this->setId($id);
256
257 $fields = $this->getPropertiesForStorage();
258 $fields["id"] = array("integer", $id);
259
260 $ilDB->insert("badge_image_template", $fields);
261
262 $this->saveTypes();
263 }
264
265 public function update(): void
266 {
267 $ilDB = $this->db;
268
269 if (!$this->getId()) {
270 $this->create();
271 return;
272 }
273
274 $fields = $this->getPropertiesForStorage();
275
276 $ilDB->update(
277 "badge_image_template",
278 $fields,
279 array("id" => array("integer", $this->getId()))
280 );
281
282 $this->saveTypes();
283 }
284
285 public function delete(): void
286 {
287 $ilDB = $this->db;
288
289 if (!$this->getId()) {
290 return;
291 }
292
293 $path = $this->getFilePath($this->getId());
295
296 $ilDB->manipulate("DELETE FROM badge_image_template" .
297 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
298 }
299
303 protected function getPropertiesForStorage(): array
304 {
305 return [
306 "title" => ["text", $this->getTitle()],
307 "image" => ["text", $this->getImage()]
308 ];
309 }
310
311 protected function saveTypes(): void
312 {
313 $ilDB = $this->db;
314
315 if ($this->getId()) {
316 $ilDB->manipulate("DELETE FROM badge_image_templ_type" .
317 " WHERE tmpl_id = " . $ilDB->quote($this->getId(), "integer"));
318
319 if ($this->getTypes()) {
320 foreach ($this->getTypes() as $type) {
321 $fields = array(
322 "tmpl_id" => array("integer", $this->getId()),
323 "type_id" => array("text", $type)
324 );
325 $ilDB->insert("badge_image_templ_type", $fields);
326 }
327 }
328 }
329 }
330}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$filename
Definition: buildRTE.php:78
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getFilePath(int $a_id, string $a_subdir=null)
Init file system storage.
uploadImage(array $a_upload_meta)
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
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
$path
Definition: ltiservices.php:32
$res
Definition: ltiservices.php:69
$type