ILIAS  release_8 Revision v8.24
ilPreview Class Reference
+ Collaboration diagram for ilPreview:

Public Member Functions

 __construct (int $a_obj_id, string $a_type="")
 Creates a new ilPreview. More...
 
 exists ()
 Determines whether the preview exists or not. More...
 
 create (\ilObject $a_obj, bool $a_force=false)
 Creates the preview. More...
 
 delete ()
 Deletes the preview. More...
 
 getImages ()
 Gets an array of preview images. More...
 
 save ()
 Saves the preview data to the database. More...
 
 getObjId ()
 Gets the id of the object the preview is for. More...
 
 getObjType ()
 Gets the type of the object the preview is for. More...
 
 getStoragePath ()
 Gets the path where the previews are stored relative to the web directory. More...
 
 getAbsoluteStoragePath ()
 Gets the absolute path where the previews are stored. More...
 
 getFilePathFormat ()
 Gets the absolute file path for preview images that contains a placeholder in the file name ('%02d') to be formatted with the preview number (use 'sprintf' for that). More...
 
 getRenderDate ()
 
 setRenderDate (string $a_date)
 
 getRenderStatus ()
 Gets the status of the rendering process. More...
 
 setRenderStatus (string $a_status)
 Sets the status of the rendering process. More...
 
 getStorage ()
 Gets the storage object for the preview. More...
 

Static Public Member Functions

static createPreview (\ilObject $a_obj, bool $a_force=false)
 Creates the preview for the object with the specified id. More...
 
static deletePreview (int $a_obj_id)
 Deletes the preview for the object with the specified id. More...
 
static copyPreviews (int $a_src_id, int $a_dest_id)
 Copies the preview images from one preview to a new preview object. More...
 
static hasPreview (int $a_obj_id, string $a_type="")
 Determines whether the object with the specified reference id has a preview. More...
 
static lookupRenderStatus (int $a_obj_id)
 Gets the render status for the object with the specified id. More...
 

Data Fields

const RENDER_STATUS_NONE = "none"
 
const RENDER_STATUS_PENDING = "pending"
 
const RENDER_STATUS_CREATED = "created"
 
const RENDER_STATUS_FAILED = "failed"
 

Protected Member Functions

 doCreate ()
 Create entry in database. More...
 
 doRead ()
 Read data from database. More...
 
 doUpdate ()
 Update data in database. More...
 
 doDelete ()
 Delete data from database. More...
 

Protected Attributes

ilRendererFactory $factory
 

Private Member Functions

 init ()
 Initializes the preview object. More...
 

Private Attributes

const FILENAME_FORMAT = "preview_%02d.jpg"
 
int $obj_id = null
 The object id. More...
 
string $obj_type = null
 The type of the object. More...
 
ilFSStoragePreview $storage = null
 The file storage instance. More...
 
bool $exists = false
 Defines whether the preview exists. More...
 
string $render_date = null
 The timestamp when the preview was rendered. More...
 
string $render_status = self::RENDER_STATUS_NONE
 The status of the rendering process. More...
 

Detailed Description

Definition at line 28 of file class.ilPreview.php.

Constructor & Destructor Documentation

◆ __construct()

ilPreview::__construct ( int  $a_obj_id,
string  $a_type = "" 
)

Creates a new ilPreview.

Parameters
int$a_obj_idThe object id.
string$a_typeThe type of the object.

Definition at line 75 of file class.ilPreview.php.

76 {
77 $this->obj_id = $a_obj_id;
78 $this->obj_type = $a_type;
79 $this->factory = new ilRendererFactory();
80 $this->init();
81 }
init()
Initializes the preview object.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...

References init().

+ Here is the call graph for this function:

Member Function Documentation

◆ copyPreviews()

static ilPreview::copyPreviews ( int  $a_src_id,
int  $a_dest_id 
)
static

Copies the preview images from one preview to a new preview object.

Parameters
int$a_src_idThe id of the object to copy from.
int$a_dest_idThe id of the object to copy to.

Definition at line 113 of file class.ilPreview.php.

