Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
migratefromcaldav.php
1<?
3
6
12{
17 public static function doMigration()
18 {
19 global $DB;
20 $strSql = "SHOW TABLES LIKE 'b_calendar_tmp_migration'";
21 $sqlResult = $DB->Query($strSql);
22 $canDoMigration = false;
23 if ($tableExists = $sqlResult->Fetch())
24 {
25 $canDoMigration = true;
26 }
27
28 //Stop migration process if impossible or empty table or table not exists.
29 if (!\Bitrix\Main\Loader::includeModule('dav') || !$canDoMigration)
30 {
31 \CCalendar::ClearCache();
32 \CAgent::RemoveAgent("Bitrix\\Calendar\\Update\\MigrateFromCaldav::doMigration();", "calendar");
33 return null;
34 }
35 else
36 {
37 //Do migration process
38 $query = $DB->Query("SELECT * FROM b_calendar_tmp_migration WHERE ATTEMPTS < 10 ORDER BY ATTEMPTS ASC LIMIT 3");
39 $hasRows = false;
40 while ($row = $query->Fetch())
41 {
42 $hasRows = true;
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;
46
47 if ($sectionsList)
48 {
49 $googleApiConnection = new GoogleApiSync($userId);
50 $googleCalendars = $googleApiConnection->getCalendarItems();
51 $errorCode = $googleApiConnection->getTransportConnectionError();
52
53
54 if (!$errorCode)
55 {
56 foreach ($sectionsList as $section)
57 {
58 $localEventsList = \CCalendarEvent::getList(array(
59 'userId' => $userId,
60 'arFilter' => array('SECTION' => $section['ID'])
61 ));
62
63 $davXmlId = preg_replace('/(\/caldav\/v2\/)|(\/events\/)/', '', $section['CAL_DAV_CAL'], -1, $replaced);
64 if ($replaced == 2)
65 {
66 $isVirtual = preg_match('/(@virtual)/', $davXmlId);
67
68 foreach ($googleCalendars as $googleCalendar)
69 {
70 if ($isVirtual && ($googleCalendar['id'] == $section['DESCRIPTION'] || $googleCalendar['summary'] == $section['NAME']))
71 {
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']) . "'");
73 continue;
74 }
75 if ($davXmlId != $googleCalendar['id'])
76 {
77 continue;
78 }
79 $section['GAPI_CALENDAR_ID'] = $davXmlId;
80 $sectionSyncToken = NULL;
81 $externalEvents = $googleApiConnection->getEvents($section);
82 $sectionSyncToken = $googleApiConnection->getNextSyncToken();
83
84 if ($localEventsList)
85 {
86 foreach ($localEventsList as $localEvent)
87 {
88 if (preg_match('/(@google.com)/', $localEvent['DAV_XML_ID']) === 1)
89 continue;
90 foreach ($externalEvents as $externalEvent)
91 {
92 if (!empty($localEvent['DAV_XML_ID']) && !empty($externalEvent['iCalUID']) && ($localEvent['DAV_XML_ID'] == $externalEvent['iCalUID']))
93 {
94 $localEvent['DAV_XML_ID'] = $externalEvent['DAV_XML_ID'];
95
96 $newEventData = array_merge(
97 $localEvent,
98 array(
99 'SECTIONS' => array($section['ID']),
100 'OWNER_ID' => $userId,
101 'userId' => $userId,
102 )
103 );
104 \CCalendarEvent::Edit(array('arFields' => $newEventData));
105 break;
106 }
107 }
108 }
109 }
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']) . "'");
111 break;
112 }
113 }
114 }
115 //In a case when google api can return data and we have access to it
116 $DB->Query("DELETE FROM b_calendar_tmp_migration WHERE ID = '" . intval($row['ID']) . "'");
117
118 \CDavConnection::Update($connectionId, ['ACCOUNT_TYPE' => Google\Helper::GOOGLE_ACCOUNT_TYPE_API, 'SERVER' => Google\Helper::GOOGLE_SERVER_PATH_V3], false);
119 }
120 else
121 {
122 if ($row['ATTEMPTS'] >= 1000)
123 {
124 $DB->Query("DELETE FROM b_calendar_tmp_migration WHERE ID = " . intval($row['ID']));
125 \CDavConnection::SetLastResult($connectionId, "GAPI_MIGRATE_ERROR");
126 }
127 $DB->Query("UPDATE b_calendar_tmp_migration SET ATTEMPTS = ATTEMPTS + 1 WHERE ID = " . intval($row['ID']));
128 }
129 }
130 else
131 {
132 //In a case when no tables for migration received.
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);
135 }
136 }
137
138 if (!$hasRows)
139 {
140 $DB->Query("DROP TABLE b_calendar_tmp_migration");
141 }
142
143 return "Bitrix\\Calendar\\Update\\MigrateFromCaldav::doMigration();";
144 }
145 }
146}