1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
eventbuilder.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sync\Icloud;
4
5use Bitrix\Calendar\Core\Base\DateTimeZone;
6use Bitrix\Calendar\Core\Base\SingletonTrait;
7use Bitrix\Calendar\Core\Event\Event;
8use Bitrix\Calendar\Core\Event\Properties\Remind;
9use Bitrix\Calendar\ICal\IcsBuilder;
10use Bitrix\Calendar\Rooms;
11use Bitrix\Calendar\Sync\Util\EventDescription;
12use Bitrix\Calendar\Util;
13use Bitrix\Main\Loader;
14use Bitrix\Main\SystemException;
15
17{
18 use SingletonTrait;
19
26 public function getContent(Event $event, ?array $data = null): ?array
27 {
28 if (!Loader::includeModule('dav'))
29 {
30 return null;
31 }
32
33 $content = [
34 'TYPE' => 'VEVENT',
35 'CREATED' => date('Ymd\\THis\\Z', $event->getDateCreate()->getTimestamp()),
36 'LAST-MODIFIED' => date('Ymd\\THis\\Z', $event->getDateModified()->getTimestamp()),
37 'DTSTAMP' => date('Ymd\\THis\\Z', $event->getDateModified()->getTimestamp()),
38 'UID' => $event->getUid(),
39 'SUMMARY' => $event->getName(),
40 ];
41
42 if ($event->isFullDayEvent())
43 {
44 $content['DTSTART'] = [
45 'VALUE' => $event->getStart()->format('Ymd'),
46 'PARAMETERS' => ['VALUE' => 'DATE'],
47 ];
48 $content['DTEND'] = [
49 'VALUE' => $event->getEnd()->add('1 day')->format('Ymd'),
50 'PARAMETERS' => ['VALUE' => 'DATE'],
51 ];
52 }
53 else
54 {
55 $content['DTSTART'] = [
56 'VALUE' => $event->getStart()->format('Ymd\\THis'),
57 'PARAMETERS' => ['TZID' => $this->prepareTimeZone($event->getStartTimeZone())],
58 ];
59 $content['DTEND'] = [
60 'VALUE' => $event->getEnd()->format('Ymd\\THis'),
61 'PARAMETERS' => ['TZID' => $this->prepareTimeZone($event->getEndTimeZone())],
62 ];
63 }
64
65 if ($event->getOriginalDateFrom())
66 {
67 if ($event->isFullDayEvent())
68 {
69 $content['RECURRENCE-ID'] = [
70 'VALUE' => $event->getOriginalDateFrom()->format('Ymd'),
71 'PARAMETERS' => ['VALUE' => 'DATE'],
72 ];
73 }
74 else
75 {
76 $content['RECURRENCE-ID'] = [
77 'VALUE' => $event->getOriginalDateFrom()->format('Ymd\\THis'),
78 'PARAMETERS' => ['TZID' => $this->prepareTimeZone($event->getStartTimeZone())],
79 ];
80 }
81 }
82
83 if ($event->getAccessibility() === 'free')
84 {
85 $content['TRANSP'] = 'TRANSPARENT';
86 }
87 else
88 {
89 $content['TRANSP'] = 'OPAQUE';
90 }
91
92 if ($event->getLocation() && $event->getLocation()->getActualLocation())
93 {
94 $content['LOCATION'] = Rooms\Util::getTextLocation($event->getLocation()->getActualLocation());
95 }
96
97 $importance = $event->getImportance();
98 if ($importance === 'low')
99 {
100 $content['PRIORITY'] = 9;
101 }
102 else if ($importance === 'high')
103 {
104 $content['PRIORITY'] = 1;
105 }
106 else
107 {
108 $content['PRIORITY'] = 5;
109 }
110
111 $content['DESCRIPTION'] = $this->prepareDescription($event);
112 if (!$content['DESCRIPTION'])
113 {
114 unset($content['DESCRIPTION']);
115 }
116
117 if ($event->getRemindCollection() && $event->getRemindCollection()->getCollection())
118 {
119 $content['@VALARM'] = $this->prepareReminders($event);
120 }
121
122 if ($event->isRecurrence())
123 {
124 $content['RRULE'] = IcsBuilder::prepareRecurrenceRule($event->getRecurringRule(), $event->getStartTimeZone());
125 }
126
127 $content['SEQUENCE'] = $event->getVersion();
128
129 if ($event->getExcludedDateCollection() && $event->isRecurrence())
130 {
131 $content['EXDATE'] = $this->prepareExcludedDates($event);
132 }
133
134 $this->prepareOuterParams($data, $content);
135
136 return $content;
137 }
138
144 private function prepareDescription(Event $event): string
145 {
146 return (new EventDescription())->prepareForExport($event);
147 }
148
156 private function prepareReminderValue(
157 Remind $remind,
158 bool $isFullDay
159 ): array
160 {
161 $valueType = '';
162 $value = '';
163
164 if ($remind->getUnits() === 'minutes')
165 {
166 $valueType = 'DURATION';
167 if ($remind->getTime() === 60 || $remind->getTime() === 120)
168 {
169 $value = '-PT' . $remind->getTime() / 60 . 'H';
170 }
171 else if ($remind->getTime() === 0)
172 {
173 $value = 'PT' . $remind->getTime() . 'S';
174 }
175 else
176 {
177 $value = '-PT' . $remind->getTime() . 'M';
178 }
179 }
180 else if ($remind->getSpecificTime() && $remind->getDaysBefore() !== null)
181 {
182 $valueType = 'DURATION';
183 $diff = $remind->getTimeBeforeStartInMinutes();
184 $parsedDiff = Util::minutesToDayHoursMinutes(abs($diff));
185 if ($isFullDay && $remind->getDaysBefore() === 0)
186 {
187 $value = 'PT' . $parsedDiff['hours'] . 'H';
188 }
189 else if (
190 ($remind->getDaysBefore() === 0 && !$isFullDay && $diff > 0)
191 || ($remind->getDaysBefore() === 1 && $parsedDiff['days'] === 0)
192 )
193 {
194 $hours = '';
195 $minutes = '';
196 if ($parsedDiff['hours'])
197 {
198 $hours = $parsedDiff['hours'] . 'H';
199 }
200 if ($parsedDiff['minutes'])
201 {
202 $minutes = $parsedDiff['minutes'] . 'M';
203 }
204 $value = '-PT' . $hours . $minutes;
205 }
206 else if ($parsedDiff['days'] > 0)
207 {
208 $hours = '';
209 $minutes = '';
210 $value = '-P' . $parsedDiff['days'] . 'D';
211 if ($parsedDiff['hours'])
212 {
213 $hours = $parsedDiff['hours'] . 'H';
214 }
215 if ($parsedDiff['minutes'])
216 {
217 $minutes = $parsedDiff['minutes'] . 'M';
218 }
219 if ($hours || $minutes)
220 {
221 $value .= 'T' . $hours . $minutes;
222 }
223 }
224 else
225 {
226 return [null, null];
227 }
228 }
229 else if ($remind->getSpecificTime())
230 {
231 $valueType = 'DATE-TIME';
232 $value = date('Ymd\\THis\\Z', $remind->getSpecificTime()->getTimestamp());
233 }
234
235 return [$value, $valueType];
236 }
237
243 private function prepareTimeZone(?DateTimeZone $timeZone): string
244 {
245 if ($timeZone)
246 {
247 return $timeZone->getTimeZone()->getName();
248 }
249
250 return 'UTC';
251 }
252
259 private function prepareReminders(Event $event): array
260 {
261 $result = [];
263 foreach ($event->getRemindCollection()->getCollection() as $remind)
264 {
265 [$value, $valueType] = $this->prepareReminderValue(
266 $remind,
267 $event->isFullDayEvent()
268 );
269
270 if (!$value || !$valueType)
271 {
272 continue;
273 }
274
276 $result[] = [
277 'X-WR-ALARMUID' => $uuId,
278 'UID' => $uuId,
279 'TYPE' => 'VALARM',
280 'ACTION' => 'DISPLAY',
281 'TRIGGER' => [
282 'PARAMETERS' => ['VALUE' => $valueType],
283 'VALUE' => $value,
284 ],
285 ];
286 }
287
288 return $result;
289 }
290
296 private function prepareExcludedDates(Event $event): array
297 {
298 $result = [];
299 $exDate = $event->getExcludedDateCollection()->getCollection();
300 foreach ($exDate as $date)
301 {
302 $fields = $date->getFields();
303 if ($event->isFullDayEvent())
304 {
305 $result[] = [
306 'VALUE' => date('Ymd', MakeTimeStamp($fields['date'])),
307 'PARAMETERS' => ['VALUE' => 'DATE'],
308 ];
309 }
310 else
311 {
312 $result[] = [
313 'VALUE' => date('Ymd', MakeTimeStamp($fields['date']))
314 . 'T' . $event->getStart()->format('His'),
315 'PARAMETERS' => ['TZID' => $this->prepareTimeZone($event->getStartTimeZone())],
316 ];
317 }
318 }
319
320 return $result;
321 }
322
329 private function prepareOuterParams(?array $data, array &$content): void
330 {
331 if (!$data)
332 {
333 return;
334 }
335
336 if ($data['ATTENDEE'])
337 {
338 foreach ($data['ATTENDEE'] as $attendee)
339 {
340 $value = $attendee['VALUE'];
341 unset($attendee['VALUE']);
342
343 $content['ATTENDEE'][] = [
344 'PARAMETERS' => $attendee,
345 'VALUE' => $value,
346 ];
347 }
348 }
349
350 if ($data['ATTACH'])
351 {
352 foreach ($data['ATTACH'] as $attachment)
353 {
354 $value = $attachment['VALUE'];
355 unset($attachment['VALUE']);
356
357 $content['ATTACH'][] = [
358 'PARAMETERS' => $attachment,
359 'VALUE' => $value,
360 ];
361 }
362 }
363
364 if ($data['ORGANIZER'])
365 {
366 $value = $data['ORGANIZER']['VALUE'];
367 unset($data['ORGANIZER']['VALUE']);
368
369 $content['ORGANIZER'] = [
370 'PARAMETERS' => $data['ORGANIZER'],
371 'VALUE' => $value,
372 ];
373 }
374
375 if ($data['URL'])
376 {
377 $content['URL'] = $data['URL'];
378 }
379 }
380}
getContent(Event $event, ?array $data=null)
Определения eventbuilder.php:26
static minutesToDayHoursMinutes(int $minutes)
Определения util.php:711
$content
Определения commerceml.php:144
$hours
Определения cron_html_pages.php:15
$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
$result
Определения get_property_values.php:14
MakeTimeStamp($datetime, $format=false)
Определения tools.php:538
$value
Определения Param.php:39
$event
Определения prolog_after.php:141
$fields
Определения yandex_run.php:501