ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
16abstract 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_tree_path = "./libs/bower/bower_components/jstree/dist/jstree.js";
35 protected static $js_tree_path_css = "./libs/bower/bower_components/jstree/dist/themes/default/style.min.css";
36
37 protected static $js_expl_path = "./Services/UIComponent/Explorer2/js/Explorer2.js";
38 protected $skip_root_node = false;
39 protected $ajax = false;
40 protected $custom_open_nodes = array();
41 protected $selected_nodes = array();
42 protected $select_postvar = "";
43 protected $offline_mode = false;
44 protected $sec_highl_nodes = array();
45 protected $enable_dnd = false;
46 protected $search_term = "";
47
51 protected $child_limit = 0;
52
54
58 public function __construct($a_expl_id, $a_parent_obj, $a_parent_cmd)
59 {
60 global $DIC;
61
62 $this->log = $DIC["ilLog"];
63 $this->ctrl = $DIC->ctrl();
64 $this->tpl = $DIC["tpl"];
65 $this->id = $a_expl_id;
66 $this->parent_obj = $a_parent_obj;
67 $this->parent_cmd = $a_parent_cmd;
68 // get open nodes
69 include_once("./Services/Authentication/classes/class.ilSessionIStorage.php");
70 $this->store = new ilSessionIStorage("expl2");
71 $open_nodes = $this->store->get("on_" . $this->id);
72 $this->open_nodes = unserialize($open_nodes);
73 if (!is_array($this->open_nodes)) {
74 $this->open_nodes = array();
75 }
76
77 $this->nodeOnclickEnabled = true;
78 }
79
85 public function setChildLimit($a_val)
86 {
87 $this->child_limit = $a_val;
88 }
89
95 public function getChildLimit()
96 {
97 return $this->child_limit;
98 }
99
105 public function setSearchTerm($a_val)
106 {
107 $this->search_term = $a_val;
108 }
109
115 public function getSearchTerm()
116 {
117 return $this->search_term;
118 }
119
120
126 public function setMainTemplate(ilTemplate $a_main_tpl = null)
127 {
128 $this->tpl = $a_main_tpl;
129 }
130
131
135 public static function getLocalExplorerJsPath()
136 {
137 return self::$js_expl_path;
138 }
139
143 public static function getLocalJsTreeJsPath()
144 {
145 return self::$js_tree_path;
146 }
147
151 public static function getLocalJsTreeCssPath()
152 {
154 }
155
161 public static function createHTMLExportDirs($a_target_dir)
162 {
163 ilUtil::makeDirParents($a_target_dir . "/Services/UIComponent/Explorer2/lib/jstree-v.pre1.0");
164 ilUtil::makeDirParents($a_target_dir . "/Services/UIComponent/Explorer2/js");
165 }
166
167
168
169 //
170 // Abstract functions that need to be overwritten in derived classes
171 //
172
181 abstract public function getRootNode();
182
189 abstract public function getChildsOfNode($a_parent_node_id);
190
197 abstract public function getNodeContent($a_node);
198
205 abstract public function getNodeId($a_node);
206
207
208 //
209 // Functions with standard implementations that may be overwritten
210 //
211
218 public function getNodeHref($a_node)
219 {
220 return "#";
221 }
222
232 public function nodeHasVisibleChilds($a_node)
233 {
234 $childs = $this->getChildsOfNode($this->getNodeId($a_node));
235
236 foreach ($childs as $child) {
237 if ($this->isNodeVisible($child)) {
238 return true;
239 }
240 }
241 return false;
242 }
243
252 public function sortChilds($a_childs, $a_parent_node_id)
253 {
254 return $a_childs;
255 }
256
263 public function getNodeIcon($a_node)
264 {
265 return "";
266 }
267
274 public function getNodeIconAlt($a_node)
275 {
276 return "";
277 }
278
285 public function getNodeTarget($a_node)
286 {
287 return "";
288 }
289
296 public function getNodeOnClick($a_node)
297 {
298 if ($this->select_postvar != "") {
299 return $this->getSelectOnClick($a_node);
300 }
301 return "";
302 }
303
310 public function isNodeVisible($a_node)
311 {
312 return true;
313 }
314
321 public function isNodeHighlighted($a_node)
322 {
323 return false;
324 }
325
332 public function isNodeClickable($a_node)
333 {
334 return true;
335 }
336
343 protected function isNodeSelectable($a_node)
344 {
345 return true;
346 }
347
348
349 //
350 // Basic configuration / setter/getter
351 //
352
358 public function getId()
359 {
360 return $this->id;
361 }
362
370 public function setSkipRootNode($a_val)
371 {
372 $this->skip_root_node = $a_val;
373 }
374
380 public function getSkipRootNode()
381 {
383 }
384
390 public function setAjax($a_val)
391 {
392 $this->ajax = $a_val;
393 }
394
400 public function getAjax()
401 {
402 return $this->ajax;
403 }
404
410 public function setSecondaryHighlightedNodes($a_val)
411 {
412 $this->sec_highl_nodes = $a_val;
413 }
414
421 {
423 }
424
431 public function setNodeOpen($a_id)
432 {
433 if (!in_array($a_id, $this->custom_open_nodes)) {
434 $this->custom_open_nodes[] = $a_id;
435 }
436 }
437
444 final protected function getNodeToggleOnClick($a_node)
445 {
446 return "$('#" . $this->getContainerId() . "').jstree('toggle_node' , '#" .
447 $this->getDomNodeIdForNodeId($this->getNodeId($a_node)) . "'); return false;";
448 }
449
456 final protected function getSelectOnClick($a_node)
457 {
458 $dn_id = $this->getDomNodeIdForNodeId($this->getNodeId($a_node));
459 $oc = "il.Explorer2.selectOnClick(event, '" . $dn_id . "'); return false;";
460 return $oc;
461 }
462
470 public function setSelectMode($a_postvar, $a_multi = false)
471 {
472 $this->select_postvar = $a_postvar;
473 $this->select_multi = $a_multi;
474 }
475
482 public function setNodeSelected($a_id)
483 {
484 if (!in_array($a_id, $this->selected_nodes)) {
485 $this->selected_nodes[] = $a_id;
486 }
487 }
488
494 public function setOfflineMode($a_val)
495 {
496 $this->offline_mode = $a_val;
497 }
498
504 public function getOfflineMode()
505 {
506 return $this->offline_mode;
507 }
508
509 //
510 // Standard functions that usually are not overwritten / internal use
511 //
512
518 public function handleCommand()
519 {
520 if ($_GET["exp_cmd"] != "" &&
521 $_GET["exp_cont"] == $this->getContainerId()) {
522 $cmd = $_GET["exp_cmd"];
523 if (in_array($cmd, array("openNode", "closeNode", "getNodeAsync"))) {
524 $this->$cmd();
525 }
526
527 return true;
528 }
529 return false;
530 }
531
538 public function getContainerId()
539 {
540 return "il_expl2_jstree_cont_" . $this->getId();
541 }
542
546 public function openNode()
547 {
549
550 $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
551 if (!in_array($id, $this->open_nodes)) {
552 $this->open_nodes[] = $id;
553 }
554 $this->store->set("on_" . $this->id, serialize($this->open_nodes));
555 exit;
556 }
557
561 public function closeNode()
562 {
564
565 $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
566 if (in_array($id, $this->open_nodes)) {
567 $k = array_search($id, $this->open_nodes);
568 unset($this->open_nodes[$k]);
569 }
570 $this->store->set("on_" . $this->id, serialize($this->open_nodes));
571 exit;
572 }
573
577 public function getNodeAsync()
578 {
579 $this->beforeRendering();
580
581 $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
582
583 $root = $this->getNodeId($this->getRootNode());
584 if (!in_array($root, $this->open_nodes)) {
585 $this->open_nodes[] = $root;
586 }
587
588 if ($_GET["node_id"] != "") {
589 $id = $this->getNodeIdForDomNodeId($_GET["node_id"]);
590 $this->setSearchTerm(ilUtil::stripSlashes($_GET["searchterm"]));
591 $this->renderChilds($id, $etpl);
592 } else {
593 $id = $this->getNodeId($this->getRootNode());
594 $this->renderNode($this->getRootNode(), $etpl);
595 }
596 echo $etpl->get("tag");
597 exit;
598 }
599
603 public function beforeRendering()
604 {
605 }
606
613 protected function isNodeOpen($node_id)
614 {
615 return ($this->getNodeId($this->getRootNode()) == $node_id
616 || in_array($node_id, $this->open_nodes)
617 || in_array($node_id, $this->custom_open_nodes));
618 }
619
620
627 public function getOnLoadCode()
628 {
630
631 $container_id = $this->getContainerId();
632 $container_outer_id = "il_expl2_jstree_cont_out_" . $this->getId();
633
634 // collect open nodes
635 $open_nodes = array($this->getDomNodeIdForNodeId($this->getNodeId($this->getRootNode())));
636 foreach ($this->open_nodes as $nid) {
637 $open_nodes[] = $this->getDomNodeIdForNodeId($nid);
638 }
639 foreach ($this->custom_open_nodes as $nid) {
640 $dnode = $this->getDomNodeIdForNodeId($nid);
641 if (!in_array($dnode, $open_nodes)) {
642 $open_nodes[] = $dnode;
643 }
644 }
645
646 // ilias config options
647 $url = "";
648 if (!$this->getOfflineMode()) {
649 if (is_object($this->parent_obj)) {
650 $url = $ilCtrl->getLinkTarget($this->parent_obj, $this->parent_cmd, "", true);
651 } else {
652 $url = $ilCtrl->getLinkTargetByClass($this->parent_obj, $this->parent_cmd, "", true);
653 }
654 }
655
656 // secondary highlighted nodes
657 $shn = array();
658 foreach ($this->sec_highl_nodes as $sh) {
659 $shn[] = $this->getDomNodeIdForNodeId($sh);
660 }
661 $config = array(
662 "container_id" => $container_id,
663 "container_outer_id" => $container_outer_id,
664 "url" => $url,
665 "second_hnodes" => $shn,
666 "ajax" => $this->getAjax(),
667 );
668
669
670 // jstree config options
671 $js_tree_config = array(
672 "core" => array(
673 "animation" => 0,
674 "initially_open" => $open_nodes,
675 "open_parents" => false,
676 "strings" => array("loading" => "Loading ...", "new_node" => "New node"),
677 "themes" => array("dots" => false, "icons" => false, "theme" => "")
678 ),
679 "plugins" => $this->getJSTreePlugins(),
680 "html_data" => array()
681 );
682
683 return 'il.Explorer2.init(' . json_encode($config) . ', ' . json_encode($js_tree_config) . ');';
684 }
685
686 protected function getJSTreePlugins()
687 {
688 $plugins = array("html_data", "themes", "json_data");
689 if ($this->isEnableDnd()) {
690 $plugins[] = "dnd";
691 }
692 return $plugins;
693 }
694
695
699 public static function init($a_main_tpl = null)
700 {
701 global $DIC;
702
703 if ($a_main_tpl == null) {
704 $tpl = $DIC["tpl"];
705 } else {
706 $tpl = $a_main_tpl;
707 }
708
709 include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
711
712 $tpl->addJavascript(self::getLocalExplorerJsPath());
713 $tpl->addJavascript(self::getLocalJsTreeJsPath());
714 $tpl->addCss(self::getLocalJsTreeCssPath());
715 }
716
717
721 public function getHTML()
722 {
725
726 $root = $this->getNodeId($this->getRootNode());
727 if (!in_array($root, $this->open_nodes)) {
728 $this->open_nodes[] = $root;
729 }
730
731 $this->beforeRendering();
732
734 $container_id = $this->getContainerId();
735 $container_outer_id = "il_expl2_jstree_cont_out_" . $this->getId();
736
737 if (!$ilCtrl->isAsynch()) {
738 $tpl->addOnLoadCode($this->getOnLoadCode());
739 }
740
741 $etpl = new ilTemplate("tpl.explorer2.html", true, true, "Services/UIComponent/Explorer2");
742
743 // render childs
744 $root_node = $this->getRootNode();
745
746 if (!$this->getSkipRootNode() &&
747 $this->isNodeVisible($this->getRootNode())) {
748 $this->listStart($etpl);
749 $this->renderNode($this->getRootNode(), $etpl);
750 $this->listEnd($etpl);
751 } else {
752 $childs = $this->getChildsOfNode($this->getNodeId($root_node));
753 $childs = $this->sortChilds($childs, $this->getNodeId($root_node));
754 $any = false;
755 foreach ($childs as $child_node) {
756 if ($this->isNodeVisible($child_node)) {
757 if (!$any) {
758 $this->listStart($etpl);
759 $any = true;
760 }
761 $this->renderNode($child_node, $etpl);
762 }
763 }
764 if ($any) {
765 $this->listEnd($etpl);
766 }
767 }
768
769 $etpl->setVariable("CONTAINER_ID", $container_id);
770 $etpl->setVariable("CONTAINER_OUTER_ID", $container_outer_id);
771
772 $add = "";
773 if ($ilCtrl->isAsynch()) {
774 $add = "<script>" . $this->getOnLoadCode() . "</script>";
775 }
776
777 $content = $etpl->get();
778 //echo $content.$add; exit;
779 return $content . $add;
780 }
781
788 public function renderNode($a_node, $tpl)
789 {
790 $skip = ($this->getSkipRootNode()
791 && $this->getNodeId($this->getRootNode()) == $this->getNodeId($a_node));
792 if (!$skip) {
793 $this->listItemStart($tpl, $a_node);
794
795 // select mode?
796 if ($this->select_postvar != "" && $this->isNodeSelectable($a_node)) {
797 if ($this->select_multi) {
798 $tpl->setCurrentBlock("cb");
799 if (in_array($this->getNodeId($a_node), $this->selected_nodes)) {
800 $tpl->setVariable("CHECKED", 'checked="checked"');
801 }
802 $tpl->setVariable("CB_VAL", $this->getNodeId($a_node));
803 $tpl->setVariable("CB_NAME", $this->select_postvar . "[]");
804 $tpl->parseCurrentBlock();
805 } else {
806 $tpl->setCurrentBlock("rd");
807 if (in_array($this->getNodeId($a_node), $this->selected_nodes)) {
808 $tpl->setVariable("SELECTED", 'checked="checked"');
809 }
810 $tpl->setVariable("RD_VAL", $this->getNodeId($a_node));
811 $tpl->setVariable("RD_NAME", $this->select_postvar);
812 $tpl->parseCurrentBlock();
813 }
814 }
815
816
817 if ($this->isNodeHighlighted($a_node)) {
818 $tpl->touchBlock("hl");
819 }
820 $tpl->setCurrentBlock("content");
821 if ($this->getNodeIcon($a_node) != "") {
822 $tpl->setVariable("ICON", ilUtil::img($this->getNodeIcon($a_node), $this->getNodeIconAlt($a_node)) . " ");
823 }
824 $tpl->setVariable("CONTENT", $this->getNodeContent($a_node));
825 if ($this->isNodeClickable($a_node)) {
826 $tpl->setVariable("HREF", $this->getNodeHref($a_node));
827 }
828 $target = $this->getNodeTarget($a_node);
829 if ($target != "") {
830 $targetRelatedParams = array(
831 'target="' . $target . '"'
832 );
833
834 if ('_blank' === $target) {
835 $targetRelatedParams[] = 'rel="noopener"';
836 }
837
838 $tpl->setVariable('TARGET', implode(' ', $targetRelatedParams));
839 }
840 if (!$this->isNodeOnclickEnabled() || !$this->isNodeClickable($a_node)) {
841 $tpl->setVariable("ONCLICK", 'onclick="return false;"');
842 $tpl->setVariable("A_CLASS", 'class="disabled"');
843 } else {
844 $onclick = $this->getNodeOnClick($a_node);
845 if ($onclick != "") {
846 $tpl->setVariable("ONCLICK", 'onclick="' . $onclick . '"');
847 }
848 }
849 $tpl->parseCurrentBlock();
850
851 $tpl->touchBlock("tag");
852 }
853
854 if (!$this->getAjax() || in_array($this->getNodeId($a_node), $this->open_nodes)
855 || in_array($this->getNodeId($a_node), $this->custom_open_nodes)) {
856 $this->renderChilds($this->getNodeId($a_node), $tpl);
857 }
858
859 if (!$skip) {
860 $this->listItemEnd($tpl);
861 }
862 }
863
870 final public function renderChilds($a_node_id, $tpl)
871 {
872 $childs = $this->getChildsOfNode($a_node_id);
873 $childs = $this->sortChilds($childs, $a_node_id);
874
875 if (count($childs) > 0 || $this->getSearchTerm() != "") {
876 // collect visible childs
877
878 $visible_childs = [];
879 $cnt_child = 0;
880
881 foreach ($childs as $child) {
882 $cnt_child++;
883 if ($this->getChildLimit() > 0 && $this->getChildLimit() < $cnt_child) {
884 continue;
885 }
886
887 if ($this->isNodeVisible($child)) {
888 $visible_childs[] = $child;
889 }
890 }
891
892 // search field, if too many childs
893 $any = false;
894 if ($this->getChildLimit() > 0 && $this->getChildLimit() < $cnt_child
895 || $this->getSearchTerm() != "") {
896 if (!$any) {
897 $this->listStart($tpl);
898 $any = true;
899 }
900 $tpl->setCurrentBlock("list_search");
901 $tpl->setVariable("SEARCH_CONTAINER_ID", $a_node_id);
902 $tpl->setVariable("SEARCH_VAL", $this->getSearchTerm());
903 $tpl->parseCurrentBlock();
904 $tpl->touchBlock("tag");
905 }
906
907 // render visible childs
908 foreach ($visible_childs as $child) {
909 // check child limit
910 $cnt_child++;
911
912 if ($this->isNodeVisible($child)) {
913 if (!$any) {
914 $this->listStart($tpl);
915 $any = true;
916 }
917 $this->renderNode($child, $tpl);
918 }
919 }
920 if ($any) {
921 $this->listEnd($tpl);
922 }
923 }
924 }
925
932 public function getDomNodeIdForNodeId($a_node_id)
933 {
934 return "exp_node_" . $this->getId() . "_" . $a_node_id;
935 }
936
943 public function getNodeIdForDomNodeId($a_dom_node_id)
944 {
945 $i = strlen("exp_node_" . $this->getId() . "_");
946 return substr($a_dom_node_id, $i);
947 }
948
955 public function listItemStart($tpl, $a_node)
956 {
957 $tpl->setCurrentBlock("list_item_start");
958 if ($this->getAjax() && $this->nodeHasVisibleChilds($a_node) && !$this->isNodeOpen($this->getNodeId($a_node))) {
959 $tpl->touchBlock("li_closed");
960 }
961 if ($this->isNodeOpen($this->getNodeId($a_node))) {
962 $tpl->touchBlock("li_opened");
963 }
964
965 $tpl->setVariable(
966 "DOM_NODE_ID",
967 $this->getDomNodeIdForNodeId($this->getNodeId($a_node))
968 );
969 $tpl->parseCurrentBlock();
970 $tpl->touchBlock("tag");
971 }
972
979 public function listItemEnd($tpl)
980 {
981 $tpl->touchBlock("list_item_end");
982 $tpl->touchBlock("tag");
983 }
984
991 public function listStart($tpl)
992 {
993 $tpl->touchBlock("list_start");
994 $tpl->touchBlock("tag");
995 }
996
1003 public function listEnd($tpl)
1004 {
1005 $tpl->touchBlock("list_end");
1006 $tpl->touchBlock("tag");
1007 }
1008
1012 public function isNodeOnclickEnabled()
1013 {
1015 }
1016
1021 {
1022 $this->nodeOnclickEnabled = $nodeOnclickEnabled;
1023 }
1024
1025 public function isEnableDnd()
1026 {
1027 return $this->enable_dnd;
1028 }
1029
1034 public function setEnableDnd($enable_dnd)
1035 {
1036 $this->enable_dnd = $enable_dnd;
1037 }
1038}
exit
Definition: backend.php:16
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
Explorer base GUI class.
setNodeSelected($a_id)
Set node to be opened (additional custom opened node, not standard expand behaviour)
getNodeIcon($a_node)
Get node icon path.
getNodeContent($a_node)
Get content of a node.
getId()
Get id of explorer element.
isNodeSelectable($a_node)
Is node selectable?
getContainerId()
Get container id.
setNodeOnclickEnabled($nodeOnclickEnabled)
getNodeIconAlt($a_node)
Get node icon alt attribute.
getNodeAsync()
Get node asynchronously.
getNodeOnClick($a_node)
Get node onclick attribute.
setSecondaryHighlightedNodes($a_val)
Set secondary (background) highlighted nodes.
static init($a_main_tpl=null)
Init JS.
static getLocalJsTreeCssPath()
Get local path of jsTree js.
beforeRendering()
Before rendering.
setEnableDnd($enable_dnd)
Enable Drag & Drop functionality.
setMainTemplate(ilTemplate $a_main_tpl=null)
Set main template (that is responsible for adding js/css)
getOnLoadCode()
Get on load code.
renderChilds($a_node_id, $tpl)
Render childs.
getChildLimit()
Get child limit.
static getLocalExplorerJsPath()
Get local path of explorer js.
renderNode($a_node, $tpl)
Render node.
getChildsOfNode($a_parent_node_id)
Get childs of node.
isNodeClickable($a_node)
Is node clickable?
getDomNodeIdForNodeId($a_node_id)
Get DOM node id for node id.
getNodeTarget($a_node)
Get node target (frame) attribute.
getNodeIdForDomNodeId($a_dom_node_id)
Get node id for dom node id.
static getLocalJsTreeJsPath()
Get local path of jsTree js.
handleCommand()
Handle explorer internal command.
nodeHasVisibleChilds($a_node)
Node has childs?
setChildLimit($a_val)
Set child limit.
getSecondaryHighlightedNodes()
Get secondary (background) highlighted nodes.
sortChilds($a_childs, $a_parent_node_id)
Sort childs.
getNodeId($a_node)
Get id of a node.
setSearchTerm($a_val)
Set search term.
getSkipRootNode()
Get skip root node.
getSearchTerm()
Get search term.
getNodeToggleOnClick($a_node)
Get onclick attribute for node toggling.
getOfflineMode()
Get offline mode.
setSkipRootNode($a_val)
Set skip root node.
setNodeOpen($a_id)
Set node to be opened (additional custom opened node, not standard expand behaviour)
__construct($a_expl_id, $a_parent_obj, $a_parent_cmd)
Constructor.
getNodeHref($a_node)
Get href for node.
isNodeHighlighted($a_node)
Is node highlighted?
static createHTMLExportDirs($a_target_dir)
Create html export directories.
listItemEnd($tpl)
List item end.
isNodeOpen($node_id)
Get all open nodes.
setSelectMode($a_postvar, $a_multi=false)
Set select mode (to deactivate, pass an empty string as postvar)
getRootNode()
Get root node.
setOfflineMode($a_val)
Set offline mode.
isNodeVisible($a_node)
Is node visible?
getSelectOnClick($a_node)
Get onclick attribute for selecting radio/checkbox.
listItemStart($tpl, $a_node)
List item start.
Session based immediate storage.
special template class to simplify handling of ITX/PEAR
static img($a_src, $a_alt=null, $a_width="", $a_height="", $a_border=0, $a_id="", $a_class="")
Build img tag.
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static makeDirParents($a_dir)
Create a new directory and all parent directories.
static initjQuery($a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
$i
Definition: disco.tpl.php:19
if(!array_key_exists('StateId', $_REQUEST)) $id
global $ilCtrl
Definition: ilias.php:18
$config
Definition: bootstrap.php:15
$target
Definition: test.php:19
$url
$root
Definition: sabredav.php:45
global $DIC
Definition: saml.php:7