Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
updatedgoogleeventcleaner.php
1<?php
2
4
7
9{
10 private const PORTION = 200;
11 protected static $moduleId = "calendar";
12
13 public static function className(): string
14 {
15 return __CLASS__;
16 }
17
24 public function execute(array &$option)
25 {
26 if (!Loader::includeModule(self::$moduleId))
27 {
29 }
30
31 $linksInfo = $this->getDuplicatedLinkInfo();
32
33 if ($linksInfo->SelectedRowsCount())
34 {
35 while ($linkInfo = $linksInfo->Fetch())
36 {
37 $duplicatedLinkId = (int)$linkInfo['FIRST_LINK_ID'];
38 $originalLinkId = (int)$linkInfo['LAST_LINK_ID'];
39
40 $events = $this->getEventList([$duplicatedLinkId, $originalLinkId]);
41 $duplicatedEvent = $events[$duplicatedLinkId];
42 $originalEvent = $events[$originalLinkId];
43
44 if (!$duplicatedEvent)
45 {
46 $this->deleteDuplicatedLink($duplicatedLinkId);
47 }
48 if (!$originalEvent)
49 {
50 $this->deleteDuplicatedLink($originalLinkId);
51 }
52 if (!$duplicatedEvent || !$originalEvent)
53 {
54 continue;
55 }
56
57 if ($duplicatedEvent['DELETED'] === 'Y')
58 {
59 $this->deleteDuplicatedLink($duplicatedLinkId);
60 continue;
61 }
62
63 $this->markEventAsDeleted((int)$duplicatedEvent['ID']);
64 $this->deleteDuplicatedLink($duplicatedLinkId);
65
66 if ($originalEvent['DELETED'] === 'Y')
67 {
68 $this->restoreEvent((int)$originalEvent['PARENT_ID']);
69 }
70 }
71
73 }
74
75 \CCalendar::ClearCache();
77 }
78
82 private function getDuplicatedLinkInfo()
83 {
84 global $DB;
85
86 return $DB->Query("
87 SELECT
88 MIN(ID) as FIRST_LINK_ID,
89 MAX(ID) as LAST_LINK_ID,
90 VENDOR_EVENT_ID,
91 COUNT(1) as CNT
92 FROM b_calendar_event_connection
93 WHERE VENDOR_EVENT_ID <> '' AND ENTITY_TAG <> ''
94 GROUP BY VENDOR_EVENT_ID, CONNECTION_ID
95 HAVING CNT = 2
96 LIMIT " . self::PORTION . ";
97 ");
98 }
99
105 private function getEventList(array $linkList): ?array
106 {
107 $result = [];
108 global $DB;
109
110 $query = $DB->Query("
111 SELECT EV.ID, EV.DELETED, EV.PARENT_ID, CON.ID as LINK_ID
112 FROM b_calendar_event EV
113 INNER JOIN b_calendar_event_connection CON ON EV.ID = CON.EVENT_ID
114 WHERE CON.ID IN ( " . implode(',', $linkList) . ");
115 ");
116
117 while ($event = $query->Fetch())
118 {
119 $result[$event['LINK_ID']] = $event;
120 }
121
122 return $result;
123 }
124
130 private function markEventAsDeleted(int $eventId): void
131 {
132 global $DB;
133 $DB->Query("
134 UPDATE b_calendar_event
135 SET DELETED = 'Y'
136 WHERE ID = " . $eventId . ";
137 ");
138 }
139
145 private function deleteDuplicatedLink(int $linkId): void
146 {
147 global $DB;
148 $DB->Query("
149 DELETE FROM b_calendar_event_connection
150 WHERE ID = " . $linkId . ";
151 ");
152 }
153
159 private function restoreEvent(int $parentEventId): void
160 {
161 global $DB;
162 $DB->Query("
163 UPDATE b_calendar_event
164 SET DELETED = 'N'
165 WHERE PARENT_ID = " . $parentEventId . "
166 ");
167 }
168}