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