1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
CalendarService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Link\Calendar;
4
5use Bitrix\Im\Dialog;
6use Bitrix\Im\Model\LinkCalendarIndexTable;
7use Bitrix\Im\V2\Chat;
8use Bitrix\Im\V2\Common\ContextCustomer;
9use Bitrix\Im\V2\Entity\Calendar\CalendarError;
10use Bitrix\Im\V2\Link\Push;
11use Bitrix\Im\V2\Message;
12use Bitrix\Im\V2\RelationCollection;
13use Bitrix\Im\V2\Result;
14use Bitrix\Main\Loader;
15use Bitrix\Main\Localization\Loc;
16
18{
19 use ContextCustomer;
20
21 protected const ADD_CALENDAR_EVENT = 'calendarAdd';
22 protected const UPDATE_CALENDAR_EVENT = 'calendarUpdate';
23 protected const DELETE_CALENDAR_EVENT = 'calendarDelete';
24
25 public function registerCalendar(int $chatId, ?int $messageId, \Bitrix\Im\V2\Entity\Calendar\CalendarItem $calendar): Result
26 {
27 $result = new Result();
28
29 $userId = $this->getContext()->getUserId();
30
31 $calendarLink = new CalendarItem();
32 $calendarLink->setEntity($calendar)->setChatId($chatId)->setAuthorId($userId);
33
34 if (isset($messageId))
35 {
36 $calendarLink->setMessageId($messageId);
37 }
38
39 $sendMessageResult = $this->sendMessageAboutCalendar($calendarLink, $chatId);
40
41 if (!$sendMessageResult->isSuccess())
42 {
43 $result->addErrors($sendMessageResult->getErrors());
44 }
45
46 $systemMessageId = $sendMessageResult->getResult();
47
48 $calendarLink->setMessageId($messageId ?: $systemMessageId);
49 $saveResult = $calendarLink->save();
50
51 if (!$saveResult->isSuccess())
52 {
53 return $result->addErrors($saveResult->getErrors());
54 }
55
57 ->setContext($this->context)
58 ->sendFull($calendarLink, self::ADD_CALENDAR_EVENT, ['RECIPIENT' => $calendar->getMembersIds()])
59 ;
60
61 return $result;
62 }
63
64 public function unregisterCalendar(CalendarItem $calendar): Result
65 {
66 $calendar->delete();
68 ->setContext($this->context)
69 ->sendIdOnly($calendar, self::DELETE_CALENDAR_EVENT, ['CHAT_ID' => $calendar->getChatId()])
70 ;
71
72 return new Result();
73 }
74
75 public function updateCalendar(CalendarItem $calendarLink): Result
76 {
77 $result = new Result();
78
79 LinkCalendarIndexTable::delete($calendarLink->getPrimaryId());
80 $saveResult = $calendarLink->save();
81
82 if (!$saveResult->isSuccess())
83 {
84 return $result->addErrors($saveResult->getErrors());
85 }
86
88 ->setContext($this->context)
89 ->sendFull($calendarLink, self::UPDATE_CALENDAR_EVENT, ['RECIPIENT' => $calendarLink->getEntity()->getMembersIds()])
90 ;
91
92 return new Result();
93 }
94
95 public function updateCalendarLink(CalendarItem $calendarLink): Result
96 {
97 $result = new Result();
98
99 $saveResult = $calendarLink->save();
100
101 if (!$saveResult->isSuccess())
102 {
103 return $result->addErrors($saveResult->getErrors());
104 }
105
107 ->setContext($this->context)
108 ->sendFull($calendarLink, self::UPDATE_CALENDAR_EVENT, ['RECIPIENT' => $calendarLink->getEntity()->getMembersIds()])
109 ;
110
111 return new Result();
112 }
113
114 public function updateCalendarLinks(CalendarCollection $calendarCollection): Result
115 {
116 $result = new Result();
117
118 if ($calendarCollection->count() === 0)
119 {
120 return $result;
121 }
122
123 $saveResult = $calendarCollection->save();
124
125 if (!$saveResult->isSuccess())
126 {
127 return $result->addErrors($saveResult->getErrors());
128 }
129
130 $calendarCollection->fillEntities();
131
132 foreach ($calendarCollection as $calendarItem)
133 {
134 $pushRecipient = ['RECIPIENT' => $calendarItem->getEntity()->getMembersIds()];
136 ->setContext($this->context)
137 ->sendFull($calendarItem, self::UPDATE_CALENDAR_EVENT, $pushRecipient)
138 ;
139 }
140
141 return new Result();
142 }
143
144 public function prepareDataForCreateSlider(Chat $chat, ?Message $message = null): Result
145 {
146 $currentUserId = $this->getContext()->getUserId();
147 $result = new Result();
148
149 if (!Loader::includeModule('calendar'))
150 {
152 }
153
154 $chat->setContext($this->context);
155
156 $randomPostfix = mt_rand() & 1000; // get random number from 0 to 1000
157 $data['params']['sliderId'] = "im:chat{$chat->getChatId()}{$randomPostfix}";
158 $data['params']['createChatId'] = $chat->getId();
159
160 if ($chat->getEntityType() === Chat\ExtendedType::Sonet->value)
161 {
162 $data['params']['type'] = 'group';
163 $data['params']['ownerId'] = $chat->getEntityId();
164 }
165 else
166 {
167 $userIds = $this->getParticipants($chat);
168 $users = array_values(array_map(static fn($item) => ['id' => (int)$item, 'entityId' => 'user'], $userIds));
169 $data['params']['participantsEntityList'] = $users;
170 $data['params']['type'] = 'user';
171 }
172
173 if (isset($message))
174 {
175 $message->setContext($this->context);
176 $data['params']['entryDescription'] = \CIMShare::PrepareText([
177 'CHAT_ID' => $chat->getChatId(),
178 'MESSAGE_ID' => $message->getMessageId(),
179 'MESSAGE_TYPE' => $chat->getType(),
180 'MESSAGE' => $message->getMessage(),
181 'AUTHOR_ID' => $message->getAuthorId(),
182 'FILES' => $this->getFilesForPrepareText($message)
183 ]);
184 }
185
186 return $result->setResult($data);
187 }
188
189 protected function getParticipants(Chat $chat): array
190 {
191 $participants = RelationCollection::find(
192 [
193 'ACTIVE' => true,
194 'ONLY_INTERNAL_TYPE' => true,
195 'CHAT_ID' => $chat->getId(),
196 'IS_HIDDEN' => false,
197 'ONLY_INTRANET' => true,
198 ],
199 limit: 50,
200 select: ['ID', 'USER_ID', 'CHAT_ID']
201 )->getUserIds();
202 $currentUserId = $this->getContext()->getUserId();
203 $participants[$currentUserId] = $currentUserId;
204
205 return $participants;
206 }
207
208 protected function sendMessageAboutCalendar(CalendarItem $calendarLink, int $chatId): Result
209 {
210 //todo: Replace with new API
211 $dialogId = Dialog::getDialogId($chatId);
212 $authorId = $this->getContext()->getUserId();
213
214 $messageId = \CIMChat::AddMessage([
215 'DIALOG_ID' => $dialogId,
216 'SYSTEM' => 'Y',
217 'MESSAGE' => $this->getMessageText($calendarLink),
218 'FROM_USER_ID' => $authorId,
219 'PARAMS' => ['CLASS' => "bx-messenger-content-item-system"],
220 'URL_PREVIEW' => 'N',
221 'SKIP_CONNECTOR' => 'Y',
222 'SKIP_COMMAND' => 'Y',
223 'SILENT_CONNECTOR' => 'Y',
224 'SKIP_URL_INDEX' => 'Y',
225 ]);
226
227 $result = new Result();
228
229 if ($messageId === false)
230 {
232 }
233
234 return $result->setResult($messageId);
235 }
236
238 {
239 $files = $message->getFiles();
240 $filesForPrepare = [];
241
242 foreach ($files as $file)
243 {
244 $filesForPrepare[] = ['name' => $file->getDiskFile()->getName()];
245 }
246
247 return $filesForPrepare;
248 }
249
250 protected function getMessageText(CalendarItem $calendar): string
251 {
252 $genderModifier = ($this->getContext()->getUser()->getGender() === 'F') ? '_F' : '';
253
254 if ($calendar->getMessageId() !== null)
255 {
256 $text = (new Message($calendar->getMessageId()))->getQuotedMessage() . "\n";
257 $text .= Loc::getMessage(
258 'IM_CHAT_CALENDAR_REGISTER_FROM_MESSAGE_NOTIFICATION' . $genderModifier . '_MSGVER_1',
259 [
260 '#LINK#' => $calendar->getEntity()->getUrl(),
261 '#USER_ID#' => $this->getContext()->getUserId(),
262 '#MESSAGE_ID#' => $calendar->getMessageId(),
263 '#DIALOG_ID#' => Chat::getInstance($calendar->getChatId())->getDialogContextId(),
264 ]
265 );
266
267 return $text;
268 }
269 return Loc::getMessage(
270 'IM_CHAT_CALENDAR_REGISTER_FROM_CHAT_NOTIFICATION' . $genderModifier . '_MSGVER_1',
271 [
272 '#LINK#' => $calendar->getEntity()->getUrl(),
273 '#USER_ID#' => $this->getContext()->getUserId(),
274 '#EVENT_TITLE#' => $calendar->getEntity()->getTitle(),
275 ]
276 );
277 }
278}
return select
Определения access_edit.php:440
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getType($chatData, bool $camelCase=true)
Определения chat.php:45
static getDialogId(int $chatId, $userId=null)
Определения dialog.php:50
static find(array $filter, array $order=[], ?int $limit=null, ?Context $context=null, array $select=self::COMMON_FIELDS)
Определения RelationCollection.php:50
static getInstance()
Определения application.php:98
Определения result.php:20
static PrepareText($quoteMessage)
Определения im_share.php:512
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения Uuid.php:3
Определения ActionUuid.php:3
Определения ufield.php:9
$files
Определения mysql_to_pgsql.php:30
$message
Определения payment.php:8
$text
Определения template_pdf.php:79