ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilPreviewGUI.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.ilPreview.php");
6
16{
17
21 private $node_id = null;
25 private $obj_id = null;
29 private $preview = null;
33 private $access_handler = null;
37 private $context = null;
41 private $ctrl = null;
45 private $lng = null;
49 private static $initialized = false;
50
53
61 public function __construct($a_node_id = null, $a_context = self::CONTEXT_REPOSITORY, $a_obj_id = null, $a_access_handler = null)
62 {
63 global $DIC;
64 $ilCtrl = $DIC['ilCtrl'];
65 $lng = $DIC['lng'];
66 $ilAccess = $DIC['ilAccess'];
67
68 // if we are the base class, get the id's from the query string
69 if (strtolower($_GET["baseClass"]) == "ilpreviewgui") {
70 $this->node_id = (int) $_GET["node_id"];
71 $this->context = (int) $_GET["context"];
72 $a_obj_id = (int) $_GET['obj_id'];
73 } else {
74 $this->node_id = $a_node_id;
75 $this->context = $a_context;
76 }
77
78 // assign values
79 $this->ctrl = $ilCtrl;
80 $this->lng = $lng;
81
82 // access handler NOT provided?
83 if ($a_access_handler == null) {
84 if ($this->context == self::CONTEXT_WORKSPACE) {
85 include_once("./Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php");
86 $a_access_handler = new ilWorkspaceAccessHandler();
87 } else {
88 $a_access_handler = $ilAccess;
89 }
90 }
91 $this->access_handler = $a_access_handler;
92
93 // object id NOT provided?
94 if ($a_obj_id == null) {
95 if ($this->context == self::CONTEXT_WORKSPACE) {
96 $a_obj_id = $this->access_handler->getTree()->lookupObjectId($this->node_id);
97 } else {
98 $a_obj_id = ilObject::_lookupObjId($this->node_id);
99 }
100 }
101 $this->obj_id = $a_obj_id;
102
103 // create preview object
104 $this->preview = new ilPreview($this->obj_id);
105
106 // if the call is NOT async initialize our stuff
107 if (!$ilCtrl->isAsynch()) {
109 }
110 }
111
112
116 public function executeCommand()
117 {
118 $cmd = $this->ctrl->getCmd("getPreviewHTML");
119 $next_class = $this->ctrl->getNextClass($this);
120
121 switch ($next_class) {
122 default:
123 return $this->$cmd();
124 break;
125 }
126 }
127
133 public function getJSCall($a_html_id)
134 {
135 $status = $this->preview->getRenderStatus();
136 $command = $status == ilPreview::RENDER_STATUS_NONE ? "renderPreview" : "";
137 $loading_text = self::jsonSafeString($this->lng->txt($status == ilPreview::RENDER_STATUS_NONE ? "preview_status_creating" : "preview_loading"));
138
139 // build the url
140 $link = $this->buildUrl($command);
141 return "il.Preview.toggle(event, { id: '{$this->node_id}', htmlId: '{$a_html_id}', url: '$link', status: '$status', loadingText: '$loading_text' });";
142 }
143
148 public function getPreviewHTML()
149 {
150 require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
151 // load the template
152 $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
153 $tmpl->setVariable("PREVIEW_ID", $this->getHtmlId());
154
155 // check for read access and get object id
156 $preview_status = $this->preview->getRenderStatus();
157
158 // has read access?
159 if ($this->access_handler->checkAccess("read", "", $this->node_id)) {
160 // preview images available?
161 $images = $this->preview->getImages();
162 if (count($images) > 0) {
163 foreach ($images as $image) {
164 $tmpl->setCurrentBlock("preview_item");
165 $tmpl->setVariable("IMG_URL", ilWACSignedPath::signFile($image["url"]));
166 $tmpl->setVariable("WIDTH", $image["width"]);
167 $tmpl->setVariable("HEIGHT", $image["height"]);
168 $tmpl->parseCurrentBlock();
169 }
170 } else {
171 // set text depending on the status
172 $tmpl->setCurrentBlock("no_preview");
173 switch ($preview_status) {
175 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_pending"));
176 break;
177
179 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_failed"));
180 break;
181
182 default:
183 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_missing"));
184 break;
185 }
186 $tmpl->parseCurrentBlock();
187 }
188 } else {
189 // display error message
190 $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("no_access_item"));
191 }
192
193 // output
194 if ($this->ctrl->isAsynch()) {
195 include_once("./Services/JSON/classes/class.ilJsonUtil.php");
196
197 $response = new stdClass();
198 $response->html = $tmpl->get();
199 $response->status = $preview_status;
200
201 // send response object (don't use 'application/json' as IE wants to download it!)
202 header('Vary: Accept');
203 header('Content-type: text/plain');
205
206 // no further processing!
207 exit;
208 } else {
209 return $tmpl->get();
210 }
211 }
212
217 public function getInlineHTML()
218 {
219 $tmpl = new ilTemplate("tpl.preview_inline.html", true, true, "Services/Preview");
220 $tmpl->setVariable("PREVIEW", $this->getPreviewHTML());
221
222 // rendering allowed?
223 if ($this->access_handler->checkAccess("read", "", $this->node_id)) {
224 $this->renderCommand(
225 $tmpl,
226 "render",
227 "preview_create",
228 "preview_status_creating",
230 );
231 }
232
233 // delete allowed?
234 if ($this->access_handler->checkAccess("write", "", $this->node_id)) {
235 $this->renderCommand(
236 $tmpl,
237 "delete",
238 "preview_delete",
239 "preview_status_deleting",
241 );
242 }
243
244 return $tmpl->get();
245 }
246
255 private function renderCommand($tmpl, $a_cmd, $btn_topic, $loading_topic, $a_display_status)
256 {
257 $preview_html_id = $this->getHtmlId();
258 $preview_status = $this->preview->getRenderStatus();
259 $loading_text = self::jsonSafeString($this->lng->txt($loading_topic));
260
261 $link = $this->buildUrl($a_cmd . "Preview");
262 $script_args = "event, { id: '{$this->node_id}', htmlId: '$preview_html_id', url: '$link', loadingText: '$loading_text' }";
263
264 $action_class = "";
265 if (!is_array($a_display_status) || !in_array($preview_status, $a_display_status)) {
266 $action_class = "ilPreviewActionHidden";
267 }
268
269 $tmpl->setCurrentBlock("preview_action");
270 $tmpl->setVariable("CLICK_ACTION", "il.Preview.$a_cmd($script_args);");
271 $tmpl->setVariable("ACTION_CLASS", "$action_class");
272 $tmpl->setVariable("ACTION_ID", "preview_{$a_cmd}_" . $preview_html_id);
273 $tmpl->setVariable("TXT_ACTION", $this->lng->txt($btn_topic));
274 $tmpl->parseCurrentBlock();
275 }
276
281 public function renderPreview()
282 {
283 // has read access?
284 if ($this->access_handler->checkAccess("read", "", $this->node_id)) {
285 // get the object
286 $obj = ilObjectFactory::getInstanceByObjId($this->obj_id);
287 $this->preview->create($obj);
288 }
289
290 return $this->getPreviewHTML();
291 }
292
297 public function deletePreview()
298 {
299 // has read access?
300 if ($this->access_handler->checkAccess("write", "", $this->node_id)) {
301 // get the preview
302 require_once("./Services/Preview/classes/class.ilPreview.php");
303 $this->preview->delete();
304 }
305
306 return $this->getPreviewHTML();
307 }
308
313 private function getHtmlId()
314 {
315 return "preview_" . $this->node_id;
316 }
317
324 private function buildUrl($a_cmd = "", $a_async = true)
325 {
326 $link = "ilias.php?baseClass=ilPreviewGUI&node_id={$this->node_id}&context={$this->context}&obj_id={$this->obj_id}";
327
328 if ($a_async) {
329 $link .= "&cmdMode=asynch";
330 }
331
332 if (!empty($a_cmd)) {
333 $link .= "&cmd=$a_cmd";
334 }
335
336 return $link;
337 }
338
339
343 public static function initPreview()
344 {
345 if (self::$initialized) {
346 return;
347 }
348
349 global $DIC;
350 // jquery
352
353 // load qtip
355
356 // needed scripts & styles
357 $DIC->ui()->mainTemplate()->addJavaScript("./libs/bower/bower_components/jquery-mousewheel/jquery.mousewheel.js");
358 $DIC->ui()->mainTemplate()->addJavaScript("./Services/Preview/js/ilPreview.js");
359 $DIC->ui()->mainTemplate()->addCss(ilUtil::getStyleSheetLocation("filesystem", "preview.css", "Services/Preview"));
360
361 // create loading template
362 $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
363 $tmpl->setCurrentBlock("no_preview");
364 $tmpl->setVariable("TXT_NO_PREVIEW", "%%0%%");
365 $tmpl->parseCurrentBlock();
366
367 $initialHtml = str_replace(array("\r\n", "\r"), "\n", $tmpl->get());
368 $lines = explode("\n", $initialHtml);
369 $new_lines = array();
370 foreach ($lines as $i => $line) {
371 if (!empty($line)) {
372 $new_lines[] = trim($line);
373 }
374 }
375 $initialHtml = implode($new_lines);
376
377 // add default texts and values
378 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.texts.preview = \"" . self::jsonSafeString($DIC->language()->txt("preview")) . "\";");
379 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.texts.showPreview = \"" . self::jsonSafeString($DIC->language()->txt("preview_show"))
380 . "\";");
381 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.texts.close = \"" . ilUtil::prepareFormOutput($DIC->language()->txt("close")) . "\";");
382 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.previewSize = " . ilPreviewSettings::getImageSize() . ";");
383 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.initialHtml = " . json_encode($initialHtml) . ";");
384 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.highlightClass = \"ilContainerListItemOuterHighlight\";");
385 $DIC->ui()->mainTemplate()->addOnLoadCode("il.Preview.init();");
386
387 self::$initialized = true;
388 }
389
396 private static function jsonSafeString($text)
397 {
398 if (!is_string($text)) {
399 return $text;
400 }
401
402 $text = htmlentities($text, ENT_COMPAT | ENT_HTML401, "UTF-8");
403 $text = str_replace("'", "&#039;", $text);
404 return $text;
405 }
406}
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
static encode($mixed, $suppress_native=false)
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
static _lookupObjId($a_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 jsonSafeString($text)
Makes the specified string safe for JSON.
deletePreview()
Deletes the preview and returns the HTML code that displays the preview.
getHtmlId()
Gets the HTML id for the preview.
getPreviewHTML()
Gets the HTML that displays the preview.
executeCommand()
execute command
static initPreview()
Initializes the preview and loads the needed javascripts and styles.
__construct($a_node_id=null, $a_context=self::CONTEXT_REPOSITORY, $a_obj_id=null, $a_access_handler=null)
Creates a new preview GUI.
getJSCall($a_html_id)
Gets the JavaScript code to show the preview.
renderCommand($tmpl, $a_cmd, $btn_topic, $loading_topic, $a_display_status)
Renders a command to the specified template.
buildUrl($a_cmd="", $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
static init()
Initializes the needed tooltip libraries.
static getStyleSheetLocation($mode="output", $a_css_name="", $a_css_location="")
get full style sheet file name (path inclusive) of current user
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
static signFile($path_to_file)
Access handler for personal workspace.
static initjQuery($a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
$i
Definition: disco.tpl.php:19
global $ilCtrl
Definition: ilias.php:18
$response
global $DIC
Definition: saml.php:7
$text
Definition: errorreport.php:18