26 private Event $originalEvent;
43 $this->originalEvent = $event;
44 $this->eventConnection = $eventConnection;
56 $event[
'summary'] = $this->originalEvent->getName();
57 $event[
'description'] = $this->prepareDescription($this->originalEvent);
60 $event[
'reminders'] = $this->prepareReminders();
61 $event[
'location'] = $this->prepareLocation();
62 $event[
'visibility'] = $this->prepareVisibility();
65 if ($this->originalEvent->getUid() !==
null)
67 $event[
'iCalUID'] = $this->originalEvent->getUid();
70 if ($this->originalEvent->isRecurrence())
72 $event[
'recurrence'] = $this->prepareRecurrenceRule();
85 private function prepareDescription(
Event $event): string
87 $description = \CCalendarEvent::ParseText((
new EventDescription())->prepareForExport($event));
89 return preg_replace(
"/<br>/i",
"\r\n", $description);
98 if ($this->eventConnection ===
null)
100 throw new BaseException(
'you should initialize eventConnection before update event');
104 if ($this->eventConnection && $this->eventConnection->getVendorEventId() !==
null)
106 $event[
'id'] = $this->eventConnection->getVendorEventId();
108 if ($this->originalEvent->isInstance())
110 $event[
'recurringEventId'] = $this->eventConnection->
getRecurrenceId();
125 $event[
'status'] =
'cancelled';
135 return $this->originalEvent->getAccessibility() ===
'busy'
148 if ($this->originalEvent->isFullDayEvent())
150 $date[
'start'][
'date'] = $this->originalEvent->getStart()->format(
'Y-m-d');
151 $date[
'end'][
'date'] = $this->originalEvent->getEnd()->add((
'+1 day'))->format(
'Y-m-d');
156 $date[
'start'][
'timeZone'] = $this->originalEvent->getStartTimeZone()
157 ? $this->originalEvent->getStartTimeZone()->getTimeZone()->getName()
158 : (new \DateTime())->getTimezone()->getName()
161 $date[
'end'][
'timeZone'] = $this->originalEvent->getEndTimeZone()
162 ? $this->originalEvent->getEndTimeZone()->getTimeZone()->getName()
163 : (new \DateTime())->getTimezone()->getName()
167 if ($this->originalEvent->getOriginalDateFrom() !==
null)
169 if ($this->originalEvent->isFullDayEvent())
185 private function prepareReminders(): array
188 $reminders[
'useDefault'] =
false;
190 $remindCollection = $this->originalEvent->getRemindCollection();
191 if ($remindCollection && $remindCollection->count() > self::MAX_COUNT_REMINDERS_FOR_SYNC)
193 $remindCollection->sortFromStartEvent();
197 foreach ($remindCollection as $remind)
199 if (!$remind->isBeforeEventStart())
204 $reminders[
'overrides'][] = [
205 Remind::UNIT_MINUTES => $remind->getTimeBeforeStartInMinutes(),
209 if (count($reminders[
'overrides']) >= self::MAX_COUNT_REMINDERS_FOR_SYNC)
224 private function prepareLocation(): string
226 if ($this->originalEvent->getLocation())
228 return \Bitrix\Calendar\Rooms\Util::getTextLocation((
string)$this->originalEvent->getLocation());
237 private function prepareVisibility(): string
239 return $this->originalEvent->isPrivate()
248 private function prepareRecurrenceRule(): array
252 if (!$this->originalEvent->isRecurrence())
257 $rule[] = $this->prepareDescriptionRecurrenceRule();
259 if ($this->originalEvent->getExcludedDateCollection() !==
null)
261 array_push($rule, ...$this->prepareExcludedDates());
270 public function prepareDescriptionRecurrenceRule(): string
273 $eventRule = $this->originalEvent->getRecurringRule();
274 $descriptionRule =
'RRULE:';
275 $descriptionRule .=
'FREQ=' . $eventRule->getFrequency();
276 $descriptionRule .=
';INTERVAL=' . $eventRule->getInterval();
278 if ($eventRule->hasDay())
280 $descriptionRule .=
';BYDAY=' . implode(
",", $eventRule->getByday());
283 if ($eventRule->hasCount())
285 $descriptionRule .=
';COUNT=' . $eventRule->getCount();
287 elseif ($eventRule->hasUntil())
289 $until = clone $eventRule->getUntil();
290 if (!$this->originalEvent->isFullDayEvent() && !$eventRule->isUntilEndOfTime())
292 $until = $until->add(
'1 day')->sub(
'1 second');
297 return $descriptionRule;
303 public function prepareExcludedDates(): array
308 $this->originalEvent->getExcludedDateCollection()
309 && ($exdateCollection = $this->originalEvent->getExcludedDateCollection()->getDateCollectionNewerThanInterval())
312 $originalDateList = [];
313 if ($this->instanceCollection !==
null)
316 foreach ($this->instanceCollection as $instance)
318 if ($originalDateFrom = $instance->getEvent()->getOriginalDateFrom())
320 $originalDateList[] = $originalDateFrom->format(
'Ymd');
325 if ($this->originalEvent->isFullDayEvent())
327 foreach ($exdateCollection as $exDate)
329 $date = $exDate->format(
'Ymd');
330 if (!in_array($date, $originalDateList,
true))
332 $rule[] =
'EXDATE;VALUE=DATE:' . $date;
338 $postfix = (clone $this->originalEvent->getStart())
339 ->setTimezone(
new \DateTimeZone(
'UTC'))
340 ->format(
'\\THis\\Z')
343 foreach ($exdateCollection as $exDate)
345 $date = $exDate->format(
'Ymd');
346 if (!in_array($date, $originalDateList,
true))
348 $rule[] =
'EXDATE;TZID=UTC:' . $date . $postfix;
363 'sendUpdates' =>
'all',