ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilTreeExplorerGUI.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 include_once("./Services/UIComponent/Explorer2/classes/class.ilExplorerBaseGUI.php");
5 
14 abstract class ilTreeExplorerGUI extends ilExplorerBaseGUI
15 {
19  protected $lng;
20 
21  protected $tree = null;
22  protected $order_field = "";
23  protected $order_field_numeric = false;
24  protected $type_white_list = array();
25  protected $type_black_list = array();
26  protected $childs = array(); // preloaded childs
27  protected $preloaded = false;
28  protected $preload_childs = false;
29  protected $root_node_data = null;
30  protected $all_childs = array();
31 
35  public function __construct($a_expl_id, $a_parent_obj, $a_parent_cmd, $a_tree)
36  {
37  global $DIC;
38 
39  $this->lng = $DIC->language();
40  parent::__construct($a_expl_id, $a_parent_obj, $a_parent_cmd);
41  $this->tree = $a_tree;
42  }
43 
49  public function getTree()
50  {
51  return $this->tree;
52  }
53 
59  public function setOrderField($a_val, $a_numeric = false)
60  {
61  $this->order_field = $a_val;
62  $this->order_field_numeric = $a_numeric;
63  }
64 
70  public function getOrderField()
71  {
72  return $this->order_field;
73  }
74 
80  public function setTypeWhiteList($a_val)
81  {
82  $this->type_white_list = $a_val;
83  }
84 
90  public function getTypeWhiteList()
91  {
93  }
94 
100  public function setTypeBlackList($a_val)
101  {
102  $this->type_black_list = $a_val;
103  }
104 
110  public function getTypeBlackList()
111  {
112  return $this->type_black_list;
113  }
114 
120  public function setPreloadChilds($a_val)
121  {
122  $this->preload_childs = $a_val;
123  }
124 
130  public function getPreloadChilds()
131  {
132  return $this->preload_childs;
133  }
134 
138  protected function preloadChilds()
139  {
140  $subtree = $this->tree->getSubTree($this->getRootNode());
141  foreach ($subtree as $s) {
142  $wl = $this->getTypeWhiteList();
143  if (is_array($wl) && count($wl) > 0 && !in_array($s["type"], $wl)) {
144  continue;
145  }
146  $bl = $this->getTypeBlackList();
147  if (is_array($bl) && count($bl) > 0 && in_array($s["type"], $bl)) {
148  continue;
149  }
150  $this->childs[$s["parent"]][] = $s;
151  $this->all_childs[$s["child"]] = $s;
152  }
153 
154  if ($this->order_field != "") {
155  foreach ($this->childs as $k => $childs) {
156  $this->childs[$k] = ilUtil::sortArray($childs, $this->order_field, "asc", $this->order_field_numeric);
157  }
158  }
159 
160  // sort childs and store prev/next reference
161  if ($this->order_field == "") {
162  $this->all_childs =
163  ilUtil::sortArray($this->all_childs, "lft", "asc", true, true);
164  $prev = false;
165  foreach ($this->all_childs as $k => $c) {
166  if ($prev) {
167  $this->all_childs[$prev]["next_node_id"] = $k;
168  }
169  $this->all_childs[$k]["prev_node_id"] = $prev;
170  $this->all_childs[$k]["next_node_id"] = false;
171  $prev = $k;
172  }
173  }
174 
175  $this->preloaded = true;
176  }
177 
178 
179 
187  public function getSuccessorNode($a_node_id, $a_type = "")
188  {
189  if ($this->order_field != "") {
190  die("ilTreeExplorerGUI::getSuccessorNode not implemented for order field " . $this->order_field);
191  }
192 
193  if ($this->preloaded) {
194  $next_id = $a_node_id;
195  while (($next_id = $this->all_childs[$next_id]["next_node_id"]) && $a_type != "" &&
196  $this->all_childs[$next_id]["type"] != $a_type);
197  if ($next_id) {
198  return $this->all_childs[$next_id];
199  }
200  return false;
201  }
202  return $this->getTree()->fetchSuccessorNode($a_node_id, $a_type);
203  }
204 
205 
206 
213  public function getChildsOfNode($a_parent_node_id)
214  {
215  if ($this->preloaded && $this->getSearchTerm() == "") {
216  if (is_array($this->childs[$a_parent_node_id])) {
217  return $this->childs[$a_parent_node_id];
218  }
219  return array();
220  }
221 
222  $wl = $this->getTypeWhiteList();
223  if (is_array($wl) && count($wl) > 0) {
224  $childs = $this->tree->getChildsByTypeFilter($a_parent_node_id, $wl, $this->getOrderField());
225  } else {
226  $childs = $this->tree->getChilds($a_parent_node_id, $this->getOrderField());
227  }
228 
229  // apply black list filter
230  $bl = $this->getTypeBlackList();
231  if (is_array($bl) && count($bl) > 0) {
232  $bl_childs = array();
233  foreach ($childs as $k => $c) {
234  if (!in_array($c["type"], $bl) && $this->matches($c)) {
235  $bl_childs[$k] = $c;
236  }
237  }
238  return $bl_childs;
239  }
240 
241  $final_childs = [];
242  foreach ($childs as $k => $c) {
243  if ($this->matches($c)) {
244  $final_childs[$k] = $c;
245  }
246  }
247 
248  return $final_childs;
249  }
250 
257  protected function matches($node) : bool
258  {
259  if ($this->getSearchTerm() == "" ||
260  is_int(stripos($this->getNodeContent($node), $this->getSearchTerm()))) {
261  return true;
262  }
263  return false;
264  }
265 
266 
273  public function getNodeId($a_node)
274  {
275  return $a_node["child"];
276  }
277 
284  public function getNodeIconAlt($a_node)
285  {
286  $lng = $this->lng;
287 
288  return $lng->txt("icon") . " " . $lng->txt("obj_" . $a_node["type"]);
289  }
290 
296  public function getRootNode()
297  {
298  if (!isset($this->root_node_data)) {
299  $this->root_node_data = $this->getTree()->getNodeData($this->getRootId());
300  }
301  return $this->root_node_data;
302  }
303 
304  public function setRootId($a_root)
305  {
306  $this->root_id = $a_root;
307  }
308 
309  protected function getRootId()
310  {
311  return $this->root_id
312  ? $this->root_id
313  : $this->getTree()->readRootId();
314  }
315 
321  public function setPathOpen($a_id)
322  {
323  $path = $this->getTree()->getPathId($a_id);
324  foreach ($path as $id) {
325  $this->setNodeOpen($id);
326  }
327  }
328 
334  public function getHTML()
335  {
336  if ($this->getPreloadChilds()) {
337  $this->preloadChilds();
338  }
339  return parent::getHTML();
340  }
341 }
getSuccessorNode($a_node_id, $a_type="")
Get successor node (currently only(!) based on lft/rgt tree values)
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
$path
Definition: aliased.php:25
preloadChilds()
Preload childs.
getPreloadChilds()
Get preload childs.
__construct($a_expl_id, $a_parent_obj, $a_parent_cmd, $a_tree)
Constructor.
global $DIC
Definition: saml.php:7
getTypeBlackList()
Get type black list.
matches($node)
Does a node match a search term (or is search term empty)
if(!array_key_exists('StateId', $_REQUEST)) $id
getSearchTerm()
Get search term.
$s
Definition: pwgen.php:45
setPathOpen($a_id)
Set node path to be opened.
setNodeOpen($a_id)
Set node to be opened (additional custom opened node, not standard expand behaviour) ...
setTypeWhiteList($a_val)
Set type white list.
setPreloadChilds($a_val)
Set preload childs.
$a_type
Definition: workflow.php:92
setTypeBlackList($a_val)
Set type black list.
Explorer base GUI class.
getNodeIconAlt($a_node)
Get node icon alt attribute.
getNodeId($a_node)
Get id for node.
getNodeContent($a_node)
Get content of a node.
getRootNode()
Get root node.
getOrderField()
Get order field.
Explorer class that works on tree objects (Services/Tree)
setOrderField($a_val, $a_numeric=false)
Set order field.
getChildsOfNode($a_parent_node_id)
Get childs of node.
getTypeWhiteList()
Get type white list.