Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cleanconnectionagent.php
1<?php
2
4
5use Bitrix\Dav\Internals\DavConnectionTable;
6use Bitrix\Main\Entity\ReferenceField;
11
13{
14 private const WEEK_TIME_SLICE = 650000;
15 private const SYNC_CONNECTIONS = [
16 'icloud',
17 'office365',
18 'google_api_oauth',
19 'caldav',
20 'yandex',
21 'caldav_google_oauth'
22 ];
23
32 public static function cleanAgent(): string
33 {
34 (new self())->cleanConnections();
35
36 return "\\Bitrix\\Calendar\\Sync\\Util\\CleanConnectionAgent::cleanAgent();";
37 }
38
48 private function cleanConnections(): void
49 {
50 if (!Loader::includeModule('dav') || !Loader::includeModule('calendar'))
51 {
52 return;
53 }
54
55 $deletedUsersConnections = $this->getDeletedUsersConnection();
56
57 $deletedUsersConnectionsIds = array_map(static function($deletedConnection){
58 return (int)$deletedConnection['ID'];
59 }, $deletedUsersConnections);
60
61 if ($deletedUsersConnectionsIds)
62 {
63 $this->cleanTables($deletedUsersConnectionsIds);
64 }
65 }
66
74 private function getDeletedConnections(): array
75 {
76 return DavConnectionTable::query()
77 ->setSelect(['ID', 'ACCOUNT_TYPE'])
78 ->where('IS_DELETED', 'Y')
79 ->where('SYNCHRONIZED', '<', new Date(\CCalendar::Date(time() - self::WEEK_TIME_SLICE)))
80 ->whereIn('ACCOUNT_TYPE', self::SYNC_CONNECTIONS)
81 ->exec()->fetchAll()
82 ;
83 }
84
89 private function getDeletedUsersConnection(): array
90 {
91 return DavConnectionTable::query()
92 ->setSelect(['ID', 'ACCOUNT_TYPE'])
93 ->registerRuntimeField(
94 new ReferenceField(
95 'USER',
96 UserTable::class,
97 ['=this.ENTITY_ID' => 'ref.ID'],
98 ['join_type' => 'INNER']
99 )
100 )
101 ->where('USER.ACTIVE', 'N')
102 ->whereIn('ACCOUNT_TYPE', self::SYNC_CONNECTIONS)
103 ->exec()->fetchAll()
104 ;
105 }
106
113 private function cleanTables(array $id): void
114 {
115 global $DB;
116 $id = implode(',', $id);
117 $DB->Query("
118 DELETE FROM b_calendar_event_connection
119 WHERE CONNECTION_ID IN ( " . $id . ");"
120 );
121
122 $DB->Query("
123 DELETE FROM b_calendar_section_connection
124 WHERE CONNECTION_ID IN (" . $id . ");"
125 );
126
127 $DB->Query("
128 DELETE FROM b_dav_connections
129 WHERE ID IN (" . $id . ");"
130 );
131 }
132}
133