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
62function base(): string
63{
64 global $DIC;
65
66 $http = $DIC->http();
67 $factory = $DIC->ui()->factory();
68 $renderer = $DIC->ui()->renderer();
69 $get_request = $http->wrapper()->query();
70 $data_factory = new \ILIAS\Data\Factory();
71 $refinery_factory = new \ILIAS\Refinery\Factory($data_factory, $DIC->language());
72
73 $example_uri = $data_factory->uri((string) $http->request()->getUri());
74 $base_url_builder = new URLBuilder($example_uri);
75 [$node_id_url_builder, $node_id_parameter] = $base_url_builder->acquireParameter(
76 explode('\\', __NAMESPACE__),
77 "node_id"
78 );
79 [$process_form_url_builder, $process_form_parameter] = $base_url_builder->acquireParameter(
80 explode('\\', __NAMESPACE__),
81 "process"
82 );
83
84 $node_retrieval = new TreeMultiSelectExampleNodeRetrieval($node_id_url_builder, $node_id_parameter);
85
86 // simulates an async node rendering endpoint:
87 if ($get_request->has($node_id_parameter->getName())) {
88 $parent_node_id = $get_request->retrieve(
89 $node_id_parameter->getName(),
90 $refinery_factory->kindlyTo()->string(),
91 );
92
93 $node_generator = $node_retrieval->getNodes(
94 $factory->input()->field()->node(),
95 $factory->symbol()->icon(),
96 [],
97 $parent_node_id
98 );
99
100 $html = '';
101 foreach ($node_generator as $node) {
102 $html .= $renderer->renderAsync($node);
103 }
104
105 $http->saveResponse(
106 $http->response()
107 ->withHeader('Content-Type', 'text/html; charset=utf-8')
108 ->withBody(Streams::ofString($html))
109 );
110 $http->sendResponse();
111 $http->close();
112 }
113
114 $input = $factory->input()->field()->treeMultiSelect(
115 $node_retrieval,
116 "select multiple nodes",
117 "you can open the select input by clicking the button above.",
118 );
119
120 $form = $factory->input()->container()->form()->standard(
121 (string) $process_form_url_builder->withParameter($process_form_parameter, '1')->buildURI(),
122 [$input]
123 );
124
125 // simulates a form processing endpoint:
126 if ($get_request->has($process_form_parameter->getName())) {
127 $form = $form->withRequest($http->request());
128 $data = $form->getData();
129 } else {
130 $data = 'No submitted data yet.';
131 }
132
133 return '<pre>' . print_r($data, true) . '</pre>' . $renderer->render($form);
134}
135
138{
139 public function __construct(
140 protected URLBuilder $builder,
141 protected URLBuilderToken $node_id_parameter,
142 ) {
143 }
144
145 public function getNodes(
146 NodeFactory $node_factory,
147 IconFactory $icon_factory,
148 array $sync_node_id_whitelist = [],
149 ?string $parent_id = null
150 ): \Generator {
151 if (null !== $parent_id) {
152 yield $this->getExampleNodeChildren($node_factory, $parent_id);
153 return;
154 }
155
156 yield $node_factory->branch(['1'], 'branch 1', null, ...$this->getExampleNodeChildren($node_factory, '1'));
157 yield $node_factory->branch(['2'], 'branch 2', null, ...$this->getExampleNodeChildren($node_factory, '2'));
158 yield $node_factory->leaf(['3'], 'leaf 3');
159 }
160
161 public function getNodesAsLeaf(
162 NodeFactory $node_factory,
163 IconFactory $icon_factory,
164 array $node_ids,
165 ): \Generator {
166 foreach ($node_ids as $node_id) {
167 yield $node_factory->leaf([$node_id], "dummy leaf node $node_id");
168 }
169 }
170
171 protected function getExampleNodeChildren(NodeFactory $node_factory, string $parent_id): array
172 {
173 return [
174 $node_factory->branch(
175 $this->getFullNodePath($parent_id, "1"),
176 "branch $parent_id.1",
177 null,
178 $node_factory->leaf($this->getFullNodePath($parent_id, "1.1"), "leaf $parent_id.1.1"),
179 $node_factory->leaf($this->getFullNodePath($parent_id, "1.2"), "leaf $parent_id.1.2"),
180 $node_factory->leaf($this->getFullNodePath($parent_id, "1.3"), "leaf $parent_id.1.3"),
181 ),
182 $node_factory->branch(
183 $this->getFullNodePath($parent_id, "2"),
184 "branch $parent_id.2",
185 null,
186 $node_factory->leaf($this->getFullNodePath($parent_id, "2.1"), "leaf $parent_id.2.1"),
187 $node_factory->leaf($this->getFullNodePath($parent_id, "2.2"), "leaf $parent_id.2.2"),
188 $node_factory->leaf($this->getFullNodePath($parent_id, "2.3"), "leaf $parent_id.2.3"),
189 ),
190 $node_factory->async(
191 $this->getAsyncNodeRenderUrl("$parent_id.3"),
192 $this->getFullNodePath($parent_id, "3"),
193 "async branch $parent_id.3"
194 ),
195 $node_factory->leaf($this->getFullNodePath($parent_id, "4"), "leaf $parent_id.4"),
196 ];
197 }
198
200 protected function getFullNodePath(string $parent_id, string $child_id): array
201 {
202 $parts = explode(".", $parent_id);
203 $paths = [];
204 for ($index = 1, $count = count($parts); $index <= $count; $index++) {
205 $paths[] = implode(".", array_slice($parts, 0, $index));
206 }
207 $paths[] = "{$paths[$count - 1]}.$child_id";
208 return $paths;
209 }
210
211 protected function getAsyncNodeRenderUrl(int|string $node_id): URI
212 {
213 return $this->builder->withParameter($this->node_id_parameter, (string) $node_id)->buildURI();
214 }
215}
$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
getExampleNodeChildren(NodeFactory $node_factory, string $parent_id)
Definition: base.php:171
__construct(protected URLBuilder $builder, protected URLBuilderToken $node_id_parameter,)
Definition: base.php:139
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:145
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:161
$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'))