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