ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  protected $enable_dnd = false;
28 
30 
34  public function __construct($a_expl_id, $a_parent_obj, $a_parent_cmd)
35  {
36  $this->id = $a_expl_id;
37  $this->parent_obj = $a_parent_obj;
38  $this->parent_cmd = $a_parent_cmd;
39 
40  // get open nodes
41  include_once("./Services/Authentication/classes/class.ilSessionIStorage.php");
42  $this->store = new ilSessionIStorage("expl2");
43  $open_nodes = $this->store->get("on_".$this->id);
44  $this->open_nodes = unserialize($open_nodes);
45  if (!is_array($this->open_nodes))
46  {
47  $this->open_nodes = array();
48  }
49 
50  $this->nodeOnclickEnabled = true;
51  }
52 
56  static function getLocalExplorerJsPath()
57  {
58  return self::$js_expl_path;
59  }
60 
64  static function getLocalJsTreeJsPath()
65  {
66  return self::$js_tree_path;
67  }
68 
74  static function createHTMLExportDirs($a_target_dir)
75  {
76  ilUtil::makeDirParents($a_target_dir."/Services/UIComponent/Explorer2/lib/jstree-v.pre1.0");
77  ilUtil::makeDirParents($a_target_dir."/Services/UIComponent/Explorer2/js");
78  }
79 
80 
81 
82  //
83  // Abstract functions that need to be overwritten in derived classes
84  //
85 
94  abstract function getRootNode();
95 
102  abstract function getChildsOfNode($a_parent_node_id);
103 
110  abstract function getNodeContent($a_node);
111 
118  abstract function getNodeId($a_node);
119 
120 
121  //
122  // Functions with standard implementations that may be overwritten
123  //
124 
131  function getNodeHref($a_node)
132  {
133  return "#";
134  }
135 
145  function nodeHasVisibleChilds($a_node)
146  {
147  $childs = $this->getChildsOfNode($this->getNodeId($a_node));
148 
149  foreach ($childs as $child)
150  {
151  if ($this->isNodeVisible($child))
152  {
153  return true;
154  }
155  }
156  return false;
157  }
158 
167  function sortChilds($a_childs, $a_parent_node_id)
168  {
169  return $a_childs;
170  }
171 
178  function getNodeIcon($a_node)
179  {
180  return "";
181  }
182 
189  function getNodeIconAlt($a_node)
190  {
191  return "";
192  }
193 
200  function getNodeTarget($a_node)
201  {
202  return "";
203  }
204 
211  function getNodeOnClick($a_node)
212  {
213  if ($this->select_postvar != "")
214  {
215  return $this->getSelectOnClick($a_node);
216  }
217  return "";
218  }
219 
226  function isNodeVisible($a_node)
227  {
228  return true;
229  }
230 
237  function isNodeHighlighted($a_node)
238  {
239  return false;
240  }
241 
248  function isNodeClickable($a_node)
249  {
250  return true;
251  }
252 
259  protected function isNodeSelectable($a_node)
260  {
261  return true;
262  }
263 
264 
265  //
266  // Basic configuration / setter/getter
267  //
268 
274  function getId()
275  {
276  return $this->id;
277  }
278 
286  function setSkipRootNode($a_val)
287  {
288  $this->skip_root_node = $a_val;
289  }
290 
296  function getSkipRootNode()
297  {
298  return $this->skip_root_node;
299  }
300 
306  function setAjax($a_val)
307  {
308  $this->ajax = $a_val;
309  }
310 
316  function getAjax()
317  {
318  return $this->ajax;
319  }
320 
327  {
328  $this->sec_highl_nodes = $a_val;
329  }
330 
337  {
338  return $this->sec_highl_nodes;
339  }
340 
347  function setNodeOpen($a_id)
348  {
349  if (!in_array($a_id, $this->custom_open_nodes))
350  {
351  $this->custom_open_nodes[] = $a_id;
352  }
353  }
354 
361  final protected function getNodeToggleOnClick($a_node)
362  {
363  return "$('#".$this->getContainerId()."').jstree('toggle_node' , '#".
364  $this->getDomNodeIdForNodeId($this->getNodeId($a_node))."'); return false;";
365  }
366 
373  final protected function getSelectOnClick($a_node)
374  {
375  $dn_id = $this->getDomNodeIdForNodeId($this->getNodeId($a_node));
376  $oc = "il.Explorer2.selectOnClick('".$dn_id."'); return false;";
377  return $oc;
378  }
379 
387  function setSelectMode($a_postvar, $a_multi = false)
388  {
389  $this->select_postvar = $a_postvar;
390  $this->select_multi = $a_multi;
391  }
392 
399  function setNodeSelected($a_id)
400  {
401  if (!in_array($a_id, $this->selected_nodes))
402  {
403  $this->selected_nodes[] = $a_id;
404  }
405  }
406 
412  function setOfflineMode($a_val)
413  {
414  $this->offline_mode = $a_val;
415  }
416 
422  function getOfflineMode()
423  {
424  return $this->offline_mode;
425  }
426 
427  //
428  // Standard functions that usually are not overwritten / internal use
429  //
430 
436  function handleCommand()
437  {
438  if ($_GET["exp_cmd"] != "" &&
439  $_GET["exp_cont"] == $this->getContainerId())
440  {
441  $cmd = $_GET["exp_cmd"];
442  if (in_array($cmd, array("openNode", "closeNode", "getNodeAsync")))
443  {
444  $this->$cmd();
445  }
446 
447  return true;
448  }
449  return false;
450  }
451 
458  function getContainerId()
459  {
460  return "il_expl2_jstree_cont_".$this->getId();
461  }
462 
466  function openNode()
467  {
468  global $ilLog;
469 
470  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
471  if (!in_array($id, $this->open_nodes))
472  {
473  $this->open_nodes[] = $id;
474  }
475  $this->store->set("on_".$this->id, serialize($this->open_nodes));
476  exit;
477  }
478 
482  function closeNode()
483  {
484  global $ilLog;
485 
486  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
487  if (in_array($id, $this->open_nodes))
488  {
489  $k = array_search($id, $this->open_nodes);
490  unset($this->open_nodes[$k]);
491  }
492  $this->store->set("on_".$this->id, serialize($this->open_nodes));
493  exit;
494  }
495 
499  function getNodeAsync()
500  {
501  $this->beforeRendering();
502 
503  if ($_GET["node_id"] != "")
504  {
505  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
506  }
507  else
508  {
509  $id = $this->getNodeId($this->getRootNode());
510  }
511 
512  $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
513  $this->renderChilds($id, $etpl);
514  echo $etpl->get("tag");
515  exit;
516  }
517 
521  function beforeRendering()
522  {
523 
524  }
525 
532  function getOnLoadCode()
533  {
534  global $ilCtrl;
535 
536  $container_id = $this->getContainerId();
537  $container_outer_id = "il_expl2_jstree_cont_out_".$this->getId();
538 
539  // collect open nodes
540  $open_nodes = array($this->getDomNodeIdForNodeId($this->getNodeId($this->getRootNode())));
541  foreach ($this->open_nodes as $nid)
542  {
543  $open_nodes[] = $this->getDomNodeIdForNodeId($nid);
544  }
545  foreach ($this->custom_open_nodes as $nid)
546  {
547  $dnode = $this->getDomNodeIdForNodeId($nid);
548  if (!in_array($dnode, $open_nodes))
549  {
550  $open_nodes[] = $dnode;
551  }
552  }
553 
554  // ilias config options
555  $url = "";
556  if (!$this->getOfflineMode())
557  {
558  if (is_object($this->parent_obj))
559  {
560  $url = $ilCtrl->getLinkTarget($this->parent_obj, $this->parent_cmd, "", true);
561  }
562  else
563  {
564  $url = $ilCtrl->getLinkTargetByClass($this->parent_obj, $this->parent_cmd, "", true);
565  }
566  }
567 
568  // secondary highlighted nodes
569  $shn = array();
570  foreach ($this->sec_highl_nodes as $sh)
571  {
572  $shn[] = $this->getDomNodeIdForNodeId($sh);
573  }
574  $config = array(
575  "container_id" => $container_id,
576  "container_outer_id" => $container_outer_id,
577  "url" => $url,
578  "second_hnodes" => $shn,
579  "ajax" => $this->getAjax(),
580  );
581 
582 
583  // jstree config options
584  $js_tree_config = array(
585  "core" => array(
586  "animation" => 300,
587  "initially_open" => $open_nodes,
588  "open_parents" => false,
589  "strings" => array("loading" => "Loading ...", "new_node" => "New node")
590  ),
591  "plugins" => $this->getJSTreePlugins(),
592  "themes" => array("dots" => false, "icons" => false, "theme" => ""),
593  "html_data" => array()
594  );
595 
596  return 'il.Explorer2.init('.json_encode($config).', '.json_encode($js_tree_config).');';
597  }
598 
599  protected function getJSTreePlugins() {
600  $plugins = array("html_data", "themes", "json_data");
601  if($this->isEnableDnd()) {
602  $plugins[] = "crrm";
603  $plugins[] = "dnd";
604  }
605  return $plugins;
606  }
607 
608 
612  static function init()
613  {
614  global $tpl;
615 
616  include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
618 
619  $tpl->addJavascript(self::getLocalExplorerJsPath());
620  $tpl->addJavascript(self::getLocalJsTreeJsPath());
621  }
622 
623 
627  function getHTML()
628  {
629  global $tpl, $ilCtrl;
630 
631  $this->beforeRendering();
632 
633  self::init();
634  $container_id = $this->getContainerId();
635  $container_outer_id = "il_expl2_jstree_cont_out_".$this->getId();
636 
637  if (!$ilCtrl->isAsynch())
638  {
639  $tpl->addOnLoadCode($this->getOnLoadCode());
640  }
641 
642  $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
643 
644  // render childs
645  $root_node = $this->getRootNode();
646 
647  if (!$this->getSkipRootNode() &&
648  $this->isNodeVisible($this->getRootNode()))
649  {
650  $this->listStart($etpl);
651  $this->renderNode($this->getRootNode(), $etpl);
652  $this->listEnd($etpl);
653  }
654  else
655  {
656  $childs = $this->getChildsOfNode($this->getNodeId($root_node));
657  $childs = $this->sortChilds($childs, $this->getNodeId($root_node));
658  $any = false;
659  foreach ($childs as $child_node)
660  {
661  if ($this->isNodeVisible($child_node))
662  {
663  if (!$any)
664  {
665  $this->listStart($etpl);
666  $any = true;
667  }
668  $this->renderNode($child_node, $etpl);
669  }
670  }
671  if ($any)
672  {
673  $this->listEnd($etpl);
674  }
675  }
676 
677  $etpl->setVariable("CONTAINER_ID", $container_id);
678  $etpl->setVariable("CONTAINER_OUTER_ID", $container_outer_id);
679 
680  $add = "";
681  if ($ilCtrl->isAsynch())
682  {
683  $add = "<script>".$this->getOnLoadCode()."</script>";
684  }
685 
686  return $etpl->get().$add;
687  }
688 
695  function renderNode($a_node, $tpl)
696  {
697  $this->listItemStart($tpl, $a_node);
698 
699  // select mode?
700  if ($this->select_postvar != "" && $this->isNodeSelectable($a_node))
701  {
702  if ($this->select_multi)
703  {
704  $tpl->setCurrentBlock("cb");
705  if (in_array($this->getNodeId($a_node), $this->selected_nodes))
706  {
707  $tpl->setVariable("CHECKED", 'checked="checked"');
708  }
709  $tpl->setVariable("CB_VAL", $this->getNodeId($a_node));
710  $tpl->setVariable("CB_NAME", $this->select_postvar."[]");
711  $tpl->parseCurrentBlock();
712  }
713  else
714  {
715  $tpl->setCurrentBlock("rd");
716  if (in_array($this->getNodeId($a_node), $this->selected_nodes))
717  {
718  $tpl->setVariable("SELECTED", 'checked="checked"');
719  }
720  $tpl->setVariable("RD_VAL", $this->getNodeId($a_node));
721  $tpl->setVariable("RD_NAME", $this->select_postvar);
722  $tpl->parseCurrentBlock();
723  }
724  }
725 
726 
727  if ($this->isNodeHighlighted($a_node))
728  {
729  $tpl->touchBlock("hl");
730  }
731  $tpl->setCurrentBlock("content");
732  if ($this->getNodeIcon($a_node) != "")
733  {
734  $tpl->setVariable("ICON", ilUtil::img($this->getNodeIcon($a_node), $this->getNodeIconAlt($a_node))." ");
735  }
736  $tpl->setVariable("CONTENT", $this->getNodeContent($a_node));
737  $tpl->setVariable("HREF", $this->getNodeHref($a_node));
738  $target = $this->getNodeTarget($a_node);
739  if ($target != "")
740  {
741  $targetRelatedParams = array(
742  'target="' . $target . '"'
743  );
744 
745  if ('_blank' === $target) {
746  $targetRelatedParams[] = 'rel="noopener"';
747  }
748 
749  $tpl->setVariable('TARGET', implode(' ', $targetRelatedParams));
750  }
751  if (!$this->isNodeOnclickEnabled() || !$this->isNodeClickable($a_node))
752  {
753  $tpl->setVariable("ONCLICK", 'onclick="return false;"');
754  $tpl->setVariable("A_CLASS", 'class="disabled"');
755  }
756  else
757  {
758  $onclick = $this->getNodeOnClick($a_node);
759  if ($onclick != "")
760  {
761  $tpl->setVariable("ONCLICK", 'onclick="'.$onclick.'"');
762  }
763  }
764  $tpl->parseCurrentBlock();
765 
766  $tpl->touchBlock("tag");
767 
768  if (!$this->getAjax() || in_array($this->getNodeId($a_node), $this->open_nodes)
769  || in_array($this->getNodeId($a_node), $this->custom_open_nodes))
770  {
771  $this->renderChilds($this->getNodeId($a_node), $tpl);
772  }
773 
774  $this->listItemEnd($tpl);
775  }
776 
783  final function renderChilds($a_node_id, $tpl)
784  {
785  $childs = $this->getChildsOfNode($a_node_id);
786  $childs = $this->sortChilds($childs, $a_node_id);
787 
788  if (count($childs) > 0)
789  {
790  $any = false;
791  foreach ($childs as $child)
792  {
793  if ($this->isNodeVisible($child))
794  {
795  if (!$any)
796  {
797  $this->listStart($tpl);
798  $any = true;
799  }
800  $this->renderNode($child, $tpl);
801  }
802  }
803  if ($any)
804  {
805  $this->listEnd($tpl);
806  }
807  }
808  }
809 
816  function getDomNodeIdForNodeId($a_node_id)
817  {
818  return "exp_node_".$this->getId()."_".$a_node_id;
819  }
820 
827  function getNodeIdForDomNodeId($a_dom_node_id)
828  {
829  $i = strlen("exp_node_".$this->getId()."_");
830  return substr($a_dom_node_id, $i);
831  }
832 
839  function listItemStart($tpl, $a_node)
840  {
841  $tpl->setCurrentBlock("list_item_start");
842  if ($this->getAjax() && $this->nodeHasVisibleChilds($a_node))
843  {
844  $tpl->touchBlock("li_closed");
845  }
846  $tpl->setVariable("DOM_NODE_ID",
847  $this->getDomNodeIdForNodeId($this->getNodeId($a_node)));
848  $tpl->parseCurrentBlock();
849  $tpl->touchBlock("tag");
850  }
851 
858  function listItemEnd($tpl)
859  {
860  $tpl->touchBlock("list_item_end");
861  $tpl->touchBlock("tag");
862  }
863 
870  function listStart($tpl)
871  {
872  $tpl->touchBlock("list_start");
873  $tpl->touchBlock("tag");
874  }
875 
882  function listEnd($tpl)
883  {
884  $tpl->touchBlock("list_end");
885  $tpl->touchBlock("tag");
886  }
887 
891  public function isNodeOnclickEnabled()
892  {
894  }
895 
900  {
901  $this->nodeOnclickEnabled = $nodeOnclickEnabled;
902  }
903 
904  public function isEnableDnd() {
905  return $this->enable_dnd;
906  }
907 
912  public function setEnableDnd($enable_dnd) {
913  $this->enable_dnd = $enable_dnd;
914  }
915 }
916 
917 ?>
static makeDirParents($a_dir)
Create a new directory and all parent directories.
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.
$url
Definition: shib_logout.php:72
setNodeOpen($a_id)
Set node to be opened (additional custom opened node, not standard expand behaviour) ...
isNodeSelectable($a_node)
Is node selectable?
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) ...
setEnableDnd($enable_dnd)
Enable Drag & Drop functionality.
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.
Create styles array
The data for the language used.
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.
setNodeOnclickEnabled($nodeOnclickEnabled)
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.