ILIAS  release_8 Revision v8.24
class.ilTreeExplorerGUI.php
Go to the documentation of this file.
1<?php
2
20
27{
28 protected ilLanguage $lng;
29 protected \Psr\Http\Message\ServerRequestInterface $httpRequest;
30 protected ?ilTree $tree = null;
31 protected string $tree_label = "";
32 protected string $order_field = "";
33 protected bool $order_field_numeric = false;
34 protected array $type_white_list = array();
35 protected array $type_black_list = array();
36 protected array $childs = array(); // preloaded childs
37 protected bool $preloaded = false;
38 protected bool $preload_childs = false;
39 protected ?array $root_node_data = null;
40 protected array $all_childs = array();
41 protected $root_id = 0;
42 protected \ILIAS\DI\UIServices $ui;
43
44 public function __construct(
45 string $a_expl_id,
46 $a_parent_obj,
47 string $a_parent_cmd,
48 ilTree $a_tree,
49 string $a_node_parameter_name = "node_id"
50 ) {
51 global $DIC;
52
53 $this->httpRequest = $DIC->http()->request();
54 $this->ui = $DIC->ui();
55 $this->lng = $DIC->language();
56 parent::__construct($a_expl_id, $a_parent_obj, $a_parent_cmd, $a_node_parameter_name);
57 $this->tree = $a_tree;
58 }
59
60 public function getTree(): ilTree
61 {
62 return $this->tree;
63 }
64
65 public function setOrderField(
66 string $a_val,
67 bool $a_numeric = false
68 ): void {
69 $this->order_field = $a_val;
70 $this->order_field_numeric = $a_numeric;
71 }
72
73 public function getOrderField(): string
74 {
75 return $this->order_field;
76 }
77
82 public function setTypeWhiteList(array $a_val): void
83 {
84 $this->type_white_list = $a_val;
85 }
86
91 public function getTypeWhiteList(): array
92 {
93 return $this->type_white_list;
94 }
95
100 public function setTypeBlackList(array $a_val): void
101 {
102 $this->type_black_list = $a_val;
103 }
104
109 public function getTypeBlackList(): array
110 {
111 return $this->type_black_list;
112 }
113
114 public function setPreloadChilds(bool $a_val): void
115 {
116 $this->preload_childs = $a_val;
117 }
118
119 public function getPreloadChilds(): bool
120 {
121 return $this->preload_childs;
122 }
123
124 protected function preloadChilds(): void
125 {
126 $subtree = $this->tree->getSubTree($this->getRootNode());
127 foreach ($subtree as $s) {
128 $wl = $this->getTypeWhiteList();
129 if (is_array($wl) && count($wl) > 0 && !in_array($s["type"], $wl)) {
130 continue;
131 }
132 $bl = $this->getTypeBlackList();
133 if (is_array($bl) && count($bl) > 0 && in_array($s["type"], $bl)) {
134 continue;
135 }
136 $this->childs[$s["parent"]][] = $s;
137 $this->all_childs[$s["child"]] = $s;
138 }
139
140 if ($this->order_field !== "") {
141 foreach ($this->childs as $k => $childs) {
142 $this->childs[$k] = ilArrayUtil::sortArray(
143 $childs,
144 $this->order_field,
145 "asc",
146 $this->order_field_numeric
147 );
148 }
149 }
150
151 // sort childs and store prev/next reference
152 if ($this->order_field === "") {
153 $this->all_childs =
154 ilArrayUtil::sortArray($this->all_childs, "lft", "asc", true, true);
155 $prev = false;
156 foreach ($this->all_childs as $k => $c) {
157 if ($prev) {
158 $this->all_childs[$prev]["next_node_id"] = $k;
159 }
160 $this->all_childs[$k]["prev_node_id"] = $prev;
161 $this->all_childs[$k]["next_node_id"] = false;
162 $prev = $k;
163 }
164 }
165
166 $this->preloaded = true;
167 }
168
169
170
177 public function getSuccessorNode(
178 $a_node_id,
179 string $a_type = ""
180 ) {
181 if ($this->order_field !== "") {
182 die("ilTreeExplorerGUI::getSuccessorNode not implemented for order field " . $this->order_field);
183 }
184
185 if ($this->preloaded) {
186 $next_id = $a_node_id;
187 while (($next_id = $this->all_childs[$next_id]["next_node_id"]) && $a_type !== "" &&
188 $this->all_childs[$next_id]["type"] !== $a_type) {
189 // do nothing
190 }
191 if ($next_id) {
192 return $this->all_childs[$next_id];
193 }
194 return false;
195 }
196 return $this->getTree()->fetchSuccessorNode($a_node_id, $a_type);
197 }
198
199
200
206 public function getChildsOfNode($a_parent_node_id): array
207 {
208 if ($this->preloaded && $this->getSearchTerm() === "") {
209 if (isset($this->childs[$a_parent_node_id]) && is_array($this->childs[$a_parent_node_id])) {
210 return $this->childs[$a_parent_node_id];
211 }
212 return array();
213 }
214
215 $wl = $this->getTypeWhiteList();
216 if (count($wl) > 0) {
217 $childs = $this->tree->getChildsByTypeFilter($a_parent_node_id, $wl, $this->getOrderField());
218 } else {
219 $childs = $this->tree->getChilds($a_parent_node_id, $this->getOrderField());
220 }
221
222 // apply black list filter
223 $bl = $this->getTypeBlackList();
224 if (is_array($bl) && count($bl) > 0) {
225 $bl_childs = array();
226 foreach ($childs as $k => $c) {
227 if (!in_array($c["type"], $bl, true) && ($this->matches($c) || !$this->isNodeRequested($a_parent_node_id))) {
228 $bl_childs[$k] = $c;
229 }
230 }
231 return $bl_childs;
232 }
233
234 $final_childs = [];
235 foreach ($childs as $k => $c) {
236 if ($this->matches($c) || !$this->isNodeRequested($a_parent_node_id)) {
237 $final_childs[$k] = $c;
238 }
239 }
240 return $final_childs;
241 }
242
248 protected function matches($node): bool
249 {
250 return (
251 $this->getSearchTerm() === "" ||
252 is_int(ilStr::striPos($this->getNodeContent($node), $this->getSearchTerm()))
253 );
254 }
255
256
263 public function getNodeId($a_node)
264 {
265 return $a_node["child"];
266 }
267
273 public function getNodeIconAlt($a_node): string
274 {
276
277 return $lng->txt("icon") . " " . $lng->txt("obj_" . ($a_node["type"] ?? ''));
278 }
279
285 public function getRootNode()
286 {
287 if (!isset($this->root_node_data)) {
288 $this->root_node_data = $this->getTree()->getNodeData($this->getRootId());
289 }
290 return $this->root_node_data;
291 }
292
296 public function setRootId($a_root): void
297 {
298 $this->root_id = $a_root;
299 }
300
301 protected function getRootId(): int
302 {
303 return $this->root_id
304 ?: $this->getTree()->readRootId();
305 }
306
312 public function setPathOpen($a_id): void
313 {
314 $path = $this->getTree()->getPathId($a_id);
315 foreach ($path as $id) {
316 $this->setNodeOpen($id);
317 }
318 }
319
325 public function getHTML($new = false): string
326 {
327 if ($this->getPreloadChilds()) {
328 $this->preloadChilds();
329 }
330 if (!$new) {
331 return parent::getHTML();
332 }
333 return $this->render();
334 }
335
336 // New implementation
337
338 public function getChildren($record, $environment = null): array
339 {
340 return $this->getChildsOfNode($record["child"]);
341 }
342
346 protected function createNode(
347 \ILIAS\UI\Component\Tree\Node\Factory $factory,
348 $record
349 ): \ILIAS\UI\Component\Tree\Node\Node {
350 $nodeIconPath = $this->getNodeIcon($record);
351
352 $icon = null;
353 if ($nodeIconPath !== '') {
354 $icon = $this->ui
355 ->factory()
356 ->symbol()
357 ->icon()
358 ->custom($nodeIconPath, $this->getNodeIconAlt($record));
359 }
360
361 return $factory->simple($this->getNodeContent($record), $icon);
362 }
363
370 protected function getNodeStateToggleCmdClasses($record): array
371 {
372 return [];
373 }
374
375 public function build(
376 \ILIAS\UI\Component\Tree\Node\Factory $factory,
377 $record,
378 $environment = null
379 ): \ILIAS\UI\Component\Tree\Node\Node {
380 $node = $this->createNode($factory, $record);
381
382 $href = $this->getNodeHref($record);
383 if ($href !== '' && '#' !== $href) {
384 $node = $node->withLink(new \ILIAS\Data\URI(ILIAS_HTTP_PATH . '/' . $href));
385 }
386
387 if ($this->isNodeOpen((int) $this->getNodeId($record))) {
388 $node = $node->withExpanded(true);
389 }
390
391 $nodeStateToggleCmdClasses = $this->getNodeStateToggleCmdClasses($record);
392 $cmdClass = end($nodeStateToggleCmdClasses);
393
394 if (is_string($cmdClass) && $cmdClass !== '') {
395 $node = $node->withAdditionalOnLoadCode(function ($id) use ($record, $nodeStateToggleCmdClasses, $cmdClass): string {
396 $serverNodeId = $this->getNodeId($record);
397
398 $this->ctrl->setParameterByClass($cmdClass, $this->node_parameter_name, $serverNodeId);
399 $url = $this->ctrl->getLinkTargetByClass($nodeStateToggleCmdClasses, 'toggleExplorerNodeState', '', true, false);
400 $this->ctrl->setParameterByClass($cmdClass, $this->node_parameter_name, null);
401
402 $javascript = "il.UI.tree.registerToggleNodeAsyncAction('$id', '$url', 'prior_state');";
403
404 return $javascript;
405 });
406 }
407
408 return $node;
409 }
410
411 public function getTreeLabel(): string
412 {
413 return $this->tree_label;
414 }
415
416 public function getTreeComponent(): Tree
417 {
418 $f = $this->ui->factory();
419 $tree = $this->getTree();
420
421 $data = array(
422 $tree->getNodeData($tree->readRootId())
423 );
424
425 $label = $this->getTreeLabel();
426 if ($this->getTreeLabel() === "" && $this->getNodeContent($this->getRootNode())) {
427 $label = $this->getNodeContent($this->getRootNode());
428 }
429
430 $tree = $f->tree()->expandable($label, $this)
431 ->withData($data)
432 ->withHighlightOnNodeClick(true);
433
434 return $tree;
435 }
436
440 public function toggleExplorerNodeState(): void
441 {
442 $nodeId = (int) ($this->httpRequest->getQueryParams()[$this->node_parameter_name] ?? 0);
443 $priorState = (int) ($this->httpRequest->getQueryParams()['prior_state'] ?? 0);
444
445 if ($nodeId > 0) {
446 if (0 === $priorState && !in_array($nodeId, $this->open_nodes)) {
447 $this->open_nodes[] = $nodeId;
448 } elseif (1 === $priorState && in_array($nodeId, $this->open_nodes)) {
449 $key = array_search($nodeId, $this->open_nodes);
450 unset($this->open_nodes[$key]);
451 }
452
453 $this->store->set('on_' . $this->id, serialize($this->open_nodes));
454 }
455 exit();
456 }
457
458 protected function render(): string
459 {
460 $r = $this->ui->renderer();
461
462 return $r->render([
463 $this->getTreeComponent()
464 ]);
465 }
466}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static sortArray(array $array, string $a_array_sortby_key, string $a_array_sortorder="asc", bool $a_numeric=false, bool $a_keep_keys=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
Explorer class that works on tree objects (Services/Tree)
getNodeStateToggleCmdClasses($record)
Should return an array of ilCtrl-enabled command classes which should be used to build the URL for th...
setOrderField(string $a_val, bool $a_numeric=false)
matches($node)
Does a node match a search term (or is search term empty)
getChildren($record, $environment=null)
Get a list of records (that list can also be empty).
createNode(\ILIAS\UI\Component\Tree\Node\Factory $factory, $record)
Creates at tree node, can be overwritten in derivatives if another node type should be used.
setPathOpen($a_id)
Set node path to be opened.
getTypeWhiteList()
Get type white list.
getNodeId($a_node)
Get id for node.
getSuccessorNode( $a_node_id, string $a_type="")
Get successor node (currently only(!) based on lft/rgt tree values)
getChildsOfNode($a_parent_node_id)
Get childs of node.
toggleExplorerNodeState()
Should be called by an ilCtrl-enabled command class if a tree node toggle action should be processed.
getTypeBlackList()
Get type black list.
__construct(string $a_expl_id, $a_parent_obj, string $a_parent_cmd, ilTree $a_tree, string $a_node_parameter_name="node_id")
getNodeIconAlt($a_node)
Get node icon alt attribute.
setTypeBlackList(array $a_val)
Set type black list.
build(\ILIAS\UI\Component\Tree\Node\Factory $factory, $record, $environment=null)
ILIAS DI UIServices $ui
setTypeWhiteList(array $a_val)
Set type white list.
getHTML($new=false)
Get HTML.
Psr Http Message ServerRequestInterface $httpRequest
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getNodeData(int $a_node_id, ?int $a_tree_pk=null)
get all information of a node.
$c
Definition: cli.php:38
global $DIC
Definition: feed.php:28
Interface for mapping data-structures to the Tree.
This describes a Tree Control.
Definition: Tree.php:29
exit
Definition: login.php:28
$path
Definition: ltiservices.php:32
$factory
Definition: metadata.php:75
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.
$url
$lng