ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
MailNotificationProvider.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Mail\Provider;
22 
28 use ilDateTime;
30 use Throwable;
32 
34 {
35  final public const string MUTED_UNTIL_PREFERENCE_KEY = 'mail_nc_muted_until';
36 
37  public function getNotifications(): array
38  {
39  $id = fn(string $id): IdentificationInterface => $this->if->identifier($id);
40 
41  if ($this->dic->user()->getId() === 0 || $this->dic->user()->isAnonymous()) {
42  return [];
43  }
44 
45  $has_internal_mail_access = $this->dic->rbac()->system()->checkAccess(
46  'internal_mail',
48  );
49  if (!$has_internal_mail_access) {
50  return [];
51  }
52 
53  $left_interval_timestamp = $this->dic->user()->getPref(self::MUTED_UNTIL_PREFERENCE_KEY);
55  $this->dic->user(),
56  is_numeric($left_interval_timestamp) ? (int) $left_interval_timestamp : 0
57  );
58 
59  $number_of_new_messages = $new_mail_data['count'];
60  if ($number_of_new_messages === 0) {
61  return [];
62  }
63 
64  $this->dic->language()->loadLanguageModule('mail');
65 
66  $factory = $this->globalScreen()->notifications()->factory();
67 
68  $mail_url = 'ilias.php?baseClass=' . \ilMailGUI::class;
69 
70  if ($number_of_new_messages === 1) {
71  $link_text = $this->dic->language()->txt('nc_mail_unread_messages_number_s');
72  } else {
73  $link_text = \sprintf(
74  $this->dic->language()->txt('nc_mail_unread_messages_number_p'),
75  $number_of_new_messages
76  );
77  }
78 
79  $body = \sprintf(
80  $this->dic->language()->txt('nc_mail_unread_messages'),
81  $this->dic->ui()->renderer()->render(
82  $this->dic->ui()->factory()
83  ->link()
84  ->standard($link_text, $mail_url)
85  )
86  );
87 
88  $icon = $this->dic->ui()->factory()->symbol()->icon()->standard(Standard::MAIL, 'mail');
89  $title = $this->dic->ui()->factory()->link()->standard(
90  $this->dic->language()->txt('nc_mail_noti_item_title'),
91  $mail_url
92  );
93 
95  $notification_item = $this->dic->ui()->factory()
96  ->item()
97  ->notification($title, $icon)
98  ->withDescription($body);
99 
100  try {
101  $date_time = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $new_mail_data['max_time']);
102  $notification_item = $notification_item->withProperties([
103  $this->dic->language()->txt('nc_mail_prop_time') => ilDatePresentation::formatDate(
104  new ilDateTime($date_time->getTimestamp(), IL_CAL_UNIX)
105  ),
106  ]);
107  } catch (Throwable) {
108  }
109 
110  $group = $factory->standardGroup($id('mail_bucket_group'))
111  ->withTitle($this->dic->language()->txt('mail'))
112  ->addNotification(
113  $factory->standard($id('mail_bucket'))
114  ->withNotificationItem($notification_item)
115  ->withClosedCallable(
116  function (): void {
117  $this->dic->user()->writePref(self::MUTED_UNTIL_PREFERENCE_KEY, (string) time());
118  }
119  )
120  ->withNewAmount(1)
121  );
122 
123  return [
124  $group,
125  ];
126  }
127 }
const IL_CAL_UNIX
static getNewMailsData(ilObjUser $user, int $left_interval=0)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)