ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 require_once("./Services/Preview/classes/class.ilPreviewSettings.php");
5 require_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 
51  const CONTEXT_REPOSITORY = 1;
52  const CONTEXT_WORKSPACE = 2;
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 $ilCtrl, $lng, $ilAccess;
64 
65  // if we are the base class, get the id's from the query string
66  if (strtolower($_GET["baseClass"]) == "ilpreviewgui")
67  {
68  $this->node_id = (int)$_GET["node_id"];
69  $this->context = (int)$_GET["context"];
70  $a_obj_id = (int)$_GET['obj_id'];
71  }
72  else
73  {
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  {
85  if ($this->context == self::CONTEXT_WORKSPACE)
86  {
87  include_once("./Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php");
88  $a_access_handler = new ilWorkspaceAccessHandler();
89  }
90  else
91  {
92  $a_access_handler = $ilAccess;
93  }
94  }
95  $this->access_handler = $a_access_handler;
96 
97  // object id NOT provided?
98  if ($a_obj_id == null)
99  {
100  if ($this->context == self::CONTEXT_WORKSPACE)
101  $a_obj_id = $this->access_handler->getTree()->lookupObjectId($this->node_id);
102  else
103  $a_obj_id = ilObject::_lookupObjId($this->node_id);
104  }
105  $this->obj_id = $a_obj_id;
106 
107  // create preview object
108  $this->preview = new ilPreview($this->obj_id);
109 
110  // if the call is NOT async initialize our stuff
111  if (!$ilCtrl->isAsynch())
113  }
114 
118  function executeCommand()
119  {
120  $cmd = $this->ctrl->getCmd("getPreviewHTML");
121  $next_class = $this->ctrl->getNextClass($this);
122 
123  switch($next_class)
124  {
125  default:
126  return $this->$cmd();
127  break;
128  }
129  }
130 
136  public function getJSCall($a_html_id)
137  {
138  $status = $this->preview->getRenderStatus();
139  $command = $status == ilPreview::RENDER_STATUS_NONE ? "renderPreview" : "";
140  $loading_text = self::jsonSafeString($this->lng->txt($status == ilPreview::RENDER_STATUS_NONE ? "preview_status_creating" : "preview_loading"));
141 
142  // build the url
143  $link = $this->buildUrl($command);
144  return "il.Preview.toggle(event, { id: '{$this->node_id}', htmlId: '{$a_html_id}', url: '$link', status: '$status', loadingText: '$loading_text' });";
145  }
146 
151  public function getPreviewHTML()
152  {
153  require_once('./Services/WebAccessChecker/classes/class.ilWACSignedPath.php');
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  {
164  // preview images available?
165  $images = $this->preview->getImages();
166  if (count($images) > 0)
167  {
168  foreach ($images as $image)
169  {
170  $tmpl->setCurrentBlock("preview_item");
171  $tmpl->setVariable("IMG_URL", ilWACSignedPath::signFile($image["url"]));
172  $tmpl->setVariable("WIDTH", $image["width"]);
173  $tmpl->setVariable("HEIGHT", $image["height"]);
174  $tmpl->parseCurrentBlock();
175  }
176  }
177  else
178  {
179  // set text depending on the status
180  $tmpl->setCurrentBlock("no_preview");
181  switch ($preview_status)
182  {
184  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_pending"));
185  break;
186 
188  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_failed"));
189  break;
190 
191  default:
192  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_missing"));
193  break;
194  }
195  $tmpl->parseCurrentBlock();
196  }
197  }
198  else
199  {
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  {
207  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
208 
209  $response = new stdClass();
210  $response->html = $tmpl->get();
211  $response->status = $preview_status;
212 
213  // send response object (don't use 'application/json' as IE wants to download it!)
214  header('Vary: Accept');
215  header('Content-type: text/plain');
216  echo ilJsonUtil::encode($response);
217 
218  // no further processing!
219  exit;
220  }
221  else
222  {
223  return $tmpl->get();
224  }
225  }
226 
231  public function getInlineHTML()
232  {
233  $tmpl = new ilTemplate("tpl.preview_inline.html", true, true, "Services/Preview");
234  $tmpl->setVariable("PREVIEW", $this->getPreviewHTML());
235 
236  // rendering allowed?
237  if ($this->access_handler->checkAccess("read", "", $this->node_id))
238  {
239  $this->renderCommand(
240  $tmpl,
241  "render",
242  "preview_create",
243  "preview_status_creating",
245  }
246 
247  // delete allowed?
248  if ($this->access_handler->checkAccess("write", "", $this->node_id))
249  {
250  $this->renderCommand(
251  $tmpl,
252  "delete",
253  "preview_delete",
254  "preview_status_deleting",
256  }
257 
258  return $tmpl->get();
259  }
260 
269  private function renderCommand($tmpl, $a_cmd, $btn_topic, $loading_topic, $a_display_status)
270  {
271  $preview_html_id = $this->getHtmlId();
272  $preview_status = $this->preview->getRenderStatus();
273  $loading_text = self::jsonSafeString($this->lng->txt($loading_topic));
274 
275  $link = $this->buildUrl($a_cmd . "Preview");
276  $script_args = "event, { id: '{$this->node_id}', htmlId: '$preview_html_id', url: '$link', loadingText: '$loading_text' }";
277 
278  $action_class = "";
279  if (!is_array($a_display_status) || !in_array($preview_status, $a_display_status))
280  $action_class = "ilPreviewActionHidden";
281 
282  $tmpl->setCurrentBlock("preview_action");
283  $tmpl->setVariable("CLICK_ACTION", "il.Preview.$a_cmd($script_args);");
284  $tmpl->setVariable("ACTION_CLASS", "$action_class");
285  $tmpl->setVariable("ACTION_ID", "preview_{$a_cmd}_" . $preview_html_id);
286  $tmpl->setVariable("TXT_ACTION", $this->lng->txt($btn_topic));
287  $tmpl->parseCurrentBlock();
288  }
289 
294  public function renderPreview()
295  {
296  // has read access?
297  if ($this->access_handler->checkAccess("read", "", $this->node_id))
298  {
299  // get the object
300  $obj = ilObjectFactory::getInstanceByObjId($this->obj_id);
301  $this->preview->create($obj);
302  }
303 
304  return $this->getPreviewHTML();
305  }
306 
311  public function deletePreview()
312  {
313  // has read access?
314  if ($this->access_handler->checkAccess("write", "", $this->node_id))
315  {
316  // get the preview
317  require_once("./Services/Preview/classes/class.ilPreview.php");
318  $this->preview->delete();
319  }
320 
321  return $this->getPreviewHTML();
322  }
323 
328  private function getHtmlId()
329  {
330  return "preview_" . $this->node_id;
331  }
332 
339  private function buildUrl($a_cmd = "", $a_async = true)
340  {
341  $link = "ilias.php?baseClass=ilPreviewGUI&node_id={$this->node_id}&context={$this->context}&obj_id={$this->obj_id}";
342 
343  if ($a_async)
344  $link .= "&cmdMode=asynch";
345 
346  if (!empty($a_cmd))
347  $link .= "&cmd=$a_cmd";
348 
349  return $link;
350  }
351 
355  private static function initPreview()
356  {
357  if (self::$initialized)
358  return;
359 
360  global $tpl, $lng, $ilCtrl;
361 
362 
363  // jquery
364  include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
366 
367  // load qtip
368  include_once("./Services/UIComponent/Tooltip/classes/class.ilTooltipGUI.php");
370 
371  // needed scripts & styles
372  $tpl->addJavaScript("./Services/Preview/js/jquery.mousewheel.js");
373  $tpl->addJavaScript("./Services/Preview/js/ilPreview.js");
374  $tpl->addCss(ilUtil::getStyleSheetLocation("filesystem", "preview.css", "Services/Preview"));
375 
376  // create loading template
377  $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
378  $tmpl->setCurrentBlock("no_preview");
379  $tmpl->setVariable("TXT_NO_PREVIEW", "%%0%%");
380  $tmpl->parseCurrentBlock();
381 
382  $initialHtml = str_replace(array("\r\n", "\r"), "\n", $tmpl->get());
383  $lines = explode("\n", $initialHtml);
384  $new_lines = array();
385  foreach ($lines as $i => $line)
386  {
387  if(!empty($line))
388  $new_lines[] = trim($line);
389  }
390  $initialHtml = implode($new_lines);
391 
392  // add default texts and values
393  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
394  $tpl->addOnLoadCode("il.Preview.texts.preview = \"" . self::jsonSafeString($lng->txt("preview")) . "\";");
395  $tpl->addOnLoadCode("il.Preview.texts.showPreview = \"" . self::jsonSafeString($lng->txt("preview_show")) . "\";");
396  $tpl->addOnLoadCode("il.Preview.texts.close = \"" . ilUtil::prepareFormOutput($lng->txt("close")) . "\";");
397  $tpl->addOnLoadCode("il.Preview.previewSize = " . ilPreviewSettings::getImageSize() . ";");
398  $tpl->addOnLoadCode("il.Preview.initialHtml = " . ilJsonUtil::encode($initialHtml) . ";");
399  $tpl->addOnLoadCode("il.Preview.highlightClass = \"ilContainerListItemOuterHighlight\";");
400  $tpl->addOnLoadCode("il.Preview.init();");
401 
402  self::$initialized = true;
403  }
404 
411  private static function jsonSafeString($text)
412  {
413  if (!is_string($text))
414  return $text;
415 
416  $text = htmlentities($text, ENT_COMPAT | ENT_HTML401, "UTF-8");
417  $text = str_replace("'", "&#039;", $text);
418  return $text;
419  }
420 }
421 ?>
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
static getStyleSheetLocation($mode="output", $a_css_name="", $a_css_location="")
get full style sheet file name (path inclusive) of current user
$_GET["client_id"]
$cmd
Definition: sahs_server.php:35
const RENDER_STATUS_FAILED
Access handler for personal workspace.
const RENDER_STATUS_PENDING
const RENDER_STATUS_NONE
getPreviewHTML()
Gets the HTML that displays the preview.
renderPreview()
Renders the preview and returns the HTML code that displays the preview.
global $tpl
Definition: ilias.php:8
global $ilCtrl
Definition: ilias.php:18
static initLibrary()
Initializes the needed tooltip libraries.
buildUrl($a_cmd="", $a_async=true)
Builds the URL to call the preview GUI.
executeCommand()
execute command
getJSCall($a_html_id)
Gets the JavaScript code to show the preview.
static encode($mixed, $suppress_native=false)
static _lookupObjId($a_id)
special template class to simplify handling of ITX/PEAR
static initPreview()
Initializes the preview and loads the needed javascripts and styles.
Add a drawing to the header
Definition: 04printing.php:69
static signFile($path_to_file)
static getInstanceByObjId($a_obj_id, $stop_on_error=true)
get an instance of an Ilias object by object id
Create styles array
The data for the language used.
const RENDER_STATUS_CREATED
static getImageSize()
Gets the size of the preview images in pixels.
static jsonSafeString($text)
Makes the specified string safe for JSON.
__construct($a_node_id=null, $a_context=self::CONTEXT_REPOSITORY, $a_obj_id=null, $a_access_handler=null)
Creates a new preview GUI.
$text
getInlineHTML()
Gets the HTML that is used for displaying the preview inline.
static initjQuery($a_tpl=null)
Init jQuery.
renderCommand($tmpl, $a_cmd, $btn_topic, $loading_topic, $a_display_status)
Renders a command to the specified template.
getHtmlId()
Gets the HTML id for the preview.
deletePreview()
Deletes the preview and returns the HTML code that displays the preview.