ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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  private $node_id = null;
18  private $obj_id = null;
19  private $preview = null;
20  private $access_handler = null;
21  private $context = null;
22  private $ctrl = null;
23  private $lng = null;
24 
25  private static $initialized = false;
26 
27  const CONTEXT_REPOSITORY = 1;
28  const CONTEXT_WORKSPACE = 2;
29 
37  public function __construct($a_node_id = null, $a_context = self::CONTEXT_REPOSITORY, $a_obj_id = null, $a_access_handler = null)
38  {
39  global $ilCtrl, $lng, $ilAccess;
40 
41  // if we are the base class, get the id's from the query string
42  if (strtolower($_GET["baseClass"]) == "ilpreviewgui")
43  {
44  $this->node_id = (int)$_GET["node_id"];
45  $this->context = (int)$_GET["context"];
46  }
47  else
48  {
49  $this->node_id = $a_node_id;
50  $this->context = $a_context;
51  }
52 
53  // assign values
54  $this->ctrl =& $ilCtrl;
55  $this->lng =& $lng;
56 
57  // access handler NOT provided?
58  if ($a_access_handler == null)
59  {
60  if ($this->context == self::CONTEXT_WORKSPACE)
61  {
62  include_once("./Services/PersonalWorkspace/classes/class.ilWorkspaceAccessHandler.php");
63  $a_access_handler = new ilWorkspaceAccessHandler();
64  }
65  else
66  {
67  $a_access_handler = $ilAccess;
68  }
69  }
70  $this->access_handler = $a_access_handler;
71 
72  // object id NOT provided?
73  if ($a_obj_id == null)
74  {
75  if ($this->context == self::CONTEXT_WORKSPACE)
76  $a_obj_id = $this->access_handler->getTree()->lookupObjectId($this->node_id);
77  else
78  $a_obj_id = ilObject::_lookupObjId($this->node_id);
79  }
80  $this->obj_id = $a_obj_id;
81 
82  // create preview object
83  $this->preview = new ilPreview($this->obj_id);
84 
85  // if the call is NOT async initialize our stuff
86  if (!$ilCtrl->isAsynch())
88  }
89 
93  function &executeCommand()
94  {
95  $cmd = $this->ctrl->getCmd("getPreviewHTML");
96  $next_class = $this->ctrl->getNextClass($this);
97 
98  switch($next_class)
99  {
100  default:
101  return $this->$cmd();
102  break;
103  }
104  }
105 
111  public function getJSCall($a_html_id)
112  {
113  $status = $this->preview->getRenderStatus();
114  $command = $status == ilPreview::RENDER_STATUS_NONE ? "renderPreview" : "";
115  $loading_text = self::jsonSafeString($this->lng->txt($status == ilPreview::RENDER_STATUS_NONE ? "preview_status_creating" : "preview_loading"));
116 
117  // build the url
118  $link = $this->buildUrl($command);
119  return "il.Preview.toggle(event, { id: '{$this->node_id}', htmlId: '{$a_html_id}', url: '$link', status: '$status', loadingText: '$loading_text' });";
120  }
121 
126  public function getPreviewHTML()
127  {
128  // load the template
129  $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
130  $tmpl->setVariable("PREVIEW_ID", $this->getHtmlId());
131 
132  // check for read access and get object id
133  $preview_status = $this->preview->getRenderStatus();
134 
135  // has read access?
136  if ($this->access_handler->checkAccess("read", "", $this->node_id))
137  {
138  // preview images available?
139  $images = $this->preview->getImages();
140  if (count($images) > 0)
141  {
142  foreach ($images as $image)
143  {
144  $tmpl->setCurrentBlock("preview_item");
145  $tmpl->setVariable("IMG_URL", $image["url"]);
146  $tmpl->setVariable("WIDTH", $image["width"]);
147  $tmpl->setVariable("HEIGHT", $image["height"]);
148  $tmpl->parseCurrentBlock();
149  }
150  }
151  else
152  {
153  // set text depending on the status
154  $tmpl->setCurrentBlock("no_preview");
155  switch ($preview_status)
156  {
158  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_pending"));
159  break;
160 
162  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_failed"));
163  break;
164 
165  default:
166  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("preview_status_missing"));
167  break;
168  }
169  $tmpl->parseCurrentBlock();
170  }
171  }
172  else
173  {
174  // display error message
175  $tmpl->setVariable("TXT_NO_PREVIEW", $this->lng->txt("no_access_item"));
176  }
177 
178  // output
179  if ($this->ctrl->isAsynch())
180  {
181  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
182 
183  $response = new stdClass();
184  $response->html = $tmpl->get();
185  $response->status = $preview_status;
186 
187  // send response object (don't use 'application/json' as IE wants to download it!)
188  header('Vary: Accept');
189  header('Content-type: text/plain');
190  echo ilJsonUtil::encode($response);
191 
192  // no further processing!
193  exit;
194  }
195  else
196  {
197  return $tmpl->get();
198  }
199  }
200 
205  public function getInlineHTML()
206  {
207  $tmpl = new ilTemplate("tpl.preview_inline.html", true, true, "Services/Preview");
208  $tmpl->setVariable("PREVIEW", $this->getPreviewHTML());
209 
210  // rendering allowed?
211  if ($this->access_handler->checkAccess("read", "", $this->node_id))
212  {
213  $this->renderCommand(
214  $tmpl,
215  "render",
216  "preview_create",
217  "preview_status_creating",
219  }
220 
221  // delete allowed?
222  if ($this->access_handler->checkAccess("write", "", $this->node_id))
223  {
224  $this->renderCommand(
225  $tmpl,
226  "delete",
227  "preview_delete",
228  "preview_status_deleting",
230  }
231 
232  return $tmpl->get();
233  }
234 
243  private function renderCommand($tmpl, $a_cmd, $btn_topic, $loading_topic, $a_display_status)
244  {
245  $preview_html_id = $this->getHtmlId();
246  $preview_status = $this->preview->getRenderStatus();
247  $loading_text = self::jsonSafeString($this->lng->txt($loading_topic));
248 
249  $link = $this->buildUrl($a_cmd . "Preview");
250  $script_args = "event, { id: '{$this->node_id}', htmlId: '$preview_html_id', url: '$link', loadingText: '$loading_text' }";
251 
252  $action_class = "";
253  if (!is_array($a_display_status) || !in_array($preview_status, $a_display_status))
254  $action_class = "ilPreviewActionHidden";
255 
256  $tmpl->setCurrentBlock("preview_action");
257  $tmpl->setVariable("CLICK_ACTION", "il.Preview.$a_cmd($script_args);");
258  $tmpl->setVariable("ACTION_CLASS", "$action_class");
259  $tmpl->setVariable("ACTION_ID", "preview_{$a_cmd}_" . $this->node_id);
260  $tmpl->setVariable("TXT_ACTION", $this->lng->txt($btn_topic));
261  $tmpl->parseCurrentBlock();
262  }
263 
268  public function renderPreview()
269  {
270  // has read access?
271  if ($this->access_handler->checkAccess("read", "", $this->node_id))
272  {
273  // get the object
274  $obj = ilObjectFactory::getInstanceByObjId($this->obj_id);
275  $this->preview->create($obj);
276  }
277 
278  return $this->getPreviewHTML();
279  }
280 
285  public function deletePreview()
286  {
287  // has read access?
288  if ($this->access_handler->checkAccess("write", "", $this->node_id))
289  {
290  // get the preview
291  require_once("./Services/Preview/classes/class.ilPreview.php");
292  $this->preview->delete();
293  }
294 
295  return $this->getPreviewHTML();
296  }
297 
302  private function getHtmlId()
303  {
304  return "preview_" . $this->node_id;
305  }
306 
313  private function buildUrl($a_cmd = "", $a_async = true)
314  {
315  $link = "ilias.php?baseClass=ilPreviewGUI&node_id={$this->node_id}&context={$this->context}";
316 
317  if ($a_async)
318  $link .= "&cmdMode=asynch";
319 
320  if (!empty($a_cmd))
321  $link .= "&cmd=$a_cmd";
322 
323  return $link;
324  }
325 
329  private static function initPreview()
330  {
331  if (self::$initialized)
332  return;
333 
334  global $tpl, $lng, $ilCtrl;
335 
336 
337  // jquery
338  include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
340 
341  // load qtip
342  include_once("./Services/UIComponent/Tooltip/classes/class.ilTooltipGUI.php");
344 
345  // needed scripts & styles
346  $tpl->addJavaScript("./Services/Preview/js/jquery.mousewheel.js");
347  $tpl->addJavaScript("./Services/Preview/js/ilPreview.js");
348  $tpl->addCss(ilUtil::getStyleSheetLocation("filesystem", "preview.css", "Services/Preview"));
349 
350  // create loading template
351  $tmpl = new ilTemplate("tpl.preview.html", true, true, "Services/Preview");
352  $tmpl->setCurrentBlock("no_preview");
353  $tmpl->setVariable("TXT_NO_PREVIEW", "%%0%%");
354  $tmpl->parseCurrentBlock();
355 
356  $initialHtml = str_replace(array("\r\n", "\r"), "\n", $tmpl->get());
357  $lines = explode("\n", $initialHtml);
358  $new_lines = array();
359  foreach ($lines as $i => $line)
360  {
361  if(!empty($line))
362  $new_lines[] = trim($line);
363  }
364  $initialHtml = implode($new_lines);
365 
366  // add default texts and values
367  include_once("./Services/JSON/classes/class.ilJsonUtil.php");
368  $tpl->addOnLoadCode("il.Preview.texts.preview = \"" . self::jsonSafeString($lng->txt("preview")) . "\";");
369  $tpl->addOnLoadCode("il.Preview.texts.showPreview = \"" . self::jsonSafeString($lng->txt("preview_show")) . "\";");
370  $tpl->addOnLoadCode("il.Preview.texts.close = \"" . self::jsonSafeString($lng->txt("close")) . "\";");
371  $tpl->addOnLoadCode("il.Preview.previewSize = " . ilPreviewSettings::getImageSize() . ";");
372  $tpl->addOnLoadCode("il.Preview.initialHtml = " . ilJsonUtil::encode($initialHtml) . ";");
373  $tpl->addOnLoadCode("il.Preview.highlightClass = \"ilContainerListItemOuterHighlight\";");
374  $tpl->addOnLoadCode("il.Preview.init();");
375 
376  self::$initialized = true;
377  }
378 
385  private static function jsonSafeString($text)
386  {
387  if (!is_string($text))
388  return $text;
389 
390  $text = htmlentities($text, ENT_COMPAT | ENT_HTML401, "UTF-8");
391  $text = str_replace("'", "&#039;", $text);
392  return $text;
393  }
394 }
395 ?>