ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
base.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
13 use ILIAS\Data\URI;
15 
45 function base(): string
46 {
47  global $DIC;
48 
49  $http = $DIC->http();
50  $factory = $DIC->ui()->factory();
51  $renderer = $DIC->ui()->renderer();
52  $get_request = $http->wrapper()->query();
53  $data_factory = new \ILIAS\Data\Factory();
54  $refinery_factory = new \ILIAS\Refinery\Factory($data_factory, $DIC->language());
55 
56  $example_uri = $data_factory->uri((string) $http->request()->getUri());
57  $base_url_builder = new URLBuilder($example_uri);
58  [$node_id_url_builder, $node_id_parameter] = $base_url_builder->acquireParameter(explode('\\', __NAMESPACE__), "node_id");
59  [$process_form_url_builder ,$process_form_parameter] = $base_url_builder->acquireParameter(explode('\\', __NAMESPACE__), "process");
60 
61  $node_retrieval = new TreeSelectExampleNodeRetrieval($node_id_url_builder, $node_id_parameter);
62 
63  // simulates an async node rendering endpoint:
64  if ($get_request->has($node_id_parameter->getName())) {
65  $parent_node_id = $get_request->retrieve(
66  $node_id_parameter->getName(),
67  $refinery_factory->kindlyTo()->string(),
68  );
69 
70  $node_generator = $node_retrieval->getNodes(
71  $factory->input()->field()->node(),
72  $factory->symbol()->icon(),
73  $parent_node_id
74  );
75 
76  $html = '';
77  foreach ($node_generator as $node) {
78  $html .= $renderer->renderAsync($node);
79  }
80 
81  $http->saveResponse(
82  $http->response()
83  ->withHeader('Content-Type', 'text/html; charset=utf-8')
84  ->withBody(Streams::ofString($html))
85  );
86  $http->sendResponse();
87  $http->close();
88  }
89 
90  $input = $factory->input()->field()->treeSelect(
91  $node_retrieval,
92  "select a single node",
93  "you can open the select input by clicking the button above.",
94  );
95 
96  $form = $factory->input()->container()->form()->standard(
97  (string) $process_form_url_builder->withParameter($process_form_parameter, '1')->buildURI(),
98  [$input]
99  );
100 
101  // simulates a form processing endpoint:
102  if ($get_request->has($process_form_parameter->getName())) {
103  $form = $form->withRequest($http->request());
104  $data = $form->getData();
105  } else {
106  $data = 'No submitted data yet.';
107  }
108 
109  return '<pre>' . print_r($data, true) . '</pre>' . $renderer->render($form);
110 }
111 
114 {
115  public function __construct(
116  protected URLBuilder $builder,
117  protected URLBuilderToken $node_id_parameter,
118  ) {
119  }
120 
121  public function getNodes(NodeFactory $node_factory, IconFactory $icon_factory, ?string $parent_id = null): \Generator
122  {
123  if (null !== $parent_id) {
124  yield $this->getExampleNodeChildren($node_factory, $parent_id);
125  return;
126  }
127 
128  yield $node_factory->branch('1', 'branch 1', null, ...$this->getExampleNodeChildren($node_factory, '1'));
129  yield $node_factory->branch('2', 'branch 2', null, ...$this->getExampleNodeChildren($node_factory, '2'));
130  yield $node_factory->leaf('3', 'leaf 3');
131  }
132 
133  public function getNodesAsLeaf(
134  NodeFactory $node_factory,
135  IconFactory $icon_factory,
136  array $node_ids,
137  ): \Generator {
138  foreach ($node_ids as $node_id) {
139  yield $node_factory->leaf($node_id, "dummy leaf node $node_id");
140  }
141  }
142 
143  protected function getExampleNodeChildren(NodeFactory $node_factory, string|int $parent_id): array
144  {
145  return [
146  $node_factory->branch("$parent_id.1", "branch $parent_id.1", null,
147  $node_factory->leaf("$parent_id.1.1", "leaf $parent_id.1.1"),
148  $node_factory->leaf("$parent_id.1.2", "leaf $parent_id.1.2"),
149  $node_factory->leaf("$parent_id.1.3", "leaf $parent_id.1.3"),
150  ),
151  $node_factory->branch("$parent_id.2", "branch $parent_id.2", null,
152  $node_factory->leaf("$parent_id.2.1", "leaf $parent_id.2.1"),
153  $node_factory->leaf("$parent_id.2.2", "leaf $parent_id.2.2"),
154  $node_factory->leaf("$parent_id.2.3", "leaf $parent_id.2.3"),
155  ),
156  $node_factory->async($this->getAsyncNodeRenderUrl("$parent_id.3"),"$parent_id.3", "async branch $parent_id.3"),
157  $node_factory->leaf("$parent_id.4", "leaf $parent_id.4"),
158  ];
159  }
160 
161  protected function getAsyncNodeRenderUrl(int|string $node_id): URI
162  {
163  return $this->builder->withParameter($this->node_id_parameter, (string) $node_id)->buildURI();
164  }
165 }
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:133
$renderer
getNodes(NodeFactory $node_factory, IconFactory $icon_factory, ?string $parent_id=null)
This method will be called by the tree select input and multi tree select input to generate the tree ...
Definition: base.php:121
$http
Definition: deliver.php:30
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This is how a factory for icons looks like.
Definition: Factory.php:26
global $DIC
Definition: shib_login.php:26
getExampleNodeChildren(NodeFactory $node_factory, string|int $parent_id)
Definition: base.php:143
__construct(protected URLBuilder $builder, protected URLBuilderToken $node_id_parameter,)
Definition: base.php:115
base()
description: > The example shows how to create and render a Tree Select Field and attach it to a For...
Definition: base.php:45
URLBuilder.
Definition: URLBuilder.php:40