ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
NodeRetrieval.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
35class NodeRetrieval implements UINodeRetrieval
36{
38 protected const string NODE_SORT_BY = 'title';
39
41 protected array $node_object_type_list;
42
47 public function __construct(
48 protected Language $language,
49 protected \ILIAS\Data\Factory $data_factory,
50 protected URLBuilderToken $async_node_id_parameter,
51 protected URLBuilder $async_node_url_builder,
52 protected \ilAccess $access,
53 protected \ilTree $tree,
54 protected array $branch_object_type_list,
55 protected array $leaf_object_type_list,
56 protected int $max_branch_node_depth,
57 protected int $root_node_id,
58 ) {
59 $this->node_object_type_list = array_merge($this->branch_object_type_list, $this->leaf_object_type_list);
60 }
61
62 public function getNodes(
63 NodeFactory $node_factory,
64 IconFactory $icon_factory,
65 ?string $parent_id = null,
66 ): \Generator {
67 yield from $this->createNodes($node_factory, $icon_factory, (int) ($parent_id ?? $this->root_node_id));
68 }
69
70 public function getNodesAsLeaf(
71 NodeFactory $node_factory,
72 IconFactory $icon_factory,
73 array $node_ids,
74 ): \Generator {
75 foreach ($node_ids as $node_id) {
76 try {
77 $tree_node_data = $this->tree->getNodeData((int) $node_id);
78 [$object_ref_id, $object_type, $object_title] = $this->extractObjectDataOrAbort($tree_node_data);
79 yield $node_factory->leaf(
80 $object_ref_id,
81 $object_title,
82 $this->getObjectIcon($icon_factory, $object_ref_id, $object_type),
83 );
84 } catch (\LogicException) {
85 yield $node_factory->leaf($node_id, $this->language->txt('unknown'));
86 }
87 }
88 }
89
91 protected function createNodes(
92 NodeFactory $node_factory,
93 IconFactory $icon_factory,
94 int $parent_object_id,
95 int $depth = 0,
96 ): array {
97 $children = $this->tree->getChildsByTypeFilter(
98 $parent_object_id,
99 $this->node_object_type_list,
100 self::NODE_SORT_BY,
101 );
102
103 $nodes = [];
104 foreach ($children as $child) {
105 [$object_ref_id, $object_type, $object_title] = $this->extractObjectDataOrAbort($child);
106
107 $is_object_visible = $this->isObjectVisible($object_ref_id, $object_type);
108 $is_object_container = $this->isObjectContainer($object_type);
109
110 // append visible children of an invisible branch node to the current node.
111 if (!$is_object_visible && $is_object_container) {
112 $nodes = [...$nodes, ...$this->createNodes($node_factory, $icon_factory, $object_ref_id, $depth + 1)];
113 continue;
114 }
115
116 $object_icon = $this->getObjectIcon($icon_factory, $object_ref_id, $object_type);
117
118 if ($is_object_visible && $is_object_container) {
119 if ($depth < $this->max_branch_node_depth) {
120 $nodes[] = $node_factory->branch(
121 $object_ref_id,
122 $object_title,
123 $object_icon,
124 ...$this->createNodes($node_factory, $icon_factory, $object_ref_id, $depth + 1),
125 );
126 } else {
127 $nodes[] = $node_factory->async(
128 $this->getAsyncRenderUrl($object_ref_id),
129 $object_ref_id,
130 $object_title,
131 $object_icon,
132 );
133 }
134 continue;
135 }
136 if ($is_object_visible && !$is_object_container) {
137 $nodes[] = $node_factory->leaf($object_ref_id, $object_title, $object_icon);
138 }
139 }
140 return $nodes;
141 }
142
144 protected function extractObjectDataOrAbort(array $tree_node_data): array
145 {
146 $object_ref_id = $tree_node_data['ref_id'] ?? null;
147 $object_type = $tree_node_data['type'] ?? null;
148 $object_title = $tree_node_data['title'] ?? null;
149 if (in_array(null, [$object_ref_id, $object_type, $object_title], true)) {
150 throw new \LogicException("Object data is invalid");
151 }
152 return [$object_ref_id, $object_type, $object_title];
153 }
154
155 protected function getAsyncRenderUrl(int $object_ref_id): URI
156 {
157 $async_node_url_builder = $this->async_node_url_builder->withParameter(
158 $this->async_node_id_parameter,
159 (string) $object_ref_id
160 );
161 return $this->data_factory->uri((string) $async_node_url_builder->buildURI());
162 }
163
164 protected function getObjectIcon(IconFactory $icon_factory, int $object_ref_id, string $object_type): Icon
165 {
166 // ensures plugin object types are properly handled as well.
167 $icon_path = \ilObject::_getIcon(\ilObject::_lookupObjectId($object_ref_id), Icon::SMALL);
168
169 if ($this->tree->getRootId() >= $object_ref_id) {
170 $alt_text = $this->language->txt('repository');
171 } else {
172 $alt_text = $this->language->txt($object_type);
173 }
174 return $icon_factory->custom($icon_path, $alt_text, Icon::SMALL);
175 }
176
177 protected function isObjectVisible(int $object_ref_id, string $object_type = ''): bool
178 {
179 return $this->access->checkAccess('visible', '', $object_ref_id, $object_type);
180 }
181
182 protected function isObjectContainer(string $object_type): bool
183 {
184 return in_array($object_type, $this->branch_object_type_list, true);
185 }
186
187}
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
withParameter(string $key, $value)
Get URI with modified parameters.
Definition: URI.php:388
Class ilAccessHandler Checks access for ILIAS objects.
static _lookupObjectId(int $ref_id)
static _getIcon(int $obj_id=0, string $size="big", string $type="", bool $offline=false)
Get icon for repository item.
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
This is how a factory for icons looks like.
Definition: Factory.php:27
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
This is how the factory for UI elements looks.
Definition: Factory.php:38
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.