ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
205  // bugfix mantis 23293
206  $this->delete();
207  return false;
208  }
209 
210  // exists, but still pending?
211  if ($this->getRenderStatus() == self::RENDER_STATUS_PENDING)
212  return false;
213 
214  // not forced? check if update really needed
215  if ($this->getRenderStatus() == self::RENDER_STATUS_CREATED && !$a_force)
216  {
217  // check last modified against last render date
218  if ($a_obj->getLastUpdateDate() <= $this->getRenderDate())
219  return false;
220  }
221 
222  // re-create the directory to store the previews
223  $this->getStorage()->delete();
224  $this->getStorage()->create();
225 
226  // let the renderer create the preview
227  $renderer->render($this, $a_obj, true);
228 
229  // save to database
230  $this->save();
231 
232  return true;
233  }
234 
238  public function delete()
239  {
240  // does exist?
241  if ($this->exists())
242  {
243  // delete files and database entry
244  $this->getStorage()->delete();
245  $this->doDelete();
246 
247  // reset values
248  $this->exists = false;
249  $this->render_date = false;
250  $this->render_status = self::RENDER_STATUS_NONE;
251  }
252  }
253 
259  public function getImages()
260  {
261  $images = array();
262 
263  // status must be created
264  $path = $this->getStoragePath();
265  if ($this->getRenderStatus() == self::RENDER_STATUS_CREATED)
266  {
267  // load files
268  if ($handle = @opendir($path))
269  {
270  while (false !== ($file = readdir($handle)))
271  {
272  $filepath = $path . "/" . $file;
273  if (!is_file($filepath))
274  continue;
275 
276  if ($file != '.' && $file != '..' && strpos($file, "preview_") === 0)
277  {
278  $image = array();
279  $image["url"] = ilUtil::getHtmlPath($filepath);
280 
281  // get image size
282  $size = @getimagesize($filepath);
283  if ($size !== false)
284  {
285  $image["width"] = $size[0];
286  $image["height"] = $size[1];
287  }
288 
289  $images[$file] = $image;
290  }
291  }
292  closedir($handle);
293 
294  // sort by key
295  ksort($images);
296  }
297  }
298 
299  return $images;
300  }
301 
305  public function save()
306  {
307  if ($this->exists)
308  $this->doUpdate();
309  else
310  $this->doCreate();
311  }
312 
316  protected function doCreate()
317  {
318  global $ilDB;
319 
320  $ilDB->insert(
321  "preview_data",
322  array(
323  "obj_id" => array("integer", $this->getObjId()),
324  "render_date" => array("timestamp", $this->getRenderDate()),
325  "render_status" => array("text", $this->getRenderStatus())
326  )
327  );
328  $this->exists = true;
329  }
330 
334  protected function doRead()
335  {
336  global $ilDB;
337 
338  $set = $ilDB->queryF(
339  "SELECT * FROM preview_data WHERE obj_id=%s",
340  array("integer"),
341  array($this->getObjId()));
342 
343  while ($rec = $ilDB->fetchAssoc($set))
344  {
345  $this->setRenderDate($rec["render_date"]);
346  $this->setRenderStatus($rec["render_status"]);
347  $this->exists = true;
348  }
349  }
350 
354  protected function doUpdate()
355  {
356  global $ilDB;
357 
358  $ilDB->update(
359  "preview_data",
360  array(
361  "render_date" => array("timestamp", $this->getRenderDate()),
362  "render_status" => array("text", $this->getRenderStatus())
363  ),
364  array("obj_id" => array("integer", $this->getObjId()))
365  );
366  }
367 
371  protected function doDelete()
372  {
373  global $ilDB;
374 
375  $ilDB->manipulateF(
376  "DELETE FROM preview_data WHERE obj_id=%s",
377  array("integer"),
378  array($this->getObjId()));
379  }
380 
386  public function getObjId()
387  {
388  return $this->obj_id;
389  }
390 
396  public function getObjType()
397  {
398  // not evaluated before or specified?
399  if (empty($this->obj_type))
400  $this->obj_type = ilObject::_lookupType($this->getObjId(), false);
401 
402  return $this->obj_type;
403  }
404 
410  public function getStoragePath()
411  {
412  return $this->getStorage()->getPath();
413  }
414 
420  public function getAbsoluteStoragePath()
421  {
422  return ILIAS_ABSOLUTE_PATH . substr($this->getStorage()->getPath(), 1);
423  }
424 
431  public function getFilePathFormat()
432  {
434  return $path . "/" . self::FILENAME_FORMAT;
435  }
436 
442  public function getRenderDate()
443  {
444  return $this->render_date;
445  }
446 
452  public function setRenderDate($a_date)
453  {
454  $this->render_date = $a_date;
455  }
456 
462  public function getRenderStatus()
463  {
464  return $this->render_status;
465  }
466 
472  public function setRenderStatus($a_status)
473  {
474  $this->render_status = $a_status;
475  }
476 
482  public function getStorage()
483  {
484  if ($this->storage == null)
485  $this->storage = new ilFSStoragePreview($this->obj_id);
486 
487  return $this->storage;
488  }
489 
493  private function init()
494  {
495  // read entry
496  $this->doRead();
497  }
498 }
499 ?>
$path
Definition: aliased.php:25
$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.
$a_type
Definition: workflow.php:93
static hasPreview($a_obj_id, $a_type="")
Determines whether the object with the specified reference id has a preview.
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.
Create styles array
The data for the language used.
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.
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.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
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.