ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilADNNotificationGUI.php
Go to the documentation of this file.
1 <?php
2 
24 
32 {
33  public const TAB_TABLE = 'notifications';
34  public const CMD_DEFAULT = 'index';
35  public const CMD_ADD = 'add';
36  public const CMD_CREATE = 'save';
37  public const CMD_UPDATE = 'update';
38  public const CMD_EDIT = 'edit';
39  public const CMD_DUPLICATE = 'duplicate';
40  public const CMD_CANCEL = 'cancel';
41  public const CMD_DELETE = 'delete';
42  public const CMD_RESET = 'reset';
43  public const CMD_CONFIRM_DELETE = 'confirmDelete';
44  private readonly Factory $ui_factory;
45  private readonly Renderer $ui_renderer;
46  protected Table $table;
47 
49  {
50  global $DIC;
51  parent::__construct($tab_handling);
52  $this->table = new Table($this);
53  $this->ui_factory = $DIC->ui()->factory();
54  $this->ui_renderer = $DIC->ui()->renderer();
55  }
56 
57  protected function dispatchCommand($cmd): string
58  {
59  $this->tab_handling->initTabs(
62  true
63  );
64  switch ($cmd) {
65  case self::CMD_ADD:
66  return $this->add();
67  case self::CMD_CREATE:
68  return $this->create();
69  case self::CMD_EDIT:
70  return $this->edit();
71  case self::CMD_DUPLICATE:
72  $this->duplicate();
73  break;
74  case self::CMD_UPDATE:
75  return $this->update();
76  case self::CMD_DELETE:
77  $this->delete();
78  break;
79  case self::CMD_RESET:
80  $this->reset();
81  break;
82  case self::CMD_CONFIRM_DELETE:
83  $this->confirmDelete();
84  break;
85  case self::CMD_DEFAULT:
86  default:
87  return $this->index();
88  }
89 
90  return "";
91  }
92 
93  protected function index(): string
94  {
95  $this->table = new Table($this);
96  if ($this->access->hasUserPermissionTo('write')) {
97  $btn_add_msg = $this->ui->factory()->button()->standard(
98  $this->lng->txt('common_add_msg'),
99  $this->ctrl->getLinkTarget($this, self::CMD_ADD)
100  );
101  $this->toolbar->addComponent($btn_add_msg);
102  }
103  return $this->table->getHTML();
104  }
105 
106  protected function add(): string
107  {
108  $form = new ilADNNotificationUIFormGUI(
109  new ilADNNotification(),
110  $this->ctrl->getLinkTarget($this, self::CMD_CREATE)
111  );
112  return $form->getHTML();
113  }
114 
115  protected function create(): string
116  {
117  $form = new ilADNNotificationUIFormGUI(
118  new ilADNNotification(),
119  $this->ctrl->getLinkTarget($this, self::CMD_CREATE)
120  );
121  $form->setValuesByPost();
122  if ($form->saveObject()) {
123  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_created'), true);
124  $this->ctrl->redirect($this, self::CMD_DEFAULT);
125  }
126  return $form->getHTML();
127  }
128 
129  protected function cancel(): void
130  {
131  $this->ctrl->setParameter($this, self::IDENTIFIER, null);
132  $this->ctrl->redirect($this, self::CMD_DEFAULT);
133  }
134 
135  protected function edit(): string
136  {
137  $notification = $this->getNotificationFromRequest();
138  $this->ctrl->setParameter($this, ilADNAbstractGUI::IDENTIFIER, $notification->getId());
139 
140  $form = new ilADNNotificationUIFormGUI($notification, $this->ctrl->getLinkTarget($this, self::CMD_UPDATE));
141  return $form->getHTML();
142  }
143 
144  protected function duplicate(): void
145  {
146  $notification = $this->getNotificationFromRequest();
147  $notification->setId(0);
148  $notification->create();
149  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_duplicated'), true);
150  $this->cancel();
151  }
152 
153  protected function update(): string
154  {
155  $notification = $this->getNotificationFromRequest();
156  $form = new ilADNNotificationUIFormGUI($notification, $this->ctrl->getLinkTarget($this, self::CMD_UPDATE));
157  $form->setValuesByPost();
158  if ($form->saveObject()) {
159  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_updated'), true);
160  $this->ctrl->redirect($this, self::CMD_DEFAULT);
161  }
162  return $form->getHTML();
163  }
164 
165  protected function delete(): void
166  {
167  foreach ($this->getNotificationsFromRequest() as $notification) {
168  $notification->delete();
169  }
170 
171  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_deleted'), true);
172  $this->cancel();
173  }
174 
175  protected function reset(): void
176  {
177  foreach ($this->getNotificationsFromRequest() as $notification) {
178  $notification->resetForAllUsers();
179  }
180 
181  $this->tpl->setOnScreenMessage('success', $this->lng->txt('msg_success_reset'), true);
182  $this->cancel();
183  }
184 
185  protected function confirmDelete(): void
186  {
187  $adns = $this->getNotificationsFromRequest();
188 
189  $stream = Streams::ofString(
190  $this->ui_renderer->render(
191  $this->ui_factory->modal()->interruptive(
192  $this->lng->txt('action_confirm_delete'),
193  $this->lng->txt('action_confirm_delete_msg'),
194  $this->ctrl->getLinkTarget($this, self::CMD_DELETE)
195  )->withAffectedItems(
196  array_map(fn(ilADNNotification $adn): Standard => $this->ui_factory->modal()->interruptiveItem()->standard(
197  $adn->getId(),
198  $adn->getTitle()
199  ), $adns)
200  )
201  )
202  );
203  $this->http->saveResponse($this->http->response()->withBody($stream));
204  $this->http->sendResponse();
205  $this->http->close();
206  }
207 
210  {
211  return $this->getNotificationsFromRequest()[0];
212  }
213 
217  protected function getNotificationsFromRequest(): array
218  {
219  $query_params = $this->http->request()->getQueryParams(); // aka $_GET
220  $name = $this->table->getIdToken()->getName(); // name of the query parameter from the table
221  $field_ids = $query_params[$name] ?? []; // array of field ids
222 
223  // all objects
224  if (($field_ids[0] ?? null) === 'ALL_OBJECTS') {
225  return ilADNNotification::get();
226  }
227  // check ilADNAbstractGUI::IDENTIFIER
228  if (($field_id = $this->http->request()->getQueryParams()[ilADNAbstractGUI::IDENTIFIER] ?? false)) {
229  return [ilADNNotification::findOrFail((int) $field_id)];
230  }
231 
232  $return = [];
233  foreach ($field_ids as $field_id) {
234  $return[] = ilADNNotification::findOrFail((int) $field_id);
235  }
236 
237  // check interruptive items
238  if (($interruptive_items = $this->http->request()->getParsedBody()['interruptive_items'] ?? false)) {
239  foreach ($interruptive_items as $interruptive_item) {
240  $return[] = ilADNNotification::findOrFail((int) $interruptive_item);
241  }
242  }
243 
244  return $return;
245  }
246 
247 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static findOrFail($primary_key, array $add_constructor_args=[])
Tries to find the object and throws an Exception if object is not found, instead of returning null...
getNotificationFromRequest()
PhpIncompatibleReturnTypeInspection
static http()
Fetches the global http state from ILIAS.
This is how the factory for UI elements looks.
Definition: Factory.php:37
ilADNTabHandling $tab_handling
global $DIC
Definition: shib_login.php:25
__construct(ilADNTabHandling $tab_handling)
__construct(Container $dic, ilPlugin $plugin)