113 : void
114 {
116 return;
117 }
118
119 // get source preview
120 $src = new ilPreview($a_src_id);
121 $status = $src->getRenderStatus();
122
123 // created? copy the previews
124 if ($status === self::RENDER_STATUS_CREATED) {
125 // create destination preview and set it's properties
126 $dest = new ilPreview($a_dest_id);
127 $dest->setRenderDate($src->getRenderDate());
128 $dest->setRenderStatus($src->getRenderStatus());
129
130 // create path
131 $dest->getStorage()->create();
132
133 // copy previews
134 ilFileUtils::rCopy($src->getStoragePath(), $dest->getStoragePath());
135
136 // save copy
137 $dest->doCreate();
138 }
139 }
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
static isPreviewEnabled()
Gets whether the preview functionality is enabled.

References ilPreviewSettings\isPreviewEnabled(), and ilFileUtils\rCopy().

+ Here is the call graph for this function:

◆ create()

ilPreview::create ( \ilObject  $a_obj,
bool  $a_force = false 
)

Creates the preview.

Parameters
ilObject$a_objThe object to create the preview for.
bool$a_forcetrue, to force the creation of the preview; false, to create the preview only if needed.
Returns
bool true, if the preview was created; otherwise, false.

Definition at line 192 of file class.ilPreview.php.

192 : bool
193 {
195 return false;
196 }
198 $renderer = $factory->getRenderer($this);
199
200 // no renderer available?
201 if ($renderer === null) {
202 // bugfix mantis 23293
203 $this->delete();
204 return false;
205 }
206
207 // exists, but still pending?
209 return false;
210 }
211
212 // not forced? check if update really needed
213 if (!$a_force && $this->getRenderStatus() === self::RENDER_STATUS_CREATED) {
214 // check last modified against last render date
215 if ($a_obj->getLastUpdateDate() <= $this->getRenderDate()) {
216 return false;
217 }
218 }
219
220 // re-create the directory to store the previews
221 $this->getStorage()->delete();
222 $this->getStorage()->create();
223
224 // let the renderer create the preview
225 $renderer->render($this, $a_obj, true);
226
227 // save to database
228 $this->save();
229
230 return true;
231 }
ilRendererFactory $factory
save()
Saves the preview data to the database.
getStorage()
Gets the storage object for the preview.
getRenderStatus()
Gets the status of the rendering process.
const RENDER_STATUS_CREATED
const RENDER_STATUS_PENDING
getRenderer(\ilPreview $preview)
Gets the renderer that is able to create a preview for the specified preview object.

References $factory, ilObject\getLastUpdateDate(), getRenderDate(), ilRendererFactory\getRenderer(), getRenderStatus(), getStorage(), ilPreviewSettings\isPreviewEnabled(), RENDER_STATUS_CREATED, RENDER_STATUS_PENDING, and save().

+ Here is the call graph for this function:

◆ createPreview()

static ilPreview::createPreview ( \ilObject  $a_obj,
bool  $a_force = false 
)
static

Creates the preview for the object with the specified id.

Parameters
ilObject$a_objThe object to create the preview for.
bool$a_forcetrue, to force the creation of the preview; false, to create the preview only if needed.
Returns
bool true, if the preview was created; otherwise, false.

Definition at line 90 of file class.ilPreview.php.

90 : bool
91 {
92 $preview = new ilPreview($a_obj->getId(), $a_obj->getType());
93 return $preview->create($a_obj, $a_force);
94 }
$preview
Definition: imgupload.php:81

References $preview, ilObject\getId(), and ilObject\getType().

Referenced by ilDclFileuploadRecordRepresentation\getHTML().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ delete()

ilPreview::delete ( )

Deletes the preview.

Definition at line 236 of file class.ilPreview.php.

236 : void
237 {
238 // does exist?
239 if ($this->exists()) {
240 // delete files and database entry
241 $this->getStorage()->delete();
242 $this->doDelete();
243
244 // reset values
245 $this->exists = false;
246 $this->render_date = null;
247 $this->render_status = self::RENDER_STATUS_NONE;
248 }
249 }
const RENDER_STATUS_NONE
exists()
Determines whether the preview exists or not.
doDelete()
Delete data from database.

References doDelete(), exists(), getStorage(), and RENDER_STATUS_NONE.

+ Here is the call graph for this function:

◆ deletePreview()

static ilPreview::deletePreview ( int  $a_obj_id)
static

Deletes the preview for the object with the specified id.

Parameters
int$a_obj_idThe id of the object to create the preview for.

Definition at line 101 of file class.ilPreview.php.

101 : void
102 {
103 $preview = new ilPreview($a_obj_id);
104 $preview->delete();
105 }

References $preview.

◆ doCreate()

ilPreview::doCreate ( )
protected

