ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables 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  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  // Times (currently disbaled since these methods are not on the Interface of a Toast
131  if ($item->getVanishTime() !== null) {
132  // $toast = $toast->withVanishTime($item->getVanishTime());
133  }
134 
135  if ($item->getDelayTime() !== null) {
136  // $toast = $toast->withDelayTime($item->getDelayTime());
137  }
138 
139  return $toast;
140  }
141 
142  private function getIconOrFallback(isStandardItem $item): Icon
143  {
144  $icon = $item->getIcon();
145  if ($icon !== null) {
146  return $icon;
147  }
148  return $this->ui->factory()->symbol()->icon()->standard("nota", $item->getTitle());
149  }
150 
151  protected function buildURL(string $action, IdentificationInterface $id): string
152  {
153  $query = http_build_query([
154  ClientNotifications::MODE => ClientNotifications::MODE_HANDLE_TOAST_ACTION,
155  ClientNotifications::ADDITIONAL_ACTION => $action,
156  ClientNotifications::ITEM_ID => $this->hash($id->serialize()),
157  ]);
158 
159  return rtrim(ILIAS_HTTP_PATH, "/") . "/" . ClientNotifications::NOTIFY_ENDPOINT . "?" . $query;
160  }
161 }
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:28
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.
$query
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23