Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
buildersynceventfromexternaldata.php
1<?php
2
4
5use Bitrix\Calendar\Core;
15
17{
18 private Sync\Entities\SyncSection $syncSection;
22 private array $item;
26 private Sync\Connection\Connection $connection;
27
32 public function __construct(
33 array $item,
34 Sync\Connection\Connection $connection,
35 Sync\Entities\SyncSection $syncSection
36 )
37 {
38 $this->item = $item;
39 $this->connection = $connection;
40 $this->syncSection = $syncSection;
41 }
42
46 public function build()
47 {
48 $syncEvent = new Sync\Entities\SyncEvent();
49
50 $syncEvent->setAction(Sync\Google\Dictionary::SYNC_ACTION[$this->item['status']]);
51
52 $event = $this->prepareEvent();
53
54 $syncEvent
55 ->setEventConnection($this->prepareEventConnection($event))
56 ->setEvent($event)
57 ;
58
59 return $syncEvent;
60 }
61
66 public function prepareEvent(): Core\Event\Event
67 {
68 $owner = $this->syncSection->getSection()->getOwner();
69 $event = (new Core\Event\Event)
70 ->setName($this->getName())
71 ->setUid($this->item['iCalUID'])
72 ->setSection($this->syncSection->getSection())
73 ->setOwner($owner)
74 ->setCreator($owner)
75 ->setEventHost($owner)
76 ->setAttendeesCollection($this->getAttendeeCollection($owner->getId()))
77 ->setMeetingDescription($this->getDefaultMeeting($owner->getId()))
78 ->setDescription($this->getDescription())
79 ->setLocation($this->getLocation())
80 ->setColor($this->item['colorId'] ?? null)
81 ;
82
83
84 if (!empty($this->item['start']))
85 {
86 if (!empty($this->item['start']['date']))
87 {
88 $event->setIsFullDay(true);
89 $date = new Type\Date($this->item['start']['date'], \DateTimeInterface::ATOM);
90 $event->setStart(new Core\Base\Date($date));
91 }
92 elseif (!empty($this->item['start']['dateTime']))
93 {
94 $event->setIsFullDay(false);
95 $timeZone = Util::prepareTimezone($this->item['start']['timeZone'] ?? null);
96 $date = (new Type\DateTime(
97 $this->item['start']['dateTime'],
98 \DateTimeInterface::ATOM,
100 );
101 $date->setTimeZone($timeZone);
102
103 $event->setStart(new Core\Base\Date($date));
104 $event->setStartTimeZone(new Core\Base\DateTimeZone($timeZone));
105 }
106 }
107
108 if (!empty($this->item['end']))
109 {
110 if (!empty($this->item['end']['date']))
111 {
112 $date = new Type\Date($this->item['end']['date'], \DateTimeInterface::ATOM);
113 $event->setEnd((new Core\Base\Date($date))->sub('1 day'));
114 }
115 elseif (!empty($this->item['end']['dateTime']))
116 {
117 $timeZone = Util::prepareTimezone($this->item['end']['timeZone'] ?? null);
118 $date = new Type\DateTime(
119 $this->item['end']['dateTime'],
120 \DateTimeInterface::ATOM,
122 );
123 $date->setTimeZone($timeZone);
124
125 $event->setEnd(new Core\Base\Date($date));
126 $event->setEndTimeZone(new Core\Base\DateTimeZone($timeZone));
127 }
128 }
129
130 if (isset($this->item['transparency']) && ($this->item['transparency'] === 'transparent'))
131 {
132 $event->setAccessibility('free');
133 }
134
135 if (isset($this->item['visibility']) && $this->item['visibility'] === 'private')
136 {
137 $event->setIsPrivate(true);
138 }
139
140 if (!empty($this->item['reminders']))
141 {
142 $event->setRemindCollection($this->getReminders($event->getStart()));
143 }
144
145 $event->setRecurringRule($this->prepareRecurringRule($event->getStart()));
146 $event->setExcludedDateCollection($this->prepareExcludedDatesCollection());
147
148 $event->setOriginalDateFrom($this->prepareOriginalStart());
149
150 return $event;
151 }
152
158 {
159 return (new Sync\Connection\EventConnection)
160 ->setConnection($this->connection)
161 ->setEntityTag($this->item['etag'] ?? null)
162 ->setVendorEventId($this->item['id'] ?? null)
163 // ->setVendorVersionId(($this->item['sequence'] ?? 0))
164 ->setVendorVersionId($this->item['etag'] ?? null)
165 ->setVersion(($this->item['sequence'] ?? 0))
166 // ->setVersion($event->getVersion())
167 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['success'])
168 ->setRecurrenceId($this->item['recurringEventId'] ?? null)
169 ->setEvent($event)
170
171 ;
172 }
173
178 private function prepareRecurringRule(Core\Base\Date $start): ?RecurringEventRules
179 {
180 if (empty($this->item['recurrence']))
181 {
182 return null;
183 }
184 foreach ($this->item['recurrence'] as $row)
185 {
186 if (strpos($row, 'RRULE:') === 0)
187 {
188 $rrule = [];
189
190 $rules = explode(';', substr($row, 6));
191
192 foreach ($rules as $rule)
193 {
194 if (empty($rule))
195 {
196 continue;
197 }
198 [$name, $value] = explode('=', $rule);
199
200 if (empty($name) || empty($value))
201 {
202 continue;
203 }
204
205 $rrule[$name] = $value;
206 }
207
208 if (!empty($rrule['FREQ']) && in_array($rrule['FREQ'], RecurringEventRules::FREQUENCY, true))
209 {
210 $property = new RecurringEventRules($rrule['FREQ']);
211 }
212 else
213 {
214 return null;
215 }
216
217 if (!empty($rrule['COUNT']))
218 {
219 $property->setCount((int)$rrule['COUNT']);
220 }
221 if (!empty($rrule['INTERVAL']))
222 {
223 $property->setInterval((int)$rrule['INTERVAL']);
224 }
225 if (!empty($rrule['UNTIL']))
226 {
227 try
228 {
229 $phpDate = new \DateTime($rrule['UNTIL']);
230 $date = new Core\Base\Date(Type\DateTime::createFromPhp($phpDate));
231
232 $property->setUntil($date);
233 }
234 catch (\Exception $e)
235 {
236 return null;
237 }
238 }
239 if ($rrule['FREQ'] === RecurringEventRules::FREQUENCY_WEEKLY)
240 {
241 if (!empty($rrule['BYDAY']))
242 {
243 $property->setByDay(explode(',', $rrule['BYDAY']));
244 }
245 else
246 {
247 $dayOfWeek = $start->format('w');
248 $day = \CCalendar::WeekDayByInd($dayOfWeek);
249 if ($day)
250 {
251 $property->setByDay([$day]);
252 }
253 }
254 }
255
256 return $property;
257 }
258 }
259
260 return null;
261 }
262
267 private function prepareExcludedDatesCollection(): ?ExcludedDatesCollection
268 {
269 if (empty($this->item['recurrence']))
270 {
271 return null;
272 }
273
274 $exDatesCollection = new ExcludedDatesCollection();
275 foreach ($this->item['recurrence'] as $row)
276 {
277 if (strpos($row, 'EXDATE;') === 0)
278 {
279 $exDate = explode(':', substr($row, 7));
280
281 if ($exDate[0] === 'VALUE=DATE-TIME')
282 {
283 $date = Core\Base\Date::createDateTimeFromFormat(
284 $exDate[1],
286 );
287
288 if ($date)
289 {
290 $exDatesCollection->add($date);
291 }
292 }
293 else if ($exDate[0] === 'VALUE=DATE')
294 {
295 $date = Core\Base\Date::createDateTimeFromFormat(
296 $exDate[1],
298 );
299
300 if ($date)
301 {
302 $exDatesCollection->add($date);
303 }
304 }
305 }
306 }
307
308 if ($exDatesCollection->count())
309 {
310 return $exDatesCollection;
311 }
312
313 return null;
314 }
315
320 public function prepareOriginalStart(): ?Core\Base\Date
321 {
322 if (!empty($this->item['originalStartTime']))
323 {
324 if (!empty($this->item['originalStartTime']['dateTime']))
325 {
326 return Core\Base\Date::createDateTimeFromFormat(
327 $this->item['originalStartTime']['dateTime'],
328 \DateTimeInterface::ATOM
329 );
330 }
331
332 if (!empty($this->item['originalStartTime']['date']))
333 {
334 return Core\Base\Date::createDateFromFormat(
335 $this->item['originalStartTime']['date'],
336 Sync\Google\Helper::DATE_FORMAT
337 );
338 }
339 }
340
341 return null;
342 }
343
347 private function getLocation(): ?Core\Event\Properties\Location
348 {
349 if (!empty($this->item['location']))
350 {
351 $parsedLocation = \Bitrix\Calendar\Rooms\Util::unParseTextLocation($this->item['location']);
352
353 return new Core\Event\Properties\Location($parsedLocation['NEW']);
354 }
355
356 return null;
357 }
358
362 private function getDescription(): string
363 {
364 if (!empty($this->item['description']))
365 {
366 $languageId = \CCalendar::getUserLanguageId($this->syncSection->getSection()->getOwner()->getId());
367 $this->item['description'] = \CCalendar::ParseHTMLToBB($this->item['description']);
368
369 return (new Sync\Util\EventDescription())->prepareAfterImport($this->item['description'], $languageId);
370 }
371
372 return '';
373 }
374
379 private function getReminders(Core\Base\Date $start): RemindCollection
380 {
381 $collection = new RemindCollection();
382 $collection->setEventStart($start);
383
384 if (!empty($this->item['reminders']['overrides']) && is_array($this->item['reminders']['overrides']))
385 {
386 foreach ($this->item['reminders']['overrides'] as $remind)
387 {
388 $collection->add((new Core\Event\Properties\Remind())
389 ->setTimeBeforeEvent($remind['minutes'])
390 ->setEventStart($start)
391 );
392 }
393 }
394
395 if (!empty($this->item['reminders']['useDefault']))
396 {
397 $collection->add((new Core\Event\Properties\Remind())
398 ->setTimeBeforeEvent(30)
399 ->setEventStart($start)
400 );
401 }
402
403 return $collection;
404 }
405
409 private function getName()
410 {
411 if (empty($this->item['summary']))
412 {
413 IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/calendar/classes/general/calendar_js.php");
414 $this->item['summary'] = Loc::getMessage('EC_DEFAULT_ENTRY_NAME');
415 }
416
417 return $this->item['summary'];
418 }
419
425 private function getAttendeeCollection(int $userId): AttendeeCollection
426 {
427 return (new AttendeeCollection())
428 ->setAttendeesCodes(['U' . $userId])
429 ;
430 }
431
437 private function getDefaultMeeting(int $userId): MeetingDescription
438 {
439 return (new MeetingDescription())
440 ->setHostName(\CCalendar::GetUserName($userId))
441 ->setIsNotify(true)
442 ->setReInvite(false)
443 ->setAllowInvite(false)
444 ->setMeetingCreator($userId)
445 ->setHideGuests(true)
446 ->setLanguageId(\CCalendar::getUserLanguageId($userId))
447 ;
448 }
449}
__construct(array $item, Sync\Connection\Connection $connection, Sync\Entities\SyncSection $syncSection)
static prepareTimezone(?string $tz=null)
Definition util.php:75
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static createFromPhp(\DateTime $datetime)
Definition datetime.php:232