Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ReminderService.php
1<?php
2
4
7use Bitrix\Im\V2\Common\ContextCustomer;
15use CIMNotify;
16
18{
19 use ContextCustomer;
20
21 public const ADD_REMINDERS_EVENT = 'reminderAdd';
22 public const DELETE_REMINDERS_EVENT = 'reminderDelete';
23
24 public static function remindAgent(): string
25 {
26 (new static())->remind();
27
28 return __METHOD__. '();';
29 }
30
31 public function getCount(int $chatId): int
32 {
33 $filter = Query::filter()
34 ->where('CHAT_ID', $chatId)
35 ->where('AUTHOR_ID', $this->getContext()->getUserId())
36 ;
37
38 return LinkReminderTable::getCount($filter);
39 }
40
41 public function addMessageToReminders(Message $message, DateTime $dateRemind): Result
42 {
43 $result = new Result();
44
45 if ($dateRemind->getTimestamp() < (new DateTime())->getTimestamp())
46 {
47 return $result->addError(new ReminderError(ReminderError::DATE_REMIND_PASSED));
48 }
49
50 $reminder = ReminderItem::createFromMessage($message, $this->getContext())->setDateRemind($dateRemind);
51 $saveResult = $this->saveReminder($reminder);
52
53 if (!$saveResult->isSuccess())
54 {
55 return $result->addErrors($saveResult->getErrors());
56 }
57
58 $pushRecipient = ['RECIPIENT' => [$this->getContext()->getUserId()]];
59
61 ->setContext($this->context)
62 ->sendFull($reminder, static::ADD_REMINDERS_EVENT, $pushRecipient)
63 ;
64
65 return $result;
66 }
67
68 public function deleteRemindersByMessage(Message $message): Result
69 {
70 $result = new Result();
71
72 $reminders = ReminderCollection::getByMessage($message);
73
74 if ($reminders->count() === 0)
75 {
76 return $result;
77 }
78
79 $deleteResult = $reminders->delete();
80
81 if (!$deleteResult->isSuccess())
82 {
83 return $result->addErrors($deleteResult->getErrors());
84 }
85
86 foreach ($reminders as $reminder)
87 {
88 $pushRecipient = ['RECIPIENT' => [$reminder->getAuthorId()]];
90 ->setContext((new Context())->setUserId($reminder->getAuthorId()))
91 ->sendIdOnly($reminder, static::DELETE_REMINDERS_EVENT, $pushRecipient)
92 ;
93 }
94
95 return $result;
96 }
97
98 public function deleteReminder(ReminderItem $reminder): Result
99 {
100 $result = new Result();
101
102 $deleteResult = $reminder->delete();
103
104 if (!$deleteResult->isSuccess())
105 {
106 return $result->addErrors($deleteResult->getErrors());
107 }
108
109 $deleteNotifyResult = $this->deleteNotify($reminder);
110
111 if (!$deleteNotifyResult->isSuccess())
112 {
113 $result->addErrors($deleteNotifyResult->getErrors());
114 }
115
116 $pushRecipient = ['RECIPIENT' => [$reminder->getAuthorId()]];
117
119 ->setContext((new Context())->setUserId($reminder->getAuthorId()))
120 ->sendIdOnly($reminder, static::DELETE_REMINDERS_EVENT, $pushRecipient)
121 ;
122
123 return $result;
124 }
125
126 public function remind(): Result
127 {
128 $result = new Result();
130
131 $reminders->getMessageCollection()->fillFiles();
132
133 foreach ($reminders as $reminder)
134 {
135 $sendResult = $this->sendNotifyAboutReminder($reminder);
136 if (!$sendResult->isSuccess())
137 {
138 continue;
139 }
140 $reminder->setIsReminded(true);
141 }
142
143 $saveResult = $reminders->save(true);
144
145 if(!$saveResult->isSuccess())
146 {
147 return $result->addErrors($saveResult->getErrors());
148 }
149
150 return $result;
151 }
152
153 protected function sendNotifyAboutReminder(ReminderItem $reminder): Result
154 {
155 $result = new Result();
156
157 $attach = new \CIMMessageParamAttach();
158
159 $user = $reminder->getEntity()->getAuthor();
160
161 if ($user !== null)
162 {
163 $attach->AddUser([
164 'NAME' => $user->getFullName(),
165 'AVATAR' => $user->getAvatar(),
166 ]);
167 }
168
169 $attach->AddMessage($reminder->getEntity()->getPreviewMessage());
170
171 $notifyParams = [
172 'TO_USER_ID' => $reminder->getAuthorId(),
173 'FROM_USER_ID' => 0,
174 'NOTIFY_TYPE' => IM_NOTIFY_SYSTEM,
175 'NOTIFY_MODULE' => 'im',
176 'NOTIFY_SUB_TAG' => $this->getSubTag($reminder),
177 'NOTIFY_MESSAGE' => $this->getNotifyMessageText($reminder, false),
178 'NOTIFY_MESSAGE_OUT' => $this->getNotifyMessageText($reminder, true),
179 'ATTACH' => $attach
180 ];
181
182 $notifyId = CIMNotify::Add($notifyParams);
183
184 if ($notifyId === false)
185 {
186 return $result->addError(new ReminderError(ReminderError::REMINDER_NOTIFY_ADD_ERROR));
187 }
188
189 return $result;
190 }
191
192 protected function deleteNotify(ReminderItem $reminder): Result
193 {
194 $isDeleteSuccess = CIMNotify::DeleteBySubTag($this->getSubTag($reminder));
195
196 if ($isDeleteSuccess)
197 {
198 return new Result();
199 }
200
202 }
203
204 protected function saveReminder(ReminderItem $reminder): Result
205 {
206 try
207 {
208 return $reminder->save();
209 }
210 catch (\Bitrix\Main\SystemException $exception)
211 {
212 return (new Result())->addError(new Message\MessageError(Message\MessageError::MESSAGE_IS_ALREADY_IN_REMINDERS));
213 }
214 }
215
216 protected function getNotifyMessageText(ReminderItem $reminder, bool $isOut): callable
217 {
218 $chat = Chat::getInstance($reminder->getChatId())->setContext((new Context())->setUserId($reminder->getAuthorId()));
219
220 $chatTitle = $isOut ? $chat->getDisplayedTitle() : "[CHAT={$chat->getChatId()}]{$chat->getDisplayedTitle()}[/CHAT]";
221
222 return fn (?string $languageId = null) => Loc::getMessage(
223 'IM_CHAT_REMINDER_REMIND_NOTIFICATION',
224 ['#CHAT_TITLE#' => $chatTitle],
225 $languageId
226 );
227 }
228
229 private function getSubTag(ReminderItem $reminder): string
230 {
231 return "MESSAGE_REMINDER_{$reminder->getId()}";
232 }
233}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getCount($filter=array(), array $cache=array())