Create entry in database.

Definition at line 308 of file class.ilPreview.php.

308 : void
309 {
310 global $DIC;
311 $ilDB = $DIC['ilDB'];
312
313 $ilDB->insert(
314 "preview_data",
315 array(
316 "obj_id" => array("integer", $this->getObjId()),
317 "render_date" => array("timestamp", $this->getRenderDate()),
318 "render_status" => array("text", $this->getRenderStatus())
319 )
320 );
321 $this->exists = true;
322 }
getObjId()
Gets the id of the object the preview is for.
global $DIC
Definition: feed.php:28

References $DIC, $ilDB, exists(), getObjId(), getRenderDate(), and getRenderStatus().

Referenced by save().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doDelete()

ilPreview::doDelete ( )
protected

Delete data from database.

Definition at line 366 of file class.ilPreview.php.

366 : void
367 {
368 global $DIC;
369 $ilDB = $DIC['ilDB'];
370
371 $ilDB->manipulateF(
372 "DELETE FROM preview_data WHERE obj_id=%s",
373 array("integer"),
374 array($this->getObjId())
375 );
376 }

References $DIC, $ilDB, and getObjId().

Referenced by delete().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doRead()

ilPreview::doRead ( )
protected

Read data from database.

Definition at line 327 of file class.ilPreview.php.

327 : void
328 {
329 global $DIC;
330 $ilDB = $DIC['ilDB'];
331
332 $set = $ilDB->queryF(
333 "SELECT * FROM preview_data WHERE obj_id=%s",
334 array("integer"),
335 array($this->getObjId())
336 );
337
338 while ($rec = $ilDB->fetchAssoc($set)) {
339 $this->setRenderDate($rec["render_date"]);
340 $this->setRenderStatus($rec["render_status"]);
341 $this->exists = true;
342 }
343 }
setRenderDate(string $a_date)
setRenderStatus(string $a_status)
Sets the status of the rendering process.

References $DIC, $ilDB, exists(), getObjId(), setRenderDate(), and setRenderStatus().

Referenced by init().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ doUpdate()

ilPreview::doUpdate ( )
protected

Update data in database.

Definition at line 348 of file class.ilPreview.php.

348 : void
349 {
350 global $DIC;
351 $ilDB = $DIC['ilDB'];
352
353 $ilDB->update(
354 "preview_data",
355 array(
356 "render_date" => array("timestamp", $this->getRenderDate()),
357 "render_status" => array("text", $this->getRenderStatus())
358 ),
359 array("obj_id" => array("integer", $this->getObjId()))
360 );
361 }

References $DIC, $ilDB, getObjId(), getRenderDate(), and getRenderStatus().

Referenced by save().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ exists()

ilPreview::exists ( )

Determines whether the preview exists or not.

Returns
bool true, if a preview exists for the object; otherwise, false.

Definition at line 180 of file class.ilPreview.php.

180 : bool
181 {
182 return $this->exists;
183 }
bool $exists
Defines whether the preview exists.

References $exists.

Referenced by delete(), doCreate(), doRead(), and save().

+ Here is the caller graph for this function:

◆ getAbsoluteStoragePath()

ilPreview::getAbsoluteStoragePath ( )

Gets the absolute path where the previews are stored.

Returns
string The path where the previews are stored.

Definition at line 418 of file class.ilPreview.php.

418 : string
419 {
420 return ILIAS_WEB_DIR . "/" . CLIENT_ID . "/{$this->getStorage()->getPath()}";
421 }
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45

References CLIENT_ID, and ILIAS_WEB_DIR.

Referenced by getFilePathFormat(), and getImages().

+ Here is the caller graph for this function:

◆ getFilePathFormat()

ilPreview::getFilePathFormat ( )

Gets the absolute file path for preview images that contains a placeholder in the file name ('%02d') to be formatted with the preview number (use 'sprintf' for that).

Returns
string The format of the absolute file path.

Definition at line 429 of file class.ilPreview.php.

429 : string
430 {
432 return $path . "/" . self::FILENAME_FORMAT;
433 }
static removeTrailingPathSeparators(string $path)
getAbsoluteStoragePath()
Gets the absolute path where the previews are stored.
const FILENAME_FORMAT
$path
Definition: ltiservices.php:32

References $path, FILENAME_FORMAT, getAbsoluteStoragePath(), and ilFileUtils\removeTrailingPathSeparators().

