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