ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilBadgeImageTemplate.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
11{
15 protected $db;
16
17 protected $id; // [int]
18 protected $title; // [string]
19 protected $image; // [string]
20 protected $types; // [array]
21
28 public function __construct($a_id = null)
29 {
30 global $DIC;
31
32 $this->db = $DIC->database();
33 if ($a_id) {
34 $this->read($a_id);
35 }
36 }
37
38 public static function getInstances()
39 {
40 global $DIC;
41
42 $ilDB = $DIC->database();
43
44 $res = array();
45
46 $types = array();
47 $set = $ilDB->query("SELECT * FROM badge_image_templ_type");
48 while ($row = $ilDB->fetchAssoc($set)) {
49 $types[$row["tmpl_id"]][] = $row["type_id"];
50 }
51
52 $set = $ilDB->query("SELECT * FROM badge_image_template" .
53 " ORDER BY title");
54 while ($row = $ilDB->fetchAssoc($set)) {
55 $row["types"] = (array) $types[$row["id"]];
56
57 $obj = new self();
58 $obj->importDBRow($row);
59 $res[] = $obj;
60 }
61
62 return $res;
63 }
64
65 public static function getInstancesByType($a_type_unique_id)
66 {
67 $res = array();
68
69 foreach (self::getInstances() as $tmpl) {
70 if (!sizeof($tmpl->getTypes()) ||
71 in_array($a_type_unique_id, $tmpl->getTypes())) {
72 $res[] = $tmpl;
73 }
74 }
75
76 return $res;
77 }
78
79
80 //
81 // setter/getter
82 //
83
84 protected function setId($a_id)
85 {
86 $this->id = (int) $a_id;
87 }
88
89 public function getId()
90 {
91 return $this->id;
92 }
93
94 public function setTitle($a_value)
95 {
96 $this->title = trim($a_value);
97 }
98
99 public function getTitle()
100 {
101 return $this->title;
102 }
103
104 protected function setImage($a_value)
105 {
106 $this->image = trim($a_value);
107 }
108
109 public function getTypes()
110 {
111 return (array) $this->types;
112 }
113
114 public function setTypes(array $types = null)
115 {
116 $this->types = is_array($types)
117 ? array_unique($types)
118 : null;
119 }
120
121 public function getImage()
122 {
123 return $this->image;
124 }
125
130 public function uploadImage(array $a_upload_meta)
131 {
132 if ($this->getId() &&
133 $a_upload_meta["tmp_name"]) {
134 $path = $this->getFilePath($this->getId());
135
136
137 $filename = ilFileUtils::getValidFilename($a_upload_meta["name"]);
138
139 $suffix = strtolower(array_pop(explode(".", $filename)));
140 $tgt = $path . "img" . $this->getId() . "." . $suffix;
141
142 if (ilUtil::moveUploadedFile($a_upload_meta["tmp_name"], "img" . $this->getId() . "." . $suffix, $tgt)) {
143 $this->setImage($filename);
144 $this->update();
145 }
146 }
147 }
148
149 public function getImagePath()
150 {
151 if ($this->getId()) {
152 if (is_file($this->getFilePath($this->getId()) . "img" . $this->getId())) { // formerly (early 5.2 versino), images have been uploaded with no suffix
153 return $this->getFilePath($this->getId()) . "img" . $this->getId();
154 } else {
155 $suffix = strtolower(array_pop(explode(".", $this->getImage())));
156 return $this->getFilePath($this->getId()) . "img" . $this->getId() . "." . $suffix;
157 }
158 }
159 return "";
160 }
161
169 protected function getFilePath($a_id, $a_subdir = null)
170 {
171 $storage = new ilFSStorageBadgeImageTemplate($a_id);
172 $storage->create();
173
174 $path = $storage->getAbsolutePath() . "/";
175
176 if ($a_subdir) {
177 $path .= $a_subdir . "/";
178
179 if (!is_dir($path)) {
180 mkdir($path);
181 }
182 }
183
184 return $path;
185 }
186
187
188 //
189 // crud
190 //
191
192 protected function read($a_id)
193 {
195
196 $set = $ilDB->query("SELECT * FROM badge_image_template" .
197 " WHERE id = " . $ilDB->quote($a_id, "integer"));
198 if ($ilDB->numRows($set)) {
199 $row = $ilDB->fetchAssoc($set);
200 $row["types"] = $this->readTypes($a_id);
201 $this->importDBRow($row);
202 }
203 }
204
205 protected function readTypes($a_id)
206 {
208
209 $res = array();
210
211 $set = $ilDB->query("SELECT * FROM badge_image_templ_type" .
212 " WHERE tmpl_id = " . $ilDB->quote($a_id, "integer"));
213 while ($row = $ilDB->fetchAssoc($set)) {
214 $res[] = $row["type_id"];
215 }
216
217 if (!sizeof($res)) {
218 $res = null;
219 }
220
221 return $res;
222 }
223
224 protected function importDBRow(array $a_row)
225 {
226 $this->setId($a_row["id"]);
227 $this->setTitle($a_row["title"]);
228 $this->setImage($a_row["image"]);
229 $this->setTypes($a_row["types"]);
230 }
231
232 public function create()
233 {
235
236 if ($this->getId()) {
237 return $this->update();
238 }
239
240 $id = $ilDB->nextId("badge_image_template");
241 $this->setId($id);
242
243 $fields = $this->getPropertiesForStorage();
244 $fields["id"] = array("integer", $id);
245
246 $ilDB->insert("badge_image_template", $fields);
247
248 $this->saveTypes();
249 }
250
251 public function update()
252 {
254
255 if (!$this->getId()) {
256 return $this->create();
257 }
258
259 $fields = $this->getPropertiesForStorage();
260
261 $ilDB->update(
262 "badge_image_template",
263 $fields,
264 array("id" => array("integer", $this->getId()))
265 );
266
267 $this->saveTypes();
268 }
269
270 public function delete()
271 {
273
274 if (!$this->getId()) {
275 return;
276 }
277
278 $path = $this->getFilePath($this->getId());
279 ilUtil::delDir($path);
280
281 $ilDB->manipulate("DELETE FROM badge_image_template" .
282 " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
283 }
284
285 protected function getPropertiesForStorage()
286 {
287 return array(
288 "title" => array("text", $this->getTitle()),
289 "image" => array("text", $this->getImage())
290 );
291 }
292
293 protected function saveTypes()
294 {
296
297 if ($this->getId()) {
298 $ilDB->manipulate("DELETE FROM badge_image_templ_type" .
299 " WHERE tmpl_id = " . $ilDB->quote($this->getId(), "integer"));
300
301 if ($this->getTypes()) {
302 foreach ($this->getTypes() as $type) {
303 $fields = array(
304 "tmpl_id" => array("integer", $this->getId()),
305 "type_id" => array("text", $type)
306 );
307 $ilDB->insert("badge_image_templ_type", $fields);
308 }
309 }
310 }
311 }
312}
$filename
Definition: buildRTE.php:89
An exception for terminatinating execution or to throw for unit testing.
uploadImage(array $a_upload_meta)
__construct($a_id=null)
Constructor.
getFilePath($a_id, $a_subdir=null)
Init file system storage.
static getInstancesByType($a_type_unique_id)
static getValidFilename($a_filename)
Get valid filename.
static moveUploadedFile($a_file, $a_name, $a_target, $a_raise_errors=true, $a_mode="move_uploaded")
move uploaded file
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
global $DIC
Definition: goto.php:24
$type
foreach($_POST as $key=> $value) $res
global $ilDB