ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
StandardToastRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 use ILIAS\GlobalScreen\Client\Notifications as ClientNotifications;
31 
36 {
37  use Hasher;
38 
39  public function __construct(protected UIServices $ui)
40  {
41  }
42 
43  public function getToastComponentForItem(isItem $item): Component
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 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Provides fluid interface to RBAC services.
Definition: UIServices.php:24
getToastComponentForItem(isItem $item)
Returns the UI Component for the past item.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23