ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Recursion.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use ILIAS\MetaData\Paths\FactoryInterface as PathFactoryInterface;
38
42class Recursion implements TreeRecursion
43{
44 protected const int MAX_LENGTH = 128;
45
49 protected array $current_elements;
50
51 protected PathFactoryInterface $path_factory;
54 protected LinkFactory $link_factory;
55
56 public function __construct(
57 PathFactoryInterface $path_factory,
60 LinkFactory $link_factory,
62 ) {
63 $this->path_factory = $path_factory;
64 $this->presenter = $presenter;
65 $this->dictionary = $dictionary;
66 $this->link_factory = $link_factory;
67
68 $this->current_elements = $current_elements;
69 }
70
71 public function getChildren($record, $environment = null): array
72 {
76 $tag = $this->dictionary->tagForElement(
77 is_array($record) ? $record[0] : $record
78 );
79 if ($tag?->isLastInTree()) {
80 return [];
81 }
82 if (!is_array($record)) {
83 return $this->getCollectedSubElements($record);
84 }
85 return $record;
86 }
87
91 protected function getCollectedSubElements(
92 ElementInterface $element
93 ): array {
94 $res = [];
95 foreach ($element->getSubElements() as $sub) {
96 if ($sub->isScaffold()) {
97 continue;
98 }
99 $tag = $this->dictionary->tagForElement($sub);
100 if ($tag?->isCollected()) {
101 $res[$sub->getDefinition()->name()][] = $sub;
102 continue;
103 }
104 $res[] = $sub;
105 }
106 return $res;
107 }
108
109 public function build(
110 Factory $factory,
111 $record,
112 $environment = null
113 ): Node {
117 $elements = is_array($record) ? $record : [$record];
118 $tag = $this->dictionary->tagForElement($elements[0]);
119 $is_collection = is_array($record);
120 $is_last_in_tree = (bool) $tag?->isLastInTree();
121 $is_root_or_directly_under = $elements[0]->getSuperElement()?->isRoot() ?? true;
122
123 $with_extended_info = !$is_root_or_directly_under || !$is_collection;
124 $label = $this->getNameWithRepresentation(
125 $with_extended_info,
126 $is_collection,
127 ...$elements
128 );
129 $value = $this->getPreview(
130 $with_extended_info,
131 ...$elements
132 );
133
134 $is_linked = !$is_collection || $is_last_in_tree;
135 $node = $factory
136 ->keyValue($label, $value)
137 ->withExpanded($this->isExpanded(...$elements))
138 ->withHighlighted($this->isHighlighted($is_linked, ...$elements));
139
140 if ($is_linked) {
141 $node = $node->withLink(
142 $this->getLink(is_array($record), ...$elements)
143 );
144 }
145
146 return $node;
147 }
148
149 protected function isExpanded(ElementInterface ...$elements): bool
150 {
151 $current_and_parents = [$curr = $this->current_elements[0]];
152 while (!$curr->isRoot()) {
153 $curr = $curr->getSuperElement();
154 $current_and_parents[] = $curr;
155 }
156 foreach ($elements as $el) {
157 if (in_array($el, $current_and_parents, true)) {
158 return true;
159 }
160 }
161 return false;
162 }
163
164 protected function isHighlighted(
165 bool $can_be_highlighted,
166 ElementInterface ...$elements
167 ): bool {
168 return $can_be_highlighted && in_array($this->current_elements[0], $elements, true);
169 }
170
171 protected function getNameWithRepresentation(
172 bool $with_representation,
173 bool $plural,
174 ElementInterface ...$elements
175 ): string {
176 if (!$with_representation) {
177 $name = $this->presenter->elements()->name(
178 $elements[0],
179 $plural
180 );
181 } else {
182 $name = $this->presenter->elements()->nameWithRepresentation(
183 $plural,
184 ...$elements
185 );
186 }
187 return $this->presenter->utilities()->shortenString(
188 $name,
189 self::MAX_LENGTH
190 );
191 }
192
193 protected function getPreview(
194 bool $with_preview,
195 ElementInterface ...$elements
196 ): string {
197 if (!$with_preview) {
198 return '';
199 }
200 return $this->presenter->utilities()->shortenString(
201 $this->presenter->elements()->preview(...$elements),
202 self::MAX_LENGTH
203 );
204 }
205
206 protected function getLink(
207 bool $record_is_array,
208 ElementInterface ...$elements
209 ): URI {
210 $builder = $this->path_factory->custom();
211 $el = $elements[0];
212 $skip_last_id = $record_is_array;
213
214 while (!$el->isRoot()) {
215 $builder = $builder->withNextStep(
216 $el->getDefinition()->name(),
217 true
218 );
219 if (!$skip_last_id) {
220 $builder = $builder->withAdditionalFilterAtCurrentStep(
221 FilterType::MDID,
222 (string) $el->getMDID()
223 );
224 }
225 $el = $el->getSuperElement();
226 if (is_null($el)) {
227 throw new \ilMDEditorException('Invalid md set when building tree');
228 }
229 $skip_last_id = false;
230 }
231 $path_string = $builder->get()->toString();
232 return $this->link_factory->custom(Command::SHOW_FULL)
233 ->withParameter(Parameter::BASE_PATH, $path_string)
234 ->get();
235 }
236}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Tree Recursion, putting Entries into a Tree.
Definition: Recursion.php:43
PathFactoryInterface $path_factory
Definition: Recursion.php:51
__construct(PathFactoryInterface $path_factory, PresenterInterface $presenter, DictionaryInterface $dictionary, LinkFactory $link_factory, ElementInterface ... $current_elements)
Definition: Recursion.php:56
getPreview(bool $with_preview, ElementInterface ... $elements)
Definition: Recursion.php:193
isHighlighted(bool $can_be_highlighted, ElementInterface ... $elements)
Definition: Recursion.php:164
isExpanded(ElementInterface ... $elements)
Definition: Recursion.php:149
getCollectedSubElements(ElementInterface $element)
Definition: Recursion.php:91
DictionaryInterface $dictionary
Definition: Recursion.php:53
getLink(bool $record_is_array, ElementInterface ... $elements)
Definition: Recursion.php:206
getNameWithRepresentation(bool $with_representation, bool $plural, ElementInterface ... $elements)
Definition: Recursion.php:171
return true
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes a Tree Node.
Definition: Node.php:31
Interface for mapping data-structures to the Tree.
getChildren( $record, $environment=null)
Get a list of records (that list can also be empty).
$res
Definition: ltiservices.php:69
FilterType
Values should always be all lowercase.
Definition: FilterType.php:27
if(!file_exists('../ilias.ini.php'))