20 $strSql =
"SHOW TABLES LIKE 'b_calendar_tmp_migration'";
21 $sqlResult = $DB->Query($strSql);
22 $canDoMigration =
false;
23 if ($tableExists = $sqlResult->Fetch())
25 $canDoMigration =
true;
29 if (!\
Bitrix\Main\Loader::includeModule(
'dav') || !$canDoMigration)
31 \CCalendar::ClearCache();
32 \CAgent::RemoveAgent(
"Bitrix\\Calendar\\Update\\MigrateFromCaldav::doMigration();",
"calendar");
38 $query = $DB->Query(
"SELECT * FROM b_calendar_tmp_migration WHERE ATTEMPTS < 10 ORDER BY ATTEMPTS ASC LIMIT 3");
40 while ($row = $query->Fetch())
43 $connectionId = $row[
'CONNECTION_ID'];
44 $userId = intval($row[
'USER_ID']);
45 $sectionsList = checkSerializedData($row[
'SECTION_DATA']) ? unserialize($row[
'SECTION_DATA'], [
'allowed_classes' =>
false]) :
false;
50 $googleCalendars = $googleApiConnection->getCalendarItems();
51 $errorCode = $googleApiConnection->getTransportConnectionError();
56 foreach ($sectionsList as $section)
58 $localEventsList = \CCalendarEvent::getList(array(
60 'arFilter' => array(
'SECTION' => $section[
'ID'])
63 $davXmlId = preg_replace(
'/(\/caldav\/v2\/)|(\/events\/)/',
'', $section[
'CAL_DAV_CAL'], -1, $replaced);
66 $isVirtual = preg_match(
'/(@virtual)/', $davXmlId);
68 foreach ($googleCalendars as $googleCalendar)
70 if ($isVirtual && ($googleCalendar[
'id'] == $section[
'DESCRIPTION'] || $googleCalendar[
'summary'] == $section[
'NAME']))
72 $DB->Query(
"UPDATE b_calendar_section SET GAPI_CALENDAR_ID = '" . $DB->ForSql($googleCalendar[
'id']) .
"', TEXT_COLOR = '" . $DB->ForSql($googleCalendar[
'textColor']) .
"', COLOR = '" . $DB->ForSql($googleCalendar[
'backgroundColor']) .
"' WHERE ID = '" . intval($section[
'ID']) .
"'");
75 if ($davXmlId != $googleCalendar[
'id'])
79 $section[
'GAPI_CALENDAR_ID'] = $davXmlId;
80 $sectionSyncToken = NULL;
81 $externalEvents = $googleApiConnection->getEvents($section);
82 $sectionSyncToken = $googleApiConnection->getNextSyncToken();
86 foreach ($localEventsList as $localEvent)
88 if (preg_match(
'/(@google.com)/', $localEvent[
'DAV_XML_ID']) === 1)
90 foreach ($externalEvents as $externalEvent)
92 if (!empty($localEvent[
'DAV_XML_ID']) && !empty($externalEvent[
'iCalUID']) && ($localEvent[
'DAV_XML_ID'] == $externalEvent[
'iCalUID']))
94 $localEvent[
'DAV_XML_ID'] = $externalEvent[
'DAV_XML_ID'];
96 $newEventData = array_merge(
99 'SECTIONS' => array($section[
'ID']),
100 'OWNER_ID' => $userId,
104 \CCalendarEvent::Edit(array(
'arFields' => $newEventData));
110 $DB->Query(
"UPDATE b_calendar_section SET GAPI_CALENDAR_ID = '" . $DB->ForSql($davXmlId) .
"', SYNC_TOKEN = '" . $DB->ForSql($sectionSyncToken) .
"', TEXT_COLOR = '" . $DB->ForSql($googleCalendar[
'textColor']) .
"', COLOR = '" . $DB->ForSql($googleCalendar[
'backgroundColor']) .
"' WHERE ID = '" . intval($section[
'ID']) .
"'");
116 $DB->Query(
"DELETE FROM b_calendar_tmp_migration WHERE ID = '" . intval($row[
'ID']) .
"'");
118 \CDavConnection::Update($connectionId, [
'ACCOUNT_TYPE' => Google\Helper::GOOGLE_ACCOUNT_TYPE_API,
'SERVER' => Google\Helper::GOOGLE_SERVER_PATH_V3],
false);
122 if ($row[
'ATTEMPTS'] >= 1000)
124 $DB->Query(
"DELETE FROM b_calendar_tmp_migration WHERE ID = " . intval($row[
'ID']));
125 \CDavConnection::SetLastResult($connectionId,
"GAPI_MIGRATE_ERROR");
127 $DB->Query(
"UPDATE b_calendar_tmp_migration SET ATTEMPTS = ATTEMPTS + 1 WHERE ID = " . intval($row[
'ID']));
133 $DB->Query(
"DELETE FROM b_calendar_tmp_migration WHERE ID = '" . intval($row[
'ID']) .
"'");
134 \CDavConnection::Update($connectionId, array(
'ACCOUNT_TYPE' => Google\Helper::GOOGLE_ACCOUNT_TYPE_API,
'SERVER' => Google\Helper::GOOGLE_SERVER_PATH_V3),
false);
140 $DB->Query(
"DROP TABLE b_calendar_tmp_migration");
143 return "Bitrix\\Calendar\\Update\\MigrateFromCaldav::doMigration();";