Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
incomingeventmanager.php
1<?php
2
3
5
6
20use Bitrix\Tasks\Integration\Disk;
21use Bitrix\Calendar\ICal\Basic\{Dictionary, ICalUtil};
23
24
26{
27 public static function handleRequest($params)
28 {
29 $event = $params['event'];
30 $userId = $params['userId'];
31 $answer = $params['answer'];
32 $userEmail = $params['emailTo'];
33 $organizerEmail = $event['ORGANIZER_MAIL']['EMAIL'];
34
35 $event = static::updateEvent($event, $userId, $answer);
36 $attendees = static::handleAttendeesFromRequest([
37 'attendees' => $event['ATTENDEES_MAIL'],
38 'answer' => $answer,
39 'userId' => $userId,
40 'userEmail' => $userEmail,
41 'organizerEmail' => $organizerEmail
42 ]);
43
44 $parentId = \CCalendar::SaveEvent([
45 'arFields' => static::updateParentEvent($event),
46 ]);
47
48 \CCalendar::SaveEvent([
49 'arFields' => static::updateChildEvent($event, $parentId),
50 ]);
51
52
54 'icalMethod' => 'reply',
55 'arFields' => $event,
56 'userIndex' => $attendees,
57 'receiver' => $event['ORGANIZER_MAIL'],
58 'sender' => $attendees[$event['OWNER_ID']],
59 ])->replyInvitation()->getStatus();
60 }
61
62 public static function handleReply(array $params): bool
63 {
64 $uid = $params['event']['DAV_XML_ID'];
65 $emailUser = $params['event']['ATTENDEES_MAIL'][0];
66 $userId = ICalUtil::getUserIdByEmail($emailUser);
67 $localEvent = ICalUtil::getEventByUId($userId, $uid);
68
69 if (!empty($localEvent))
70 {
71 $status = $params['event']['ATTENDEES_MAIL'][0]['STATUS'];
72
73 \CCalendarEvent::SetMeetingStatusEx([
74 'attendeeId' => $localEvent['OWNER_ID'],
75 'eventId' => $localEvent['ID'],
76 'status' => Dictionary::LOCAL_ATTENDEES_STATUS[$status],
77 'personalNotification' => $localEvent['MEETING_HOST'],
78 ]);
79 }
80
81 return true;
82 }
83
84 public static function handleReplyReceivedICalInvent(\Bitrix\Main\Event $event)
85 {
86 $attachments = $event->getParameter('attachments');
87
88 if (is_array($attachments))
89 {
90 foreach($attachments as $key => $file)
91 {
92 if ($file['type'] === OutcomingEventManager::CONTENT_TYPE)
93 {
94 $fileObject = new \Bitrix\Main\IO\File($file['tmp_name'], $event->getParameter('site_id'));
95 try
96 {
97 $fileContent = $fileObject->getContents();
98 }
99 catch (FileNotFoundException $e)
100 {
101 die();
102 }
103 $fileContent = Encoding::convertEncoding($fileContent, OutcomingEventManager::CHARSET, SITE_CHARSET);
104 $icalComponent = static::getDataInfo($fileContent);
105
106 if ($method === Dictionary::METHODS['reply'])
107 {
108 static::handleReply(['event' => $icalEvent]);
109 }
110 }
111 }
112 }
113 }
114
115 public static function handleCancel($params)
116 {
117 $event = $params['event'];
118 $userId = $params['userId'];
119
120 $originalValue = ICalUtil::getEventByUId($userId, $event['DAV_XML_ID']);
121 if (!empty($originalValue))
122 {
123 $deleteParams = [
124 'sendNotification' => true,
125 'checkPermissions' => false,
126 ];
127
128 \CCalendar::DeleteEvent($originalValue['ID'], true, $deleteParams);
129 }
130 }
131
132 public static function getDataInfo($data, $params = []): array
133 {
134
135 $attachmentManager = IncomingAttachmentManager::getInstance([
136 'data' => $data,
137 ]);
138 $event = $attachmentManager->prepareEventAttachment()->getEvent();
139 $method = $attachmentManager->getMethod();
140
141 return [$event, $method];
142 }
143
144 public static function rehandleRequest($params)
145 {
146 $params['event']['SKIP_TIME'] = $params['event']['DT_SKIP_TIME'] === 'Y';
147
148 $attendees = static::handleAttendeesByUser(
149 ICalUtil::getUsersByCode($params['event']['ATTENDEES_CODES']),
150 $params['answer'],
151 $params['userId']
152 );
153
154 $mailBoxQuery = MessageAccessTable::query()
155 ->setSelect(['EMAIL' => 'MAILBOX.EMAIL'])
156 ->registerRuntimeField(
157 'MAILBOX',
158 new Reference(
159 'MAILBOX',
160 MailboxTable::class,
161 Join::on('this.MAILBOX_ID', 'ref.ID'),
162 ['join_type' => Join::TYPE_INNER]
163 )
164 )
166 ->where('ENTITY_ID', $params['event']['ID'])
167 ;
168
169 $mailbox = $mailBoxQuery->fetch();
170 if (!$mailbox)
171 {
172 return;
173 }
174
175 $params['event']['ORGANIZER_MAIL'] = $attendees[$params['event']['MEETING_HOST']];
176 $params['event']['ORGANIZER_MAIL']['MAILTO'] = $params['event']['MEETING']['MAILTO'];
177 $params['event']['ATTENDEES_MAIL'] = $attendees[$params['event']['OWNER_ID']];
178 $params['event']['ICAL_ORGANIZER'] = (Helper::getAttendee($params['event']['MEETING_HOST'], $params['event']['PARENT_ID'], false))
179 ->setStatus(null)
180 ->setRole(null)
181 ->setRsvp(false)
182 ->setCutype(null)
183 ;
184
185 $answer = isset($params['answer']) && $params['answer']
186 ? IncomingInvitationRequestHandler::MEETING_STATUS_ACCEPTED_CODE
187 : IncomingInvitationRequestHandler::MEETING_STATUS_DECLINED_CODE
188 ;
189 $attendee = Helper::getAttendee($params['event']['OWNER_ID'], $params['event']['PARENT_ID'], false);
190 $status = Dictionary::OUT_ATTENDEES_STATUS[$answer];
191 $attendee->setStatus($status);
192 $attendee->setEmail($mailbox['EMAIL'])->setMailto($mailbox['EMAIL']);
193 $attendeesCollection = new AttendeesCollection([$attendee]);
194
196 'icalMethod' => 'reply',
197 'arFields' => $params['event'],
198 'userIndex' => $attendeesCollection,
199 'receiver' => $params['event']['ORGANIZER_MAIL'],
200 'sender' => $attendees[$params['event']['OWNER_ID']],
201 'answer' => $answer,
202 ])->replyInvitation()->getStatus();
203 }
204
205 private static function updateEvent(array $event, $userId, $answer): array
206 {
207 if (isset($event['DATE_FROM']))
208 {
209 $event['DATE_FROM_MAIL'] = $event['SKIP_TIME']
210 ? static::getDateString($event['DATE_FROM'])
211 : static::getDateTimeString($event['DATE_FROM']);
212 $event['DATE_FROM'] = static::getDateTimeString($event['DATE_FROM']);
213 }
214
215 if (isset($event['DATE_TO']))
216 {
217 $event['DATE_TO'] = static::getDateTimeString($event['DATE_TO']);
218 }
219
220 if (isset($event['DATE_CREATE']))
221 {
222 $event['DATE_CREATE'] = static::getDateTimeString($event['DATE_CREATE']);
223 }
224
225 if (isset($event['TIMESTAMP_X']))
226 {
227 $event['TIMESTAMP_X'] = static::getDateTimeString($event['TIMESTAMP_X']);
228 }
229
230 if (isset($event['DT_STAMP']))
231 {
232 $event['DT_STAMP'] = static::getDateTimeString($event['DT_STAMP']);
233 }
234
235 if (isset($event['TZ_FROM']))
236 {
237 $event['TZ_FROM'] = static::getTimezoneNameString($event['TZ_FROM']);
238 }
239
240 if (isset($event['TZ_TO']))
241 {
242 $event['TZ_TO'] = static::getTimezoneNameString($event['TZ_TO']);
243 }
244
245 $event['OWNER_ID'] = $userId;
246 $event['MEETING_HOST'] = ICalUtil::getUserIdByEmail($event['ORGANIZER_MAIL']);
247 $event['IS_MEETING'] = 1;
248 $event['SECTION_CAL_TYPE'] = 'user';
249 $event['ATTENDEES_CODES'] = ['U'.$event['OWNER_ID'], 'U'.$event['MEETING_HOST']];
250 $event['MEETING_STATUS'] = $answer === 'confirmed' ? 'Y' : 'N';
251
252 if (empty($event['ACCESSIBILITY']))
253 {
254 $event['ACCESSIBILITY'] = 'free';
255 }
256
257 if (empty($event['IMPORTANCE']))
258 {
259 $event['IMPORTANCE'] = 'normal';
260 }
261
262 if (empty($event['STATUS']))
263 {
264 $event['STATUS'] = $answer === 'ACCEPT' ? 'CONFIRMED' : 'CANCELLED';
265 }
266
267 if (empty($event['REMIND']))
268 {
269 $event['REMIND'] = [
270 'type' => 'min',
271 'count' => '15'
272 ];
273 }
274
275 if (empty($event['MEETING']))
276 {
277 $event['MEETING'] = [
278 'HOST_NAME' => $event['ORGANIZER_MAIL']['name'],
279 'NOTIFY' => 1,
280 'REINVITE' => 0,
281 'ALLOW_INVITE' => 0,
282 'MEETING_CREATOR' => $event['MEETING_HOST'],
283 'EXTERNAL_TYPE' => 'mail',
284 ];
285 }
286
287 $originalValue = ICalUtil::getEventByUId($userId, $event['DAV_XML_ID']);
288 $event['ID'] = $originalValue ? $originalValue['ID'] : 0;
289
290 return $event;
291 }
292
293 private static function getDateTimeString(Date $date): string
294 {
295 return $date->format(Date::convertFormatToPhp(FORMAT_DATETIME));
296 }
297
298 private static function getDateString(Date $date): string
299 {
300 return $date->format('d.m.Y');
301 }
302
303 private static function getTimezoneNameString(\DateTimeZone $tz)
304 {
305 return $tz->getName();
306 }
307
308 private static function handleAttendeesFromRequest(array $params): array
309 {
310 $result = [];
311
312 foreach ($params['attendees'] as $attendee)
313 {
314 if ($attendee['EMAIL'] == $params['userEmail'])
315 {
316 $usersInfo = ICalUtil::getIndexUsersById([$params['userId']]);
317 $attendee['NAME'] = $usersInfo[$params['userId']]['NAME'];
318 $attendee['LAST_NAME'] = $usersInfo[$params['userId']]['LAST_NAME'];
319 $attendee['STATUS'] = $params['answer'] === 'confirmed' ? 'accepted' : 'declined';
320 $result[$params['userId']] = $attendee;
321 }
322
323 if ($attendee['EMAIL'] == $params['organizerEmail'])
324 {
325 $id = ICalUtil::getUserIdByEmail($attendee);
326 $result[$id] = $attendee;
327 }
328 }
329
330 return $result;
331 }
332
333 private static function handleAttendeesByUser(array $attendees, $answer, $userId)
334 {
335 $result = [];
336 foreach ($attendees as $attendee)
337 {
338 if ($attendee['ID'] == $userId)
339 {
340 $attendee['STATUS'] = $answer ? 'accepted' : 'declined';
341 }
342
343 $result[$attendee['ID']] = $attendee;
344 }
345
346 return $result;
347 }
348
349 private static function updateChildEvent(array $event, $parentId)
350 {
351 $event['PARENT_ID'] = $parentId;
352 $event['MEETING']['MAILTO'] = $event['ORGANIZER_MAIL']['MAILTO'];
353 $event['DESCRIPTION'] .= "\r\n"
354 . Loc::getMessage('EC_ORGANIZER_NAME_TITLE') . ': ' . static::getOrganizerString($event['ORGANIZER_MAIL']) . "\r\n"
355 . Loc::getMessage('EC_ATTENDEES_LIST_TITLE') . ': ' . static::getAttendeesString($event['ATTENDEES_MAIL']);
356 if (!empty($event['ATTACHMENT_LINK']))
357 {
358 $event['DESCRIPTION'] .= "\r\n"
359 . Loc::getMessage('EC_FILES_TITLE') . ': ' . static::getFilesString($event['ATTACHMENT_LINK']);
360 }
361 return $event;
362 }
363
364 private static function getAttendeesString($attendees)
365 {
366 $res = [];
367 foreach ($attendees as $attendee)
368 {
369 $res[] = $attendee['NAME'] . ' ' . $attendee['LAST_NAME'] . ' ('. $attendee['EMAIL'] .')';
370 }
371
372 return implode(', ', $res);
373 }
374
375 private static function getOrganizerString($organizer)
376 {
377 return $organizer['NAME'] . ' ' . $organizer['LAST_NAME'] . ' ('. $organizer['EMAIL'] .')';
378 }
379
380 private static function updateParentEvent(array $event)
381 {
382 $event['OWNER_ID'] = $event['MEETING_HOST'];
383 unset($event['DAV_XML_ID']);
384
385 return $event;
386 }
387
388 private static function getFilesString(array $attachments)
389 {
390 $res = [];
391 foreach ($attachments as $attachment)
392 {
393 $res[] = $attachment['filename'] . ' (' . $attachment['link'] . ')';
394 }
395 return implode(', ', $res);
396 }
397}
static handleReplyReceivedICalInvent(\Bitrix\Main\Event $event)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29