ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilPreview.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/Preview/classes/class.ilPreviewSettings.php");
5 require_once("./Services/Preview/classes/class.ilFSStoragePreview.php");
6 
17 class ilPreview
18 {
19  // status values
20  const RENDER_STATUS_NONE = "none";
21  const RENDER_STATUS_PENDING = "pending";
22  const RENDER_STATUS_CREATED = "created";
23  const RENDER_STATUS_FAILED = "failed";
24 
25  const FILENAME_FORMAT = "preview_%02d.jpg";
26 
31  private $obj_id = null;
32 
37  private $obj_type = null;
38 
43  private $storage = null;
44 
49  private $exists = false;
50 
55  private $render_date = false;
56 
61  private $render_status = self::RENDER_STATUS_NONE;
62 
69  public function __construct($a_obj_id, $a_type = "")
70  {
71  $this->obj_id = $a_obj_id;
72  $this->obj_type = $a_type;
73 
74  $this->init();
75  }
76 
84  public static function createPreview($a_obj, $a_force = false)
85  {
86  $preview = new ilPreview($a_obj->getId(), $a_obj->getType());
87  return $preview->create($a_obj, $a_force);
88  }
89 
95  public static function deletePreview($a_obj_id)
96  {
97  $preview = new ilPreview($a_obj_id);
98  $preview->delete();
99  }
100 
107  public static function copyPreviews($a_src_id, $a_dest_id)
108  {
110  return;
111 
112  // get source preview
113  $src = new ilPreview($a_src_id);
114  $status = $src->getRenderStatus();
115 
116  // created? copy the previews
117  if ($status == self::RENDER_STATUS_CREATED)
118  {
119  // create destination preview and set it's properties
120  $dest = new ilPreview($a_dest_id);
121  $dest->setRenderDate($src->getRenderDate());
122  $dest->setRenderStatus($src->getRenderStatus());
123 
124  // create path
125  $dest->getStorage()->create();
126 
127  // copy previews
128  ilUtil::rCopy($src->getStoragePath(), $dest->getStoragePath());
129 
130  // save copy
131  $dest->doCreate();
132  }
133  else
134  {
135  // all other status need no action
136  // self::RENDER_STATUS_FAILED
137  // self::RENDER_STATUS_NONE
138  // self::RENDER_STATUS_PENDING
139  }
140  }
141 
149  public static function hasPreview($a_obj_id, $a_type = "")
150  {
152  return false;
153 
154  $preview = new ilPreview($a_obj_id, $a_type);
155  if ($preview->exists())
156  return true;
157 
158  // does not exist, enable on demand rendering if there's any renderer that supports our object
159  require_once("./Services/Preview/classes/class.ilRendererFactory.php");
161  return $renderer != null;
162  }
163 
170  public static function lookupRenderStatus($a_obj_id)
171  {
172  $preview = new ilPreview($a_obj_id);
173  return $preview->getRenderStatus();
174  }
175 
181  public function exists()
182  {
183  return $this->exists;
184  }
185 
193  public function create($a_obj, $a_force = false)
194  {
196  return false;
197 
198  // get renderer for preview
199  require_once("./Services/Preview/classes/class.ilRendererFactory.php");
200  $renderer = ilRendererFactory::getRenderer($this);
201 
202  // no renderer available?
203  if ($renderer == null)
204  return false;
205 
206  // exists, but still pending?
207  if ($this->getRenderStatus() == self::RENDER_STATUS_PENDING)
208  return false;
209 
210  // not forced? check if update really needed
211  if ($this->getRenderStatus() == self::RENDER_STATUS_CREATED && !$a_force)
212  {
213  // check last modified against last render date
214  if ($a_obj->getLastUpdateDate() <= $this->getRenderDate())
215  return false;
216  }
217 
218  // re-create the directory to store the previews
219  $this->getStorage()->delete();
220  $this->getStorage()->create();
221 
222  // let the renderer create the preview
223  $renderer->render($this, $a_obj, true);
224 
225  // save to database
226  $this->save();
227 
228  return true;
229  }
230 
234  public function delete()
235  {
236  // does exist?
237  if ($this->exists())
238  {
239  // delete files and database entry
240  $this->getStorage()->delete();
241  $this->doDelete();
242 
243  // reset values
244  $this->exists = false;
245  $this->render_date = false;
246  $this->render_status = self::RENDER_STATUS_NONE;
247  }
248  }
249 
255  public function getImages()
256  {
257  $images = array();
258 
259  // status must be created
260  $path = $this->getStoragePath();
261  if ($this->getRenderStatus() == self::RENDER_STATUS_CREATED)
262  {
263  // load files
264  if ($handle = @opendir($path))
265  {
266  while (false !== ($file = readdir($handle)))
267  {
268  $filepath = $path . "/" . $file;
269  if (!is_file($filepath))
270  continue;
271 
272  if ($file != '.' && $file != '..' && strpos($file, "preview_") === 0)
273  {
274  $image = array();
275  $image["url"] = ilUtil::getHtmlPath($filepath);
276 
277  // get image size
278  $size = @getimagesize($filepath);
279  if ($size !== false)
280  {
281  $image["width"] = $size[0];
282  $image["height"] = $size[1];
283  }
284 
285  $images[$file] = $image;
286  }
287  }
288  closedir($handle);
289 
290  // sort by key
291  ksort($images);
292  }
293  }
294 
295  return $images;
296  }
297 
301  public function save()
302  {
303  if ($this->exists)
304  $this->doUpdate();
305  else
306  $this->doCreate();
307  }
308 
312  protected function doCreate()
313  {
314  global $ilDB;
315 
316  $ilDB->insert(
317  "preview_data",
318  array(
319  "obj_id" => array("integer", $this->getObjId()),
320  "render_date" => array("timestamp", $this->getRenderDate()),
321  "render_status" => array("text", $this->getRenderStatus())
322  )
323  );
324  $this->exists = true;
325  }
326 
330  protected function doRead()
331  {
332  global $ilDB;
333 
334  $set = $ilDB->queryF(
335  "SELECT * FROM preview_data WHERE obj_id=%s",
336  array("integer"),
337  array($this->getObjId()));
338 
339  while ($rec = $ilDB->fetchAssoc($set))
340  {
341  $this->setRenderDate($rec["render_date"]);
342  $this->setRenderStatus($rec["render_status"]);
343  $this->exists = true;
344  }
345  }
346 
350  protected function doUpdate()
351  {
352  global $ilDB;
353 
354  $ilDB->update(
355  "preview_data",
356  array(
357  "render_date" => array("timestamp", $this->getRenderDate()),
358  "render_status" => array("text", $this->getRenderStatus())
359  ),
360  array("obj_id" => array("integer", $this->getObjId()))
361  );
362  }
363 
367  protected function doDelete()
368  {
369  global $ilDB;
370 
371  $ilDB->manipulateF(
372  "DELETE FROM preview_data WHERE obj_id=%s",
373  array("integer"),
374  array($this->getObjId()));
375  }
376 
382  public function getObjId()
383  {
384  return $this->obj_id;
385  }
386 
392  public function getObjType()
393  {
394  // not evaluated before or specified?
395  if (empty($this->obj_type))
396  $this->obj_type = ilObject::_lookupType($this->getObjId(), false);
397 
398  return $this->obj_type;
399  }
400 
406  public function getStoragePath()
407  {
408  return $this->getStorage()->getPath();
409  }
410 
416  public function getAbsoluteStoragePath()
417  {
418  return ILIAS_ABSOLUTE_PATH . substr($this->getStorage()->getPath(), 1);
419  }
420 
427  public function getFilePathFormat()
428  {
430  return $path . "/" . self::FILENAME_FORMAT;
431  }
432 
438  public function getRenderDate()
439  {
440  return $this->render_date;
441  }
442 
448  public function setRenderDate($a_date)
449  {
450  $this->render_date = $a_date;
451  }
452 
458  public function getRenderStatus()
459  {
460  return $this->render_status;
461  }
462 
468  public function setRenderStatus($a_status)
469  {
470  $this->render_status = $a_status;
471  }
472 
478  public function getStorage()
479  {
480  if ($this->storage == null)
481  $this->storage = new ilFSStoragePreview($this->obj_id);
482 
483  return $this->storage;
484  }
485 
489  private function init()
490  {
491  // read entry
492  $this->doRead();
493  }
494 }
495 ?>
print $file
$size
Definition: RandomTest.php:79
__construct($a_obj_id, $a_type="")
Creates a new ilPreview.
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
$preview
const RENDER_STATUS_FAILED
getObjType()
Gets the type of the object the preview is for.
exists()
Determines whether the preview exists or not.
getAbsoluteStoragePath()
Gets the absolute path where the previews are stored.
const RENDER_STATUS_PENDING
getFilePathFormat()
Gets the absolute file path for preview images that contains a placeholder in the file name (&#39;%02d&#39;) ...
const RENDER_STATUS_NONE
doCreate()
Create entry in database.
static lookupRenderStatus($a_obj_id)
Gets the render status for the object with the specified id.
init()
Initializes the preview object.
static getRenderer($preview)
Gets the renderer that is able to create a preview for the specified preview object.
static hasPreview($a_obj_id, $a_type="")
Determines whether the object with the specified reference id has a preview.
const ILIAS_ABSOLUTE_PATH
static createPreview($a_obj, $a_force=false)
Creates the preview for the object with the specified id.
static removeTrailingPathSeparators($path)
getRenderStatus()
Gets the status of the rendering process.
static getHtmlPath($relative_path)
get url of path
static isPreviewEnabled()
Gets whether the preview functionality is enabled.
static _lookupType($a_id, $a_reference=false)
lookup object type
const RENDER_STATUS_CREATED
doRead()
Read data from database.
getObjId()
Gets the id of the object the preview is for.
static copyPreviews($a_src_id, $a_dest_id)
Copies the preview images from one preview to a new preview object.
doDelete()
Delete data from database.
doUpdate()
Update data in database.
setRenderStatus($a_status)
Sets the status of the rendering process.
getRenderDate()
Gets the date when the preview was rendered.
getStoragePath()
Gets the path where the previews are stored relative to the web directory.
$path
Definition: index.php:22
static deletePreview($a_obj_id)
Deletes the preview for the object with the specified id.
global $ilDB
setRenderDate($a_date)
Sets the date when the preview was rendered.
const FILENAME_FORMAT
save()
Saves the preview data to the database.
getImages()
Gets an array of preview images.
getStorage()
Gets the storage object for the preview.
create($a_obj, $a_force=false)
Creates the preview.