Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventwithlocationupdate.php
1<?php
2
4
11use CCalendar;
12use CCalendarEvent;
13
15{
16 const PORTION = 100;
17 const TIMESLICE = 5200000;
18
19 protected static $moduleId = 'calendar';
20
21 public static function className(): string
22 {
23 return get_called_class();
24 }
25
26 public function execute(array &$result)
27 {
28 $connection = Application::getConnection();
29 $sqlHelper = $connection->getSqlHelper();
30 if (Loader::includeModule("calendar")
31 && (Option::get('calendar', 'eventWithLocationConverted', 'N') === 'Y')
32 )
33 {
34 Rooms\Manager::createInstance()->clearCache();
36 }
37 $status = $this->loadCurrentStatus();
38
39 $newStatus = [
40 'count' => $status['count'],
41 'steps' => $status['steps'],
42 'newFinished' => $status['newFinished'],
43 'lastEventId' => $status['lastEventId']
44 ];
45
46 // Update calendar room events
47 if (!$status['newFinished'])
48 {
49 $res = $this->getLocationEvent($newStatus['lastEventId']);
50 while ($event = $res->Fetch())
51 {
52 $eventId = (int)$event['ID'];
53 $newStatus['lastEventId'] = $eventId;
54
55 $parentRes = $this->getLocationParentEvent($eventId);
56 if ($parentEvent = $parentRes->Fetch())
57 {
58 $ownerName = $sqlHelper->forSql(CCalendar::GetUserName($parentEvent['CREATED_BY']));
59 $parentId = (int)$parentEvent['ID'];
60 $this->updateLocationEvent($parentId, $ownerName, $eventId);
61 }
62 else
63 {
64 $this->deleteEvent($eventId);
65 }
66
67 $newStatus['steps']++;
68 }
69
70 if (isset($newStatus['lastEventId']) && $res->SelectedRowsCount() !== 0)
71 {
72 Option::set('calendar', 'eventWithLocationConvertedStatus', serialize($newStatus));
73 $result = [
74 'title' => Loc::getMessage("CALENDAR_UPDATE_EVENT_WITH_LOCATION"),
75 'count' => $newStatus['count'],
76 'steps' => $newStatus['steps'],
77 'lastEventId' => $newStatus['lastEventId'],
78 'newFinished' => $newStatus['newFinished']
79 ];
80
82 }
83
84 $newStatus['newFinished'] = true;
85 $newStatus['lastEventId'] = PHP_INT_MAX;
86 }
87
88 //update IBlock room events
89 $meetingRoomArray = $this->getMeetingRoomArray();
90
91 if ($meetingRoomArray !== null)
92 {
93 $res = $this->getIBlockEvent($newStatus['lastEventId']);
94 while ($event = $res->Fetch())
95 {
96 $eventId = (int)$event['ID'];
97 $newStatus['lastEventId'] = $eventId;
98 $phrases = $this->prepareLocationEvent($event, $meetingRoomArray);
99
100 if ($phrases !== null && isset($phrases['child']) && isset($phrases['parent']))
101 {
102 $this->updateLocationValue($phrases['parent'], $eventId);
103 if ($event['IS_MEETING'])
104 {
105 $this->updateLocationValueForChildEvents($phrases['child'], $eventId);
106 }
107 Rooms\Manager::setEventIdForLocation($eventId);
108 }
109
110 $newStatus['steps']++;
111 }
112
113 if (isset($newStatus['lastEventId']) && $res->SelectedRowsCount() !== 0)
114 {
115 Option::set('calendar', 'eventWithLocationConvertedStatus', serialize($newStatus));
116 $result = [
117 'title' => Loc::getMessage("CALENDAR_UPDATE_EVENT_WITH_LOCATION"),
118 'count' => $newStatus['count'],
119 'steps' => $newStatus['steps'],
120 'lastEventId' => $newStatus['lastEventId'],
121 'newFinished' => $newStatus['newFinished']
122 ];
123
125 }
126 }
127
128 Option::set('calendar', 'eventWithLocationConverted', 'Y');
129 Option::delete('calendar', ['name' => 'eventWithLocationConvertedStatus']);
130 Rooms\Manager::createInstance()->clearCache();
131
133 }
134
138 private function loadCurrentStatus()
139 {
140 $status = Option::get('calendar', 'eventWithLocationConvertedStatus', 'default');
141 $status = ($status !== 'default' ? @unserialize($status, ['allowed_classes' => false]) : []);
142 $status = (is_array($status) ? $status : []);
143
144 if (empty($status))
145 {
146 $status = [
147 'count' => $this->getTotalCount(),
148 'steps' => 0,
149 'lastEventId' => PHP_INT_MAX,
150 'newFinished' => false
151 ];
152 }
153
154 return $status;
155 }
156
160 private function getTotalCount(): int
161 {
162 return $this->getTotalCountLocation() + $this->getTotalCountIBlock();
163 }
164
168 private function getTotalCountLocation(): int
169 {
170 global $DB;
171 $count = 0;
172 $result = $DB->Query("
173 SELECT COUNT(*) AS cnt
174 FROM b_calendar_event
175 WHERE ID = PARENT_ID
176 AND CAL_TYPE = 'location'
177 AND DELETED = 'N';"
178 );
179 if ($res = $result->Fetch())
180 {
181 $count = (int)$res['cnt'];
182 }
183
184 return $count;
185 }
186
190 private function getTotalCountIBlock(): int
191 {
192 global $DB;
193 $timestamp = time() - self::TIMESLICE;
194 $count = 0;
195 $result = $DB->Query("
196 SELECT COUNT(*) AS cnt
197 FROM b_calendar_event
198 WHERE DELETED = 'N'
199 AND DATE_TO_TS_UTC > " . $timestamp . "
200 AND PARENT_ID = ID
201 AND LOCATION LIKE 'ECMR%'"
202 );
203 if ($res = $result->Fetch())
204 {
205 $count = (int)$res['cnt'];
206 }
207
208 return $count;
209 }
210
214 private function getLocationEvent(int $lastEventId)
215 {
216 global $DB;
217 return $DB->Query("
218 SELECT ID, PARENT_ID, CAL_TYPE
219 FROM b_calendar_event
220 WHERE ID = PARENT_ID
221 AND CAL_TYPE = 'location'
222 AND DELETED = 'N'
223 AND ID < ".$lastEventId."
224 ORDER BY ID DESC
225 LIMIT ".self::PORTION.";"
226 );
227 }
228
233 private function getLocationParentEvent(int $eventId)
234 {
235 global $DB;
236 return $DB->Query("
237 SELECT ID, CREATED_BY, LOCATION
238 FROM b_calendar_event
239 WHERE LOCATION LIKE 'calendar_%_".$eventId."'
240 AND DELETED = 'N'
241 LIMIT 1"
242 );
243 }
244
251 private function updateLocationEvent(int $parentId, string $ownerName, int $eventId): void
252 {
253 global $DB;
254 $DB->Query("
255 UPDATE b_calendar_event
256 SET PARENT_ID = " . $parentId . ", NAME = '" . $ownerName . "'
257 WHERE ID = " . $eventId . ";"
258 );
259 }
260
264 private function getMeetingRoomArray()
265 {
266 $newMeetingRooms = Option::get('calendar', 'converted_meeting_rooms');
267 $newMeetingRooms = json_decode($newMeetingRooms, true);
268
269 if (!empty($newMeetingRooms) && is_array($newMeetingRooms))
270 {
271 return $newMeetingRooms;
272 }
273
274 return null;
275 }
276
280 private function getIBlockEvent(int $lastEventId)
281 {
282 global $DB;
283 $timestamp = time() - self::TIMESLICE;
284
285 return $DB->Query("
286 SELECT ID, PARENT_ID, DATE_FROM,
287 DATE_TO, TZ_FROM, TZ_TO, IS_MEETING,
288 RRULE, EXDATE, CREATED_BY, DT_SKIP_TIME
289 FROM b_calendar_event
290 WHERE DELETED = 'N'
291 AND DATE_TO_TS_UTC > " . $timestamp . "
292 AND LOCATION LIKE 'ECMR%'
293 AND PARENT_ID = ID
294 AND ID < ".$lastEventId."
295 ORDER BY ID DESC
296 LIMIT " . self::PORTION . ";"
297 );
298 }
299
305 private function prepareLocationEvent($event, $meetingRoomArray): ?array
306 {
307 global $DB;
308 $id = (int)$event['ID'];
309 $dateToRaw = strtotime($event['DATE_TO']);
310 $dateFromRaw = strtotime($event['DATE_FROM']);
311 $dateTo = CCalendar::Date($dateToRaw);
312 $dateFrom = CCalendar::Date($dateFromRaw);
313
314 $RRule = CCalendarEvent::ParseRRULE($event['RRULE']);
315 if (isset($RRule['~UNTIL']))
316 {
317 unset($RRule['~UNTIL']);
318 }
319 if ($RRule['FREQ'] === 'WEEKLY' && !isset($RRule['BYDAY']))
320 {
321 return null;
322 }
323
324 $skipTime = $event['DT_SKIP_TIME'] === 'Y';
325
326 $phraseLocationParent = 'calendar_#ROOMID#_#EVENTID#';
327 $phraseLocationChild = 'calendar_#ROOMID#';
328 $result = [];
329
330 $res = $DB->Query("
331 SELECT LOCATION
332 FROM b_calendar_event
333 WHERE ID = " . $id . ";"
334 );
335 if ($location = $res->Fetch())
336 {
337 $location = explode("_", $location['LOCATION']);
338 $mrId = $location[1];
339 $roomId = $meetingRoomArray[$mrId];
340
341 $locationEventId = Rooms\Manager::reserveRoom([
342 'parentParams' => [
343 'arFields' => [
344 'DATE_FROM' => $dateFrom,
345 'DATE_TO' => $dateTo,
346 'TZ_FROM' => $event['TZ_FROM'],
347 'TZ_TO' => $event['TZ_TO'],
348 'SKIP_TIME' => $skipTime,
349 'RRULE' => $RRule,
350 'EXDATE' => $event['EXDATE'],
351 'CREATED_BY' => (int)$event['CREATED_BY']
352 ],
353 'userId' => (int)$event['CREATED_BY']
354 ],
355 'room_event_id' => false,
356 'room_id' => (int)$roomId
357 ]);
358
359 if ($locationEventId && $roomId)
360 {
361 $result['parent'] = str_replace(
362 ['#ROOMID#', '#EVENTID#'],
363 [$roomId, $locationEventId],
364 $phraseLocationParent
365 );
366 $result['child'] = str_replace(
367 ['#ROOMID#'],
368 [$roomId],
369 $phraseLocationChild
370 );
371 return $result;
372 }
373 }
374
375 return null;
376 }
377
382 private function updateLocationValue(string $phraseLocation, int $id) : void
383 {
384 global $DB;
385 $DB->Query("
386 UPDATE b_calendar_event
387 SET LOCATION = '" . $phraseLocation . "'
388 WHERE ID = ".$id.";"
389 );
390 }
391
396 private function updateLocationValueForChildEvents(string $phraseLocation, int $id) : void
397 {
398 global $DB;
399 $DB->Query("
400 UPDATE b_calendar_event
401 SET LOCATION = '" . $phraseLocation . "'
402 WHERE PARENT_ID = " . $id. "
403 AND PARENT_ID <> ID
404 AND CAL_TYPE <> 'location'
405 AND DELETED = 'N';"
406 );
407 }
408
412 private function deleteEvent(int $id)
413 {
414 global $DB;
415 $DB->Query("
416 UPDATE b_calendar_event
417 SET DELETED = 'Y'
418 WHERE ID = " . $id . ";"
419 );
420 }
421}
static getConnection($name="")
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29