1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
buildersynceventfromexternaldata.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Google\Builders;
4
5use Bitrix\Calendar\Core;
6use Bitrix\Calendar\Core\Event\Properties\AttendeeCollection;
7use Bitrix\Calendar\Core\Event\Properties\MeetingDescription;
8use Bitrix\Calendar\Core\Event\Properties\RecurringEventRules;
9use Bitrix\Calendar\Core\Event\Properties\ExcludedDatesCollection;
10use Bitrix\Calendar\Sync;
11use Bitrix\Calendar\Sync\Connection\Connection;
12use Bitrix\Calendar\Sync\Entities\SyncSection;
13use Bitrix\Calendar\Util;
14use Bitrix\Main\Localization\Loc;
15use Bitrix\Main\Type;
16use Bitrix\Calendar\Core\Event\Properties\RemindCollection;
17
19{
23 private Sync\Entities\SyncSection $syncSection;
27 private array $item;
31 private Sync\Connection\Connection $connection;
32
38 public function __construct(
39 array $item,
40 Sync\Connection\Connection $connection,
41 Sync\Entities\SyncSection $syncSection
42 )
43 {
44 $this->item = $item;
45 $this->connection = $connection;
46 $this->syncSection = $syncSection;
47 }
48
52 public function build()
53 {
54 $syncEvent = new Sync\Entities\SyncEvent();
55
56 $syncEvent->setAction(Sync\Google\Dictionary::SYNC_ACTION[$this->item['status']]);
57
58 $event = $this->prepareEvent();
59
60 $syncEvent
61 ->setEventConnection($this->prepareEventConnection($event))
62 ->setEvent($event)
63 ;
64
65 return $syncEvent;
66 }
67
72 public function prepareEvent(): Core\Event\Event
73 {
74 $owner = $this->syncSection->getSection()->getOwner();
76 ->setName($this->getName())
77 ->setUid($this->item['iCalUID'])
78 ->setSection($this->syncSection->getSection())
79 ->setOwner($owner)
80 ->setCreator($owner)
81 ->setEventHost($owner)
82 ->setAttendeesCollection($this->getAttendeeCollection($owner->getId()))
83 ->setMeetingDescription($this->getDefaultMeeting($owner->getId()))
84 ->setDescription($this->getDescription())
85 ->setLocation($this->getLocation())
86 ->setColor($this->item['colorId'] ?? null)
87 ;
88
89
90 if (!empty($this->item['start']))
91 {
92 if (!empty($this->item['start']['date']))
93 {
94 $event->setIsFullDay(true);
95 $date = new Type\Date($this->item['start']['date'], \DateTimeInterface::ATOM);
96 $event->setStart(new Core\Base\Date($date));
97 }
98 elseif (!empty($this->item['start']['dateTime']))
99 {
100 $event->setIsFullDay(false);
101 $timeZone = Util::prepareTimezone($this->item['start']['timeZone'] ?? null);
102 $date = (new Type\DateTime(
103 $this->item['start']['dateTime'],
104 \DateTimeInterface::ATOM,
106 );
107 $date->setTimeZone($timeZone);
108
109 $event->setStart(new Core\Base\Date($date));
110 $event->setStartTimeZone(new Core\Base\DateTimeZone($timeZone));
111 }
112 }
113
114 if (!empty($this->item['end']))
115 {
116 if (!empty($this->item['end']['date']))
117 {
118 $date = new Type\Date($this->item['end']['date'], \DateTimeInterface::ATOM);
119 $event->setEnd((new Core\Base\Date($date))->sub('1 day'));
120 }
121 elseif (!empty($this->item['end']['dateTime']))
122 {
123 $timeZone = Util::prepareTimezone($this->item['end']['timeZone'] ?? null);
124 $date = new Type\DateTime(
125 $this->item['end']['dateTime'],
126 \DateTimeInterface::ATOM,
128 );
129 $date->setTimeZone($timeZone);
130
131 $event->setEnd(new Core\Base\Date($date));
132 $event->setEndTimeZone(new Core\Base\DateTimeZone($timeZone));
133 }
134 }
135
136 if (isset($this->item['transparency']) && ($this->item['transparency'] === 'transparent'))
137 {
138 $event->setAccessibility('free');
139 }
140
141 if (isset($this->item['visibility']) && $this->item['visibility'] === 'private')
142 {
143 $event->setIsPrivate(true);
144 }
145
146 if (!empty($this->item['reminders']))
147 {
148 $event->setRemindCollection($this->getReminders($event->getStart()));
149 }
150
151 $event->setRecurringRule($this->prepareRecurringRule($event->getStart()));
152 $event->setExcludedDateCollection($this->prepareExcludedDatesCollection());
153
154 $event->setOriginalDateFrom($this->prepareOriginalStart());
155
156 return $event;
157 }
158
164 {
165 return (new Sync\Connection\EventConnection())
166 ->setConnection($this->connection)
167 ->setEntityTag($this->item['etag'] ?? null)
168 ->setVendorEventId($this->item['id'] ?? null)
169 ->setVendorVersionId($this->item['etag'] ?? null)
170 ->setVersion(($this->item['sequence'] ?? 0))
171 ->setLastSyncStatus(Sync\Dictionary::SYNC_STATUS['success'])
172 ->setRecurrenceId($this->item['recurringEventId'] ?? null)
173 ->setEvent($event)
174 ->setData($this->getEventConnectionData($event))
175 ;
176 }
177
182 private function prepareRecurringRule(Core\Base\Date $start): ?RecurringEventRules
183 {
184 if (empty($this->item['recurrence']))
185 {
186 return null;
187 }
188 foreach ($this->item['recurrence'] as $row)
189 {
190 if (strpos($row, 'RRULE:') === 0)
191 {
192 $rrule = [];
193
194 $rules = explode(';', substr($row, 6));
195
196 foreach ($rules as $rule)
197 {
198 if (empty($rule))
199 {
200 continue;
201 }
202 [$name, $value] = explode('=', $rule);
203
204 if (empty($name) || empty($value))
205 {
206 continue;
207 }
208
209 $rrule[$name] = $value;
210 }
211
212 if (!empty($rrule['FREQ']) && in_array($rrule['FREQ'], RecurringEventRules::FREQUENCY, true))
213 {
214 $property = new RecurringEventRules($rrule['FREQ']);
215 }
216 else
217 {
218 return null;
219 }
220
221 if (!empty($rrule['COUNT']))
222 {
223 $property->setCount((int)$rrule['COUNT']);
224 }
225 if (!empty($rrule['INTERVAL']))
226 {
227 $property->setInterval((int)$rrule['INTERVAL']);
228 }
229 if (!empty($rrule['UNTIL']))
230 {
231 try
232 {
233 $phpDate = new \DateTime($rrule['UNTIL']);
234 $date = new Core\Base\Date(Type\DateTime::createFromPhp($phpDate));
235
236 $property->setUntil($date);
237 }
238 catch (\Exception $e)
239 {
240 return null;
241 }
242 }
243 if ($rrule['FREQ'] === RecurringEventRules::FREQUENCY_WEEKLY)
244 {
245 if (!empty($rrule['BYDAY']))
246 {
247 $property->setByDay(explode(',', $rrule['BYDAY']));
248 }
249 else
250 {
251 $dayOfWeek = $start->format('w');
252 $day = \CCalendar::WeekDayByInd($dayOfWeek);
253 if ($day)
254 {
255 $property->setByDay([$day]);
256 }
257 }
258 }
259
260 return $property;
261 }
262 }
263
264 return null;
265 }
266
271 private function prepareExcludedDatesCollection(): ?ExcludedDatesCollection
272 {
273 if (empty($this->item['recurrence']))
274 {
275 return null;
276 }
277
278 $exDatesCollection = new ExcludedDatesCollection();
279 foreach ($this->item['recurrence'] as $row)
280 {
281 if (strpos($row, 'EXDATE;') === 0)
282 {
283 $exDate = explode(':', substr($row, 7));
284
285 if ($exDate[0] === 'VALUE=DATE-TIME')
286 {
287 $date = Core\Base\Date::createDateTimeFromFormat(
288 $exDate[1],
289 Sync\Google\Helper::EXCLUDED_DATE_TIME_FORMAT
290 );
291
292 if ($date)
293 {
294 $exDatesCollection->add($date);
295 }
296 }
297 else if ($exDate[0] === 'VALUE=DATE')
298 {
299 $date = Core\Base\Date::createDateTimeFromFormat(
300 $exDate[1],
301 Sync\Google\Helper::EXCLUDED_DATE_FORMAT
302 );
303
304 if ($date)
305 {
306 $exDatesCollection->add($date);
307 }
308 }
309 }
310 }
311
312 if ($exDatesCollection->count())
313 {
314 return $exDatesCollection;
315 }
316
317 return null;
318 }
319
324 public function prepareOriginalStart(): ?Core\Base\Date
325 {
326 if (!empty($this->item['originalStartTime']))
327 {
328 if (!empty($this->item['originalStartTime']['dateTime']))
329 {
331 $this->item['originalStartTime']['dateTime'],
332 \DateTimeInterface::ATOM
333 );
334 }
335
336 if (!empty($this->item['originalStartTime']['date']))
337 {
339 $this->item['originalStartTime']['date'],
340 Sync\Google\Helper::DATE_FORMAT
341 );
342 }
343 }
344
345 return null;
346 }
347
351 private function getLocation(): ?Core\Event\Properties\Location
352 {
353 if (!empty($this->item['location']))
354 {
355 $parsedLocation = \Bitrix\Calendar\Rooms\Util::unParseTextLocation($this->item['location']);
356
357 return new Core\Event\Properties\Location($parsedLocation['NEW']);
358 }
359
360 return null;
361 }
362
366 private function getDescription(): string
367 {
368 if (!empty($this->item['description']))
369 {
370 $languageId = \CCalendar::getUserLanguageId($this->syncSection->getSection()->getOwner()->getId());
371 $this->item['description'] = \CCalendar::ParseHTMLToBB($this->item['description']);
372
373 return (new Sync\Util\EventDescription())->prepareAfterImport($this->item['description'], $languageId);
374 }
375
376 return '';
377 }
378
383 private function getReminders(Core\Base\Date $start): RemindCollection
384 {
385 $collection = new RemindCollection();
386 $collection->setEventStart($start);
387
388 if (!empty($this->item['reminders']['overrides']) && is_array($this->item['reminders']['overrides']))
389 {
390 foreach ($this->item['reminders']['overrides'] as $remind)
391 {
392 $collection->add((new Core\Event\Properties\Remind())
393 ->setTimeBeforeEvent($remind['minutes'])
394 ->setEventStart($start)
395 );
396 }
397 }
398
399 if (!empty($this->item['reminders']['useDefault']))
400 {
401 $collection->add((new Core\Event\Properties\Remind())
402 ->setTimeBeforeEvent(30)
403 ->setEventStart($start)
404 );
405 }
406
407 return $collection;
408 }
409
413 private function getName()
414 {
415 if (empty($this->item['summary']))
416 {
417 IncludeModuleLangFile($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/calendar/classes/general/calendar_js.php");
418 $this->item['summary'] = Loc::getMessage('EC_DEFAULT_ENTRY_NAME');
419 }
420
421 return $this->item['summary'];
422 }
423
429 private function getAttendeeCollection(int $userId): AttendeeCollection
430 {
431 return (new AttendeeCollection())
432 ->setAttendeesCodes(['U' . $userId])
433 ;
434 }
435
441 private function getDefaultMeeting(int $userId): MeetingDescription
442 {
443 return (new MeetingDescription())
444 ->setHostName(\CCalendar::GetUserName($userId))
445 ->setIsNotify(true)
446 ->setReInvite(false)
447 ->setAllowInvite(false)
448 ->setMeetingCreator($userId)
449 ->setHideGuests(true)
450 ->setLanguageId(\CCalendar::getUserLanguageId($userId))
451 ;
452 }
453
454
459 private function getEventConnectionData(Core\Event\Event $event): array
460 {
461 $data = [];
462 if (!empty($this->item['attendees']))
463 {
464 $data['attendees'] = $this->item['attendees'];
465 }
466
467 return $data;
468 }
469}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static createDateTimeFromFormat(string $date, string $format)
Определения date.php:26
static createDateFromFormat(string $date, string $format)
Определения date.php:50
const SYNC_STATUS
Определения dictionary.php:16
__construct(array $item, Sync\Connection\Connection $connection, Sync\Entities\SyncSection $syncSection)
static prepareTimezone(?string $tz=null)
Определения util.php:80
Определения date.php:9
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$start
Определения get_search.php:9
$_SERVER["DOCUMENT_ROOT"]
Определения cron_frame.php:9
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
$name
Определения menu_edit.php:35
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393