ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
class.ilExplorerBaseGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
16 abstract class ilExplorerBaseGUI
17 {
18  protected static $js_tree_path = "./Services/UIComponent/Explorer2/lib/jstree-v.pre1.0/jquery.jstree.js";
19  protected static $js_expl_path = "./Services/UIComponent/Explorer2/js/Explorer2.js";
20  protected $skip_root_node = false;
21  protected $ajax = false;
22  protected $custom_open_nodes = array();
23  protected $selected_nodes = array();
24  protected $select_postvar = "";
25  protected $offline_mode = false;
26  protected $sec_highl_nodes = array();
27 
31  public function __construct($a_expl_id, $a_parent_obj, $a_parent_cmd)
32  {
33  $this->id = $a_expl_id;
34  $this->parent_obj = $a_parent_obj;
35  $this->parent_cmd = $a_parent_cmd;
36 
37  // get open nodes
38  include_once("./Services/Authentication/classes/class.ilSessionIStorage.php");
39  $this->store = new ilSessionIStorage("expl2");
40  $open_nodes = $this->store->get("on_".$this->id);
41  $this->open_nodes = unserialize($open_nodes);
42  if (!is_array($this->open_nodes))
43  {
44  $this->open_nodes = array();
45  }
46 
47  }
48 
52  static function getLocalExplorerJsPath()
53  {
54  return self::$js_expl_path;
55  }
56 
60  static function getLocalJsTreeJsPath()
61  {
62  return self::$js_tree_path;
63  }
64 
70  static function createHTMLExportDirs($a_target_dir)
71  {
72  ilUtil::makeDirParents($a_target_dir."/Services/UIComponent/Explorer2/lib/jstree-v.pre1.0");
73  ilUtil::makeDirParents($a_target_dir."/Services/UIComponent/Explorer2/js");
74  }
75 
76 
77 
78  //
79  // Abstract functions that need to be overwritten in derived classes
80  //
81 
90  abstract function getRootNode();
91 
98  abstract function getChildsOfNode($a_parent_node_id);
99 
106  abstract function getNodeContent($a_node);
107 
114  abstract function getNodeId($a_node);
115 
116 
117  //
118  // Functions with standard implementations that may be overwritten
119  //
120 
127  function getNodeHref($a_node)
128  {
129  return "#";
130  }
131 
141  function nodeHasVisibleChilds($a_node)
142  {
143  $childs = $this->getChildsOfNode($this->getNodeId($a_node));
144 
145  foreach ($childs as $child)
146  {
147  if ($this->isNodeVisible($child))
148  {
149  return true;
150  }
151  }
152  return false;
153  }
154 
163  function sortChilds($a_childs, $a_parent_node_id)
164  {
165  return $a_childs;
166  }
167 
174  function getNodeIcon($a_node)
175  {
176  return "";
177  }
178 
185  function getNodeIconAlt($a_node)
186  {
187  return "";
188  }
189 
196  function getNodeTarget($a_node)
197  {
198  return "";
199  }
200 
207  function getNodeOnClick($a_node)
208  {
209  if ($this->select_postvar != "")
210  {
211  return $this->getSelectOnClick($a_node);
212  }
213  return "";
214  }
215 
222  function isNodeVisible($a_node)
223  {
224  return true;
225  }
226 
233  function isNodeHighlighted($a_node)
234  {
235  return false;
236  }
237 
244  function isNodeClickable($a_node)
245  {
246  return true;
247  }
248 
249 
250  //
251  // Basic configuration / setter/getter
252  //
253 
259  function getId()
260  {
261  return $this->id;
262  }
263 
271  function setSkipRootNode($a_val)
272  {
273  $this->skip_root_node = $a_val;
274  }
275 
281  function getSkipRootNode()
282  {
283  return $this->skip_root_node;
284  }
285 
291  function setAjax($a_val)
292  {
293  $this->ajax = $a_val;
294  }
295 
301  function getAjax()
302  {
303  return $this->ajax;
304  }
305 
312  {
313  $this->sec_highl_nodes = $a_val;
314  }
315 
322  {
323  return $this->sec_highl_nodes;
324  }
325 
332  function setNodeOpen($a_id)
333  {
334  if (!in_array($a_id, $this->custom_open_nodes))
335  {
336  $this->custom_open_nodes[] = $a_id;
337  }
338  }
339 
346  final protected function getNodeToggleOnClick($a_node)
347  {
348  return "$('#".$this->getContainerId()."').jstree('toggle_node' , '#".
349  $this->getDomNodeIdForNodeId($this->getNodeId($a_node))."'); return false;";
350  }
351 
358  final protected function getSelectOnClick($a_node)
359  {
360  $dn_id = $this->getDomNodeIdForNodeId($this->getNodeId($a_node));
361  $oc = "il.Explorer2.selectOnClick('".$dn_id."'); return false;";
362  return $oc;
363  }
364 
372  function setSelectMode($a_postvar, $a_multi = false)
373  {
374  $this->select_postvar = $a_postvar;
375  $this->select_multi = $a_multi;
376  }
377 
384  function setNodeSelected($a_id)
385  {
386  if (!in_array($a_id, $this->selected_nodes))
387  {
388  $this->selected_nodes[] = $a_id;
389  }
390  }
391 
397  function setOfflineMode($a_val)
398  {
399  $this->offline_mode = $a_val;
400  }
401 
407  function getOfflineMode()
408  {
409  return $this->offline_mode;
410  }
411 
412  //
413  // Standard functions that usually are not overwritten / internal use
414  //
415 
421  function handleCommand()
422  {
423  if ($_GET["exp_cmd"] != "" &&
424  $_GET["exp_cont"] == $this->getContainerId())
425  {
426  $cmd = $_GET["exp_cmd"];
427  if (in_array($cmd, array("openNode", "closeNode", "getNodeAsync")))
428  {
429  $this->$cmd();
430  }
431 
432  return true;
433  }
434  return false;
435  }
436 
443  function getContainerId()
444  {
445  return "il_expl2_jstree_cont_".$this->getId();
446  }
447 
451  function openNode()
452  {
453  global $ilLog;
454 
455  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
456  if (!in_array($id, $this->open_nodes))
457  {
458  $this->open_nodes[] = $id;
459  }
460  $this->store->set("on_".$this->id, serialize($this->open_nodes));
461  exit;
462  }
463 
467  function closeNode()
468  {
469  global $ilLog;
470 
471  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
472  if (in_array($id, $this->open_nodes))
473  {
474  $k = array_search($id, $this->open_nodes);
475  unset($this->open_nodes[$k]);
476  }
477  $this->store->set("on_".$this->id, serialize($this->open_nodes));
478  exit;
479  }
480 
484  function getNodeAsync()
485  {
486  $this->beforeRendering();
487 
488  if ($_GET["node_id"] != "")
489  {
490  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
491  }
492  else
493  {
494  $id = $this->getNodeId($this->getRootNode());
495  }
496 
497  $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
498  $this->renderChilds($id, $etpl);
499  echo $etpl->get("tag");
500  exit;
501  }
502 
506  function beforeRendering()
507  {
508 
509  }
510 
517  function getOnLoadCode()
518  {
519  global $ilCtrl;
520 
521  $container_id = $this->getContainerId();
522  $container_outer_id = "il_expl2_jstree_cont_out_".$this->getId();
523 
524  // collect open nodes
525  $open_nodes = array($this->getDomNodeIdForNodeId($this->getNodeId($this->getRootNode())));
526  foreach ($this->open_nodes as $nid)
527  {
528  $open_nodes[] = $this->getDomNodeIdForNodeId($nid);
529  }
530  foreach ($this->custom_open_nodes as $nid)
531  {
532  $dnode = $this->getDomNodeIdForNodeId($nid);
533  if (!in_array($dnode, $open_nodes))
534  {
535  $open_nodes[] = $dnode;
536  }
537  }
538 
539  // ilias config options
540  $url = "";
541  if (!$this->getOfflineMode())
542  {
543  if (is_object($this->parent_obj))
544  {
545  $url = $ilCtrl->getLinkTarget($this->parent_obj, $this->parent_cmd, "", true);
546  }
547  else
548  {
549  $url = $ilCtrl->getLinkTargetByClass($this->parent_obj, $this->parent_cmd, "", true);
550  }
551  }
552 
553  // secondary highlighted nodes
554  $shn = array();
555  foreach ($this->sec_highl_nodes as $sh)
556  {
557  $shn[] = $this->getDomNodeIdForNodeId($sh);
558  }
559  $config = array(
560  "container_id" => $container_id,
561  "container_outer_id" => $container_outer_id,
562  "url" => $url,
563  "second_hnodes" => $shn,
564  "ajax" => $this->getAjax(),
565  );
566 
567 
568  // jstree config options
569  $js_tree_config = array(
570  "core" => array(
571  "animation" => 300,
572  "initially_open" => $open_nodes,
573  "open_parents" => false,
574  "strings" => array("loading" => "Loading ...", "new_node" => "New node")
575  ),
576  "plugins" => array("html_data", "themes"),
577  "themes" => array("dots" => false, "icons" => false, "theme" => ""),
578  "html_data" => array()
579  );
580 
581  return 'il.Explorer2.init('.json_encode($config).', '.json_encode($js_tree_config).');';
582  }
583 
584 
588  static function init()
589  {
590  global $tpl;
591 
592  include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
594 
595  $tpl->addJavascript(self::getLocalExplorerJsPath());
596  $tpl->addJavascript(self::getLocalJsTreeJsPath());
597  }
598 
599 
603  function getHTML()
604  {
605  global $tpl, $ilCtrl;
606 
607  $this->beforeRendering();
608 
609  self::init();
610  $container_id = $this->getContainerId();
611  $container_outer_id = "il_expl2_jstree_cont_out_".$this->getId();
612 
613  $tpl->addOnLoadCode($this->getOnLoadCode());
614 
615  $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
616 
617  // render childs
618  $root_node = $this->getRootNode();
619 
620  if (!$this->getSkipRootNode() &&
621  $this->isNodeVisible($this->getRootNode()))
622  {
623  $this->listStart($etpl);
624  $this->renderNode($this->getRootNode(), $etpl);
625  $this->listEnd($etpl);
626  }
627  else
628  {
629  $childs = $this->getChildsOfNode($this->getNodeId($root_node));
630  $childs = $this->sortChilds($childs, $this->getNodeId($root_node));
631  $any = false;
632  foreach ($childs as $child_node)
633  {
634  if ($this->isNodeVisible($child_node))
635  {
636  if (!$any)
637  {
638  $this->listStart($etpl);
639  $any = true;
640  }
641  $this->renderNode($child_node, $etpl);
642  }
643  }
644  if ($any)
645  {
646  $this->listEnd($etpl);
647  }
648  }
649 
650  $etpl->setVariable("CONTAINER_ID", $container_id);
651  $etpl->setVariable("CONTAINER_OUTER_ID", $container_outer_id);
652 
653  return $etpl->get();
654  }
655 
662  function renderNode($a_node, $tpl)
663  {
664  $this->listItemStart($tpl, $a_node);
665 
666  // select mode?
667  if ($this->select_postvar != "" && $this->isNodeClickable($a_node))
668  {
669  if ($this->select_multi)
670  {
671  $tpl->setCurrentBlock("cb");
672  if (in_array($this->getNodeId($a_node), $this->selected_nodes))
673  {
674  $tpl->setVariable("CHECKED", 'checked="checked"');
675  }
676  $tpl->setVariable("CB_VAL", $this->getNodeId($a_node));
677  $tpl->setVariable("CB_NAME", $this->select_postvar."[]");
678  $tpl->parseCurrentBlock();
679  }
680  else
681  {
682  $tpl->setCurrentBlock("rd");
683  if (in_array($this->getNodeId($a_node), $this->selected_nodes))
684  {
685  $tpl->setVariable("SELECTED", 'checked="checked"');
686  }
687  $tpl->setVariable("RD_VAL", $this->getNodeId($a_node));
688  $tpl->setVariable("RD_NAME", $this->select_postvar);
689  $tpl->parseCurrentBlock();
690  }
691  }
692 
693 
694  if ($this->isNodeHighlighted($a_node))
695  {
696  $tpl->touchBlock("hl");
697  }
698  $tpl->setCurrentBlock("content");
699  if ($this->getNodeIcon($a_node) != "")
700  {
701  $tpl->setVariable("ICON", ilUtil::img($this->getNodeIcon($a_node), $this->getNodeIconAlt($a_node))." ");
702  }
703  $tpl->setVariable("CONTENT", $this->getNodeContent($a_node));
704  $tpl->setVariable("HREF", $this->getNodeHref($a_node));
705  $target = $this->getNodeTarget($a_node);
706  if ($target != "")
707  {
708  $tpl->setVariable("TARGET", 'target="'.$target.'"');
709  }
710  if (!$this->isNodeClickable($a_node))
711  {
712  $tpl->setVariable("ONCLICK", 'onclick="return false;"');
713  $tpl->setVariable("A_CLASS", 'class="disabled"');
714  }
715  else
716  {
717  $onclick = $this->getNodeOnClick($a_node);
718  if ($onclick != "")
719  {
720  $tpl->setVariable("ONCLICK", 'onclick="'.$onclick.'"');
721  }
722  }
723  $tpl->parseCurrentBlock();
724 
725  $tpl->touchBlock("tag");
726 
727  if (!$this->getAjax() || in_array($this->getNodeId($a_node), $this->open_nodes)
728  || in_array($this->getNodeId($a_node), $this->custom_open_nodes))
729  {
730  $this->renderChilds($this->getNodeId($a_node), $tpl);
731  }
732 
733  $this->listItemEnd($tpl);
734  }
735 
742  final function renderChilds($a_node_id, $tpl)
743  {
744  $childs = $this->getChildsOfNode($a_node_id);
745  $childs = $this->sortChilds($childs, $a_node_id);
746 
747  if (count($childs) > 0)
748  {
749  $any = false;
750  foreach ($childs as $child)
751  {
752  if ($this->isNodeVisible($child))
753  {
754  if (!$any)
755  {
756  $this->listStart($tpl);
757  $any = true;
758  }
759  $this->renderNode($child, $tpl);
760  }
761  }
762  if ($any)
763  {
764  $this->listEnd($tpl);
765  }
766  }
767  }
768 
775  function getDomNodeIdForNodeId($a_node_id)
776  {
777  return "exp_node_".$this->getId()."_".$a_node_id;
778  }
779 
786  function getNodeIdForDomNodeId($a_dom_node_id)
787  {
788  $i = strlen("exp_node_".$this->getId()."_");
789  return substr($a_dom_node_id, $i);
790  }
791 
798  function listItemStart($tpl, $a_node)
799  {
800  $tpl->setCurrentBlock("list_item_start");
801  if ($this->getAjax() && $this->nodeHasVisibleChilds($a_node))
802  {
803  $tpl->touchBlock("li_closed");
804  }
805  $tpl->setVariable("DOM_NODE_ID",
806  $this->getDomNodeIdForNodeId($this->getNodeId($a_node)));
807  $tpl->parseCurrentBlock();
808  $tpl->touchBlock("tag");
809  }
810 
817  function listItemEnd($tpl)
818  {
819  $tpl->touchBlock("list_item_end");
820  $tpl->touchBlock("tag");
821  }
822 
829  function listStart($tpl)
830  {
831  $tpl->touchBlock("list_start");
832  $tpl->touchBlock("tag");
833  }
834 
841  function listEnd($tpl)
842  {
843  $tpl->touchBlock("list_end");
844  $tpl->touchBlock("tag");
845  }
846 }
847 
848 ?>
static makeDirParents($a_dir)
Create a new directory and all parent directories.
exit
Definition: login.php:54
setOfflineMode($a_val)
Set offline mode.
$_GET["client_id"]
getNodeOnClick($a_node)
Get node onclick attribute.
isNodeClickable($a_node)
Is node clickable?
renderChilds($a_node_id, $tpl)
Render childs.
getNodeIcon($a_node)
Get node icon path.
getNodeIconAlt($a_node)
Get node icon alt attribute.
setSkipRootNode($a_val)
Set skip root node.
setSelectMode($a_postvar, $a_multi=false)
Set select mode (to deactivate, pass an empty string as postvar)
$cmd
Definition: sahs_server.php:35
getNodeToggleOnClick($a_node)
Get onclick attribute for node toggling.
__construct($a_expl_id, $a_parent_obj, $a_parent_cmd)
Constructor.
renderNode($a_node, $tpl)
Render node.
setNodeOpen($a_id)
Set node to be opened (additional custom opened node, not standard expand behaviour) ...
isNodeHighlighted($a_node)
Is node highlighted?
isNodeVisible($a_node)
Is node visible?
handleCommand()
Handle explorer internal command.
setSecondaryHighlightedNodes($a_val)
Set secondary (background) highlighted nodes.
static getLocalJsTreeJsPath()
Get local path of jsTree js.
sortChilds($a_childs, $a_parent_node_id)
Sort childs.
global $tpl
Definition: ilias.php:8
global $ilCtrl
Definition: ilias.php:18
getSkipRootNode()
Get skip root node.
setNodeSelected($a_id)
Set node to be opened (additional custom opened node, not standard expand behaviour) ...
getSecondaryHighlightedNodes()
Get secondary (background) highlighted nodes.
static createHTMLExportDirs($a_target_dir)
Create html export directories.
getOnLoadCode()
Get on load code.
Explorer base GUI class.
getRootNode()
Get root node.
static getLocalExplorerJsPath()
Get local path of explorer js.
getNodeId($a_node)
Get id of a node.
special template class to simplify handling of ITX/PEAR
beforeRendering()
Before rendering.
getNodeAsync()
Get node asynchronously.
getOfflineMode()
Get offline mode.
getNodeContent($a_node)
Get content of a node.
static img($a_src, $a_alt="", $a_width="", $a_height="", $a_border=0, $a_id="", $a_class="")
Build img tag.
listStart($tpl)
List start.
getSelectOnClick($a_node)
Get onclick attribute for selecting radio/checkbox.
getNodeTarget($a_node)
Get node target (frame) attribute.
getChildsOfNode($a_parent_node_id)
Get childs of node.
Session based immediate storage.
static initjQuery($a_tpl=null)
Init jQuery.
listItemStart($tpl, $a_node)
List item start.
getDomNodeIdForNodeId($a_node_id)
Get DOM node id for node id.
nodeHasVisibleChilds($a_node)
Node has childs?
listItemEnd($tpl)
List item end.
getNodeIdForDomNodeId($a_dom_node_id)
Get node id for dom node id.
getNodeHref($a_node)
Get href for node.
getId()
Get id of explorer element.
getContainerId()
Get container id.