29 use Sync\Internals\HasContextTrait;
31 private const CALENDAR_TYPE =
'user';
38 $this->context = $context;
53 $start = $this->prepareDate($eventData->start, $eventData->originalStartTimeZone);
54 $reminders = $this->makeReminders(
55 $eventData->reminderMinutesBeforeStart,
56 $eventData->isReminderOn,
60 $event = (
new Event())
61 ->setName($this->prepareName($eventData->subject))
65 ->setLocation($this->prepareLocation($eventData->location))
67 ->setEnd($this->prepareDate($eventData->end, $eventData->originalEndTimeZone))
68 ->setStartTimeZone($this->prepareDateTimezone($eventData->start, $eventData->originalStartTimeZone))
69 ->setEndTimeZone($this->prepareDateTimezone($eventData->end, $eventData->originalEndTimeZone))
70 ->setIsFullDay($eventData->isAllDay)
71 ->setAttendeesCollection($this->prepareAttendeeCollection($section->
getOwner()->getId()))
72 ->setRemindCollection($reminders)
73 ->setSection($section)
74 ->setDescription($this->prepareBody($eventData->body, $section->
getOwner()->getId()))
75 ->setMeetingDescription($this->prepareDefaultMeeting($section->
getOwner()->getId()))
77 ->setImportance($eventData->importance)
78 ->setCalendarType(self::CALENDAR_TYPE)
79 ->setIsActive(!$eventData->isCancelled && !$eventData->isDraft)
80 ->setIsDeleted($eventData->isCancelled)
81 ->setRecurringRule($this->makeRecurringRule($eventData->recurrence))
84 if (!empty($eventData->originalStart))
87 'dateTime' => $eventData->originalStart,
88 'timeZone' => $eventData->originalStartTimeZone,
90 $event->setOriginalDateFrom($this->prepareDate($originalDto, $eventData->originalStartTimeZone));
94 if ($event->isFullDayEvent())
96 $event->setEnd($event->getEnd()->add(
"-1 day"));
110 ->setColor($this->getOurColor($data->color, $data->hexColor))
122 private function prepareLocation(Office365\
Dto\LocationDto $location): ?
Location
124 $parsedLocation = \Bitrix\Calendar\Rooms\Util::unParseTextLocation($location->displayName);
126 return new Location($parsedLocation[
'NEW']);
138 private function prepareDate(Office365\
Dto\
DateTimeDto $dateDto,
string $originalTZ =
null):
Date
142 : $this->getDefaultTimezone();
144 $phpDateTime = new \DateTime($dateDto->dateTime,
new DateTimeZone($tz));
150 $eventDateTime->setTimeZone($original);
153 return new Date($eventDateTime);
159 private function getDefaultTimezone(): string
171 private function prepareDateTimezone(Office365\Dto\DateTimeDto $dateDto,
string $originalTZ =
null): \
Bitrix\Calendar\Core\Base\DateTimeZone
177 return new \Bitrix\Calendar\Core\Base\DateTimeZone($original);
181 : $this->getDefaultTimezone();
183 return new \Bitrix\Calendar\Core\Base\DateTimeZone(
184 new DateTimeZone($tz)
195 private function makeReminders(
int $minutes,
bool $isReminderOn, Date $start): RemindCollection
197 $collection =
new RemindCollection();
198 $collection->setEventStart($start)->setSingle(
true);
203 $hours =
'+'. abs($minutes) / 60 .
' hour';
204 $specificTime = (clone $start)->add($hours);
205 $reminder = (
new Remind())
206 ->setSpecificTime($specificTime)
212 $reminder = (
new Remind())->setTimeBeforeEvent($minutes,
'minutes');
214 $reminder->setEventStart($start);
215 $collection->add($reminder);
227 private function makeRecurringRule(?Office365\Dto\RecurrenceDto $recurrenceDto =
null): ?RecurringEventRules
233 switch ($recurrenceDto->pattern->type)
236 $result =
new RecurringEventRules(
237 RecurringEventRules::FREQUENCY[
'daily'],
238 $recurrenceDto->pattern->interval
243 $result =
new RecurringEventRules(
244 RecurringEventRules::FREQUENCY[
'weekly'],
245 $recurrenceDto->pattern->interval
247 if ($recurrenceDto->pattern->daysOfWeek)
249 $byDay = array_map(
static function ($value)
251 return strtoupper(substr($value, 0, 2));
252 }, $recurrenceDto->pattern->daysOfWeek);
254 $result->setByDay($byDay);
259 $result =
new RecurringEventRules(
260 RecurringEventRules::FREQUENCY[
'monthly'],
261 $recurrenceDto->pattern->interval
266 $result =
new RecurringEventRules(
267 RecurringEventRules::FREQUENCY[
'yearly'],
268 $recurrenceDto->pattern->interval
276 if (!empty($recurrenceDto->range->numberOfOccurrences))
278 $result->setCount($recurrenceDto->range->numberOfOccurrences);
280 if ($recurrenceDto->range->endDate >= $recurrenceDto->range->startDate)
282 $until = new \Bitrix\Main\Type\Date($recurrenceDto->range->endDate,
'Y-m-d');
283 $result->setUntil(
new Date($until));
287 $result->setUntil($this->getFarFarAwayDate());
298 private function getFarFarAwayDate(): Date
309 private function getOurColor(
string $color, ?
string $hexColor =
null): ?string
320 private function prepareBody(Office365\Dto\RichTextDto $body,
int $userId): string
322 if ($body->contentType ===
'html')
324 $text = CCalendar::ParseHTMLToBB($body->content);
328 $text = $body->content;
331 $text = html_entity_decode($text, ENT_QUOTES | ENT_XML1);
332 $text = html_entity_decode($text, ENT_QUOTES | ENT_XML1);
333 $languageId = CCalendar::getUserLanguageId($userId);
335 return (
new Sync\Util\EventDescription())->prepareAfterImport($text, $languageId);
343 private function prepareName(?
string $name): string
347 IncludeModuleLangFile($_SERVER[
"DOCUMENT_ROOT"].
"/bitrix/modules/calendar/classes/general/calendar_js.php");
359 private function prepareDefaultMeeting(
int $userId): MeetingDescription
361 return (
new MeetingDescription())
362 ->setHostName(CCalendar::GetUserName($userId))
365 ->setAllowInvite(
false)
366 ->setMeetingCreator($userId)
367 ->setHideGuests(
true)
368 ->setLanguageId(CCalendar::getUserLanguageId($userId))
377 private function prepareAttendeeCollection(
int $userId): AttendeeCollection
379 return (
new AttendeeCollection())
380 ->setAttendeesCodes([
'U' . $userId])
static getMessage($code, $replace=null, $language=null)
static createFromPhp(\DateTime $datetime)