ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
StandardToastRenderer.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
29use ILIAS\GlobalScreen\Client\Notifications as ClientNotifications;
31
36{
37 use Hasher;
38
39 public function __construct(protected UIServices $ui)
40 {
41 }
42
44 {
45 if (!$item instanceof isStandardItem) {
46 return $this->ui->factory()->legacy()->content("Cannot render item of type " . $item::class . "");
47 }
48
49 // build empty UI\Toast
50 $toast = $this->ui->factory()->toast()->standard(
51 $item->getTitle(),
52 $this->getIconOrFallback($item)
53 );
54
55 if ($item->getDescription() !== null) {
56 $toast = $toast->withDescription($item->getDescription());
57 }
58
59 // onClose
60 if ($item->hasClosedAction()) {
61 $closed_action = $this->buildURL(
62 $item->getClosedAction()->getIdentifier(),
64 );
65 $toast = $toast->withAdditionalOnLoadCode(fn($id): string => "
66 $('#$id').on('removeToast', function() {
67 $.ajax({
68 async: false,
69 type: 'GET',
70 url: '$closed_action'
71 });
72 });");
73 }
74
75 // on Show (there is currently no such event in the UI-Service)
76 if ($item->hasShownAction()) {
77 $shown_action = $this->buildURL(
78 $item->getShownAction()->getIdentifier(),
80 );
81 $toast = $toast->withAdditionalOnLoadCode(fn($id): string => "
82 $('#$id').on('showToast', function() {
83 $.ajax({
84 async: false,
85 type: 'GET',
86 url: '$shown_action'
87 });
88 });");
89 }
90
91 // onVanish (there is currently no such event in the UI-Service)
92 if ($item->hasVanishedAction()) {
93 $vanished_action = $this->buildURL(
94 $item->getVanishedAction()->getIdentifier(),
96 );
97 $toast = $toast->withAdditionalOnLoadCode(fn($id): string => "
98 $('#$id').on('vanishToast', function() {
99 $.ajax({
100 async: false,
101 type: 'GET',
102 url: '$vanished_action'
103 });
104 });");
105 }
106
107 // additional Actions
108 foreach ($item->getAdditionalToastActions() as $toast_action) {
109 $action = $this->buildURL(
110 $toast_action->getIdentifier(),
112 );
113 $link = $this->ui->factory()->link()->standard(
114 $toast_action->getTitle(),
115 $action
116 );
117
118 $toast = $toast->withAdditionalLink($link);
119 }
120
121 return $toast;
122 }
123
124 private function getIconOrFallback(isStandardItem $item): Icon
125 {
126 $icon = $item->getIcon();
127 if ($icon !== null) {
128 return $icon;
129 }
130 return $this->ui->factory()->symbol()->icon()->standard("nota", $item->getTitle());
131 }
132
133 protected function buildURL(string $action, IdentificationInterface $id): string
134 {
135 $query = http_build_query([
136 ClientNotifications::MODE => ClientNotifications::MODE_HANDLE_TOAST_ACTION,
137 ClientNotifications::ADDITIONAL_ACTION => $action,
138 ClientNotifications::ITEM_ID => $this->hash($id->serialize()),
139 ]);
140
141 return rtrim(ILIAS_HTTP_PATH, "/") . "/" . ClientNotifications::NOTIFY_ENDPOINT . "?" . $query;
142 }
143}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
getToastComponentForItem(isItem $item)
Returns the UI Component for the past item.
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29