ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
expandable_async_repo.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 global $DIC;
8 $refinery = $DIC->refinery();
9 $request_wrapper = $DIC->http()->wrapper()->query();
10 
11 if ($request_wrapper->has('async_ref') && $request_wrapper->retrieve('async_ref', $refinery->kindlyTo()->bool())) {
12  $ref = $request_wrapper->retrieve('async_ref', $refinery->kindlyTo()->int());
14  exit();
15 }
16 
26 function expandable_async_repo($ref = null)
27 {
28  global $DIC;
29  $ilTree = $DIC['tree'];
30 
31  if (is_null($ref)) {
32  $do_async = false;
33  $ref = 1;
34  $data = array(
35  $ilTree->getNodeData(1)
36  );
37  } else {
38  $do_async = true;
39  $data = $ilTree->getChilds($ref);
40  if (count($data) === 0) {
41  return;
42  }
43  }
44 
45  $recursion = new class () implements \ILIAS\UI\Component\Tree\TreeRecursion {
46  public function getChildren($record, $environment = null): array
47  {
48  return [];
49  }
50 
51  public function build(
52  \ILIAS\UI\Component\Tree\Node\Factory $factory,
53  $record,
54  $environment = null
55  ): \ILIAS\UI\Component\Tree\Node\Node {
56  $ref_id = $record['ref_id'];
57  $label = $record['title']
58  . ' (' . $record['type'] . ', ' . $ref_id . ')';
59 
60  $icon = $environment['icon_factory']->standard($record["type"], '');
61  $url = $this->getAsyncURL($environment, (string) $ref_id);
62 
63  $node = $factory->simple($label, $icon)
64  ->withAsyncURL($url);
65 
66  //find these under ILIAS->Administration in the example tree
67  if ((int) $ref_id > 9 && (int) $ref_id < 20) {
68  $label = $environment['modal']->getShowSignal()->getId();
69  $node = $factory->simple($label)
70  ->withAsyncURL($url)
71  ->withOnClick($environment['modal']->getShowSignal());
72  }
73 
74  return $node;
75  }
76 
77  protected function getAsyncURL($environment, string $ref_id): string
78  {
79  $url = $environment['url'];
80  $base = substr($url, 0, strpos($url, '?') + 1);
81  $query = parse_url($url, PHP_URL_QUERY);
82  if ($query) {
83  parse_str($query, $params);
84  } else {
85  $params = [];
86  }
87  $params['async_ref'] = $ref_id;
88  $url = $base . http_build_query($params);
89  return $url;
90  }
91  };
92 
93  $f = $DIC->ui()->factory();
94  $renderer = $DIC->ui()->renderer();
95  $image = $f->image()->responsive("assets/ui-examples/images/Image/mountains.jpg", "Image source: https://stocksnap.io, Creative Commons CC0 license");
96  $page = $f->modal()->lightboxImagePage($image, 'Mountains');
97  $modal = $f->modal()->lightbox($page);
98 
99  $environment = [
100  'url' => $DIC->http()->request()->getRequestTarget(),
101  'modal' => $modal,
102  'icon_factory' => $f->symbol()->icon()
103  ];
104 
105  $tree = $f->tree()->expandable("Label", $recursion)
106  ->withEnvironment($environment)
107  ->withData($data);
108 
109  if (!$do_async) {
110  return $renderer->render([$modal, $tree]);
111  } else {
112  echo $renderer->renderAsync([$modal, $tree->withIsSubTree(true)]);
113  }
114 }
$renderer
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:31
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$url
Definition: shib_logout.php:63
$ref_id
Definition: ltiauth.php:66
This is how the factory for UI elements looks.
Definition: Factory.php:37
if($request_wrapper->has('async_ref') && $request_wrapper->retrieve('async_ref', $refinery->kindlyTo() ->bool())) expandable_async_repo($ref=null)
description: > Example for rendering an expandable tree with asynchronous repositories.