Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
converter.php
1<?php
2
4
23use CCalendar;
24use DateTimeZone;
25use Exception;
26
28{
29 use Sync\Internals\HasContextTrait;
30
31 private const CALENDAR_TYPE = 'user';
32
36 public function __construct(Sync\Office365\Office365Context $context)
37 {
38 $this->context = $context;
39 }
40
51 public function convertEvent(EventDto $eventData, Section $section): Event
52 {
53 $start = $this->prepareDate($eventData->start, $eventData->originalStartTimeZone);
54 $reminders = $this->makeReminders(
55 $eventData->reminderMinutesBeforeStart,
56 $eventData->isReminderOn,
57 $start,
58 );
59
60 $event = (new Event())
61 ->setName($this->prepareName($eventData->subject))
62 ->setOwner($section->getOwner())
63 ->setCreator($section->getOwner())
64 ->setEventHost($section->getOwner())
65 ->setLocation($this->prepareLocation($eventData->location))
66 ->setStart($start)
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()))
76 ->setAccessibility(EventConverter::ACCESSIBILITY_IMPORT_MAP[$eventData->showAs] ?? null)
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))
82 ;
83
84 if (!empty($eventData->originalStart))
85 {
86 $originalDto = new Office365\Dto\DateTimeDto([
87 'dateTime' => $eventData->originalStart,
88 'timeZone' => $eventData->originalStartTimeZone,
89 ]);
90 $event->setOriginalDateFrom($this->prepareDate($originalDto, $eventData->originalStartTimeZone));
91 }
92
93 // dependence from specific of office all-day events
94 if ($event->isFullDayEvent())
95 {
96 $event->setEnd($event->getEnd()->add("-1 day"));
97 }
98 return $event;
99 }
100
106 public function convertSection(SectionDto $data): Section
107 {
108 return (new Section())
109 ->setName($data->name)
110 ->setColor($this->getOurColor($data->color, $data->hexColor))
111 ;
112 }
113
122 private function prepareLocation(Office365\Dto\LocationDto $location): ?Location
123 {
124 $parsedLocation = \Bitrix\Calendar\Rooms\Util::unParseTextLocation($location->displayName);
125
126 return new Location($parsedLocation['NEW']);
127 }
128
138 private function prepareDate(Office365\Dto\DateTimeDto $dateDto, string $originalTZ = null): Date
139 {
140 $tz = Util::isTimezoneValid($dateDto->timeZone ?? '')
141 ? $dateDto->timeZone
142 : $this->getDefaultTimezone();
143
144 $phpDateTime = new \DateTime($dateDto->dateTime, new DateTimeZone($tz));
145 $eventDateTime = DateTime::createFromPhp($phpDateTime);
146
147 if ($originalTZ)
148 {
149 $original = Util::prepareTimezone($originalTZ);
150 $eventDateTime->setTimeZone($original);
151 }
152
153 return new Date($eventDateTime);
154 }
155
159 private function getDefaultTimezone(): string
160 {
161 return 'UTC';
162 }
163
171 private function prepareDateTimezone(Office365\Dto\DateTimeDto $dateDto, string $originalTZ = null): \Bitrix\Calendar\Core\Base\DateTimeZone
172 {
173 if ($originalTZ)
174 {
175 $original = Util::prepareTimezone($originalTZ);
176
177 return new \Bitrix\Calendar\Core\Base\DateTimeZone($original);
178 }
179 $tz = Util::isTimezoneValid($dateDto->timeZone ?? '')
180 ? $dateDto->timeZone
181 : $this->getDefaultTimezone();
182
183 return new \Bitrix\Calendar\Core\Base\DateTimeZone(
184 new DateTimeZone($tz)
185 );
186 }
187
195 private function makeReminders(int $minutes, bool $isReminderOn, Date $start): RemindCollection
196 {
197 $collection = new RemindCollection();
198 $collection->setEventStart($start)->setSingle(true);
199 if ($isReminderOn)
200 {
201 if ($minutes < 0)
202 {
203 $hours = '+'. abs($minutes) / 60 . ' hour';
204 $specificTime = (clone $start)->add($hours);
205 $reminder = (new Remind())
206 ->setSpecificTime($specificTime)
207 ->setDaysBefore(0)
208 ;
209 }
210 else
211 {
212 $reminder = (new Remind())->setTimeBeforeEvent($minutes, 'minutes');
213 }
214 $reminder->setEventStart($start);
215 $collection->add($reminder);
216 }
217
218 return $collection;
219 }
220
227 private function makeRecurringRule(?Office365\Dto\RecurrenceDto $recurrenceDto = null): ?RecurringEventRules
228 {
229 if (!$recurrenceDto)
230 {
231 return null;
232 }
233 switch ($recurrenceDto->pattern->type)
234 {
235 case Helper::RECURRENCE_TYPES['daily']:
236 $result = new RecurringEventRules(
237 RecurringEventRules::FREQUENCY['daily'],
238 $recurrenceDto->pattern->interval
239 );
240
241 break;
242 case Helper::RECURRENCE_TYPES['weekly']:
243 $result = new RecurringEventRules(
244 RecurringEventRules::FREQUENCY['weekly'],
245 $recurrenceDto->pattern->interval
246 );
247 if ($recurrenceDto->pattern->daysOfWeek)
248 {
249 $byDay = array_map(static function ($value)
250 {
251 return strtoupper(substr($value, 0, 2));
252 }, $recurrenceDto->pattern->daysOfWeek);
253
254 $result->setByDay($byDay);
255 }
256
257 break;
258 case Helper::RECURRENCE_TYPES['absoluteMonthly']:
259 $result = new RecurringEventRules(
260 RecurringEventRules::FREQUENCY['monthly'],
261 $recurrenceDto->pattern->interval
262 );
263
264 break;
265 case Helper::RECURRENCE_TYPES['absoluteYearly']:
266 $result = new RecurringEventRules(
267 RecurringEventRules::FREQUENCY['yearly'],
268 $recurrenceDto->pattern->interval
269 );
270
271 break;
272 default:
273 return null;
274 }
275
276 if (!empty($recurrenceDto->range->numberOfOccurrences))
277 {
278 $result->setCount($recurrenceDto->range->numberOfOccurrences);
279 }
280 if ($recurrenceDto->range->endDate >= $recurrenceDto->range->startDate)
281 {
282 $until = new \Bitrix\Main\Type\Date($recurrenceDto->range->endDate, 'Y-m-d');
283 $result->setUntil(new Date($until));
284 }
285 else
286 {
287 $result->setUntil($this->getFarFarAwayDate());
288 }
289
290 return $result;
291 }
292
298 private function getFarFarAwayDate(): Date
299 {
300 return new Date(Util::getDateObject('01.01.2038'));
301 }
302
309 private function getOurColor(string $color, ?string $hexColor = null): ?string
310 {
311 return ColorConverter::fromOffice($color, $hexColor);
312 }
313
320 private function prepareBody(Office365\Dto\RichTextDto $body, int $userId): string
321 {
322 if ($body->contentType === 'html')
323 {
324 $text = CCalendar::ParseHTMLToBB($body->content);
325 }
326 else
327 {
328 $text = $body->content;
329 }
330
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);
334
335 return (new Sync\Util\EventDescription())->prepareAfterImport($text, $languageId);
336 }
337
343 private function prepareName(?string $name): string
344 {
345 if (!$name)
346 {
347 IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/calendar/classes/general/calendar_js.php");
348 $name = Loc::getMessage('EC_DEFAULT_ENTRY_NAME');
349 }
350
351 return $name;
352 }
353
359 private function prepareDefaultMeeting(int $userId): MeetingDescription
360 {
361 return (new MeetingDescription())
362 ->setHostName(CCalendar::GetUserName($userId))
363 ->setIsNotify(true)
364 ->setReInvite(false)
365 ->setAllowInvite(false)
366 ->setMeetingCreator($userId)
367 ->setHideGuests(true)
368 ->setLanguageId(CCalendar::getUserLanguageId($userId))
369 ;
370 }
371
377 private function prepareAttendeeCollection(int $userId): AttendeeCollection
378 {
379 return (new AttendeeCollection())
380 ->setAttendeesCodes(['U' . $userId])
381 ;
382 }
383}
static fromOffice(string $color, ?string $hexColor=null)
__construct(Sync\Office365\Office365Context $context)
Definition converter.php:36
convertEvent(EventDto $eventData, Section $section)
Definition converter.php:51
static prepareTimezone(?string $tz=null)
Definition util.php:75
static isTimezoneValid(?string $timeZone)
Definition util.php:66
static getDateObject(string $date=null, ?bool $fullDay=true, ?string $tz='UTC')
Definition util.php:102
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static createFromPhp(\DateTime $datetime)
Definition datetime.php:232