Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventconnectorhelper.php
1<?php
2
4
11
13{
14 private int $eventId;
15 private ?string $eventOriginalDate;
16 private ?DateTime $targetDateTime;
17
18 public function __construct(private int $userId, private string $xmlId)
19 {
20 $xmlIdExploded = explode('_', $this->xmlId);
21
22 $this->eventId = (int)($xmlIdExploded[1] ?? null);
23 $this->eventOriginalDate = $xmlIdExploded[2] ?? null;
24 $this->targetDateTime = DateTime::createFromText($this->eventOriginalDate);
25 }
26
27 public function canView(int $eventId, int $userId): bool
28 {
29 return \CCalendarEvent::CanView($eventId, $userId);
30 }
31
32 public function findActualEventId(): ?int
33 {
34 if ($this->eventId <= 0 || empty($this->eventOriginalDate) || empty($this->targetDateTime))
35 {
36 return null;
37 }
38
39 $query = $this->getSearchExceptionsQuery($this->userId);
40
41 $offset = 0;
42 while (true)
43 {
44 $query->setOffset($offset);
45 $exceptionEvents = $query->fetchAll();
46
47 $actualEventId = $this->processExceptionEvents($exceptionEvents);
48
49 if ($actualEventId > 0 || count($exceptionEvents) < $query->getLimit())
50 {
51 break;
52 }
53
54 $offset += $query->getLimit();
55 }
56
57 return $actualEventId;
58 }
59
60 private function getSearchExceptionsQuery(int $userId): Query
61 {
62 $select = ['ID', 'PARENT_ID', 'DELETED', 'OWNER_ID', 'DATE_FROM_TS_UTC', 'DATE_TO_TS_UTC', 'EXDATE', 'RELATIONS'];
63
64 return \Bitrix\Calendar\Internals\EventTable::query()
65 ->setSelect($select)
66 ->registerRuntimeField(
67 (new Reference(
68 'ORIGINAL_RECURSION',
69 EventOriginalRecursionTable::class,
70 Join::on('this.PARENT_ID', 'ref.PARENT_EVENT_ID'),
71 ))
72 ->configureJoinType(Join::TYPE_LEFT)
73 )
74 ->where('OWNER_ID', $userId)
75 ->where('CAL_TYPE', Dictionary::CALENDAR_TYPE['user'])
76 ->where('DELETED', 'N')
77 ->where('ORIGINAL_RECURSION.ORIGINAL_RECURSION_EVENT_ID', $this->eventId)
78 ->addOrder('ID', 'DESC')
79 ->setLimit(50)
80 ;
81 }
82
83 private function processExceptionEvents(array $exceptionEvents): ?int
84 {
85 foreach ($exceptionEvents as $exceptionEvent)
86 {
87 //process single exception
88 if (!empty($exceptionEvent['RELATIONS']))
89 {
90 $eventRelations = unserialize($exceptionEvent['RELATIONS'], ['allowed_classes' => false]);
91 }
92 $commentXmlId = $eventRelations['COMMENT_XML_ID'] ?? '';
93 if (!empty($commentXmlId) && $commentXmlId === $this->xmlId)
94 {
95 return (int)$exceptionEvent['ID'];
96 }
97 elseif (!empty($commentXmlId))
98 {
99 continue;
100 }
101
102 //process chain exception
103 $exceptionDatesParsed = \CCalendarEvent::GetExDate($exceptionEvent['EXDATE']);
104 foreach ($exceptionDatesParsed as $exceptionDateParsed)
105 {
106 if ($exceptionDateParsed === $this->eventOriginalDate)
107 {
108 continue 2;
109 }
110 }
111 $dateFrom =
112 DateTime::createFromTimestamp($exceptionEvent['DATE_FROM_TS_UTC'] + (int)date('Z'))
113 ->setTime(0,0)
114 ;
115 $dateTo = DateTime::createFromTimestamp($exceptionEvent['DATE_TO_TS_UTC'] + (int)date('Z'));
116
117 $doEventStartsBeforeTargetDate = $dateFrom->getTimestamp() <= $this->targetDateTime->getTimestamp();
118 $doEventEndsAfterTargetDate = $dateTo->getTimestamp() >= $this->targetDateTime->getTimestamp();
119 if ($doEventStartsBeforeTargetDate && $doEventEndsAfterTargetDate)
120 {
121 return (int)$exceptionEvent['ID'];
122 }
123 }
124
125 return null;
126 }
127}
__construct(private int $userId, private string $xmlId)
setSelect(array $select)
Definition query.php:338
static createFromText($text)
Definition date.php:414
static createFromTimestamp($timestamp)
Definition datetime.php:246