77 $icalEvent = $this->icalComponent->getEvent();
79 if ($localEvent ===
null)
82 $parentId = $this->
saveEvent($preparedEvent);
83 $childEvent = EventTable::query()
84 ->setSelect([
'ID',
'PARENT_ID',
'OWNER_ID'])
85 ->where(
'PARENT_ID', $parentId)
86 ->where(
'OWNER_ID', $this->userId)
90 if ((
int)$childEvent[
'ID'] > 0)
92 $this->eventId = (int)$childEvent[
'ID'];
99 if ($this->
updateEvent($preparedEvent, $localEvent))
101 $this->eventId = $localEvent[
'ID'];
172 if ($icalEvent->
getStart() !==
null)
174 if ($icalEvent->
getStart()->getParameterValueByName(
'tzid') !==
null)
178 $icalEvent->
getStart()->getParameterValueByName(
'tzid')
179 )->format(Date::convertFormatToPhp(FORMAT_DATETIME));
180 $event[
'TZ_FROM'] = $icalEvent->
getStart()->getParameterValueByName(
'tzid');
185 ->format(Date::convertFormatToPhp(FORMAT_DATE))
187 $event[
'TZ_FROM'] =
null;
188 $event[
'SKIP_TIME'] =
'Y';
192 if ($icalEvent->
getEnd() !==
null)
194 if ($icalEvent->
getEnd()->getParameterValueByName(
'tzid') !==
null)
197 $icalEvent->
getEnd()->getValue(),
198 $icalEvent->
getEnd()->getParameterValueByName(
'tzid')
199 )->format(Date::convertFormatToPhp(FORMAT_DATETIME));
200 $event[
'TZ_TO'] = $icalEvent->
getEnd()->getParameterValueByName(
'tzid');
206 ->format(Date::convertFormatToPhp(FORMAT_DATE));
207 $event[
'TZ_TO'] =
null;
211 if ($icalEvent->
getName() !==
null)
213 $event[
'NAME'] = !empty($icalEvent->
getName()->getValue())
214 ? $icalEvent->
getName()->getValue()
219 if ($icalEvent->
getUid() !==
null)
221 $event[
'DAV_XML_ID'] = $icalEvent->
getUid();
224 if ($icalEvent->getModified() !==
null)
227 ->format(Date::convertFormatToPhp(FORMAT_DATETIME));
230 if ($icalEvent->getCreated() !==
null)
233 ->format(Date::convertFormatToPhp(FORMAT_DATETIME));
236 if ($icalEvent->getDtStamp() !==
null)
239 ->format(Date::convertFormatToPhp(FORMAT_DATETIME));
242 if ($icalEvent->getSequence() !==
null)
244 $event[
'VERSION'] = $icalEvent->getSequence()->getValue();
247 if ($icalEvent->getRRule() !==
null)
249 $rrule = $this->parseRRule($icalEvent->getRRule());
250 if (isset($rrule[
'FREQ']) && in_array($rrule[
'FREQ'], Dictionary::RRULE_FREQUENCY,
true))
252 $event[
'RRULE'][
'FREQ'] = $rrule[
'FREQ'];
254 if (isset($rrule[
'COUNT']) && (
int)$rrule[
'COUNT'] > 0)
256 $event[
'RRULE'][
'COUNT'] = $rrule[
'COUNT'];
258 elseif (isset($rrule[
'UNTIL']))
270 $until =
new DateTime($rrule[
'UNTIL']);
274 $until =
new DateTime(CCalendar::GetMaxDate());
278 if ($now < $until->getTimestamp())
280 $event[
'RRULE'][
'UNTIL'] = $until->format(Date::convertFormatToPhp(FORMAT_DATE));
284 if ($rrule[
'FREQ'] === Dictionary::RRULE_FREQUENCY[
'weekly'] && isset($rrule[
'BYDAY']))
286 $event[
'RRULE'][
'BYDAY'] = $rrule[
'BYDAY'];
289 if (isset($rrule[
'INTERVAL']))
291 $event[
'RRULE'][
'INTERVAL'] = $rrule[
'INTERVAL'];
295 $event[
'RRULE'][
'INTERVAL'] = 1;
306 $this->organizer = $this->
parseOrganizer($icalEvent->getOrganizer());
310 $event[
'IS_MEETING'] = 1;
311 $event[
'SECTION_CAL_TYPE'] =
'user';
312 $event[
'ATTENDEES_CODES'] = [
'U'.$event[
'OWNER_ID'],
'U'.$event[
'MEETING_HOST']];
314 $event[
'MEETING_STATUS'] = Tools\Dictionary::MEETING_STATUS[
'Host'];
316 $event[
'ACCESSIBILITY'] =
'free';
317 $event[
'IMPORTANCE'] =
'normal';
318 $event[
'REMIND'][] = [
322 $event[
'MEETING'] = [
323 'HOST_NAME' => $icalEvent->getOrganizer() !==
null
324 ? $icalEvent->getOrganizer()->getParameterValueByName(
'cn')
325 : $this->organizer[
'EMAIL'],
329 'MEETING_CREATOR' => $event[
'MEETING_HOST'],
330 'EXTERNAL_TYPE' =>
'mail',
333 if ($this->decision ===
'declined')
340 $event[
'LOCATION'] = CCalendar::GetTextLocation($icalEvent->
getLocation()->getValue() ??
null);
352 $preparedEvent[
'OWNER_ID'] = $preparedEvent[
'MEETING_HOST'];
353 $preparedEvent[
'MEETING'][
'MAILTO'] = $this->organizer[
'EMAIL'] ??
$this->emailTo;
356 if ($this->icalComponent->getEvent()->getAttendees())
358 $preparedEvent[
'DESCRIPTION'] .=
"\r\n"
360 . $this->parseAttendeesForDescription($this->icalComponent->getEvent()->getAttendees());
363 if ($this->icalComponent->getEvent()->getAttachments())
365 $preparedEvent[
'DESCRIPTION'] .=
"\r\n"
367 . $this->parseAttachmentsForDescription($this->icalComponent->getEvent()->getAttachments());
370 $id = (int)CCalendar::SaveEvent([
371 'arFields' => $preparedEvent,
372 'autoDetectSection' =>
true,
375 \CCalendarNotify::Send([
377 "name" => $preparedEvent[
'NAME'] ??
null,
378 "from" => $preparedEvent[
'DATE_FROM'] ??
null,
379 "to" => $preparedEvent[
'DATE_TO'] ??
null,
380 "location" => CCalendar::GetTextLocation($preparedEvent[
"LOCATION"] ??
null),
381 "guestId" => $this->userId ??
null,
383 "userId" => $preparedEvent[
'MEETING_HOST'],
384 "fields" => $preparedEvent,
386 \CCalendar::UpdateCounter([$this->userId]);
463 if ($icalEvent->
getStart() !==
null)
465 if ($icalEvent->
getStart()->getParameterValueByName(
'tzid') !==
null)
469 $icalEvent->
getStart()->getParameterValueByName(
'tzid')
470 )->format(Date::convertFormatToPhp(FORMAT_DATETIME));
471 $event[
'TZ_FROM'] = $icalEvent->
getStart()->getParameterValueByName(
'tzid');
472 $event[
'DT_SKIP_TIME'] =
'N';
473 $event[
'SKIP_TIME'] =
false;
478 ->format(Date::convertFormatToPhp(FORMAT_DATE));
479 $event[
'TZ_FROM'] =
null;
480 $event[
'DT_SKIP_TIME'] =
'Y';
481 $event[
'SKIP_TIME'] =
true;
486 $event[
'DATE_FROM'] = $localEvent[
'DATE_FROM'];
487 $event[
'TZ_FROM'] = $localEvent[
'TZ_FROM'];
490 if ($icalEvent->
getEnd() !==
null)
492 if ($icalEvent->
getEnd()->getParameterValueByName(
'tzid') !==
null)
495 $icalEvent->
getEnd()->getValue(),
496 $icalEvent->
getEnd()->getParameterValueByName(
'tzid')
497 )->format(Date::convertFormatToPhp(FORMAT_DATETIME));
498 $event[
'TZ_TO'] = $icalEvent->
getEnd()->getParameterValueByName(
'tzid');
504 ->format(Date::convertFormatToPhp(FORMAT_DATE));
505 $event[
'TZ_TO'] =
null;
510 $event[
'DATE_TO'] = $localEvent[
'DATE_TO'];
511 $event[
'TZ_TO'] = $localEvent[
'TZ_TO'];
514 if ($icalEvent->
getName() !==
null)
516 $event[
'NAME'] = $icalEvent->
getName()->getValue();
519 if ($icalEvent->getModified() !==
null)
522 ->format(Date::convertFormatToPhp(FORMAT_DATETIME));
526 if ($icalEvent->getCreated() !==
null)
530 if ($invitationDateCreate === $localDateCreate)
533 ->format(Date::convertFormatToPhp(FORMAT_DATETIME))
538 if ($icalEvent->getDtStamp() !==
null)
541 ->format(Date::convertFormatToPhp(FORMAT_DATETIME));
544 if ($icalEvent->getSequence() !==
null && $icalEvent->getSequence()->getValue() > $localEvent[
'VERSION'])
546 $event[
'VERSION'] = $icalEvent->getSequence()->getValue();
555 $event[
'DESCRIPTION'] =
null;
558 if ($icalEvent->getRRule() !==
null)
560 $rrule = $this->parseRRule($icalEvent->getRRule());
561 if (isset($rrule[
'FREQ']) && in_array($rrule[
'FREQ'], Dictionary::RRULE_FREQUENCY,
true))
563 $event[
'RRULE'][
'FREQ'] = $rrule[
'FREQ'];
565 if (isset($rrule[
'COUNT']) && (
int)$rrule[
'COUNT'] > 0)
567 $event[
'RRULE'][
'COUNT'] = $rrule[
'COUNT'];
569 elseif (isset($rrule[
'UNTIL']))
581 $until =
new DateTime($rrule[
'UNTIL']);
585 $until =
new DateTime(CCalendar::GetMaxDate());
589 if ($now < $until->getTimestamp())
591 $event[
'RRULE'][
'UNTIL'] = $until->format(Date::convertFormatToPhp(FORMAT_DATE));
595 if ($rrule[
'FREQ'] === Dictionary::RRULE_FREQUENCY[
'weekly'] && isset($rrule[
'BYDAY']))
597 $event[
'RRULE'][
'BYDAY'] = $rrule[
'BYDAY'];
600 if (isset($rrule[
'INTERVAL']))
602 $event[
'RRULE'][
'INTERVAL'] = $rrule[
'INTERVAL'];
606 $event[
'RRULE'][
'INTERVAL'] = 1;
612 if ($icalEvent->getOrganizer() !==
null)
620 : $localEvent[
'MEETING_HOST']
622 $event[
'IS_MEETING'] = 1;
623 $event[
'SECTION_CAL_TYPE'] =
'user';
624 $event[
'ATTENDEES_CODES'] = [
'U'.$event[
'OWNER_ID'],
'U'.$event[
'MEETING_HOST']];
625 $event[
'MEETING_STATUS'] = match ($this->decision) {
626 self::MEETING_STATUS_ACCEPTED_CODE => Tools\Dictionary::MEETING_STATUS[
'Yes'],
627 self::MEETING_STATUS_DECLINED_CODE => Tools\Dictionary::MEETING_STATUS[
'No'],
628 default => Tools\Dictionary::MEETING_STATUS[
'Question'],
630 $event[
'ACCESSIBILITY'] =
'free';
631 $event[
'IMPORTANCE'] =
'normal';
636 $organizerCn = $icalEvent->getOrganizer()?->getParameterValueByName(
'cn');
637 $meeting = unserialize($localEvent[
'MEETING'], [
'allowed_classes' =>
false]);
638 $event[
'MEETING'] = [
639 'HOST_NAME' => $organizerCn ??
$organizer[
'EMAIL'] ?? $meeting[
'HOST_NAME'] ??
null,
640 'NOTIFY' => $meeting[
'NOTIFY'] ?? 1,
641 'REINVITE' => $meeting[
'REINVITE'] ?? 0,
642 'ALLOW_INVITE' => $meeting[
'ALLOW_INVITE'] ?? 0,
643 'MEETING_CREATOR' => $meeting[
'MEETING_CREATOR'] ?? $event[
'MEETING_HOST'],
644 'EXTERNAL_TYPE' =>
'mail',
646 $event[
'PARENT_ID'] = $localEvent[
'PARENT_ID'] ??
null;
647 $event[
'ID'] = $localEvent[
'ID'] ??
null;
648 $event[
'CAL_TYPE'] = $localEvent[
'CAL_TYPE'] ??
null;
650 if ($this->decision ===
'declined')
657 $event[
'LOCATION'] = CCalendar::GetTextLocation($icalEvent->
getLocation()->getValue() ??
null);
668 protected function updateEvent(array $updatedEvent, array $localEvent): bool
670 $updatedEvent[
'ID'] = $updatedEvent[
'PARENT_ID'];
671 $updatedEvent[
'OWNER_ID'] = $updatedEvent[
'MEETING_HOST'];
672 $updatedEvent[
'MEETING'][
'MAILTO'] = $this->organizer[
'EMAIL'] ??
$this->emailTo;
675 if ($this->icalComponent->getEvent()->getAttendees())
677 $updatedEvent[
'DESCRIPTION'] .=
"\r\n"
679 . $this->parseAttendeesForDescription($this->icalComponent->getEvent()->getAttendees());
682 if ($this->icalComponent->getEvent()->getAttachments())
684 $updatedEvent[
'DESCRIPTION'] .=
"\r\n"
686 . $this->parseAttachmentsForDescription($this->icalComponent->getEvent()->getAttachments());
689 \CCalendar::SaveEvent([
690 'arFields' => $updatedEvent,
693 $entryChanges = \CCalendarEvent::CheckEntryChanges($updatedEvent, $localEvent);
695 \CCalendarNotify::Send([
696 'mode' =>
'change_notify',
697 'name' => $updatedEvent[
'NAME'] ??
null,
698 "from" => $updatedEvent[
'DATE_FROM'] ??
null,
699 "to" => $updatedEvent[
'DATE_TO'] ??
null,
700 "location" => CCalendar::GetTextLocation($updatedEvent[
"LOCATION"] ??
null),
701 "guestId" => $this->userId ??
null,
702 "eventId" => $updatedEvent[
'PARENT_ID'] ??
null,
703 "userId" => $updatedEvent[
'MEETING_HOST'],
704 "fields" => $updatedEvent,
705 "entryChanges" => $entryChanges,
707 \CCalendar::UpdateCounter([$this->userId]);