Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
cleanlocationeventsagent.php
1<?php
2
4
6use Bitrix\Main\Entity\ReferenceField;
11
13{
15 private const DAY_LENGTH = 86400;
16
24 public static function cleanAgent(): string
25 {
26 (new self())->cleanLocationEvents();
27
28 return "\\Bitrix\\Calendar\\Rooms\\Util\\CleanLocationEventsAgent::cleanAgent();";
29 }
30
38 private function cleanLocationEvents(): void
39 {
40 if (!Loader::includeModule('calendar'))
41 {
42 return;
43 }
44
45 $toCleanLocationEvents = $this->getLocationEventsNeededToClean();
46
47 if ($toCleanLocationEvents)
48 {
49 $this->cleanTables($toCleanLocationEvents);
50 \CCalendar::ClearCache(['event_list']);
51 }
52 }
53
59 private function getLocationEventsNeededToClean(): array
60 {
61 $toCleanLocationEvents = $this->getEmptyLocationEvents();
62 //for now, we don't do that because of needing to re-save child events after removing booking
63 //doing that may cause performance problems
64 if (self::DO_CLEAR_DELETED_USER_LOCATION_EVENTS)
65 {
66 $toCleanLocationEvents = array_unique(
67 array_merge($toCleanLocationEvents, $this->getDeletedUsersLocationEvents())
68 );
69 }
70
71 return array_map(static function($toCleanLocationEvent){
72 return (int)$toCleanLocationEvent['ID'];
73 }, $toCleanLocationEvents);
74 }
75
80 private function cleanTables($ids)
81 {
82 global $DB;
83 $ids = implode(',', $ids);
84
85 $DB->Query("
86 DELETE FROM b_calendar_event
87 WHERE ID IN ( " . $ids . ");"
88 );
89 }
90
96 private function getEmptyLocationEvents(): array
97 {
98 return EventTable::query()
99 ->setSelect(['ID'])
100 ->registerRuntimeField(
101 new ReferenceField(
102 'PARENT',
103 EventTable::class,
104 ['=this.PARENT_ID' => 'ref.ID'],
105 ['join_type' => 'LEFT']
106 )
107 )
108 ->where('CAL_TYPE', 'location')
109 ->where('DELETED', 'N')
110 ->where(
112 ->logic('or')
113 ->where('PARENT.DELETED', 'Y')
114 ->whereNull('PARENT.ID')
115 )
116 ->where('DATE_TO_TS_UTC', '>', $this->getTimeForQuery())
117 ->exec()->fetchAll()
118 ;
119 }
120
126 private function getDeletedUsersLocationEvents(): array
127 {
128 return EventTable::query()
129 ->setSelect(['ID'])
130 ->registerRuntimeField(
131 new ReferenceField(
132 'USER',
133 UserTable::class,
134 ['=this.CREATED_BY' => 'ref.ID'],
135 ['join_type' => 'INNER']
136 )
137 )
138 ->where('USER.ACTIVE', 'N')
139 ->where('CAL_TYPE', 'location')
140 ->where('DELETED', 'N')
141 ->where('DATE_TO_TS_UTC', '>', $this->getTimeForQuery())
142 ->exec()->fetchAll()
143 ;
144 }
145
152 private function getTimeForQuery(): int
153 {
154 return time() - self::DAY_LENGTH;
155 }
156}