Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventattendeesupdate.php
1<?php
2
4
12use Bitrix\Calendar\Core;
17
19{
30 public function process(Interfaces\Message $message): string
31 {
32 $data = $message->getBody();
33
34 if (empty($data['eventId']))
35 {
36 return self::REJECT;
37 }
38
39 $eventId = (int)$data['eventId'];
40
41 $eventArray = EventTable::getList([
42 'select' => ['*'],
43 'filter' => \Bitrix\Main\ORM\Query\Query::filter()
44 ->where('DELETED', 'N')
45 ->where('ID', $eventId)
46 ,
47 ])->fetch();
48
49 if (!$eventArray)
50 {
51 return self::REJECT;
52 }
53
54 $event = (new EventBuilderFromArray($eventArray))->build();
55
56 $fields = [
57 'ID' => $event->getId(),
58 'ATTENDEES' => \CCalendar::GetDestinationUsers($event->getAttendeesCollection()->getAttendeesCodes()),
59 'REMIND' => unserialize($eventArray['REMIND'], ['allowed_classes' => false]),
60 ];
61
62 if (
63 $event->getCalendarType() === Core\Event\Tools\Dictionary::CALENDAR_TYPE['group']
64 && Loader::includeModule('socialnetwork')
65 )
66 {
67 $fields = $this->prepareGroupEventData($event, $fields);
68 }
69
70 if ($event->getRecurringRule())
71 {
72 $previousAttendees = $this->getEventPreviousAttendees($eventId);
73 $result = $this->saveRecurrentEvent($event, $fields, $eventArray);
74 }
75 else
76 {
77 $result = $this->saveNotRecurrentEvent($event, $fields);
78 }
79
80 if ($result && $this->wasEventOwnerUpdated($fields))
81 {
82 $newHostEventData = $this->getNewHostEventData($fields, $result);
83 }
84
85 if (
86 $result
87 && Loader::includeModule('im')
88 && $event->getMeetingDescription()
89 && !empty($event->getMeetingDescription()->getFields()['CHAT_ID'])
90 )
91 {
92 $currentAttendees = $fields['ATTENDEES'];
93 $this->updateEventChat(
94 $event->getMeetingDescription()->getFields()['CHAT_ID'],
95 $fields['MEETING_HOST'] ?? null,
96 array_diff($previousAttendees ?? [], $currentAttendees),
97 );
98 }
99
100 if (!empty($newHostEventData))
101 {
102 $this->updateNewMeetingHostStatus($newHostEventData);
103 $this->updateCreatedByFieldInGroupEvent($result, $fields['MEETING_HOST']);
104 \CCalendar::ClearCache();
105 }
106
107 return self::ACK;
108 }
109
118 protected function prepareGroupEventData(Event $event, array $fields): array
119 {
120 $groupMembers = \Bitrix\Socialnetwork\UserToGroupTable::query()
121 ->setSelect(['USER_ID', 'ROLE'])
122 ->addFilter('GROUP_ID', $event->getOwner()->getId())
123 ->fetchAll()
124 ;
125
126 foreach ($groupMembers as $groupMember)
127 {
128 if ($event->getEventHost() && (int)($groupMember['USER_ID'] ?? null) === $event->getEventHost()->getId())
129 {
130 $eventHostId = (int)$groupMember['USER_ID'];
131 }
132 if (($groupMember['ROLE'] ?? null) === \Bitrix\Socialnetwork\UserToGroupTable::ROLE_OWNER)
133 {
134 $groupOwnerId = (int)$groupMember['USER_ID'];
135 }
136 }
137
138 $isEventHostGroupMember = !empty($eventHostId);
139
140 if (!$isEventHostGroupMember && !empty($groupOwnerId) && $event->getMeetingDescription())
141 {
142 $fields = $this->prepareGroupEventWithNewHostFields($event, $groupOwnerId, $fields);
143 }
144
145 return $fields;
146 }
147
154 protected function prepareGroupEventWithNewHostFields(Event $event, int $groupOwnerId, array $fields): array
155 {
156 $attendeesCodes = $event->getAttendeesCollection()->getAttendeesCodes();
157 $fields['ATTENDEES_CODES'] = $this->removePreviousMeetingHostFromAttendeesCodes($attendeesCodes, $event);
158
159 $fields['ATTENDEES'] = \CCalendar::GetDestinationUsers($fields['ATTENDEES_CODES']);
160
161 if (is_array($fields['ATTENDEES']) && !in_array($groupOwnerId, $fields['ATTENDEES'], true))
162 {
163 $fields['ATTENDEES'][] = $groupOwnerId;
164 $fields['ATTENDEES_CODES'] = array_merge(['U'. $groupOwnerId],$fields['ATTENDEES_CODES']);
165 }
166 $fields['ATTENDEES'] = array_unique($fields['ATTENDEES']);
167
168 $groupOwner = \CUser::GetByID($groupOwnerId)->Fetch();
169 $fields['MEETING_HOST'] = $groupOwnerId;
170
171 $meetingFields = $event->getMeetingDescription()->getFields();
172 $meetingFields['HOST_NAME'] = ltrim($groupOwner['NAME'] . ' ' . $groupOwner['LAST_NAME']);
173 $meetingFields['MEETING_CREATOR'] = $groupOwnerId;
174
175 $fields['MEETING'] = $meetingFields;
176
177 return $fields;
178 }
179
185 protected function removePreviousMeetingHostFromAttendeesCodes(array $attendeesCodes, Event $event): array
186 {
187 return array_filter(
188 $attendeesCodes,
189 static function ($attendeeCode) use ($event)
190 {
191 return $attendeeCode !== 'U' . $event->getEventHost()->getId();
192 },
193 );
194 }
195
201 protected function getEventPreviousAttendees(int $parentEventId): array
202 {
203 $events = EventTable::query()
204 ->addSelect('OWNER_ID')
205 ->addFilter('DELETED', 'N')
206 ->addFilter('PARENT_ID', $parentEventId)
207 ->addFilter('CAL_TYPE', Core\Event\Tools\Dictionary::CALENDAR_TYPE['user'])
208 ->fetchAll()
209 ;
210 $previousAttendees = [];
211 foreach ($events as $event)
212 {
213 $previousAttendees[] = (int)$event['OWNER_ID'];
214 }
215
216 return $previousAttendees;
217 }
218
223 protected function wasEventOwnerUpdated(array $fields): bool
224 {
225 return !empty($fields['MEETING_HOST']);
226 }
227
236 protected function getNewHostEventData(array $fields, int $eventId)
237 {
238 return EventTable::query()
239 ->setSelect(['ID', 'MEETING_STATUS'])
240 ->addFilter('DELETED', 'N')
241 ->addFilter('PARENT_ID', $eventId)
242 ->addFilter('CAL_TYPE', Core\Event\Tools\Dictionary::CALENDAR_TYPE['user'])
243 ->addFilter('OWNER_ID', $fields['MEETING_HOST'])
244 ->fetch()
245 ;
246 }
247
254 protected function updateEventChat(
255 ?int $chatId,
256 ?int $newMeetingHostId,
257 array $deletedAttendeesIds
258 ): void
259 {
260 if ($chatId > 0)
261 {
262 $chat = new \CIMChat($newMeetingHostId);
263 if ($newMeetingHostId > 0)
264 {
265 $chat->SetOwner($chatId, $newMeetingHostId, false);
266 }
267 foreach ($deletedAttendeesIds as $deletedAttendeeId)
268 {
269 $chat->DeleteUser($chatId, $deletedAttendeeId, false);
270 }
271 }
272 }
273
279 protected function updateNewMeetingHostStatus(array $newHostEventData): void
280 {
281 if (!empty($newHostEventData['ID']) && !empty($newHostEventData['MEETING_STATUS']))
282 {
283 EventTable::update((int)$newHostEventData['ID'], [
284 'MEETING_STATUS' => \Bitrix\Calendar\Core\Event\Tools\Dictionary::MEETING_STATUS['Yes'],
285 ]);
286 }
287 }
288
295 protected function updateCreatedByFieldInGroupEvent(int $eventId, int $newOwnerId): void
296 {
297 EventTable::update($eventId, [
298 'CREATED_BY' => $newOwnerId,
299 ]);
300 }
301
307 protected function saveNotRecurrentEvent(Event $event, array $fields)
308 {
309 return \CCalendar::SaveEvent([
310 'recursionEditMode' => 'skip',
311 'overSaving' => true,
312 'checkPermission' => false,
313 'sendInvitations' => true,
314 'arFields' => $fields,
315 'userId' => $this->getSaveEventUserId($fields, $event),
316 ]);
317 }
318
326 protected function saveRecurrentEvent(Event $event, array $fields, array $eventArray)
327 {
328 $entries = [];
329 \CCalendarEvent::ParseRecursion($entries, $eventArray, [
330 'fromLimitTs' => time() - $eventArray['TZ_OFFSET_FROM'],
331 'toLimitTs' => $eventArray['DATE_TO_TS_UTC'],
332 'instanceCount' => 1,
333 'loadLimit' => false,
334 'preciseLimits' => true,
335 'checkPermission' => false,
336 'userId' => $this->getSaveEventUserId($fields, $event),
337 ]);
338
339 if (!empty($entries))
340 {
341 $result = \CCalendar::SaveEventEx([
342 'recursionEditMode' => 'next',
343 'currentEventDateFrom' => $entries[0]['DATE_FROM'],
344 'overSaving' => true,
345 'checkPermission' => false,
346 'sendInvitations' => true,
347 'arFields' => $fields,
348 'userId' => $this->getSaveEventUserId($fields, $event),
349 ]);
350
351 return $result['recEventId'] ?? $result['id'] ?? null;
352 }
353
354 return 0;
355 }
356
362 protected function getSaveEventUserId(array $fields, Event $event)
363 {
364 return $fields['MEETING_HOST'] ?? ($event->getEventHost() ? $event->getEventHost()->getId() : null);
365 }
366}
removePreviousMeetingHostFromAttendeesCodes(array $attendeesCodes, Event $event)
saveRecurrentEvent(Event $event, array $fields, array $eventArray)
prepareGroupEventWithNewHostFields(Event $event, int $groupOwnerId, array $fields)
updateEventChat(?int $chatId, ?int $newMeetingHostId, array $deletedAttendeesIds)