Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
eventbuilderfromarray.php
1<?php
2
4
24use DateTime;
25
26IncludeModuleLangFile($_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/calendar/classes/general/calendar.php');
27
29{
33 protected $fields;
34
38 public function __construct(array $fields)
39 {
40 $this->fields = $fields;
41 $this->prepareRecurrenceRuleField();
42 }
43
44 private function prepareRecurrenceRuleField(): void
45 {
46 if (!empty($this->fields['RRULE']) && is_string($this->fields['RRULE']))
47 {
48 $result = [];
49 foreach (explode(';', $this->fields['RRULE']) as $item) {
50 if (!empty($item))
51 {
52 [$key, $value] = explode('=', $item);
53 $result[$key] = $value;
54 }
55 }
56 $this->fields['RRULE'] = $result;
57 }
58 }
59
63 protected function getName(): string
64 {
65 return $this->fields['NAME'] ?? '';
66 }
67
71 protected function getStartTimezone(): ?DateTimeZone
72 {
73 if (!isset($this->fields['TZ_FROM']))
74 {
75 return null;
76 }
77
78 return new DateTimeZone(Util::prepareTimezone($this->fields['TZ_FROM']));
79 }
80
84 protected function getEndTimezone(): ?DateTimeZone
85 {
86 if (!isset($this->fields['TZ_TO']))
87 {
88 return null;
89 }
90
91 return new DateTimeZone(Util::prepareTimezone($this->fields['TZ_TO']));
92 }
93
100 {
101 if (!empty($this->fields['RRULE']))
102 {
103 if (is_string($this->fields['RRULE']))
104 {
105 $this->fields['RRULE'] = \CCalendarEvent::convertDateToCulture($this->fields['RRULE']);
106 }
107 elseif (is_array($this->fields['RRULE']) && !empty($this->fields['RRULE']['UNTIL']))
108 {
109 $this->fields['RRULE']['UNTIL'] = \CCalendarEvent::convertDateToCulture($this->fields['RRULE']['UNTIL']);
110 }
111
112 return $this->prepareRecurringRule($this->fields['RRULE']);
113 }
114 else
115 {
116 return null;
117 }
118 }
119
123 protected function getLocation(): ?Location
124 {
125 return $this->prepareLocation($this->fields['LOCATION'] ?? null);
126 }
127
132 protected function getStart(): Date
133 {
134 return new Date(
136 $this->fields['DATE_FROM'] ?? null,
137 $this->isFullDay(),
138 $this->fields['TZ_FROM'] ?? null
139 )
140 );
141 }
142
147 protected function getEnd(): Date
148 {
149 return new Date(
151 $this->fields['DATE_TO'] ?? null,
152 $this->isFullDay(),
153 $this->fields['TZ_TO'] ?? null,
154 )
155 );
156 }
157
158 private function isFullDay(): bool
159 {
160 return (isset($this->fields['SKIP_TIME']) && $this->fields['SKIP_TIME'] === 'Y')
161 || (isset($this->fields['DT_SKIP_TIME']) && $this->fields['DT_SKIP_TIME'] === 'Y');
162 }
163
168 protected function getOriginalDate(): ?Date
169 {
170 if (!isset($this->fields['ORIGINAL_DATE_FROM']))
171 {
172 return null;
173 }
174
175 return new Date(Util::getDateObject(
176 $this->fields['ORIGINAL_DATE_FROM'],
177 ($this->fields['SKIP_TIME'] ?? null) === 'Y' || ($this->fields['DT_SKIP_TIME'] ?? null) === 'Y',
178 $this->fields['TZ_FROM'] ?? null
179 ));
180 }
181
185 protected function getFullDay(): bool
186 {
187 return $this->isFullDay();
188 }
189
193 protected function getAttendees(): ?AttendeeCollection
194 {
195 $collection = new AttendeeCollection();
196
197 if (isset($this->fields['ATTENDEES_CODES']))
198 {
199 if (is_string($this->fields['ATTENDEES_CODES']))
200 {
201 $collection->setAttendeesCodes(explode(',', $this->fields['ATTENDEES_CODES']));
202 }
203 else if (is_array($this->fields['ATTENDEES_CODES']))
204 {
205 $collection->setAttendeesCodes($this->fields['ATTENDEES_CODES']);
206 }
207 }
208
209 if (isset($this->fields['ATTENDEES']) && is_array($this->fields['ATTENDEES']))
210 {
211 $collection->setAttendeesId($this->fields['ATTENDEES']);
212 }
213 else
214 {
215 $collection->setAttendeesId([(int)$this->fields['OWNER_ID']]);
216 }
217
218
219 return $collection;
220 }
221
226 protected function getReminders(): RemindCollection
227 {
228 if (isset($this->fields['REMIND']) && is_string($this->fields['REMIND']))
229 {
230 $this->fields['REMIND'] = unserialize($this->fields['REMIND'], ['allowed_classes' => false]);
231 }
232
233 if (!isset($this->fields['REMIND']) || !is_array($this->fields['REMIND']))
234 {
235 return new RemindCollection();
236 }
237
238 $eventStart = $this->getStart();
239
240 $collection = new RemindCollection();
241 $collection->setEventStart($eventStart);
242
243 foreach ($this->fields['REMIND'] as $remind)
244 {
245 if ($remind['type'] === Event\Tools\Dictionary::REMIND_UNIT['date'])
246 {
247 $collection->add((new Event\Properties\Remind())
248 ->setSpecificTime(
249 new Date(
251 $remind['value'],
252 false,
253 $this->fields['TZ_FROM']
254 )
255 )
256 )
257 ->setEventStart($eventStart)
258 );
259 }
260 elseif ($remind['type'] === Event\Properties\Remind::UNIT_DAY_BEFORE)
261 {
262 $collection->add((new Event\Properties\Remind())
263 ->setEventStart($eventStart)
264 ->setSpecificTime(
265 (new Date(
267 $eventStart->toString(),
268 false,
269 $this->fields['TZ_FROM'])
270 ))
271 ->resetTime()
272 ->sub("{$remind['before']} days")
273 ->add("{$remind['time']} minutes")
274 )
275 ->setDaysBefore($remind['before'])
276 );
277 }
278 else
279 {
280 $collection->add((new Event\Properties\Remind())
281 ->setTimeBeforeEvent(
282 $remind['count'],
283 Event\Tools\Dictionary::REMIND_UNIT[$remind['type']]
284 ?? Event\Properties\Remind::UNIT_MINUTES
285 )
286 ->setEventStart($eventStart)
287 );
288 }
289 }
290
291 return $collection;
292 }
293
297 protected function getDescription(): ?string
298 {
299 return $this->fields['DESCRIPTION'] ?? null;
300 }
301
308 protected function getSection(): Section
309 {
310 $sectionId = $this->fields['SECTION_ID'] ??
311 (is_array($this->fields['SECTIONS'])
312 ? (int)$this->fields['SECTIONS'][0]
313 : null
314 );
315
316 if ($sectionId)
317 {
318 return (new \Bitrix\Calendar\Core\Mappers\Section())->getMap([
319 '=ID' => $sectionId
320 ])->fetch();
321 }
322
323 throw new BuilderException('it is impossible to find the section');
324 }
325
329 protected function getColor(): ?string
330 {
331 return $this->fields['COLOR'] ?? null;
332 }
333
337 protected function getTransparency(): ?string
338 {
339 return $this->fields['TRANSPARENT'] ?? null;
340 }
341
345 protected function getImportance(): ?string
346 {
347 return $this->fields['IMPORTANCE'] ?? null;
348 }
349
353 protected function getAccessibility(): ?string
354 {
355 return $this->fields['ACCESSIBILITY'] ?? null;
356 }
357
361 protected function getIsPrivate(): bool
362 {
363 return $this->fields['PRIVATE_EVENT'] ?? false;
364 }
365
372 protected function getEventHost(): ?Role
373 {
374 if (empty($this->fields['MEETING_HOST']))
375 {
376 return null;
377 }
378
379 try
380 {
381 return Helper::getUserRole($this->fields['MEETING_HOST']);
382 }
383 catch (BaseException $exception)
384 {
385 return null;
386 }
387 }
388
395 protected function getCreator(): ?Role
396 {
397 if (empty($this->fields['CREATED_BY']))
398 {
399 return null;
400 }
401
402 try
403 {
404 return Helper::getUserRole($this->fields['CREATED_BY']);
405 }
406 catch (BaseException $exception)
407 {
408 return null;
409 }
410 }
411
418 protected function getOwner(): ?Role
419 {
420 if (empty($this->fields['OWNER_ID']))
421 {
422 return null;
423 }
424 if (empty($this->fields['CAL_TYPE']))
425 {
426 $this->fields['CAL_TYPE'] = User::TYPE;
427 }
428
429 try
430 {
431 return Helper::getRole($this->fields['OWNER_ID'], $this->fields['CAL_TYPE']);
432 }
433 catch (BaseException $exception)
434 {
435 return null;
436 }
437 }
438
443 {
444 return $this->prepareMeetingDescription($this->fields['MEETING'] ?? null);
445 }
446
450 protected function getVersion(): int
451 {
452 return (int)($this->fields['VERSION'] ?? null);
453 }
454
458 protected function getCalendarType(): ?string
459 {
460 return $this->fields['CAL_TYPE'] ?? null;
461 }
462
466 protected function getUid(): ?string
467 {
468 return $this->fields['DAV_XML_ID'] ?? null;
469 }
470
474 protected function isDeleted(): bool
475 {
476 return isset($this->fields['DELETED']) && $this->fields['DELETED'] === 'Y';
477 }
478
482 protected function isActive(): bool
483 {
484 return isset($this->fields['ACTIVE']) && $this->fields['ACTIVE'] === 'Y';
485 }
486
490 protected function getRecurrenceId(): ?int
491 {
492 return $this->fields['RECURRENCE_ID'] ?? null;
493 }
494
499 protected function getDateCreate(): ?Date
500 {
501 if (!isset($this->fields['DATE_CREATE']))
502 {
503 return null;
504 }
505
506 return new Date(Util::getDateObject(
507 $this->fields['DATE_CREATE'],
508 false,
509 (new DateTime())->getTimezone()->getName()
510 ));
511 }
512
517 protected function getDateModified(): ?Date
518 {
519 if (!isset($this->fields['TIMESTAMP_X']))
520 {
521 return null;
522 }
523
524 return new Date(Util::getDateObject(
525 $this->fields['TIMESTAMP_X'],
526 false,
527 (new DateTime())->getTimezone()->getName()
528 ));
529 }
530
535 protected function getExcludedDate(): Event\Properties\ExcludedDatesCollection
536 {
537 if (empty($this->fields['EXDATE']))
538 {
540 }
541
543 if (is_string($this->fields['EXDATE']))
544 {
545 foreach (explode(";", $this->fields['EXDATE']) as $exDate)
546 {
547 $collection->add($this->createDateForRecurrence($exDate));
548 }
549 }
550
551 else if (is_array($this->fields['EXDATE']))
552 {
553 foreach ($this->fields['EXDATE'] as $exDate)
554 {
555 $collection->add($this->createDateForRecurrence($exDate));
556 }
557 }
558
559 return $collection;
560 }
561
565 protected function getId(): ?int
566 {
567 return $this->fields['ID'] ?? null;
568 }
569
573 protected function getParentId(): ?int
574 {
575 return $this->fields['PARENT_ID'] ?? null;
576 }
577
581 protected function isMeeting(): bool
582 {
583 return (bool)($this->fields['IS_MEETING'] ?? null);
584 }
585
589 protected function getMeetingStatus(): ?string
590 {
591 return $this->fields['MEETING_STATUS'] ?? null;
592 }
593
597 protected function getRelations(): ?Relations
598 {
599 return $this->prepareRelations($this->fields['RELATIONS'] ?? null);
600 }
601
605 protected function getSpecialLabel(): ?string
606 {
607 return $this->fields['EVENT_TYPE'] ?? null;
608 }
609}
static prepareTimezone(?string $tz=null)
Definition util.php:75
static getDateObject(string $date=null, ?bool $fullDay=true, ?string $tz='UTC')
Definition util.php:102