Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
initlocaldatatogoogle.php
1<?php
2
4
9use CAgent;
10
12{
13 protected static $moduleId = "calendar";
14
15 public static function className()
16 {
17 return get_called_class();
18 }
19
20 public function execute(array &$result)
21 {
23
24 if (!Loader::includeModule("calendar") && !Loader::includeModule("dav"))
25 {
27 }
28
29 $pushEnabled = \CCalendar::IsBitrix24() || \COption::GetOptionString('calendar', 'sync_by_push', false);
30 if (!$pushEnabled)
31 {
33 }
34
35 $lastId = Option::get('calendar', 'initLocalDataToGoogleLastId', 0);
36
37 $connections = $this->getNotSyncLocalConnection((int)$lastId);
38 if ($connections)
39 {
40 CAgent::RemoveAgent("CCalendarSync::doSync();", "calendar");
41 foreach ($connections as $connection)
42 {
43 $connection = $this->stopChannels($connection);
44
45 $lastId = $connection['ID'];
46// \CCalendarSync::dataSync($connection);
47 }
48
49 Option::set('calendar', 'initLocalDataToGoogleLastId', (string)$lastId);
50 CAgent::AddAgent("CCalendarSync::doSync();", "calendar", "N", 120);
51
53 }
54
56 }
57
62 private function getNotSyncLocalConnection(int $lastId): array
63 {
64 global $DB;
65 $connections = [];
66
67 $strSql = "SELECT DISTINCT c.*, GROUP_CONCAT(s.ID) as SECTION_LIST"
68 . " FROM b_dav_connections c"
69 . " INNER JOIN b_calendar_section s ON s.OWNER_ID = c.ENTITY_ID"
70 . " WHERE (s.EXTERNAL_TYPE = 'local' OR s.EXTERNAL_TYPE = 'google') AND c.ACCOUNT_TYPE = 'google_api_oauth' AND c.ID > " . $lastId
71 . " GROUP BY c.ID"
72 . " ORDER BY ID ASC"
73 . " LIMIT 3;"
74 ;
75 $connectionsDb = $DB->Query($strSql);
76
77 while ($connection = $connectionsDb->Fetch())
78 {
79 $connections[] = $connection;
80 }
81
82 return $connections;
83 }
84
91 private function stopChannels($connection)
92 {
93 GoogleApiPush::stopChannel(GoogleApiPush::getPush(GoogleApiPush::TYPE_CONNECTION, (int)$connection['ID']));
94 if (isset($connection['SECTION_LIST']) && is_string($connection['SECTION_LIST']))
95 {
96 foreach (explode(',', $connection['SECTION_LIST']) as $sectionId)
97 {
98 GoogleApiPush::stopChannel(GoogleApiPush::getPush(GoogleApiPush::TYPE_SECTION, $sectionId));
99 }
100 }
101
102 return $connection;
103 }
104
105}