ILIAS  release_8 Revision v8.24
show_popover_with_dynamic_changing_content.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
8
10{
11 global $DIC;
12 $factory = $DIC->ui()->factory();
13 $renderer = $DIC->ui()->renderer();
14 $refinery = $DIC->refinery();
15 $request_wrapper = $DIC->http()->wrapper()->query();
16
17 // This example shows how to change the content of a popover dynamically with ajax requests.
18 // Each popover offers a signal to replace its content, similar to the show signal which shows the popover.
19 // The replace signal will load the new content via ajax from a given URL and insert it into the popover.
20
21 // The popover in this example initially shows three buttons. Each button will replace the content
22 // of the popover with a new "page" showing some text. Each page also contains a back button which
23 // again replaces the content of the popover with the overview page.
24
25 $url = $_SERVER['REQUEST_URI'];
26
27 // This is an ajax request to render the overview page showing the three buttons
28 if ($request_wrapper->has('page') && $request_wrapper->retrieve('page', $refinery->kindlyTo()->string()) == 'overview') {
29 // Note: The ID of the replace signal is sent explicitly as GET parameter. This is a proof of concept
30 // and may be subject to change, as the framework could send such parameters implicitly.
31 $signalId = $request_wrapper->retrieve('replaceSignal', $refinery->kindlyTo()->string());
32 $replaceSignal = new ReplaceContentSignal($signalId);
33 $button1 = $factory->button()->standard('Go to page 1', '#')
34 ->withOnClick($replaceSignal->withAsyncRenderUrl($url . '&page=1&replaceSignal=' . $signalId));
35 $button2 = $factory->button()->standard('Go to page 2', '#')
36 ->withOnClick($replaceSignal->withAsyncRenderUrl($url . '&page=2&replaceSignal=' . $signalId));
37 $button3 = $factory->button()->standard('Go to page 3', '#')
38 ->withOnClick($replaceSignal->withAsyncRenderUrl($url . '&page=3&replaceSignal=' . $signalId));
39 $list = $factory->listing()->unordered([$button1, $button2, $button3]);
40 echo $renderer->renderAsync($list);
41 exit();
42 }
43
44 // This is an ajax request to render a page
45 if ($request_wrapper->has('page')) {
46 $page = $request_wrapper->retrieve('page', $refinery->kindlyTo()->int());
47 $signalId = $request_wrapper->retrieve('replaceSignal', $refinery->kindlyTo()->string());
48 $replaceSignal = new ReplaceContentSignal($signalId);
49 $button = $factory->button()->standard('Back to Overview', '#')
50 ->withOnClick($replaceSignal->withAsyncRenderUrl($url . '&page=overview&replaceSignal=' . $signalId));
51 $intro = $factory->legacy("<p>You are viewing page $page</p>");
52 echo $renderer->renderAsync([$intro, $button]);
53 exit();
54 }
55
56 // This is the "normal" request to render the popover. Any content of the popover is rendered async.
57 $popover = $factory->popover()->standard($factory->legacy(''))->withTitle('Pages');
58 $asyncUrl = $url . '&page=overview&replaceSignal=' . $popover->getReplaceContentSignal()->getId();
59 $popover = $popover->withAsyncContentUrl($asyncUrl);
60 $button = $factory->button()->standard('Show Popover', '#')
61 ->withOnClick($popover->getShowSignal());
62 return $renderer->render([$popover, $button]);
63}
global $DIC
Definition: feed.php:28
exit
Definition: login.php:28
$factory
Definition: metadata.php:75
Refinery Factory $refinery
$url
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10