ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
base.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
21
30
60function base(): string
61{
62 global $DIC;
63
64 $http = $DIC->http();
65 $factory = $DIC->ui()->factory();
66 $renderer = $DIC->ui()->renderer();
67 $get_request = $http->wrapper()->query();
68 $data_factory = new \ILIAS\Data\Factory();
69 $refinery_factory = new \ILIAS\Refinery\Factory($data_factory, $DIC->language());
70
71 $example_uri = $data_factory->uri((string) $http->request()->getUri());
72 $base_url_builder = new URLBuilder($example_uri);
73 [$node_id_url_builder, $node_id_parameter] = $base_url_builder->acquireParameter(
74 explode('\\', __NAMESPACE__),
75 "node_id"
76 );
77 [$process_form_url_builder, $process_form_parameter] = $base_url_builder->acquireParameter(
78 explode('\\', __NAMESPACE__),
79 "process"
80 );
81
82 $node_retrieval = new TreeSelectExampleNodeRetrieval($node_id_url_builder, $node_id_parameter);
83
84 // simulates an async node rendering endpoint:
85 if ($get_request->has($node_id_parameter->getName())) {
86 $parent_node_id = $get_request->retrieve(
87 $node_id_parameter->getName(),
88 $refinery_factory->kindlyTo()->string(),
89 );
90
91 $node_generator = $node_retrieval->getNodes(
92 $factory->input()->field()->node(),
93 $factory->symbol()->icon(),
94 [],
95 $parent_node_id
96 );
97
98 $html = '';
99 foreach ($node_generator as $node) {
100 $html .= $renderer->renderAsync($node);
101 }
102
103 $http->saveResponse(
104 $http->response()
105 ->withHeader('Content-Type', 'text/html; charset=utf-8')
106 ->withBody(Streams::ofString($html))
107 );
108 $http->sendResponse();
109 $http->close();
110 }
111
112 $input = $factory->input()->field()->treeSelect(
113 $node_retrieval,
114 "select a single node",
115 "you can open the select input by clicking the button above.",
116 );
117
118 $form = $factory->input()->container()->form()->standard(
119 (string) $process_form_url_builder->withParameter($process_form_parameter, '1')->buildURI(),
120 [$input]
121 );
122
123 // simulates a form processing endpoint:
124 if ($get_request->has($process_form_parameter->getName())) {
125 $form = $form->withRequest($http->request());
126 $data = $form->getData();
127 } else {
128 $data = 'No submitted data yet.';
129 }
130
131 return '<pre>' . print_r($data, true) . '</pre>' . $renderer->render($form);
132}
133
136{
137 public function __construct(
138 protected URLBuilder $builder,
139 protected URLBuilderToken $node_id_parameter,
140 ) {
141 }
142
143 public function getNodes(
144 NodeFactory $node_factory,
145 IconFactory $icon_factory,
146 array $sync_node_id_whitelist = [],
147 ?string $parent_id = null
148 ): \Generator {
149 if (null !== $parent_id) {
150 yield $this->getExampleNodeChildren($node_factory, $parent_id);
151 return;
152 }
153
154 yield $node_factory->branch(['1'], 'branch 1', null, ...$this->getExampleNodeChildren($node_factory, '1'));
155 yield $node_factory->branch(['2'], 'branch 2', null, ...$this->getExampleNodeChildren($node_factory, '2'));
156 yield $node_factory->leaf(['3'], 'leaf 3');
157 }
158
159 public function getNodesAsLeaf(
160 NodeFactory $node_factory,
161 IconFactory $icon_factory,
162 array $node_ids,
163 ): \Generator {
164 foreach ($node_ids as $node_id) {
165 yield $node_factory->leaf([$node_id], "dummy leaf node $node_id");
166 }
167 }
168
169 protected function getExampleNodeChildren(NodeFactory $node_factory, string $parent_id): array
170 {
171 return [
172 $node_factory->branch(
173 $this->getFullNodePath($parent_id, "1"),
174 "branch $parent_id.1",
175 null,
176 $node_factory->leaf($this->getFullNodePath($parent_id, "1.1"), "leaf $parent_id.1.1"),
177 $node_factory->leaf($this->getFullNodePath($parent_id, "1.2"), "leaf $parent_id.1.2"),
178 $node_factory->leaf($this->getFullNodePath($parent_id, "1.3"), "leaf $parent_id.1.3"),
179 ),
180 $node_factory->branch(
181 $this->getFullNodePath($parent_id, "2"),
182 "branch $parent_id.2",
183 null,
184 $node_factory->leaf($this->getFullNodePath($parent_id, "2.1"), "leaf $parent_id.2.1"),
185 $node_factory->leaf($this->getFullNodePath($parent_id, "2.2"), "leaf $parent_id.2.2"),
186 $node_factory->leaf($this->getFullNodePath($parent_id, "2.3"), "leaf $parent_id.2.3"),
187 ),
188 $node_factory->async(
189 $this->getAsyncNodeRenderUrl("$parent_id.3"),
190 $this->getFullNodePath($parent_id, "3"),
191 "async branch $parent_id.3"
192 ),
193 $node_factory->leaf($this->getFullNodePath($parent_id, "4"), "leaf $parent_id.4"),
194 ];
195 }
196
198 protected function getFullNodePath(string $parent_id, string $child_id): array
199 {
200 $parts = explode(".", $parent_id);
201 $paths = [];
202 for ($index = 1, $count = count($parts); $index <= $count; $index++) {
203 $paths[] = implode(".", array_slice($parts, 0, $index));
204 }
205 $paths[] = "{$paths[$count - 1]}.$child_id";
206 return $paths;
207 }
208
209 protected function getAsyncNodeRenderUrl(int|string $node_id): URI
210 {
211 return $this->builder->withParameter($this->node_id_parameter, (string) $node_id)->buildURI();
212 }
213}
$renderer
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:387
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
getNodes(NodeFactory $node_factory, IconFactory $icon_factory, array $sync_node_id_whitelist=[], ?string $parent_id=null)
This method will be called by the tree select input and tree multi select input to generate the tree ...
Definition: base.php:143
getNodesAsLeaf(NodeFactory $node_factory, IconFactory $icon_factory, array $node_ids,)
This method will be called by the UI framework in order to retrieve Leaf instances for provided/submi...
Definition: base.php:159
__construct(protected URLBuilder $builder, protected URLBuilderToken $node_id_parameter,)
Definition: base.php:137
getExampleNodeChildren(NodeFactory $node_factory, string $parent_id)
Definition: base.php:169
$http
Definition: deliver.php:30
This is how a factory for icons looks like.
Definition: Factory.php:27
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
if(!file_exists('../ilias.ini.php'))