ILIAS  release_8 Revision v8.24
class.ilPreview.php
Go to the documentation of this file.
1<?php
29{
30 // status values
31 public const RENDER_STATUS_NONE = "none";
32 public const RENDER_STATUS_PENDING = "pending";
33 public const RENDER_STATUS_CREATED = "created";
34 public const RENDER_STATUS_FAILED = "failed";
35
36 private const FILENAME_FORMAT = "preview_%02d.jpg";
37
41 private ?int $obj_id = null;
42
46 private ?string $obj_type = null;
47
52
56 private bool $exists = false;
57
61 private ?string $render_date = null;
62
68
75 public function __construct(int $a_obj_id, string $a_type = "")
76 {
77 $this->obj_id = $a_obj_id;
78 $this->obj_type = $a_type;
79 $this->factory = new ilRendererFactory();
80 $this->init();
81 }
82
90 public static function createPreview(\ilObject $a_obj, bool $a_force = false): bool
91 {
92 $preview = new ilPreview($a_obj->getId(), $a_obj->getType());
93 return $preview->create($a_obj, $a_force);
94 }
95
101 public static function deletePreview(int $a_obj_id): void
102 {
103 $preview = new ilPreview($a_obj_id);
104 $preview->delete();
105 }
106
113 public static function copyPreviews(int $a_src_id, int $a_dest_id): 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 }
140
148 public static function hasPreview(int $a_obj_id, string $a_type = ""): 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 }
162
169 public static function lookupRenderStatus(int $a_obj_id): string
170 {
171 $preview = new ilPreview($a_obj_id);
172 return $preview->getRenderStatus();
173 }
174
180 public function exists(): bool
181 {
182 return $this->exists;
183 }
184
192 public function create(\ilObject $a_obj, bool $a_force = false): 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 }
232
236 public function delete(): 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 }
250
256 public function getImages(): 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 }
292
296 public function save(): void
297 {
298 if ($this->exists) {
299 $this->doUpdate();
300 } else {
301 $this->doCreate();
302 }
303 }
304
308 protected function doCreate(): 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 }
323
327 protected function doRead(): 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 }
344
348 protected function doUpdate(): 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 }
362
366 protected function doDelete(): 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 }
377
383 public function getObjId(): ?int
384 {
385 return $this->obj_id;
386 }
387
393 public function getObjType(): 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 }
402
408 public function getStoragePath(): string
409 {
410 return $this->getStorage()->getPath();
411 }
412
418 public function getAbsoluteStoragePath(): string
419 {
420 return ILIAS_WEB_DIR . "/" . CLIENT_ID . "/{$this->getStorage()->getPath()}";
421 }
422
429 public function getFilePathFormat(): string
430 {
432 return $path . "/" . self::FILENAME_FORMAT;
433 }
434
435 public function getRenderDate(): ?string
436 {
437 return $this->render_date;
438 }
439
440 public function setRenderDate(string $a_date): void
441 {
442 $this->render_date = $a_date;
443 }
444
450 public function getRenderStatus(): string
451 {
453 }
454
460 public function setRenderStatus(string $a_status): void
461 {
462 $this->render_status = $a_status;
463 }
464
470 public function getStorage(): \ilFSStoragePreview
471 {
472 if ($this->storage === null) {
473 $this->storage = new ilFSStoragePreview($this->obj_id);
474 }
475
476 return $this->storage;
477 }
478
482 private function init(): void
483 {
484 // read entry
485 $this->doRead();
486 }
487}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static removeTrailingPathSeparators(string $path)
static rCopy(string $a_sdir, string $a_tdir, bool $preserveTimeAttributes=false)
Copies content of a directory $a_sdir recursively to a directory $a_tdir.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _lookupType(int $id, bool $reference=false)
getLastUpdateDate()
Get last update date in YYYY-MM-DD HH-MM-SS format.
static isPreviewEnabled()
Gets whether the preview functionality is enabled.
getFilePathFormat()
Gets the absolute file path for preview images that contains a placeholder in the file name ('%02d') ...
setRenderDate(string $a_date)
doUpdate()
Update data in database.
init()
Initializes the preview object.
ilRendererFactory $factory
bool $exists
Defines whether the preview exists.
ilFSStoragePreview $storage
The file storage instance.
static lookupRenderStatus(int $a_obj_id)
Gets the render status for the object with the specified id.
__construct(int $a_obj_id, string $a_type="")
Creates a new ilPreview.
create(\ilObject $a_obj, bool $a_force=false)
Creates the preview.
doRead()
Read data from database.
static hasPreview(int $a_obj_id, string $a_type="")
Determines whether the object with the specified reference id has a preview.
setRenderStatus(string $a_status)
Sets the status of the rendering process.
const RENDER_STATUS_NONE
static deletePreview(int $a_obj_id)
Deletes the preview for the object with the specified id.
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.
getStorage()
Gets the storage object for the preview.
string $render_status
The status of the rendering process.
getRenderStatus()
Gets the status of the rendering process.
getImages()
Gets an array of preview images.
doDelete()
Delete data from database.
string $render_date
The timestamp when the preview was rendered.
const RENDER_STATUS_FAILED
const RENDER_STATUS_CREATED
int $obj_id
The object id.
string $obj_type
The type of the object.
static copyPreviews(int $a_src_id, int $a_dest_id)
Copies the preview images from one preview to a new preview object.
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.
static createPreview(\ilObject $a_obj, bool $a_force=false)
Creates the preview for the object with the specified id.
const RENDER_STATUS_PENDING
const FILENAME_FORMAT
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getRenderer(\ilPreview $preview)
Gets the renderer that is able to create a preview for the specified preview object.
static getHtmlPath(string $relative_path)
get url of path
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45
global $DIC
Definition: feed.php:28
$preview
Definition: imgupload.php:81
$path
Definition: ltiservices.php:32