ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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  protected UIServices $ui;
40 
41  public function __construct(UIServices $ui)
42  {
43  $this->ui = $ui;
44  }
45 
46  public function getToastComponentForItem(isItem $item): Component
47  {
48  if (!$item instanceof isStandardItem) {
49  return $this->ui->factory()->legacy("Cannot render item of type " . get_class($item) . "");
50  }
51 
52  // build empty UI\Toast
53  $toast = $this->ui->factory()->toast()->standard(
54  $item->getTitle(),
55  $this->getIconOrFallback($item)
56  );
57 
58  if ($item->getDescription() !== null) {
59  $toast = $toast->withDescription($item->getDescription());
60  }
61 
62  // onClose
63  if ($item->hasClosedAction()) {
64  $closed_action = $this->buildURL(
65  $item->getClosedAction()->getIdentifier(),
67  );
68  $toast = $toast->withAdditionalOnLoadCode(function ($id) use ($closed_action) {
69  return "
70  $('#$id').on('removeToast', function() {
71  $.ajax({
72  async: false,
73  type: 'GET',
74  url: '$closed_action'
75  });
76  });";
77  });
78  }
79 
80  // on Show (there is currently no such event in the UI-Service)
81  if ($item->hasShownAction()) {
82  $shown_action = $this->buildURL(
83  $item->getShownAction()->getIdentifier(),
85  );
86  $toast = $toast->withAdditionalOnLoadCode(function ($id) use ($shown_action) {
87  return "
88  $('#$id').on('showToast', function() {
89  $.ajax({
90  async: false,
91  type: 'GET',
92  url: '$shown_action'
93  });
94  });";
95  });
96  }
97 
98  // onVanish (there is currently no such event in the UI-Service)
99  if ($item->hasVanishedAction()) {
100  $vanished_action = $this->buildURL(
101  $item->getVanishedAction()->getIdentifier(),
103  );
104  $toast = $toast->withAdditionalOnLoadCode(function ($id) use ($vanished_action) {
105  return "
106  $('#$id').on('vanishToast', function() {
107  $.ajax({
108  async: false,
109  type: 'GET',
110  url: '$vanished_action'
111  });
112  });";
113  });
114  }
115 
116  // additional Actions
117  foreach ($item->getAdditionalToastActions() as $toast_action) {
118  $action = $this->buildURL(
119  $toast_action->getIdentifier(),
121  );
122  $link = $this->ui->factory()->link()->standard(
123  $toast_action->getTitle(),
124  $action
125  );
126 
127  $toast = $toast->withAdditionalLink($link);
128  }
129 
130  return $toast;
131  }
132 
133  private function getIconOrFallback(isStandardItem $item): Icon
134  {
135  $icon = $item->getIcon();
136  if ($icon !== null) {
137  return $icon;
138  }
139  return $this->ui->factory()->symbol()->icon()->standard("nota", $item->getTitle());
140  }
141 
142  protected function buildURL(string $action, IdentificationInterface $id): string
143  {
144  $query = http_build_query([
145  ClientNotifications::MODE => ClientNotifications::MODE_HANDLE_TOAST_ACTION,
146  ClientNotifications::ADDITIONAL_ACTION => $action,
147  ClientNotifications::ITEM_ID => $this->hash($id->serialize()),
148  ]);
149 
150  return rtrim(ILIAS_HTTP_PATH, "/") . "/" . ClientNotifications::NOTIFY_ENDPOINT . "?" . $query;
151  }
152 }
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...
Provides fluid interface to RBAC services.
Definition: UIServices.php:23
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:24