41 private const IMPORT_SECTIONS_LIMIT = 10;
51 $this->context = $context;
72 $sections = $this->context->getVendorSyncService()->getSections();
73 foreach ($sections as $sectionDto)
75 if ($sectionDto->canShare)
78 $this->prepareSyncSection($sectionDto),
84 return $result->setData([
85 'externalSyncSectionMap' => $syncSectionMap,
96 $section = $this->context->getConverter()->convertSection($sectionDto);
98 ->setExternalType(Helper::ACCOUNT_TYPE)
99 ->setOwner($this->connection->getOwner())
100 ->setCreator($this->connection->getOwner())
105 ->setSection($section)
106 ->setConnection($this->connection)
107 ->setVendorSectionId($sectionDto->id)
109 ->setLastSyncDate(
null)
110 ->setPrimary($sectionDto->isDefaultCalendar ??
false)
111 ->setOwner($this->connection->getOwner())
113 ->setVersionId($sectionDto->changeKey)
117 ->setSection($section)
118 ->setSectionConnection($sectionConnection)
119 ->setVendorName($this->getServiceName())
138 public function getEvents(SyncSection $syncSection):
Result
140 $syncEventMap =
new SyncEventMap();
141 $result = (
new Result())->setData([
142 'externalSyncEventMap' => $syncEventMap,
144 $service = $this->context->getVendorSyncService();
145 $this->lastSectionConnection = $syncSection->getSectionConnection();
146 foreach ($service->getCalendarDelta($syncSection->getSectionConnection()) as $eventId => $eventPack)
149 if ($dto = ($eventPack[Helper::EVENT_TYPES[
'deleted']] ??
null))
151 $this->pushIntoSyncEventMap(
154 $this->prepareDeletedSyncEvent($dto, $syncSection),
158 elseif ($dto = ($eventPack[Helper::EVENT_TYPES[
'single']] ??
null))
160 $this->pushIntoSyncEventMap($syncEventMap, $eventId, $this->prepareSyncEvent($dto, $syncSection));
162 elseif ($dto = ($eventPack[Helper::EVENT_TYPES[
'series']] ??
null))
164 $master = $this->prepareSyncEvent($dto, $syncSection);
165 $this->pushIntoSyncEventMap($syncEventMap, $eventId, $master);
166 if ($master->getEvent()->getRecurringRule())
169 ->setExcludedDateCollection(
new Core\Event\Properties\ExcludedDatesCollection([]));
170 if ($exceptions = ($eventPack[Helper::EVENT_TYPES[
'exception']] ??
null))
172 foreach ($exceptions as $exceptionDto)
174 $exception = $this->prepareSyncEvent($exceptionDto, $syncSection)
177 $master->addInstance($exception);
181 $instances = array_map(
function (DateTimeDto $val) use ($master) {
182 $result = (
new DateTime(
184 new DateTimeZone($val->timeZone)
185 ))->setTimezone($master->getEvent()->getStartTimeZone()->getTimeZone());
187 return $result->format(
'd.m.Y');
188 }, ($eventPack[Helper::EVENT_TYPES[
'occurrence']] ?? []));
189 $deltaPeriod = $this->context->getHelper()->getDeltaInterval();
190 $computedInstances = (
new Core\Event\Tools\Recurrence())->getEventOccurenceDates(
193 'limitDateFrom' => $deltaPeriod[
'from'],
194 'limitDateTo' => $deltaPeriod[
'to'],
197 foreach ($computedInstances as $date => $dateTime)
199 if (!in_array($date, $instances))
201 $master->getEvent()->getExcludedDateCollection()->add(
202 Core\Base\Date::createDateFromFormat($date,
'd.m.Y')
204 ->setDateTimeFormat(
'd.m.Y')
225 private function pushIntoSyncEventMap(
229 string $action =
'save'
232 $event->setAction($action);
233 $map->add($event, $key);
242 private function prepareSyncEvent(
248 $event = $this->context->getConverter()
249 ->convertEvent($eventDto, $section->getSection());
250 $eventConnection = (
new EventConnection())
252 ->setConnection($section->getSectionConnection()->getConnection())
253 ->setVendorEventId($eventDto->id)
254 ->setVendorVersionId($eventDto->changeKey)
255 ->setEntityTag($eventDto->etag)
256 ->setRecurrenceId($eventDto->seriesMasterId ?: null)
257 ->setData($this->prepareCustomData($eventDto))
258 ->setLastSyncStatus(Dictionary::SYNC_STATUS[
'success'])
261 return (
new SyncEvent())
263 ->setEventConnection($eventConnection);
271 private function prepareCustomData(EventDto $eventDto): array
274 if (!empty($eventDto->location))
276 $data[
'location'] = $eventDto->location->toArray(
true);
278 if (!empty($eventDto->locations))
280 foreach ($eventDto->locations as $location)
282 $data[
'locations'][] = $location->toArray(
true);
286 if (!empty($eventDto->attendees))
288 $data[
'attendees'] = [];
289 foreach ($eventDto->attendees as $attendee)
291 $data[
'attendees'][] = $attendee->toArray(
true);
308 if ($this->lastSectionConnection)
310 return $this->lastSectionConnection->getPageToken();
317 if ($this->lastSectionConnection)
319 return $this->lastSectionConnection->getVersionId();
326 if ($this->lastSectionConnection)
328 return $this->lastSectionConnection->getSyncToken();
335 if ($this->lastSectionConnection)
337 return $this->lastSectionConnection->getLastSyncStatus();
345 'connection' => $this->context->getConnection(),
361 ->setVendorEventId($dto->id)
366 ->setEventConnection($eventConnection);