Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
synceventmergehandler.php
1<?php
2
4
6use Bitrix\Calendar\Core;
8
10{
21 public function __invoke(
22 SyncEvent $savedSyncEvent,
23 SyncEvent $externalSyncEvent,
24 ?int $id = null,
25 ?int $eventConnectionId = null
26 ): SyncEvent
27 {
28 $localEvent = $savedSyncEvent->getEvent();
29 $externalEvent = $externalSyncEvent->getEvent();
30
31 $externalEvent
32 ->setOwner($localEvent->getOwner())
33 ->setSection($localEvent->getSection())
34 ->setId($id)
35 ->setCreator($localEvent->getCreator())
36 ->setCalendarType($localEvent->getCalendarType())
37 ->setSpecialLabel($localEvent->getSpecialLabel())
38 ->setMeetingDescription($localEvent->getMeetingDescription())
39 ->setEventHost($localEvent->getEventHost())
40 ->setAttendeesCollection($localEvent->getAttendeesCollection())
41 ->setIsMeeting($localEvent->isMeeting())
42 ->setMeetingStatus($localEvent->getMeetingStatus())
43 ->setRelations($localEvent->getRelations())
44 ->setVersion($localEvent->getVersion())
45 ->setRemindCollection($this->prepareReminders($localEvent, $externalEvent))
46 ;
47
48 $externalEventConnection = $externalSyncEvent->getEventConnection();
49
50 if ($externalEventConnection)
51 {
52 $externalEventConnection
53 ->setId($eventConnectionId)
54 ->setVersion($localEvent->getVersion())
55 ->setRetryCount(0)
56 ;
57 }
58
59 return $externalSyncEvent;
60 }
61
70 private function prepareReminders(Event $localEvent, Event $externalEvent): ?Core\Event\Properties\RemindCollection
71 {
72 $localRemindCollection = $localEvent->getRemindCollection();
73 $externalRemindCollection = $externalEvent->getRemindCollection();
74 if (!$externalRemindCollection || $externalRemindCollection->count() === 0)
75 {
76 if ($localRemindCollection)
77 {
78 // if reminders count more than one, we don't know which reminder was deleted,
79 // and leave all of them
80 return ($localRemindCollection->count() > 1)
81 ? $localEvent->getRemindCollection()
82 : null
83 ;
84 }
85 else
86 {
87 return null;
88 }
89 }
90
91 if ((!$localRemindCollection || $localRemindCollection->count() < 2))
92 {
93 return $externalEvent->getRemindCollection()->setSingle(false);
94 }
95 else
96 {
97 if ($externalRemindCollection->isSingle())
98 {
99 $this->removeClosestRemind($localRemindCollection);
100 $localRemindCollection->add($externalRemindCollection->fetch());
101 return $localRemindCollection;
102 }
103 else
104 {
105 return $this->mergeRemindCollections(
106 $externalRemindCollection,
107 $localRemindCollection,
108 );
109 }
110 }
111 }
112
118 private function removeClosestRemind(?Core\Event\Properties\RemindCollection $remindCollection)
119 {
120 $minValue = null;
121 $minIndex = null;
122
124 foreach ($remindCollection->getCollection() as $index => $item)
125 {
126 $offset = $item->getTimeBeforeStartInMinutes();
127 if ($offset < 0)
128 {
129 continue;
130 }
131
132 if ($minValue === null || $offset < $minValue)
133 {
134 $minValue = $offset;
135 $minIndex = $index;
136 }
137 }
138 if ($minIndex !== null)
139 {
140 $remindCollection->remove($minIndex);
141 }
142 }
143
150 private function mergeRemindCollections(
151 Core\Event\Properties\RemindCollection $externalRemindCollection,
152 Core\Event\Properties\RemindCollection $localRemindCollection
153 ): Core\Event\Properties\RemindCollection
154 {
155 $prepareMap = function (?Core\Event\Properties\RemindCollection $collection)
156 {
157 $result = [];
159 foreach ($collection as $index => $item)
160 {
161 $result[$item->getTimeBeforeStartInMinutes()] = $index;
162 }
163 return $result;
164 };
165
166 $externalMap = $prepareMap($externalRemindCollection);
167 $localMap = $prepareMap($localRemindCollection);
168
169 foreach ($localMap as $key => $index)
170 {
171 if (!array_key_exists($key, $externalMap))
172 {
173 $localRemindCollection->remove($index);
174 }
175 }
176
177 return $localRemindCollection->addItems($externalRemindCollection->getCollection());
178 }
179}
__invoke(SyncEvent $savedSyncEvent, SyncEvent $externalSyncEvent, ?int $id=null, ?int $eventConnectionId=null)