29 use Sync\Internals\HasContextTrait;
31 private const CALENDAR_TYPE =
'user';
38 $this->context = $context;
50 $start = $this->prepareDate($eventData->start, $eventData->originalStartTimeZone);
51 $reminders = $this->makeReminders(
52 $eventData->reminderMinutesBeforeStart,
53 $eventData->isReminderOn,
57 $event = (
new Event())
59 ->setName($this->prepareName($eventData->subject))
63 ->setLocation($this->prepareLocation($eventData->location))
65 ->setEnd($this->prepareDate($eventData->end, $eventData->originalEndTimeZone))
66 ->setStartTimeZone($this->prepareDateTimezone($eventData->start, $eventData->originalStartTimeZone))
67 ->setEndTimeZone($this->prepareDateTimezone($eventData->end, $eventData->originalEndTimeZone))
68 ->setIsFullDay($eventData->isAllDay)
69 ->setAttendeesCollection($this->prepareAttendeeCollection($section->
getOwner()->getId()))
70 ->setRemindCollection($reminders)
71 ->setSection($section)
72 ->setDescription($this->prepareBody($eventData->body, $section->
getOwner()->getId()))
73 ->setMeetingDescription($this->prepareDefaultMeeting($section->
getOwner()->getId()))
76 ->setDateModified($this->makeDateFromString($eventData->lastModifiedDateTime))
77 ->setDateCreate($this->makeDateFromString($eventData->createdDateTime))
78 ->setImportance($eventData->importance)
81 ->setCalendarType(self::CALENDAR_TYPE)
83 ->setIsActive(!$eventData->isCancelled && !$eventData->isDraft)
84 ->setIsDeleted($eventData->isCancelled)
90 ->setRecurringRule($this->makeRecurringRule($eventData->recurrence))
92 if (!empty($eventData->originalStart))
95 'dateTime' => $eventData->originalStart,
96 'timeZone' => $eventData->originalStartTimeZone,
98 $event->setOriginalDateFrom($this->prepareDate($originalDto, $eventData->originalStartTimeZone));
102 if ($event->isFullDayEvent())
104 $event->setEnd($event->getEnd()->add(
"-1 day"));
118 ->setColor($this->getOurColor($data->color, $data->hexColor))
127 private function prepareLocation(Office365\
Dto\LocationDto $location): ?
Location
129 $parsedLocation = \Bitrix\Calendar\Rooms\Util::unParseTextLocation($location->displayName);
131 return new Location($parsedLocation[
'NEW']);
143 private function prepareDate(Office365\
Dto\
DateTimeDto $dateDto,
string $originalTZ =
null):
Date
147 : $this->getDefaultTimezone();
149 $phpDateTime = new \DateTime($dateDto->dateTime,
new DateTimeZone($tz));
155 $eventDateTime->setTimeZone($original);
158 return new Date($eventDateTime);
164 private function getDefaultTimezone(): string
175 private function prepareDateTimezone(Office365\Dto\DateTimeDto $dateDto,
string $originalTZ =
null): \
Bitrix\Calendar\Core\Base\DateTimeZone
181 return new \Bitrix\Calendar\Core\Base\DateTimeZone($original);
185 : $this->getDefaultTimezone();
187 return new \Bitrix\Calendar\Core\Base\DateTimeZone(
188 new DateTimeZone($tz)
199 private function makeReminders(
int $minutes,
bool $isReminderOn, Date $start): RemindCollection
201 $collection =
new RemindCollection();
202 $collection->setEventStart($start)->setSingle(
true);
207 $hours =
'+'. abs($minutes) / 60 .
' hour';
208 $specificTime = (clone $start)->add($hours);
209 $reminder = (
new Remind())
210 ->setSpecificTime($specificTime)
216 $reminder = (
new Remind())->setTimeBeforeEvent($minutes,
'minutes');
218 $reminder->setEventStart($start);
219 $collection->add($reminder);
231 private function makeRecurringRule(?Office365\Dto\RecurrenceDto $recurrenceDto =
null): ?RecurringEventRules
237 switch ($recurrenceDto->pattern->type)
240 $result =
new RecurringEventRules(
241 RecurringEventRules::FREQUENCY[
'daily'],
242 $recurrenceDto->pattern->interval
247 $result =
new RecurringEventRules(
248 RecurringEventRules::FREQUENCY[
'weekly'],
249 $recurrenceDto->pattern->interval
251 if ($recurrenceDto->pattern->daysOfWeek)
253 $byDay = array_map(
function ($value)
255 return strtoupper(substr($value, 0, 2));
256 }, $recurrenceDto->pattern->daysOfWeek);
257 $result->setByDay($byDay);
262 $result =
new RecurringEventRules(
263 RecurringEventRules::FREQUENCY[
'monthly'],
264 $recurrenceDto->pattern->interval
269 $result =
new RecurringEventRules(
270 RecurringEventRules::FREQUENCY[
'yearly'],
271 $recurrenceDto->pattern->interval
279 if (!empty($recurrenceDto->range->numberOfOccurrences))
281 $result->setCount($recurrenceDto->range->numberOfOccurrences);
283 if ($recurrenceDto->range->endDate >= $recurrenceDto->range->startDate)
285 $until = new \Bitrix\Main\Type\Date($recurrenceDto->range->endDate,
'Y-m-d');
286 $result->setUntil(
new Date($until));
290 $result->setUntil($this->getFarFarAwayDate());
301 private function getFarFarAwayDate(): Date
314 private function makeDateFromString(
string $time): Date
325 private function getOurColor(
string $color, ?
string $hexColor =
null): ?string
336 private function prepareBody(Office365\Dto\RichTextDto $body,
int $userId): string
338 if ($body->contentType ===
'html')
340 $text = CCalendar::ParseHTMLToBB($body->content);
344 $text = $body->content;
347 $text = html_entity_decode($text, ENT_QUOTES | ENT_XML1);
348 $text = html_entity_decode($text, ENT_QUOTES | ENT_XML1);
349 $languageId = CCalendar::getUserLanguageId($userId);
351 return (
new Sync\Util\EventDescription())->prepareAfterImport($text, $languageId);
359 private function prepareName(?
string $name): string
363 IncludeModuleLangFile($_SERVER[
"DOCUMENT_ROOT"].
"/bitrix/modules/calendar/classes/general/calendar_js.php");
375 private function prepareDefaultMeeting(
int $userId): MeetingDescription
377 return (
new MeetingDescription())
378 ->setHostName(CCalendar::GetUserName($userId))
381 ->setAllowInvite(
false)
382 ->setMeetingCreator($userId)
383 ->setHideGuests(
true)
384 ->setLanguageId(CCalendar::getUserLanguageId($userId))
393 private function prepareAttendeeCollection(
int $userId): AttendeeCollection
395 return (
new AttendeeCollection())
396 ->setAttendeesCodes([
'U' . $userId])
static fromOffice(string $color, ?string $hexColor=null)
__construct(Sync\Office365\Office365Context $context)
convertEvent(EventDto $eventData, Section $section)
convertSection(SectionDto $data)
const ACCESSIBILITY_IMPORT_MAP
static getDateObject(string $date=null, $fullDay=true, $tz='UTC')
static prepareTimezone(?string $tz=null)
static isTimezoneValid(?string $timeZone)
static getMessage($code, $replace=null, $language=null)
static createFromPhp(\DateTime $datetime)