+ Here is the call graph for this function:

◆ getImages()

ilPreview::getImages ( )

Gets an array of preview images.

Returns
array The preview images.

Definition at line 256 of file class.ilPreview.php.

256 : array
257 {
258 $images = array();
259
260 // status must be created
261 $path = $this->getAbsoluteStoragePath();
262 if ($this->getRenderStatus() === self::RENDER_STATUS_CREATED && ($handle = @opendir($path))) {
263 // load files
264 while (false !== ($file = readdir($handle))) {
265 $filepath = $path . "/" . $file;
266 if (!is_file($filepath)) {
267 continue;
268 }
269
270 if ($file !== '.' && $file !== '..' && strpos($file, "preview_") === 0) {
271 $image = array();
272 $image["url"] = ilUtil::getHtmlPath($filepath);
273
274 // get image size
275 $size = @getimagesize($filepath);
276 if ($size !== false) {
277 $image["width"] = $size[0];
278 $image["height"] = $size[1];
279 }
280
281 $images[$file] = $image;
282 }
283 }
284 closedir($handle);
285
286 // sort by key
287 ksort($images);
288 }
289
290 return $images;
291 }
static getHtmlPath(string $relative_path)
get url of path

References $path, getAbsoluteStoragePath(), ilUtil\getHtmlPath(), and getRenderStatus().

+ Here is the call graph for this function:

◆ getObjId()

ilPreview::getObjId ( )

Gets the id of the object the preview is for.

Returns
int The id of the object the preview is for.

Definition at line 383 of file class.ilPreview.php.

383 : ?int
384 {
385 return $this->obj_id;
386 }
int $obj_id
The object id.

References $obj_id.

Referenced by doCreate(), doDelete(), doRead(), doUpdate(), and getObjType().

+ Here is the caller graph for this function:

◆ getObjType()

ilPreview::getObjType ( )

Gets the type of the object the preview is for.

Returns
string The type of the object the preview is for.

Definition at line 393 of file class.ilPreview.php.

393 : string
394 {
395 // not evaluated before or specified?
396 if (empty($this->obj_type)) {
397 $this->obj_type = ilObject::_lookupType($this->getObjId(), false);
398 }
399
400 return $this->obj_type;
401 }
static _lookupType(int $id, bool $reference=false)
string $obj_type
The type of the object.

References $obj_type, ilObject\_lookupType(), and getObjId().

+ Here is the call graph for this function:

◆ getRenderDate()

ilPreview::getRenderDate ( )

Definition at line 435 of file class.ilPreview.php.

435 : ?string
436 {
437 return $this->render_date;
438 }
string $render_date
The timestamp when the preview was rendered.

References $render_date.

Referenced by create(), doCreate(), and doUpdate().

+ Here is the caller graph for this function:

◆ getRenderStatus()

ilPreview::getRenderStatus ( )

Gets the status of the rendering process.

Returns
string The status of the rendering process.

Definition at line 450 of file class.ilPreview.php.

450 : string
451 {
453 }
string $render_status
The status of the rendering process.

References $render_status.

Referenced by create(), doCreate(), doUpdate(), and getImages().

+ Here is the caller graph for this function:

◆ getStorage()

ilPreview::getStorage ( )

Gets the storage object for the preview.

Returns
ilFSStoragePreview The storage object.

Definition at line 470 of file class.ilPreview.php.

471 {
472 if ($this->storage === null) {
473 $this->storage = new ilFSStoragePreview($this->obj_id);
474 }
475
476 return $this->storage;
477 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilFSStoragePreview $storage
The file storage instance.

References $storage.

Referenced by create(), delete(), and getStoragePath().

+ Here is the caller graph for this function:

◆ getStoragePath()

ilPreview::getStoragePath ( )

Gets the path where the previews are stored relative to the web directory.

Returns
string The path where the previews are stored.

Definition at line 408 of file class.ilPreview.php.

408 : string
409 {
410 return $this->getStorage()->getPath();
411 }

References getStorage().

+ Here is the call graph for this function:

◆ hasPreview()

static ilPreview::hasPreview ( int  $a_obj_id,
string  $a_type = "" 
)
static

Determines whether the object with the specified reference id has a preview.

Parameters
int$a_obj_idThe id of the object to check.
string$a_typeThe type of the object to check.
Returns
bool true, if the object has a preview; otherwise, false.

Definition at line 148 of file class.ilPreview.php.

148 : bool
149 {
151 return false;
152 }
153
154 $preview = new ilPreview($a_obj_id, $a_type);
155 if ($preview->exists()) {
156 return true;
157 }
159 $renderer = $factory->getRenderer($preview);
160 return $renderer !== null;
161 }

