Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
icsmanager.php
1<?php
9
10Loc::loadMessages($_SERVER['DOCUMENT_ROOT'].BX_ROOT.'/modules/calendar/lib/ical/incomingeventmanager.php');
11Loc::loadMessages(__FILE__);
12
14{
15 private static ?IcsManager $instance = null;
16
17 private const FILE_TYPE = 'text/calendar';
18 private const FILE_NAME = 'event';
19 private const FILE_EXTENSION = '.ics';
20 private const MODULE_ID = 'calendar';
21
22 protected function __construct()
23 {
24 }
25
26 public static function getInstance(): IcsManager
27 {
28 if (!self::$instance)
29 {
30 self::$instance = new IcsManager();
31 }
32
33 return self::$instance;
34 }
35
39 public function createIcsFile(Event $event, array $params): int
40 {
41 $fileData = $this->getIcsFileData($event, $params);
42 $fileId = \CFile::SaveFile($fileData, 'calendar');
43
44 if (!$this->checkIcsFileExistence($fileId))
45 {
46 throw new BaseException('Error saving ICS file');
47 }
48
49 return $fileId;
50 }
51
52 private function getIcsFileData(Event $event, array $params): array
53 {
54 $fileContent = $this->getIcsFileContent($event, $params);
55 $fileContent = Encoding::convertEncoding($fileContent, SITE_CHARSET, "utf-8");
56 return [
57 'name' => self::FILE_NAME . self::FILE_EXTENSION,
58 'type' => self::FILE_TYPE,
59 'MODULE_ID' => self::MODULE_ID,
60 'content' => $fileContent,
61 ];
62 }
63
64 private function checkIcsFileExistence(int $fileId): bool
65 {
66 return is_array(\CFile::GetFileArray($fileId));
67 }
68
69 public function getIcsFileContent(Event $event, array $params): string
70 {
71 $dtEnd = Util::getTimestamp($event->getEnd());
72 if ($event->isFullDayEvent())
73 {
74 $dtEnd += \CCalendar::GetDayLen();
75 }
76 $icsBuilder = new \Bitrix\Calendar\ICal\IcsBuilder(
77 [
78 'summary' => $event->getName() ?? '',
79 'description' => $this->prepareEventDescription($event, $params),
80 'dtstart' => Util::getTimestamp($event->getStart()),
81 'dtend' => $dtEnd,
82 'dtstamp' => Util::getTimestamp($event->getDateCreate()),
83 'location' => $this->prepareLocationField($event),
84 'uid' => $event->getUid() ?? uniqid('', true),
85 ],
86 );
87 $icsBuilder->setFullDayMode($event->isFullDayEvent());
88 if (isset($params['organizer']))
89 {
90 $organizer = $params['organizer'];
91 $icsBuilder->setOrganizer($organizer['name'], $organizer['email'] ?? null, $organizer['phone'] ?? null);
92 }
93
94 if (!$event->isFullDayEvent())
95 {
96 $icsBuilder->setConfig(
97 [
98 'timezoneFrom' => $event->getStartTimeZone(),
99 'timezoneTo' => $event->getEndTimeZone() ?? $event->getStartTimeZone(),
100 ]
101 );
102 }
103
104 return $icsBuilder->render();
105 }
106
107 private function prepareEventDescription(Event $event, array $params): string
108 {
109 $languageId = \CCalendar::getUserLanguageId($event->getOwner()->getId());
110 $eventDescription = '';
111
112 if (
113 $event->getAttendeesCollection()
114 && ($attendeesCodes = $event->getAttendeesCollection()->getAttendeesCodes())
115 && count($attendeesCodes) > 1
116 )
117 {
118 $eventDescription =
119 $this->formatAttendeesDescription($attendeesCodes, $event->getParentId(), $languageId)
120 . '; \\n'
121 ;
122 }
123
124 if ($params['eventUrl'])
125 {
126 $eventDescription .= Loc::getMessage('EC_EVENT_LINK') . $params['eventUrl'] . '\\n';
127 }
128
129 if ($params['conferenceUrl'])
130 {
131 $eventDescription .= Loc::getMessage('EC_CONFERENCE_LINK') . $params['conferenceUrl']. '\\n';
132 }
133
134 if ($description = $event->getDescription())
135 {
136 $eventDescription .= Loc::getMessage('EC_CALENDAR_ICS_COMMENT') . ': '. $description;
137 }
138
139 return $eventDescription;
140 }
141
142 private function formatAttendeesDescription(array $codes, ?int $parentId, string $languageId): string
143 {
144 $users = \CCalendar::GetDestinationUsers($codes, true);
145
146 $names = array_map(static function($user) {
147 return $user['FORMATTED_NAME'];
148 }, $users);
149
150 $result = Loc::getMessage('EC_ATTENDEES_LIST_TITLE') . ": ";
151 if ($names)
152 {
153 $result .= implode(", ", $names);
154 }
155
156 return $result;
157 }
158
159 private function prepareLocationField(Event $event): string
160 {
161 $locationProperty = $event->getLocation();
162
163 return $locationProperty ? \CCalendar::getTextLocation($locationProperty->getActualLocation()) : '';
164 }
165}
getIcsFileContent(Event $event, array $params)
createIcsFile(Event $event, array $params)
static getTimestamp($date, $round=true, $getTime=true)
Definition util.php:55
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29