ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 {
21  protected $log;
22 
26  protected $ctrl;
27 
31  protected $tpl;
32 
33  protected static $js_tree_path = "./libs/bower/bower_components/jstree/jquery.jstree.js";
34  protected static $js_expl_path = "./Services/UIComponent/Explorer2/js/Explorer2.js";
35  protected $skip_root_node = false;
36  protected $ajax = false;
37  protected $custom_open_nodes = array();
38  protected $selected_nodes = array();
39  protected $select_postvar = "";
40  protected $offline_mode = false;
41  protected $sec_highl_nodes = array();
42  protected $enable_dnd = false;
43 
45 
49  public function __construct($a_expl_id, $a_parent_obj, $a_parent_cmd)
50  {
51  global $DIC;
52 
53  $this->log = $DIC["ilLog"];
54  $this->ctrl = $DIC->ctrl();
55  $this->tpl = $DIC["tpl"];
56  $this->id = $a_expl_id;
57  $this->parent_obj = $a_parent_obj;
58  $this->parent_cmd = $a_parent_cmd;
59 
60  // get open nodes
61  include_once("./Services/Authentication/classes/class.ilSessionIStorage.php");
62  $this->store = new ilSessionIStorage("expl2");
63  $open_nodes = $this->store->get("on_" . $this->id);
64  $this->open_nodes = unserialize($open_nodes);
65  if (!is_array($this->open_nodes)) {
66  $this->open_nodes = array();
67  }
68 
69  $this->nodeOnclickEnabled = true;
70  }
71 
77  public function setMainTemplate(ilTemplate $a_main_tpl = null)
78  {
79  $this->tpl = $a_main_tpl;
80  }
81 
82 
86  public static function getLocalExplorerJsPath()
87  {
88  return self::$js_expl_path;
89  }
90 
94  public static function getLocalJsTreeJsPath()
95  {
96  return self::$js_tree_path;
97  }
98 
104  public static function createHTMLExportDirs($a_target_dir)
105  {
106  ilUtil::makeDirParents($a_target_dir . "/Services/UIComponent/Explorer2/lib/jstree-v.pre1.0");
107  ilUtil::makeDirParents($a_target_dir . "/Services/UIComponent/Explorer2/js");
108  }
109 
110 
111 
112  //
113  // Abstract functions that need to be overwritten in derived classes
114  //
115 
124  abstract public function getRootNode();
125 
132  abstract public function getChildsOfNode($a_parent_node_id);
133 
140  abstract public function getNodeContent($a_node);
141 
148  abstract public function getNodeId($a_node);
149 
150 
151  //
152  // Functions with standard implementations that may be overwritten
153  //
154 
161  public function getNodeHref($a_node)
162  {
163  return "#";
164  }
165 
175  public function nodeHasVisibleChilds($a_node)
176  {
177  $childs = $this->getChildsOfNode($this->getNodeId($a_node));
178 
179  foreach ($childs as $child) {
180  if ($this->isNodeVisible($child)) {
181  return true;
182  }
183  }
184  return false;
185  }
186 
195  public function sortChilds($a_childs, $a_parent_node_id)
196  {
197  return $a_childs;
198  }
199 
206  public function getNodeIcon($a_node)
207  {
208  return "";
209  }
210 
217  public function getNodeIconAlt($a_node)
218  {
219  return "";
220  }
221 
228  public function getNodeTarget($a_node)
229  {
230  return "";
231  }
232 
239  public function getNodeOnClick($a_node)
240  {
241  if ($this->select_postvar != "") {
242  return $this->getSelectOnClick($a_node);
243  }
244  return "";
245  }
246 
253  public function isNodeVisible($a_node)
254  {
255  return true;
256  }
257 
264  public function isNodeHighlighted($a_node)
265  {
266  return false;
267  }
268 
275  public function isNodeClickable($a_node)
276  {
277  return true;
278  }
279 
286  protected function isNodeSelectable($a_node)
287  {
288  return true;
289  }
290 
291 
292  //
293  // Basic configuration / setter/getter
294  //
295 
301  public function getId()
302  {
303  return $this->id;
304  }
305 
313  public function setSkipRootNode($a_val)
314  {
315  $this->skip_root_node = $a_val;
316  }
317 
323  public function getSkipRootNode()
324  {
325  return $this->skip_root_node;
326  }
327 
333  public function setAjax($a_val)
334  {
335  $this->ajax = $a_val;
336  }
337 
343  public function getAjax()
344  {
345  return $this->ajax;
346  }
347 
353  public function setSecondaryHighlightedNodes($a_val)
354  {
355  $this->sec_highl_nodes = $a_val;
356  }
357 
364  {
365  return $this->sec_highl_nodes;
366  }
367 
374  public function setNodeOpen($a_id)
375  {
376  if (!in_array($a_id, $this->custom_open_nodes)) {
377  $this->custom_open_nodes[] = $a_id;
378  }
379  }
380 
387  final protected function getNodeToggleOnClick($a_node)
388  {
389  return "$('#" . $this->getContainerId() . "').jstree('toggle_node' , '#" .
390  $this->getDomNodeIdForNodeId($this->getNodeId($a_node)) . "'); return false;";
391  }
392 
399  final protected function getSelectOnClick($a_node)
400  {
401  $dn_id = $this->getDomNodeIdForNodeId($this->getNodeId($a_node));
402  $oc = "il.Explorer2.selectOnClick('" . $dn_id . "'); return false;";
403  return $oc;
404  }
405 
413  public function setSelectMode($a_postvar, $a_multi = false)
414  {
415  $this->select_postvar = $a_postvar;
416  $this->select_multi = $a_multi;
417  }
418 
425  public function setNodeSelected($a_id)
426  {
427  if (!in_array($a_id, $this->selected_nodes)) {
428  $this->selected_nodes[] = $a_id;
429  }
430  }
431 
437  public function setOfflineMode($a_val)
438  {
439  $this->offline_mode = $a_val;
440  }
441 
447  public function getOfflineMode()
448  {
449  return $this->offline_mode;
450  }
451 
452  //
453  // Standard functions that usually are not overwritten / internal use
454  //
455 
461  public function handleCommand()
462  {
463  if ($_GET["exp_cmd"] != "" &&
464  $_GET["exp_cont"] == $this->getContainerId()) {
465  $cmd = $_GET["exp_cmd"];
466  if (in_array($cmd, array("openNode", "closeNode", "getNodeAsync"))) {
467  $this->$cmd();
468  }
469 
470  return true;
471  }
472  return false;
473  }
474 
481  public function getContainerId()
482  {
483  return "il_expl2_jstree_cont_" . $this->getId();
484  }
485 
489  public function openNode()
490  {
491  $ilLog = $this->log;
492 
493  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
494  if (!in_array($id, $this->open_nodes)) {
495  $this->open_nodes[] = $id;
496  }
497  $this->store->set("on_" . $this->id, serialize($this->open_nodes));
498  exit;
499  }
500 
504  public function closeNode()
505  {
506  $ilLog = $this->log;
507 
508  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
509  if (in_array($id, $this->open_nodes)) {
510  $k = array_search($id, $this->open_nodes);
511  unset($this->open_nodes[$k]);
512  }
513  $this->store->set("on_" . $this->id, serialize($this->open_nodes));
514  exit;
515  }
516 
520  public function getNodeAsync()
521  {
522  $this->beforeRendering();
523 
524  if ($_GET["node_id"] != "") {
525  $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
526  } else {
527  $id = $this->getNodeId($this->getRootNode());
528  }
529 
530  $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
531  $this->renderChilds($id, $etpl);
532  echo $etpl->get("tag");
533  exit;
534  }
535 
539  public function beforeRendering()
540  {
541  }
542 
549  public function getOnLoadCode()
550  {
552 
553  $container_id = $this->getContainerId();
554  $container_outer_id = "il_expl2_jstree_cont_out_" . $this->getId();
555 
556  // collect open nodes
557  $open_nodes = array($this->getDomNodeIdForNodeId($this->getNodeId($this->getRootNode())));
558  foreach ($this->open_nodes as $nid) {
559  $open_nodes[] = $this->getDomNodeIdForNodeId($nid);
560  }
561  foreach ($this->custom_open_nodes as $nid) {
562  $dnode = $this->getDomNodeIdForNodeId($nid);
563  if (!in_array($dnode, $open_nodes)) {
564  $open_nodes[] = $dnode;
565  }
566  }
567 
568  // ilias config options
569  $url = "";
570  if (!$this->getOfflineMode()) {
571  if (is_object($this->parent_obj)) {
572  $url = $ilCtrl->getLinkTarget($this->parent_obj, $this->parent_cmd, "", true);
573  } else {
574  $url = $ilCtrl->getLinkTargetByClass($this->parent_obj, $this->parent_cmd, "", true);
575  }
576  }
577 
578  // secondary highlighted nodes
579  $shn = array();
580  foreach ($this->sec_highl_nodes as $sh) {
581  $shn[] = $this->getDomNodeIdForNodeId($sh);
582  }
583  $config = array(
584  "container_id" => $container_id,
585  "container_outer_id" => $container_outer_id,
586  "url" => $url,
587  "second_hnodes" => $shn,
588  "ajax" => $this->getAjax(),
589  );
590 
591 
592  // jstree config options
593  $js_tree_config = array(
594  "core" => array(
595  "animation" => 300,
596  "initially_open" => $open_nodes,
597  "open_parents" => false,
598  "strings" => array("loading" => "Loading ...", "new_node" => "New node")
599  ),
600  "plugins" => $this->getJSTreePlugins(),
601  "themes" => array("dots" => false, "icons" => false, "theme" => ""),
602  "html_data" => array()
603  );
604 
605  return 'il.Explorer2.init(' . json_encode($config) . ', ' . json_encode($js_tree_config) . ');';
606  }
607 
608  protected function getJSTreePlugins()
609  {
610  $plugins = array("html_data", "themes", "json_data");
611  if ($this->isEnableDnd()) {
612  $plugins[] = "crrm";
613  $plugins[] = "dnd";
614  }
615  return $plugins;
616  }
617 
618 
622  public static function init($a_main_tpl = null)
623  {
624  global $DIC;
625 
626  if ($a_main_tpl == null) {
627  $tpl = $DIC["tpl"];
628  } else {
629  $tpl = $a_main_tpl;
630  }
631 
632  include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
634 
635  $tpl->addJavascript(self::getLocalExplorerJsPath());
636  $tpl->addJavascript(self::getLocalJsTreeJsPath());
637  }
638 
639 
643  public function getHTML()
644  {
645  $tpl = $this->tpl;
647 
648  $root = $this->getNodeId($this->getRootNode());
649  if (!in_array($root, $this->open_nodes)) {
650  $this->open_nodes[] = $root;
651  }
652 
653  $this->beforeRendering();
654 
655  self::init($tpl);
656  $container_id = $this->getContainerId();
657  $container_outer_id = "il_expl2_jstree_cont_out_" . $this->getId();
658 
659  if (!$ilCtrl->isAsynch()) {
660  $tpl->addOnLoadCode($this->getOnLoadCode());
661  }
662 
663  $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
664 
665  // render childs
666  $root_node = $this->getRootNode();
667 
668  if (!$this->getSkipRootNode() &&
669  $this->isNodeVisible($this->getRootNode())) {
670  $this->listStart($etpl);
671  $this->renderNode($this->getRootNode(), $etpl);
672  $this->listEnd($etpl);
673  } else {
674  $childs = $this->getChildsOfNode($this->getNodeId($root_node));
675  $childs = $this->sortChilds($childs, $this->getNodeId($root_node));
676  $any = false;
677  foreach ($childs as $child_node) {
678  if ($this->isNodeVisible($child_node)) {
679  if (!$any) {
680  $this->listStart($etpl);
681  $any = true;
682  }
683  $this->renderNode($child_node, $etpl);
684  }
685  }
686  if ($any) {
687  $this->listEnd($etpl);
688  }
689  }
690 
691  $etpl->setVariable("CONTAINER_ID", $container_id);
692  $etpl->setVariable("CONTAINER_OUTER_ID", $container_outer_id);
693 
694  $add = "";
695  if ($ilCtrl->isAsynch()) {
696  $add = "<script>" . $this->getOnLoadCode() . "</script>";
697  }
698 
699  return $etpl->get() . $add;
700  }
701 
708  public function renderNode($a_node, $tpl)
709  {
710  $this->listItemStart($tpl, $a_node);
711 
712  // select mode?
713  if ($this->select_postvar != "" && $this->isNodeSelectable($a_node)) {
714  if ($this->select_multi) {
715  $tpl->setCurrentBlock("cb");
716  if (in_array($this->getNodeId($a_node), $this->selected_nodes)) {
717  $tpl->setVariable("CHECKED", 'checked="checked"');
718  }
719  $tpl->setVariable("CB_VAL", $this->getNodeId($a_node));
720  $tpl->setVariable("CB_NAME", $this->select_postvar . "[]");
721  $tpl->parseCurrentBlock();
722  } else {
723  $tpl->setCurrentBlock("rd");
724  if (in_array($this->getNodeId($a_node), $this->selected_nodes)) {
725  $tpl->setVariable("SELECTED", 'checked="checked"');
726  }
727  $tpl->setVariable("RD_VAL", $this->getNodeId($a_node));
728  $tpl->setVariable("RD_NAME", $this->select_postvar);
729  $tpl->parseCurrentBlock();
730  }
731  }
732 
733 
734  if ($this->isNodeHighlighted($a_node)) {
735  $tpl->touchBlock("hl");
736  }
737  $tpl->setCurrentBlock("content");
738  if ($this->getNodeIcon($a_node) != "") {
739  $tpl->setVariable("ICON", ilUtil::img($this->getNodeIcon($a_node), $this->getNodeIconAlt($a_node)) . " ");
740  }
741  $tpl->setVariable("CONTENT", $this->getNodeContent($a_node));
742  $tpl->setVariable("HREF", $this->getNodeHref($a_node));
743  $target = $this->getNodeTarget($a_node);
744  if ($target != "") {
745  $targetRelatedParams = array(
746  'target="' . $target . '"'
747  );
748 
749  if ('_blank' === $target) {
750  $targetRelatedParams[] = 'rel="noopener"';
751  }
752 
753  $tpl->setVariable('TARGET', implode(' ', $targetRelatedParams));
754  }
755  if (!$this->isNodeOnclickEnabled() || !$this->isNodeClickable($a_node)) {
756  $tpl->setVariable("ONCLICK", 'onclick="return false;"');
757  $tpl->setVariable("A_CLASS", 'class="disabled"');
758  } else {
759  $onclick = $this->getNodeOnClick($a_node);
760  if ($onclick != "") {
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  $this->renderChilds($this->getNodeId($a_node), $tpl);
771  }
772 
773  $this->listItemEnd($tpl);
774  }
775 
782  final public function renderChilds($a_node_id, $tpl)
783  {
784  $childs = $this->getChildsOfNode($a_node_id);
785  $childs = $this->sortChilds($childs, $a_node_id);
786 
787  if (count($childs) > 0) {
788  $any = false;
789  foreach ($childs as $child) {
790  if ($this->isNodeVisible($child)) {
791  if (!$any) {
792  $this->listStart($tpl);
793  $any = true;
794  }
795  $this->renderNode($child, $tpl);
796  }
797  }
798  if ($any) {
799  $this->listEnd($tpl);
800  }
801  }
802  }
803 
810  public function getDomNodeIdForNodeId($a_node_id)
811  {
812  return "exp_node_" . $this->getId() . "_" . $a_node_id;
813  }
814 
821  public function getNodeIdForDomNodeId($a_dom_node_id)
822  {
823  $i = strlen("exp_node_" . $this->getId() . "_");
824  return substr($a_dom_node_id, $i);
825  }
826 
833  public function listItemStart($tpl, $a_node)
834  {
835  $tpl->setCurrentBlock("list_item_start");
836  if ($this->getAjax() && $this->nodeHasVisibleChilds($a_node)) {
837  $tpl->touchBlock("li_closed");
838  }
839  $tpl->setVariable(
840  "DOM_NODE_ID",
841  $this->getDomNodeIdForNodeId($this->getNodeId($a_node))
842  );
843  $tpl->parseCurrentBlock();
844  $tpl->touchBlock("tag");
845  }
846 
853  public function listItemEnd($tpl)
854  {
855  $tpl->touchBlock("list_item_end");
856  $tpl->touchBlock("tag");
857  }
858 
865  public function listStart($tpl)
866  {
867  $tpl->touchBlock("list_start");
868  $tpl->touchBlock("tag");
869  }
870 
877  public function listEnd($tpl)
878  {
879  $tpl->touchBlock("list_end");
880  $tpl->touchBlock("tag");
881  }
882 
886  public function isNodeOnclickEnabled()
887  {
889  }
890 
895  {
896  $this->nodeOnclickEnabled = $nodeOnclickEnabled;
897  }
898 
899  public function isEnableDnd()
900  {
901  return $this->enable_dnd;
902  }
903 
908  public function setEnableDnd($enable_dnd)
909  {
910  $this->enable_dnd = $enable_dnd;
911  }
912 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
setOfflineMode($a_val)
Set offline mode.
global $DIC
Definition: saml.php:7
$_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)
if(!array_key_exists('StateId', $_REQUEST)) $id
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.
static init($a_main_tpl=null)
Init JS.
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 $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.
$i
Definition: disco.tpl.php:19
Session based immediate storage.
static initjQuery($a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
listItemStart($tpl, $a_node)
List item start.
$url
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.
setMainTemplate(ilTemplate $a_main_tpl=null)
Set main template (that is responsible for adding js/css)
getContainerId()
Get container id.