Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
notification.php
1<?php
2
4
7use \Bitrix\Main\Web\Uri;
8use \Bitrix\Mail\Internals\MailboxAccessTable;
9use \Bitrix\Mail\MailboxTable;
10
11Loc::loadMessages(__FILE__);
12
14{
15 const notifierSchemeTypeMail = 'new_message';
16 const notifierSchemeTypeMailTariffRestrictions = 'tariff_restrictions';
17
18 public static function getSchema()
19 {
20 Main\Loader::includeModule('im');
21
22 return [
23 'mail' => [
24 'new_message' => [
25 'NAME' => Loc::getMessage('MAIL_NOTIFY_NEW_MESSAGE'),
26 'SITE' => 'Y',
27 'SYSTEM' => 'Y',
28 'MAIL' => 'N',
29 'PUSH' => 'N',
30 'DISABLED' => [
31 IM_NOTIFY_FEATURE_PUSH,
32 IM_NOTIFY_FEATURE_MAIL,
33 ],
34 ],
35 'imposed_tariff_restrictions_on_the_mailbox' => [
36 'NAME' => Loc::getMessage('MAIL_NOTIFY_IMPOSE_TARIFF_RESTRICTIONS_ON_THE_MAILBOX'),
37 'SITE' => 'Y',
38 'SYSTEM' => 'Y',
39 'MAIL' => 'Y',
40 'PUSH' => 'Y',
41 'DISABLED' => [
42 IM_NOTIFY_FEATURE_PUSH,
43 IM_NOTIFY_FEATURE_MAIL,
44 IM_NOTIFY_FEATURE_SITE,
45 ],
46 ],
47 ],
48 ];
49 }
50
51 private static function getNotifyMessageForNewMessageSetInMail($mailboxId, $messageCount, $absoluteUrl = false): string
52 {
53 $url = htmlspecialcharsbx(sprintf("/mail/list/%u", $mailboxId));
54
55 if ($absoluteUrl)
56 {
57 $uri = new Uri($url);
58 $url = $uri->toAbsolute()->getLocator();
59 }
60
61 return Loc::getMessage('MAIL_NOTIFY_NEW_MESSAGE_MULTI_1',
62 [
63 '#COUNT#' => $messageCount,
64 '#VIEW_URL#' => $url,
65 ]
66 );
67 }
68
69 private static function getNotifyMessageForNewMessageInMail($message, $absoluteUrl = false): string
70 {
71 $url = htmlspecialcharsbx($message['__href']);
72
73 if ($absoluteUrl)
74 {
75 $uri = new Uri($url);
76 $url = $uri->toAbsolute()->getLocator();
77 }
78
79 if ($message['SUBJECT'])
80 {
81 return Loc::getMessage('MAIL_NOTIFY_NEW_SINGLE_MESSAGE_IN_MAIL_CLIENT_1',
82 [
83 '#SUBJECT#' => $message['SUBJECT'],
84 '#VIEW_URL#' => $url,
85 ]
86 );
87 }
88 else
89 {
90 return Loc::getMessage('MAIL_NOTIFY_NEW_SINGLE_MESSAGE_IN_MAIL_CLIENT_EMPTY_SUBJECT',
91 [
92 '#VIEW_URL#' => $url,
93 ]
94 );
95 }
96 }
97
98 private static function getNotifyMessageForTariffRestrictionsMailbox($mailboxId, $email, $forEmailNotification = false): string
99 {
100 $url = htmlspecialcharsbx(sprintf("/mail/list/%u", $mailboxId));
101
102 if ($forEmailNotification)
103 {
104 $uri = new Uri($url);
105 $url = $uri->toAbsolute()->getLocator();
106
107 $text = Loc::getMessage('MAIL_NOTIFY_FULL_MAILBOX_TARIFF_RESTRICTIONS_HAVE_BEEN_IMPOSED',
108 [
109 '#EMAIL#' => $email,
110 '#VIEW_URL#' => $url,
111 ]
112 );
113 }
114 else
115 {
116 $text = Loc::getMessage('MAIL_NOTIFY_MAILBOX_TARIFF_RESTRICTIONS_HAVE_BEEN_IMPOSED',
117 [
118 '#EMAIL#' => $email,
119 '#VIEW_URL#' => $url,
120 ]
121 );
122 }
123
124 return $text;
125 }
126
127 private static function notifyForNewMessagesInMail($userId, $fields): void
128 {
129 $message = $fields['message'];
130
131 \CIMNotify::add([
132 'MESSAGE_TYPE' => IM_MESSAGE_SYSTEM,
133 'NOTIFY_TYPE' => IM_NOTIFY_SYSTEM,
134 'NOTIFY_MODULE' => 'mail',
135 'NOTIFY_EVENT' => self::notifierSchemeTypeMail,
136 'NOTIFY_TITLE' => Loc::getMessage('MAIL_NOTIFY_NEW_MESSAGE_TITLE'),
137 'NOTIFY_MESSAGE_OUT' => empty($message)
138 ? self::getNotifyMessageForNewMessageSetInMail($fields['mailboxId'], $fields['count'], true)
139 : self::getNotifyMessageForNewMessageInMail($message, true),
140 'NOTIFY_MESSAGE' => empty($message)
141 ? self::getNotifyMessageForNewMessageSetInMail($fields['mailboxId'], $fields['count'])
142 : self::getNotifyMessageForNewMessageInMail($message),
143 'TO_USER_ID' => $userId,
144 ]);
145 }
146
147 private static function notifyForTariffRestrictions($mailboxId): void
148 {
149 $mailbox = MailboxTable::getList([
150 'select' => [
151 'USER_ID',
152 'EMAIL'
153 ],
154 'filter' => [
155 '=ID' => $mailboxId,
156 ],
157 'limit' => 1,
158 ])->fetch();
159
160 if (isset($mailbox['USER_ID']) && isset($mailbox['EMAIL']))
161 {
162 \CIMNotify::add([
163 'MESSAGE_TYPE' => IM_MESSAGE_SYSTEM,
164 'NOTIFY_TYPE' => IM_NOTIFY_SYSTEM,
165 'NOTIFY_MODULE' => 'mail',
166 'NOTIFY_EVENT' => self::notifierSchemeTypeMailTariffRestrictions,
167 'NOTIFY_TITLE' => Loc::getMessage('MAIL_NOTIFY_NEW_MESSAGE_TITLE'),
168 'NOTIFY_MESSAGE_OUT' => self::getNotifyMessageForTariffRestrictionsMailbox($mailboxId, $mailbox['EMAIL'], true),
169 'NOTIFY_MESSAGE' => self::getNotifyMessageForTariffRestrictionsMailbox($mailboxId, $mailbox['EMAIL']),
170 'TO_USER_ID' => $mailbox['USER_ID'],
171 ]);
172 }
173 }
174
175 public static function add($userId, $type, $fields, $mailboxId = null)
176 {
177 if (Main\Loader::includeModule('im'))
178 {
179 if ($type == 'new_message')
180 {
181 $mailboxId = $fields['mailboxId'];
182
183 $userIds = [];
184
185 $mailboxOwnerId = (int)$fields['mailboxOwnerId'] ?? 0;
186
187 if ($mailboxOwnerId)
188 {
190 }
191 else
192 {
193 $userIds[] = $userId;
194 }
195
196 foreach ($userIds as $id)
197 {
198 self::notifyForNewMessagesInMail($id, $fields);
199 }
200 }
201 else if ($type == 'imposed_tariff_restrictions_on_the_mailbox')
202 {
203 self::notifyForTariffRestrictions($mailboxId);
204 }
205 }
206 }
207
208}
static add($userId, $type, $fields, $mailboxId=null)
static getUserIdsWithAccessToTheMailbox(int $mailboxId)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29