20 private const LIMIT_SECTIONS_FOR_CHECK = 5;
35 if (!CCalendar::isGoogleApiEnabled())
40 $eventsDb = self::getEventsDb($lastHandledId);
42 if ($eventsDb ===
false)
44 return "\\Bitrix\\Calendar\\Sync\\Google\\QueueManager::checkNotSendEvents();";
48 while ($event = $eventsDb->Fetch())
50 $lastHandledId = $event[
'ID'];
57 $event = self::prepareEvent($event);
59 switch ($event[
'SYNC_STATUS'])
62 self::createEvent($event);
65 self::updateEvent($event);
68 self::deleteEvent($event);
71 self::createInstance($event);
74 self::updateParents($event);
77 self::updateNextEvents($event);
80 self::detectEventType($event);
83 self::detectEventType($event);
87 return "\\Bitrix\\Calendar\\Sync\\Google\\QueueManager::checkNotSendEvents(" . $lastHandledId .
");";
99 private static function createEvent(array $event): void
102 $fields = $google->saveEvent($event, $event[
'GAPI_CALENDAR_ID']);
104 if ($fields !==
null)
106 \CCalendarEvent::updateEventFields($event, [
107 'DAV_XML_ID' => $fields[
'DAV_XML_ID'],
108 'CAL_DAV_LABEL' => $fields[
'CAL_DAV_LABEL'],
109 'G_EVENT_ID' => $fields[
'G_EVENT_ID'],
119 private static function updateEvent(array $event): void
121 $google =
new GoogleApiSync($event[
'OWNER_ID']);
122 $fields = $google->saveEvent($event, $event[
'GAPI_CALENDAR_ID'], []);
124 if ($errors = $google->getTransportErrors())
126 $googleHelper = ServiceLocator::getInstance()->get(
'calendar.service.google.helper');
127 if (is_array($errors))
129 foreach ($errors as $error)
132 $googleHelper->isDeletedResource($error[
'message'])
133 || $googleHelper->isNotFoundError($error[
'message'])
136 \CCalendarEvent::updateEventFields($event, [
139 'CAL_DAV_LABEL' =>
'',
142 self::createEvent($event);
148 else if ($fields !==
null)
150 \CCalendarEvent::updateEventFields($event, [
151 'CAL_DAV_LABEL' => $fields[
'CAL_DAV_LABEL'],
161 private static function deleteEvent(array $event): void
163 $google =
new GoogleApiSync($event[
'OWNER_ID']);
164 $google->deleteEvent($event[
'G_EVENT_ID'], $event[
'GAPI_CALENDAR_ID']);
166 if ($errors = $google->getTransportErrors())
168 $isUpdatedResult =
false;
170 $googleHelper = ServiceLocator::getInstance()->get(
'calendar.service.google.helper');
171 if (is_array($errors))
173 foreach ($errors as $error)
176 $googleHelper->isDeletedResource($error[
'message'])
177 || $googleHelper->isNotFoundError($error[
'message'])
180 $isUpdatedResult =
true;
187 if (!$isUpdatedResult)
204 private static function createInstance(array $event): void
207 $fields = (
new GoogleApiSync($event[
'OWNER_ID']))->saveEvent($event, $event[
'GAPI_CALENDAR_ID'], [
208 'editInstance' =>
true,
209 'originalDavXmlId' => $parentEvent[
'DAV_XML_ID'],
210 'instanceTz' => $event[
'TZ_FROM'],
213 if ($fields !==
null)
215 \CCalendarEvent::updateEventFields($event, [
216 'DAV_XML_ID' => $fields[
'DAV_XML_ID'],
217 'G_EVENT_ID' => $fields[
'G_EVENT_ID'],
218 'CAL_DAV_LABEL' => $fields[
'CAL_DAV_LABEL'],
228 private static function updateParents(array $event): void
230 $fields = (
new GoogleApiSync($event[
'OWNER_ID']))->saveEvent($event, $event[
'GAPI_CALENDAR_ID'], [
231 'editParents' =>
true,
232 'editNextEvents' =>
false
235 if ($fields !==
null)
237 \CCalendarEvent::updateEventFields($event, [
238 'DAV_XML_ID' => $fields[
'DAV_XML_ID'],
239 'CAL_DAV_LABEL' => $fields[
'CAL_DAV_LABEL'],
240 'G_EVENT_ID' => $fields[
'G_EVENT_ID'],
250 private static function updateNextEvents(array $event): void
252 $fields = (
new GoogleApiSync($event[
'OWNER_ID']))->saveEvent($event, $event[
'GAPI_CALENDAR_ID'], [
253 'editNextEvents' =>
true,
256 if ($fields !==
null)
258 \CCalendarEvent::updateEventFields($event, [
259 'DAV_XML_ID' => $fields[
'DAV_XML_ID'],
260 'CAL_DAV_LABEL' => $fields[
'CAL_DAV_LABEL'],
261 'G_EVENT_ID' => $fields[
'G_EVENT_ID'],
271 private static function prepareEvent(array $event): array
273 if (!empty($event[
'RRULE']))
275 $event[
'RRULE'] = \CCalendarEvent::ParseRRULE($event[
'RRULE']);
278 if (!empty($event[
'REMIND']) && is_string($event[
'REMIND']))
280 $event[
'REMIND'] = unserialize($event[
'REMIND'], [
'allowed_classes' =>
false]);
290 private static function getEventsDb(
int $lastHandledId = 0)
295 "SELECT e.*, c.LAST_RESULT, s.GAPI_CALENDAR_ID, "
296 . $DB->DateToCharFunction(
'e.DATE_FROM') .
" as DATE_FROM, "
297 . $DB->DateToCharFunction(
'e.DATE_TO') .
" as DATE_TO, "
298 . $DB->DateToCharFunction(
'e.ORIGINAL_DATE_FROM').
" as ORIGINAL_DATE_FROM, "
299 . $DB->DateToCharFunction(
'e.DATE_CREATE').
" as DATE_CREATE, "
300 . $DB->DateToCharFunction(
'e.TIMESTAMP_X').
" as TIMESTAMP_X"
301 .
" FROM b_calendar_event e"
302 .
" INNER JOIN b_calendar_section s ON e.SECTION_ID = s.ID"
303 .
" INNER JOIN b_dav_connections c ON c.ID = s.CAL_DAV_CON"
304 .
" WHERE e.SYNC_STATUS <> 'success'"
305 .
" AND e.ID > ".$lastHandledId
306 .
" AND s.EXTERNAL_TYPE IN ('local', 'google')"
307 .
" ORDER BY e.ID ASC"
320 private static function detectEventType(array $event): void
330 if (!empty($event[
'RECURRENCE_ID']))
332 self::createInstance($event);
336 if ($event[
'DELETED'] ===
'Y' && !empty($event[
'G_EVENT_ID']))
338 self::deleteEvent($event);
340 elseif (!empty($event[
'RRULE']) && empty($event[
'G_EVENT_ID']))
342 self::updateNextEvents($event);
344 elseif (!empty($event[
'RRULE']) && !empty($event[
'G_EVENT_ID']))
346 self::updateParents($event);
348 elseif (!empty($event[
'G_EVENT_ID']))
350 self::updateEvent($event);
354 self::createEvent($event);
366 return SectionTable::query()
367 ->whereNotNull(
'PAGE_TOKEN')
369 ->setLimit(self::LIMIT_SECTIONS_FOR_CHECK)
381 public static function setIntervalForAgent(
int $agentInterval = self::REGULAR_CHECK_TIME,
int $delay = self::REGULAR_CHECK_TIME): void
383 $agent = \CAgent::getList(
386 'MODULE_ID' =>
'calendar',
387 '=NAME' =>
'\\Bitrix\\Calendar\\Sync\\Google\\QueueManager::checkIncompleteSync();'
391 if (is_array($agent) && $agent[
'ID'])
393 if ((
int)$agent[
'AGENT_INTERVAL'] !== $agentInterval)
398 'AGENT_INTERVAL' => $agentInterval,
399 'NEXT_EXEC' => ConvertTimeStamp(time() + CTimeZone::GetOffset() + $delay,
"FULL"),
407 "\\Bitrix\\Calendar\\Sync\\Google\\QueueManager::checkIncompleteSync();",