Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CalendarService.php
1<?php
2
4
8use Bitrix\Im\V2\Common\ContextCustomer;
15
17{
18 use ContextCustomer;
19
20 protected const ADD_CALENDAR_EVENT = 'calendarAdd';
21 protected const UPDATE_CALENDAR_EVENT = 'calendarUpdate';
22 protected const DELETE_CALENDAR_EVENT = 'calendarDelete';
23
24 public function registerCalendar(int $chatId, ?int $messageId, \Bitrix\Im\V2\Entity\Calendar\CalendarItem $calendar): Result
25 {
26 $result = new Result();
27
28 $userId = $this->getContext()->getUserId();
29
30 $calendarLink = new CalendarItem();
31 $calendarLink->setEntity($calendar)->setChatId($chatId)->setAuthorId($userId);
32
33 if (isset($messageId))
34 {
35 $calendarLink->setMessageId($messageId);
36 }
37
38 $sendMessageResult = $this->sendMessageAboutCalendar($calendarLink, $chatId);
39
40 if (!$sendMessageResult->isSuccess())
41 {
42 $result->addErrors($sendMessageResult->getErrors());
43 }
44
45 $systemMessageId = $sendMessageResult->getResult();
46
47 $calendarLink->setMessageId($messageId ?: $systemMessageId);
48 $saveResult = $calendarLink->save();
49
50 if (!$saveResult->isSuccess())
51 {
52 return $result->addErrors($saveResult->getErrors());
53 }
54
56 ->setContext($this->context)
57 ->sendFull($calendarLink, self::ADD_CALENDAR_EVENT, ['RECIPIENT' => $calendar->getMembersIds()])
58 ;
59
60 return $result;
61 }
62
63 public function unregisterCalendar(CalendarItem $calendar): Result
64 {
65 $calendar->delete();
67 ->setContext($this->context)
68 ->sendIdOnly($calendar, self::DELETE_CALENDAR_EVENT, ['CHAT_ID' => $calendar->getChatId()])
69 ;
70
71 return new Result();
72 }
73
74 public function updateCalendar(CalendarItem $calendarLink): Result
75 {
76 $result = new Result();
77
78 LinkCalendarIndexTable::delete($calendarLink->getPrimaryId());
79 $saveResult = $calendarLink->save();
80
81 if (!$saveResult->isSuccess())
82 {
83 return $result->addErrors($saveResult->getErrors());
84 }
85
87 ->setContext($this->context)
88 ->sendFull($calendarLink, self::UPDATE_CALENDAR_EVENT, ['RECIPIENT' => $calendarLink->getEntity()->getMembersIds()])
89 ;
90
91 return new Result();
92 }
93
94 public function updateCalendarLink(CalendarItem $calendarLink): Result
95 {
96 $result = new Result();
97
98 $saveResult = $calendarLink->save();
99
100 if (!$saveResult->isSuccess())
101 {
102 return $result->addErrors($saveResult->getErrors());
103 }
104
106 ->setContext($this->context)
107 ->sendFull($calendarLink, self::UPDATE_CALENDAR_EVENT, ['RECIPIENT' => $calendarLink->getEntity()->getMembersIds()])
108 ;
109
110 return new Result();
111 }
112
113 public function prepareDataForCreateSlider(Chat $chat, ?Message $message = null): Result
114 {
115 $result = new Result();
116
117 if (!Loader::includeModule('calendar'))
118 {
119 return $result->addError(new CalendarError(CalendarError::CALENDAR_NOT_INSTALLED));
120 }
121
122 $chat->setContext($this->context);
123
124 $randomPostfix = mt_rand() & 1000; // get random number from 0 to 1000
125 $data['params']['sliderId'] = "im:chat{$chat->getChatId()}{$randomPostfix}";
126
127 $userIds = $chat->getRelations(
128 [
129 'SELECT' => ['ID', 'USER_ID', 'CHAT_ID'],
130 'FILTER' => ['ACTIVE' => true, 'ONLY_INTERNAL_TYPE' => true],
131 'LIMIT' => 50,
132 ]
133 )->getUsers()->filterExtranet()->getIds();
134 $users = array_values(array_map(static fn($item) => ['id' => (int)$item, 'entityId' => 'user'], $userIds));
135 $data['params']['participantsEntityList'] = $users;
136
137 if (isset($message))
138 {
139 $message->setContext($this->context);
140 $data['params']['entryDescription'] = \CIMShare::PrepareText([
141 'CHAT_ID' => $chat->getChatId(),
142 'MESSAGE_ID' => $message->getMessageId(),
143 'MESSAGE_TYPE' => $chat->getType(),
144 'MESSAGE' => $message->getMessage(),
145 'AUTHOR_ID' => $message->getAuthorId(),
146 'FILES' => $this->getFilesForPrepareText($message)
147 ]);
148 }
149
150 return $result->setResult($data);
151 }
152
153 protected function sendMessageAboutCalendar(CalendarItem $calendarLink, int $chatId): Result
154 {
155 //todo: Replace with new API
156 $dialogId = Dialog::getDialogId($chatId);
157 $authorId = $this->getContext()->getUserId();
158
159 $messageId = \CIMChat::AddMessage([
160 'DIALOG_ID' => $dialogId,
161 'SYSTEM' => 'Y',
162 'MESSAGE' => $this->getMessageText($calendarLink),
163 'FROM_USER_ID' => $authorId,
164 'PARAMS' => ['CLASS' => "bx-messenger-content-item-system"],
165 'URL_PREVIEW' => 'N',
166 'SKIP_CONNECTOR' => 'Y',
167 'SKIP_COMMAND' => 'Y',
168 'SILENT_CONNECTOR' => 'Y',
169 'SKIP_URL_INDEX' => 'Y',
170 ]);
171
172 $result = new Result();
173
174 if ($messageId === false)
175 {
176 return $result->addError(new CalendarError(CalendarError::ADD_CALENDAR_MESSAGE_FAILED));
177 }
178
179 return $result->setResult($messageId);
180 }
181
182 protected function getFilesForPrepareText(Message $message): array
183 {
184 $files = $message->getFiles();
185 $filesForPrepare = [];
186
187 foreach ($files as $file)
188 {
189 $filesForPrepare[] = ['name' => $file->getDiskFile()->getName()];
190 }
191
192 return $filesForPrepare;
193 }
194
195 protected function getMessageText(CalendarItem $calendar): string
196 {
197 $genderModifier = ($this->getContext()->getUser()->getGender() === 'F') ? '_F' : '';
198
199 if ($calendar->getMessageId() !== null)
200 {
201 $text = (new Message($calendar->getMessageId()))->getQuotedMessage() . "\n";
202 $text .= Loc::getMessage(
203 'IM_CHAT_CALENDAR_REGISTER_FROM_MESSAGE_NOTIFICATION' . $genderModifier,
204 [
205 '#LINK#' => $calendar->getEntity()->getUrl(),
206 '#USER_ID#' => $this->getContext()->getUserId(),
207 '#MESSAGE_ID#' => $calendar->getMessageId(),
208 '#DIALOG_ID#' => Chat::getInstance($calendar->getChatId())->getDialogContextId(),
209 ]
210 );
211
212 return $text;
213 }
214 return Loc::getMessage(
215 'IM_CHAT_CALENDAR_REGISTER_FROM_CHAT_NOTIFICATION' . $genderModifier,
216 [
217 '#LINK#' => $calendar->getEntity()->getUrl(),
218 '#USER_ID#' => $this->getContext()->getUserId(),
219 '#EVENT_TITLE#' => $calendar->getEntity()->getTitle(),
220 ]
221 );
222 }
223}
static getType($chatData)
Definition chat.php:41
static getUsers($chatId, $options=[])
Definition chat.php:787
static getDialogId(int $chatId, $userId=null)
Definition dialog.php:48
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29