References $factory, $preview, ilRendererFactory\getRenderer(), and ilPreviewSettings\isPreviewEnabled().

Referenced by ilDclFileuploadRecordRepresentation\getHTML(), ilObjFileGUI\infoScreenForward(), and ilObjectListGUI\insertTitle().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ init()

ilPreview::init ( )
private

Initializes the preview object.

Definition at line 482 of file class.ilPreview.php.

482 : void
483 {
484 // read entry
485 $this->doRead();
486 }
doRead()
Read data from database.

References doRead().

Referenced by __construct().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ lookupRenderStatus()

static ilPreview::lookupRenderStatus ( int  $a_obj_id)
static

Gets the render status for the object with the specified id.

Parameters
int$a_obj_idThe id of the object to get the status for.
Returns
string The status of the rendering process.

Definition at line 169 of file class.ilPreview.php.

169 : string
170 {
171 $preview = new ilPreview($a_obj_id);
172 return $preview->getRenderStatus();
173 }

References $preview.

Referenced by ilDclFileuploadRecordRepresentation\getHTML(), and ilObjectListGUI\insertTitle().

+ Here is the caller graph for this function:

◆ save()

ilPreview::save ( )

Saves the preview data to the database.

Definition at line 296 of file class.ilPreview.php.

296 : void
297 {
298 if ($this->exists) {
299 $this->doUpdate();
300 } else {
301 $this->doCreate();
302 }
303 }
doUpdate()
Update data in database.
doCreate()
Create entry in database.

References doCreate(), doUpdate(), and exists().

Referenced by create().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setRenderDate()

ilPreview::setRenderDate ( string  $a_date)

Definition at line 440 of file class.ilPreview.php.

440 : void
441 {
442 $this->render_date = $a_date;
443 }

Referenced by doRead().

+ Here is the caller graph for this function:

◆ setRenderStatus()

ilPreview::setRenderStatus ( string  $a_status)

Sets the status of the rendering process.

Parameters
string$a_statusThe status to set.

Definition at line 460 of file class.ilPreview.php.

460 : void
461 {
462 $this->render_status = $a_status;
463 }

Referenced by doRead().

+ Here is the caller graph for this function:

Field Documentation

◆ $exists

bool ilPreview::$exists = false
private

Defines whether the preview exists.

Definition at line 56 of file class.ilPreview.php.

Referenced by exists().

◆ $factory

ilRendererFactory ilPreview::$factory
protected

Definition at line 67 of file class.ilPreview.php.

Referenced by create(), and hasPreview().

◆ $obj_id

int ilPreview::$obj_id = null
private

The object id.

Definition at line 41 of file class.ilPreview.php.

Referenced by getObjId().

◆ $obj_type

string ilPreview::$obj_type = null
private

The type of the object.

Definition at line 46 of file class.ilPreview.php.

Referenced by getObjType().

◆ $render_date

string ilPreview::$render_date = null
private

The timestamp when the preview was rendered.

Definition at line 61 of file class.ilPreview.php.

Referenced by getRenderDate().

◆ $render_status

string ilPreview::$render_status = self::RENDER_STATUS_NONE
private

The status of the rendering process.

Definition at line 66 of file class.ilPreview.php.

Referenced by getRenderStatus().

◆ $storage

ilFSStoragePreview ilPreview::$storage = null
private

The file storage instance.

Definition at line 51 of file class.ilPreview.php.

Referenced by getStorage().

◆ FILENAME_FORMAT

const ilPreview::FILENAME_FORMAT = "preview_%02d.jpg"
private

Definition at line 36 of file class.ilPreview.php.

Referenced by getFilePathFormat().

◆ RENDER_STATUS_CREATED

const ilPreview::RENDER_STATUS_CREATED = "created"

◆ RENDER_STATUS_FAILED

const ilPreview::RENDER_STATUS_FAILED = "failed"

◆ RENDER_STATUS_NONE

const ilPreview::RENDER_STATUS_NONE = "none"

◆ RENDER_STATUS_PENDING

const ilPreview::RENDER_STATUS_PENDING = "pending"

The documentation for this class was generated from the following file: