ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
4require_once("./Services/Preview/classes/class.ilPreviewSettings.php");
5require_once("./Services/Preview/classes/class.ilFSStoragePreview.php");
6
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
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
113 // get source preview
114 $src = new ilPreview($a_src_id);
115 $status = $src->getRenderStatus();
116
117 // created? copy the previews
118 if ($status == self::RENDER_STATUS_CREATED) {
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 } else {
133 // all other status need no action
134 // self::RENDER_STATUS_FAILED
135 // self::RENDER_STATUS_NONE
136 // self::RENDER_STATUS_PENDING
137 }
138 }
139
147 public static function hasPreview($a_obj_id, $a_type = "")
148 {
150 return false;
151 }
152
153 $preview = new ilPreview($a_obj_id, $a_type);
154 if ($preview->exists()) {
155 return true;
156 }
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
199 // get renderer for preview
200 require_once("./Services/Preview/classes/class.ilRendererFactory.php");
201 $renderer = ilRendererFactory::getRenderer($this);
202
203 // no renderer available?
204 if ($renderer == null) {
205 // bugfix mantis 23293
206 $this->delete();
207 return false;
208 }
209
210 // exists, but still pending?
212 return false;
213 }
214
215 // not forced? check if update really needed
216 if ($this->getRenderStatus() == self::RENDER_STATUS_CREATED && !$a_force) {
217 // check last modified against last render date
218 if ($a_obj->getLastUpdateDate() <= $this->getRenderDate()) {
219 return false;
220 }
221 }
222
223 // re-create the directory to store the previews
224 $this->getStorage()->delete();
225 $this->getStorage()->create();
226
227 // let the renderer create the preview
228 $renderer->render($this, $a_obj, true);
229
230 // save to database
231 $this->save();
232
233 return true;
234 }
235
239 public function delete()
240 {
241 // does exist?
242 if ($this->exists()) {
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 // load files
267 if ($handle = @opendir($path)) {
268 while (false !== ($file = readdir($handle))) {
269 $filepath = $path . "/" . $file;
270 if (!is_file($filepath)) {
271 continue;
272 }
273
274 if ($file != '.' && $file != '..' && strpos($file, "preview_") === 0) {
275 $image = array();
276 $image["url"] = ilUtil::getHtmlPath($filepath);
277
278 // get image size
279 $size = @getimagesize($filepath);
280 if ($size !== false) {
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 }
309
313 protected function doCreate()
314 {
315 global $DIC;
316 $ilDB = $DIC['ilDB'];
317
318 $ilDB->insert(
319 "preview_data",
320 array(
321 "obj_id" => array("integer", $this->getObjId()),
322 "render_date" => array("timestamp", $this->getRenderDate()),
323 "render_status" => array("text", $this->getRenderStatus())
324 )
325 );
326 $this->exists = true;
327 }
328
332 protected function doRead()
333 {
334 global $DIC;
335 $ilDB = $DIC['ilDB'];
336
337 $set = $ilDB->queryF(
338 "SELECT * FROM preview_data WHERE obj_id=%s",
339 array("integer"),
340 array($this->getObjId())
341 );
342
343 while ($rec = $ilDB->fetchAssoc($set)) {
344 $this->setRenderDate($rec["render_date"]);
345 $this->setRenderStatus($rec["render_status"]);
346 $this->exists = true;
347 }
348 }
349
353 protected function doUpdate()
354 {
355 global $DIC;
356 $ilDB = $DIC['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 $DIC;
374 $ilDB = $DIC['ilDB'];
375
376 $ilDB->manipulateF(
377 "DELETE FROM preview_data WHERE obj_id=%s",
378 array("integer"),
379 array($this->getObjId())
380 );
381 }
382
388 public function getObjId()
389 {
390 return $this->obj_id;
391 }
392
398 public function getObjType()
399 {
400 // not evaluated before or specified?
401 if (empty($this->obj_type)) {
402 $this->obj_type = ilObject::_lookupType($this->getObjId(), false);
403 }
404
405 return $this->obj_type;
406 }
407
413 public function getStoragePath()
414 {
415 return $this->getStorage()->getPath();
416 }
417
423 public function getAbsoluteStoragePath()
424 {
425 return ILIAS_ABSOLUTE_PATH . substr($this->getStorage()->getPath(), 1);
426 }
427
434 public function getFilePathFormat()
435 {
437 return $path . "/" . self::FILENAME_FORMAT;
438 }
439
445 public function getRenderDate()
446 {
447 return $this->render_date;
448 }
449
455 public function setRenderDate($a_date)
456 {
457 $this->render_date = $a_date;
458 }
459
465 public function getRenderStatus()
466 {
468 }
469
475 public function setRenderStatus($a_status)
476 {
477 $this->render_status = $a_status;
478 }
479
485 public function getStorage()
486 {
487 if ($this->storage == null) {
488 $this->storage = new ilFSStoragePreview($this->obj_id);
489 }
490
491 return $this->storage;
492 }
493
497 private function init()
498 {
499 // read entry
500 $this->doRead();
501 }
502}
$size
Definition: RandomTest.php:84
$path
Definition: aliased.php:25
An exception for terminatinating execution or to throw for unit testing.
static _lookupType($a_id, $a_reference=false)
lookup object type
static isPreviewEnabled()
Gets whether the preview functionality is enabled.
setRenderStatus($a_status)
Sets the status of the rendering process.
getFilePathFormat()
Gets the absolute file path for preview images that contains a placeholder in the file name ('%02d') ...
doUpdate()
Update data in database.
init()
Initializes the preview object.
static copyPreviews($a_src_id, $a_dest_id)
Copies the preview images from one preview to a new preview object.
static hasPreview($a_obj_id, $a_type="")
Determines whether the object with the specified reference id has a preview.
create($a_obj, $a_force=false)
Creates the preview.
doRead()
Read data from database.
static deletePreview($a_obj_id)
Deletes the preview for the object with the specified id.
const RENDER_STATUS_NONE
getAbsoluteStoragePath()
Gets the absolute path where the previews are stored.
save()
Saves the preview data to the database.
exists()
Determines whether the preview exists or not.
__construct($a_obj_id, $a_type="")
Creates a new ilPreview.
getStorage()
Gets the storage object for the preview.
static createPreview($a_obj, $a_force=false)
Creates the preview for the object with the specified id.
getRenderStatus()
Gets the status of the rendering process.
setRenderDate($a_date)
Sets the date when the preview was rendered.
getImages()
Gets an array of preview images.
doDelete()
Delete data from database.
static lookupRenderStatus($a_obj_id)
Gets the render status for the object with the specified id.
const RENDER_STATUS_FAILED
const RENDER_STATUS_CREATED
doCreate()
Create entry in database.
getStoragePath()
Gets the path where the previews are stored relative to the web directory.
getObjType()
Gets the type of the object the preview is for.
getObjId()
Gets the id of the object the preview is for.
getRenderDate()
Gets the date when the preview was rendered.
const RENDER_STATUS_PENDING
const FILENAME_FORMAT
static getRenderer($preview)
Gets the renderer that is able to create a preview for the specified preview object.
static rCopy($a_sdir, $a_tdir, $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static getHtmlPath($relative_path)
get url of path
static removeTrailingPathSeparators($path)
global $DIC
Definition: saml.php:7
global $ilDB
$preview
$a_type
Definition: workflow.php:92