Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventduplicatecleaner.php
1<?php
2
6
8{
9 const MAX_TOTAL_COUUNT = 1000;
10 protected static $moduleId = "calendar";
11
12 public static function className()
13 {
14 return get_called_class();
15 }
16
22 public function execute(array &$result): bool
23 {
24 if (!Loader::includeModule("calendar"))
25 {
27 }
28
29 $totalCount = 0;
30 // Clear duplications of child events
31 $dataToClean = $this->getDuplicatedChildEntryList();
32 if (count($dataToClean))
33 {
34 foreach ($dataToClean as $entryToClean)
35 {
36 $this->cleanDuplicates(
37 (int)$entryToClean['PARENT_ID'],
38 (int)$entryToClean['OWNER_ID'],
39 (int)$entryToClean['FIRSTID'] === (int)$entryToClean['PARENT_ID']
40 ? (int)$entryToClean['FIRSTID']
41 : (int)$entryToClean['LASTID']
42 );
43 $totalCount += $entryToClean['CNT'];
44 if ($totalCount >= self::MAX_TOTAL_COUUNT)
45 {
47 }
48 }
49
51 }
52
53 if ($this->getBogusLocationEntry())
54 {
55 $this->clearBogusLocationEntries();
57 }
58
59 if ($this->getLocationEntriesWithEmptyParent())
60 {
61 $this->clearLocationEntriesWithEmptyParent();
63 }
64
66 }
67
71 private function getDuplicatedChildEntryList(): ?array
72 {
73 global $DB;
74 $strSql = "select
75 MAX(ID) as LASTID,
76 MIN(ID) as FIRSTID,
77 PARENT_ID,
78 OWNER_ID,
79 COUNT(1) as CNT
80 from
81 b_calendar_event
82 where
83 CAL_TYPE='user'
84 and PARENT_ID is not null
85 group by
86 PARENT_ID, OWNER_ID
87 having CNT > 1
88 order by ID desc
89 limit 200";
90
91 $entries = [];
92 $res = $DB->Query($strSql);
93 while ($entry = $res->Fetch())
94 {
95 $entries[] = $entry;
96 }
97 return $entries;
98 }
99
103 private function getBogusLocationEntry(): ?array
104 {
105 global $DB;
106 $strSql = "
107 select ID from b_calendar_event
108 where
109 ID=PARENT_ID
110 and CAL_TYPE = 'location'
111 and OWNER_ID = '0'
112 limit 1;
113 ";
114
115 $res = $DB->Query($strSql);
116 if ($entry = $res->Fetch())
117 {
118 return $entry;
119 }
120 return null;
121 }
122
126 private function clearBogusLocationEntries(): void
127 {
128 global $DB;
129 $DB->Query("
130 delete from b_calendar_event
131 where
132 ID=PARENT_ID
133 and CAL_TYPE = 'location'
134 and OWNER_ID = '0'
135 limit 1000;
136 ");
137 }
138
145 private function cleanDuplicates(int $parentId, int $ownerId, int $entryToLeave): void
146 {
147 global $DB;
148
149 $DB->Query("
150 delete from
151 b_calendar_event
152 where
153 CAL_TYPE = 'user'
154 and PARENT_ID = '".$parentId."'
155 and OWNER_ID = '".$ownerId."'
156 and ID != '".$entryToLeave."'
157 limit 1000
158 ");
159 }
160
164 private function getLocationEntriesWithEmptyParent(): ?array
165 {
166 global $DB;
167 $strSql = "
168 select
169 ce.ID, ce.PARENT_ID, p.ID as PID
170 from b_calendar_event ce
171 left join b_calendar_event p
172 on ce.PARENT_ID=p.ID
173 where
174 ce.CAL_TYPE='location'
175 and (p.ID is null or p.DELETED='Y')
176 limit 1
177 ";
178
179 $res = $DB->Query($strSql);
180 if ($entry = $res->Fetch())
181 {
182 return $entry;
183 }
184 return null;
185 }
186
190 private function clearLocationEntriesWithEmptyParent(): void
191 {
192 global $DB;
193 $DB->Query("
194 delete ce
195 from b_calendar_event as ce
196 left join b_calendar_event as p
197 on ce.PARENT_ID=p.ID
198 where
199 ce.CAL_TYPE='location'
200 and (p.ID is null or p.DELETED='Y')
201 ");
202 }
203}