ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
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 
76  public static function getInstancesByType(string $a_type_unique_id): array
77  {
78  $res = [];
79 
80  foreach (self::getInstances() as $tmpl) {
81  if (!count($tmpl->getTypes()) || in_array($a_type_unique_id, $tmpl->getTypes(), true)) {
82  $res[] = $tmpl;
83  }
84  }
85 
86  return $res;
87  }
88 
89 
90  //
91  // setter/getter
92  //
93 
94  protected function setId(int $a_id): void
95  {
96  $this->id = $a_id;
97  }
98 
99  public function getId(): int
100  {
101  return $this->id;
102  }
103 
104  public function setTitle(string $a_value): void
105  {
106  $this->title = trim($a_value);
107  }
108 
109  public function getTitle(): string
110  {
111  return $this->title;
112  }
113 
114  protected function setImage(string $a_value): void
115  {
116  $this->image = trim($a_value);
117  }
118 
122  public function getTypes(): ?array
123  {
124  return $this->types;
125  }
126 
127  public function setTypes(array $types = null): void
128  {
129  $this->types = is_array($types)
130  ? array_unique($types)
131  : null;
132  }
133 
134  public function getImage(): string
135  {
136  return $this->image;
137  }
138 
143  public function uploadImage(array $a_upload_meta): void
144  {
145  if ($this->getId() &&
146  $a_upload_meta["tmp_name"]) {
147  $path = $this->getFilePath($this->getId());
148 
149 
150  $filename = ilFileUtils::getValidFilename($a_upload_meta["name"]);
151 
152  $exp = explode(".", $filename);
153  $suffix = strtolower(array_pop($exp));
154  $tgt = $path . "img" . $this->getId() . "." . $suffix;
155 
156  if (ilFileUtils::moveUploadedFile($a_upload_meta["tmp_name"], "img" . $this->getId() . "." . $suffix, $tgt)) {
157  $this->setImage($filename);
158  $this->update();
159  }
160  }
161  }
162 
163  public function getImagePath(): string
164  {
165  if ($this->getId()) {
166  if (is_file($this->getFilePath($this->getId()) . "img" . $this->getId())) { // formerly (early 5.2 versino), images have been uploaded with no suffix
167  return $this->getFilePath($this->getId()) . "img" . $this->getId();
168  }
169 
170  $exp = explode(".", $this->getImage());
171  $suffix = strtolower(array_pop($exp));
172  return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
173  }
174  return "";
175  }
176 
180  protected function getFilePath(
181  int $a_id,
182  string $a_subdir = null
183  ): string {
184  $storage = new ilFSStorageBadgeImageTemplate($a_id);
185  $storage->create();
186 
187  $path = $storage->getAbsolutePath() . "/";
188 
189  if ($a_subdir) {
190  $path .= $a_subdir . "/";
191 
192  if (!is_dir($path)) {
193  mkdir($path);
194  }
195  }
196 
197  return $path;
198  }
199 
200 
201  //
202  // crud
203  //
204 
205  protected function read(int $a_id): void
206  {
207  $ilDB = $this->db;
208 
209  $set = $ilDB->query("SELECT * FROM badge_image_template" .
210  " WHERE id = " . $ilDB->quote($a_id, "integer"));
211  if ($ilDB->numRows($set)) {
212  $row = $ilDB->fetchAssoc($set);
213  $row["types"] = $this->readTypes($a_id);
214  $this->importDBRow($row);
215  }
216  }
217 
218  protected function readTypes(int $a_id): ?array
219  {
220  $ilDB = $this->db;
221 
222  $res = array();
223 
224  $set = $ilDB->query("SELECT * FROM badge_image_templ_type WHERE tmpl_id = " . $ilDB->quote($a_id, "integer"));
225  while ($row = $ilDB->fetchAssoc($set)) {
226  $res[] = $row["type_id"];
227  }
228 
229  if (!count($res)) {
230  $res = null;
231  }
232 
233  return $res;
234  }
235 
236  protected function importDBRow(array $a_row): void
237  {
238  $this->setId($a_row["id"]);
239  $this->setTitle($a_row["title"]);
240  $this->setImage($a_row["image"]);
241  $this->setTypes($a_row["types"]);
242  }
243 
244  public function create(): void
245  {
246  $ilDB = $this->db;
247 
248  if ($this->getId()) {
249  $this->update();
250  return;
251  }
252 
253  $id = $ilDB->nextId("badge_image_template");
254  $this->setId($id);
255 
256  $fields = $this->getPropertiesForStorage();
257  $fields["id"] = array("integer", $id);
258 
259  $ilDB->insert("badge_image_template", $fields);
260 
261  $this->saveTypes();
262  }
263 
264  public function update(): void
265  {
266  $ilDB = $this->db;
267 
268  if (!$this->getId()) {
269  $this->create();
270  return;
271  }
272 
273  $fields = $this->getPropertiesForStorage();
274 
275  $ilDB->update(
276  "badge_image_template",
277  $fields,
278  array("id" => array("integer", $this->getId()))
279  );
280 
281  $this->saveTypes();
282  }
283 
284  public function delete(): void
285  {
286  $ilDB = $this->db;
287 
288  if (!$this->getId()) {
289  return;
290  }
291 
292  $path = $this->getFilePath($this->getId());
294 
295  $ilDB->manipulate("DELETE FROM badge_image_template" .
296  " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
297  }
298 
302  protected function getPropertiesForStorage(): array
303  {
304  return [
305  "title" => ["text", $this->getTitle()],
306  "image" => ["text", $this->getImage()]
307  ];
308  }
309 
310  protected function saveTypes(): void
311  {
312  $ilDB = $this->db;
313 
314  if ($this->getId()) {
315  $ilDB->manipulate("DELETE FROM badge_image_templ_type" .
316  " WHERE tmpl_id = " . $ilDB->quote($this->getId(), "integer"));
317 
318  if ($this->getTypes()) {
319  foreach ($this->getTypes() as $type) {
320  $fields = array(
321  "tmpl_id" => array("integer", $this->getId()),
322  "type_id" => array("text", $type)
323  );
324  $ilDB->insert("badge_image_templ_type", $fields);
325  }
326  }
327  }
328  }
329 }
uploadImage(array $a_upload_meta)
$res
Definition: ltiservices.php:69
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...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getValidFilename(string $a_filename)
$path
Definition: ltiservices.php:32
getFilePath(int $a_id, string $a_subdir=null)
Init file system storage.
global $DIC
Definition: feed.php:28
static delDir(string $a_dir, bool $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static moveUploadedFile(string $a_file, string $a_name, string $a_target, bool $a_raise_errors=true, string $a_mode="move_uploaded")
move uploaded file
$filename
Definition: buildRTE.php:78