1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
synceventmergehandler.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Handlers;
4
5use Bitrix\Calendar\Sync\Entities\SyncEvent;
6use Bitrix\Calendar\Core;
7use Bitrix\Calendar\Core\Event\Event;
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 $externalEventConnection?->setId($eventConnectionId)
51 ->setVersion($localEvent->getVersion())
52 ->setRetryCount(0)
53 ;
54
55 return $externalSyncEvent;
56 }
57
66 private function prepareReminders(Event $localEvent, Event $externalEvent): ?Core\Event\Properties\RemindCollection
67 {
68 $localRemindCollection = $localEvent->getRemindCollection();
69 $externalRemindCollection = $externalEvent->getRemindCollection();
70 if (!$externalRemindCollection || $externalRemindCollection->count() === 0)
71 {
72 if ($localRemindCollection)
73 {
74 // if reminders count more than one, we don't know which reminder was deleted,
75 // and leave all of them
76 return ($localRemindCollection->count() > 1)
77 ? $localEvent->getRemindCollection()
78 : null
79 ;
80 }
81 else
82 {
83 return null;
84 }
85 }
86
87 if ((!$localRemindCollection || $localRemindCollection->count() < 2))
88 {
89 return $externalEvent->getRemindCollection()->setSingle(false);
90 }
91 else
92 {
93 if ($externalRemindCollection->isSingle())
94 {
95 $this->removeClosestRemind($localRemindCollection);
96 $localRemindCollection->add($externalRemindCollection->fetch());
97 return $localRemindCollection;
98 }
99 else
100 {
101 return $this->mergeRemindCollections(
102 $externalRemindCollection,
103 $localRemindCollection,
104 );
105 }
106 }
107 }
108
114 private function removeClosestRemind(?Core\Event\Properties\RemindCollection $remindCollection)
115 {
116 $minValue = null;
117 $minIndex = null;
118
120 foreach ($remindCollection->getCollection() as $index => $item)
121 {
122 $offset = $item->getTimeBeforeStartInMinutes();
123 if ($offset < 0)
124 {
125 continue;
126 }
127
128 if ($minValue === null || $offset < $minValue)
129 {
130 $minValue = $offset;
131 $minIndex = $index;
132 }
133 }
134 if ($minIndex !== null)
135 {
136 $remindCollection->remove($minIndex);
137 }
138 }
139
146 private function mergeRemindCollections(
147 Core\Event\Properties\RemindCollection $externalRemindCollection,
148 Core\Event\Properties\RemindCollection $localRemindCollection
149 ): Core\Event\Properties\RemindCollection
150 {
151 $prepareMap = function (?Core\Event\Properties\RemindCollection $collection)
152 {
153 $result = [];
155 foreach ($collection as $index => $item)
156 {
157 $result[$item->getTimeBeforeStartInMinutes()] = $index;
158 }
159 return $result;
160 };
161
162 $externalMap = $prepareMap($externalRemindCollection);
163 $localMap = $prepareMap($localRemindCollection);
164
165 foreach ($localMap as $key => $index)
166 {
167 if (!array_key_exists($key, $externalMap))
168 {
169 $localRemindCollection->remove($index);
170 }
171 }
172
173 return $localRemindCollection->addItems($externalRemindCollection->getCollection());
174 }
175}
getRemindCollection()
Определения event.php:886
__invoke(SyncEvent $savedSyncEvent, SyncEvent $externalSyncEvent, ?int $id=null, ?int $eventConnectionId=null)
Определения synceventmergehandler.php:21
$result
Определения get_property_values.php:14
if(empty($signedUserToken)) $key
Определения quickway.php:257