ILIAS  release_8 Revision v8.24
class.ilPreviewGUI.php
Go to the documentation of this file.
1<?php
27{
28 private ?int $node_id = null;
29 private ?int $obj_id = null;
30 private ?\ilPreview $preview = null;
34 private ?object $access_handler = null;
35 private ?int $context = null;
36 private ?\ilCtrl $ctrl = null;
37 private ?\ilLanguage $lng = null;
38 private static bool $initialized = false;
39
40 public const CONTEXT_REPOSITORY = 1;
41 public const CONTEXT_WORKSPACE = 2;
42
50 public function __construct(
51 ?int $a_node_id = null,
52 ?int $a_context = self::CONTEXT_REPOSITORY,
53 ?int $a_obj_id = null,
54 ?object $a_access_handler = null
55 ) {
56 global $DIC;
57 // assign values
58 $this->ctrl = $DIC->ctrl();
59 $this->lng = $DIC->language();
60 $ilAccess = $DIC['ilAccess'];
61
62 $query = $DIC->http()->wrapper()->query();
63 $base_class = $query->has('baseClass')
64 ? $query->retrieve('baseClass', $DIC->refinery()->to()->string())
65 : null;
66
67 // if we are the base class, get the id's from the query string
68 if (strtolower($base_class) === strtolower(__CLASS__)) {
69 $this->node_id = $query->has('node_id')
70 ? $query->retrieve('node_id', $DIC->refinery()->kindlyTo()->int())
71 : 0;
72 $this->context = $query->has('context')
73 ? $query->retrieve('context', $DIC->refinery()->kindlyTo()->int())
75 $a_obj_id = $query->has('obj_id')
76 ? $query->retrieve('obj_id', $DIC->refinery()->kindlyTo()->int())
77 : null;
78 } else {
79 $this->node_id = $a_node_id;
80 $this->context = $a_context;
81 }
82
83
84
85 // access handler NOT provided?
86 if ($a_access_handler === null) {
87 if ($this->context === self::CONTEXT_WORKSPACE) {
88 $a_access_handler = new ilWorkspaceAccessHandler();
89 } else {
90 $a_access_handler = $ilAccess;
91 }
92 }
93 $this->access_handler = $a_access_handler;
94
95 // object id NOT provided?
96 if ($a_obj_id === null) {
97 if ($this->context === self::CONTEXT_WORKSPACE) {
98 $a_obj_id = $this->access_handler->getTree()->lookupObjectId($this->node_id);
99 } else {
100 $a_obj_id = ilObject::_lookupObjId($this->node_id);
101 }
102 }
103 $this->obj_id = $a_obj_id;
104
105 // create preview object
106 $this->preview = new ilPreview($this->obj_id);
107
108 // if the call is NOT async initialize our stuff
109 if (!$this->ctrl->isAsynch()) {
111 }
112 }
113
114
118 public function executeCommand()
119 {
120 $cmd = $this->ctrl->getCmd("getPreviewHTML");
121 $next_class = $this->ctrl->getNextClass($this);
122
123 switch ($next_class) {
124 default:
125 return $this->$cmd();
126 break;
127 }
128 }
129
135 public function getJSCall(string $a_html_id): string
136 {
137 $status = $this->preview->getRenderStatus();
138 $command = $status === ilPreview::RENDER_STATUS_NONE ? "renderPreview" : "";
139 $loading_text = self::jsonSafeString(
140 $this->lng->txt($status === ilPreview::RENDER_STATUS_NONE ? "preview_status_creating" : "preview_loading")
141 );
142
143 // build the url
144 $link = $this->buildUrl($command);
145 return "il.Preview.toggle(event, { id: '{$this->node_id}', htmlId: '{$a_html_id}', url: '$link', status: '$status', loadingText: '$loading_text' });";
146 }
147
152 public function getPreviewHTML(): string
153 {
154 // load the template
155 $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
156 $tmpl->setVariable("PREVIEW_ID", $this->getHtmlId());
157
158 // check for read access and get object id
159 $preview_status = $this->preview->getRenderStatus();
160
161 // has read access?
162 if ($this->access_handler->checkAccess("read", "", $this->node_id)) {
163 // preview images available?
164 $images = $this->preview->getImages();
165 if (count($images) > 0) {
166 $title = ilObject2::_lookupTitle($this->obj_id);
167 $index = 1;
168 foreach ($images as $image) {
169 $tmpl->setCurrentBlock("preview_item");
170 $tmpl->setVariable("IMG_URL", ilWACSignedPath::signFile($image["url"]));
171 $tmpl->setVariable("WIDTH", $image["width"]);
172 $tmpl->setVariable("HEIGHT", $image["height"]);
173 $tmpl->setVariable("ALT_TEXT", sprintf(
174 $this->lng->txt('preview_caption'),
175 (string) $index . ' ',
176 $title
177 ));
178 $tmpl->parseCurrentBlock();
179 $index++;
180 }
181 } else {
182 // set text depending on the status
183 $tmpl->setCurrentBlock("no_preview");
184 switch ($preview_status) {
186 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_pending"));
187 break;
188
190 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_failed"));
191 break;
192
193 default:
194 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_missing"));
195 break;
196 }
197 $tmpl->parseCurrentBlock();
198 }
199 } else {
200 // display error message
201 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("no_access_item"));
202 }
203
204 // output
205 if ($this->ctrl->isAsynch()) {
206 $response = new stdClass();
207 $response->html = $tmpl->get();
208 $response->status = $preview_status;
209
210 // send response object (don't use 'application/json' as IE wants to download it!)
211 header('Vary: Accept');
212 header('Content-type: text/plain');
213 echo json_encode($response, JSON_THROW_ON_ERROR);
214
215 // no further processing!
216 exit;
217 }
218
219 return $tmpl->get();
220 }
221
226 public function getInlineHTML(): string
227 {
228 $tmpl = new ilTemplate("tpl.preview_inline.html", true, true, "Services/Preview");
229 $tmpl->setVariable("PREVIEW", $this->getPreviewHTML());
230
231 // rendering allowed?
232 if ($this->access_handler->checkAccess("read", "", $this->node_id)) {
233 $this->renderCommand(
234 $tmpl,
235 "render",
236 "preview_create",
237 "preview_status_creating",
239 );
240 }
241
242 // delete allowed?
243 if ($this->access_handler->checkAccess("write", "", $this->node_id)) {
244 $this->renderCommand(
245 $tmpl,
246 "delete",
247 "preview_delete",
248 "preview_status_deleting",
250 );
251 }
252
253 return $tmpl->get();
254 }
255
264 private function renderCommand(ilTemplate $tmpl, string $a_cmd, string $btn_topic, string $loading_topic, array $a_display_status): void
265 {
266 $preview_html_id = $this->getHtmlId();
267 $preview_status = $this->preview->getRenderStatus();
268 $loading_text = self::jsonSafeString($this->lng->txt($loading_topic));
269
270 $link = $this->buildUrl($a_cmd . "Preview");
271 $script_args = "event, { id: '{$this->node_id}', htmlId: '$preview_html_id', url: '$link', loadingText: '$loading_text' }";
272
273 $action_class = "";
274 if (!in_array($preview_status, $a_display_status, true)) {
275 $action_class = "ilPreviewActionHidden";
276 }
277
278 $tmpl->setCurrentBlock("preview_action");
279 $tmpl->setVariable("CLICK_ACTION", "il.Preview.$a_cmd($script_args);");
280 $tmpl->setVariable("ACTION_CLASS", $action_class);
281 $tmpl->setVariable("ACTION_ID", "preview_{$a_cmd}_" . $preview_html_id);
282 $tmpl->setVariable("TXT_ACTION", $this->lng->txt($btn_topic));
283 $tmpl->parseCurrentBlock();
284 }
285
290 public function renderPreview(): string
291 {
292 // has read access?
293 if ($this->access_handler->checkAccess("read", "", $this->node_id)) {
294 // get the object
295 $obj = ilObjectFactory::getInstanceByObjId($this->obj_id);
296 $this->preview->create($obj);
297 }
298
299 return $this->getPreviewHTML();
300 }
301
306 public function deletePreview(): string
307 {
308 // has read access?
309 if ($this->access_handler->checkAccess("write", "", $this->node_id)) {
310 $this->preview->delete();
311 }
312
313 return $this->getPreviewHTML();
314 }
315
320 private function getHtmlId(): string
321 {
322 return "preview_" . $this->node_id;
323 }
324
331 private function buildUrl(string $a_cmd = "", bool $a_async = true): string
332 {
333 $link = "ilias.php?baseClass=ilPreviewGUI&node_id={$this->node_id}&context={$this->context}&obj_id={$this->obj_id}";
334
335 if ($a_async) {
336 $link .= "&cmdMode=asynch";
337 }
338
339 if (!empty($a_cmd)) {
340 $link .= "&cmd=$a_cmd";
341 }
342
343 return $link;
344 }
345
346
350 public static function initPreview(): void
351 {
352 if (self::$initialized) {
353 return;
354 }
355
356 global $DIC;
357 // jquery
359
360 // load qtip
362
363 // needed scripts & styles
364 $DIC->ui()->mainTemplate()->addJavaScript("./libs/bower/bower_components/jquery-mousewheel/jquery.mousewheel.js");
365 $DIC->ui()->mainTemplate()->addJavaScript("./Services/Preview/js/ilPreview.js");
366
367 // create loading template
368 $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
369 $tmpl->setCurrentBlock("no_preview");
370 $tmpl->setVariable("TXT_NO_PREVIEW", "%%0%%");
371 $tmpl->parseCurrentBlock();
372
373 $initialHtml = str_replace(array("\r\n", "\r"), "\n", $tmpl->get());
374 $lines = explode("\n", $initialHtml);
375 $new_lines = array();
376 foreach ($lines as $i => $line) {
377 if (!empty($line)) {
378 $new_lines[] = trim($line);
379 }
380 }
381 $initialHtml = implode($new_lines);
382
383 // add default texts and values
384 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.texts.preview = \"" . self::jsonSafeString($DIC->language()->txt("preview")) . "\";");
385 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.texts.showPreview = \"" . self::jsonSafeString($DIC->language()->txt("preview_show"))
386 . "\";");
387 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.texts.close = \"" . ilLegacyFormElementsUtil::prepareFormOutput(
388 $DIC->language()->txt("close")
389 ) . "\";");
390 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.previewSize = " . ilPreviewSettings::getImageSize() . ";");
391 $DIC->ui()->mainTemplate()->addOnLoadCode(
392 "il.Preview.initialHtml = " . json_encode($initialHtml, JSON_THROW_ON_ERROR) . ";"
393 );
394 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.highlightClass = \"ilContainerListItemOuterHighlight\";");
395 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.init();");
396
397 self::$initialized = true;
398 }
399
406 private static function jsonSafeString(string $text): string
407 {
408 if (!is_string($text)) {
409 return $text;
410 }
411
412 $text = htmlentities($text, ENT_COMPAT | ENT_HTML401, "UTF-8");
413 $text = str_replace("'", "&#039;", $text);
414 return $text;
415 }
416}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
Class ilCtrl provides processing control methods.
language handling
static prepareFormOutput($a_str, bool $a_strip=false)
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupObjId(int $ref_id)
static _lookupTitle(int $obj_id)
getInlineHTML()
Gets the HTML that is used for displaying the preview inline.
renderPreview()
Renders the preview and returns the HTML code that displays the preview.
static bool $initialized
deletePreview()
Deletes the preview and returns the HTML code that displays the preview.
__construct(?int $a_node_id=null, ?int $a_context=self::CONTEXT_REPOSITORY, ?int $a_obj_id=null, ?object $a_access_handler=null)
Creates a new preview GUI.
getHtmlId()
Gets the HTML id for the preview.
getPreviewHTML()
Gets the HTML that displays the preview.
renderCommand(ilTemplate $tmpl, string $a_cmd, string $btn_topic, string $loading_topic, array $a_display_status)
Renders a command to the specified template.
executeCommand()
execute command
getJSCall(string $a_html_id)
Gets the JavaScript code to show the preview.
static initPreview()
Initializes the preview and loads the needed javascripts and styles.
static jsonSafeString(string $text)
Makes the specified string safe for JSON.
buildUrl(string $a_cmd="", bool $a_async=true)
Builds the URL to call the preview GUI.
static getImageSize()
Gets the size of the preview images in pixels.
const RENDER_STATUS_NONE
const RENDER_STATUS_FAILED
const RENDER_STATUS_CREATED
const RENDER_STATUS_PENDING
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static init()
Initializes the needed tooltip libraries.
static signFile(string $path_to_file)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static initjQuery(ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
global $DIC
Definition: feed.php:28
Interface ilCtrlBaseClassInterface describes ilCtrl base classes.
exit
Definition: login.php:28
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
$query
$response