ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
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['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'vtt', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
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).
FilterType
Values should always be all lowercase.
Definition: FilterType.php:27
foreach($_POST as $key=> $value) $res
if(!file_exists('../ilias.ini.php'))