19 protected const NEXT =
false;
36 public function syncLocalEvents(array $events,
int $userId,
string $gApiCalendarId): array
38 return $this->syncEvents($events, $userId, $gApiCalendarId);
42 private function syncEvents($events, $userId, $gApiCalendarId): array
44 return $this->mergeLocalEventWithExternalData(
45 $events, $this->getEventsExternalFields($userId, $events, $gApiCalendarId));
54 return $this->mergeLocalEventWithExternalData(
56 $this->getInstancesExternalFields($userId, $instances, $gApiCalendarId)
68 private function calculateExDate(
int $eventId,
string $exDates): string
70 return implode(
';', array_diff(
71 explode(
';', $exDates), $this->getExDatesByInstances($eventId)
82 private function getInstanceByRecurrenceId(
int $eventId): array
84 $instancesDb = Internals\EventTable::getList([
85 'filter' => Query::filter()->where(
'RECURRENCE_ID', $eventId),
89 while ($instance = $instancesDb->fetch())
91 $instances[] = $instance;
105 private function preparedEventToBatch(array $events): array
117 private function getExDatesByInstances(
int $originalEventId): array
119 $instances = $this->getInstanceByRecurrenceId($originalEventId);
122 foreach ($instances as $instance)
124 $excludeDates[] = \CCalendar::Date(\CCalendar::Timestamp($instance[
'DATE_FROM']),
false);
127 return $excludeDates;
135 private function prepareInstanceToBatch(array $instances): array
138 foreach ($instances as $instance)
140 $currentInstance = $instance;
141 if (empty($instance[
'ORIGINAL_DATE_FROM']))
143 $instanceDate = \CCalendar::GetOriginalDate(
144 $instance[
'PARENT_DATE_FROM'],
145 $instance[
'DATE_FROM'],
146 $instance[
'PARENT_TZ_FROM']
151 $instanceDate = $instance[
'ORIGINAL_DATE_FROM'];
154 $eventOriginalStart =
Util::getDateObject($instanceDate,
false, $instance[
'PARENT_TZ_FROM']);
155 $currentInstance[
'ORIGINAL_DATE_FROM'] =
156 $eventOriginalStart->format(
Type\Date::convertFormatToPhp(FORMAT_DATETIME));
157 $currentInstance[
'DAV_XML_ID'] = $instance[
'PARENT_DAV_XML_ID'];
158 $currentInstance[
'gEventId'] = $instance[
'PARENT_G_EVENT_ID'] .
'_'
160 $currentInstance[
'G_EVENT_ID'] = $currentInstance[
'gEventId'];
161 $batch[] = $currentInstance;
172 private function mergeLocalEventWithExternalData(array $events, array $externalFields): array
176 foreach ($events as $event)
178 if (isset($externalFields[$event[
'ID']]))
181 $resultEvents[] = array_merge($event, $externalFields[$event[
'ID']]);
189 return $resultEvents;
200 private function getInstancesExternalFields(
int $userId, array $instances,
string $gApiCalendarId): array
202 return (
new GoogleApiSync($userId))
203 ->saveBatchEvents($this->prepareInstanceToBatch($instances), $gApiCalendarId, [
'method' =>
HttpClient::HTTP_PUT])
216 private function getEventsExternalFields(
int $userId, array $events,
string $gApiCalendarId): array
218 return (
new GoogleApiSync($userId))
219 ->saveBatchEvents($this->preparedEventToBatch($events), $gApiCalendarId, [
'method' =>
HttpClient::HTTP_POST])