Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
incominginvitationreplyhandler.php
1<?php
2
3
5
6
17use CCalendarEvent;
18
20{
21 public const CONTENT_TYPES = ['application/ics', 'text/calendar'];
25 private $component;
29 private $handleStatus = false;
30
35 public static function fromComponent(Calendar $component): IncomingInvitationReplyHandler
36 {
37 $handler = new self();
38 $handler->component = $component;
39 return $handler;
40 }
41
46 {
47 return new self();
48 }
49
54 public function __construct()
55 {
56 }
57
65 {
66 $localEvent = Helper::getEventByUId($this->component->getEvent()->getUid());
67 if (!is_null($localEvent))
68 {
69 $user = Helper::getUserById((int)$localEvent['OWNER_ID']);
70 if (
71 $user
72 && $user['EMAIL']
73 && !is_null($attendeeStatus = $this->getAttendeeStatus($user['EMAIL']))
74 )
75 {
76 $this->sendNotificationGuestReaction($localEvent, $attendeeStatus);
77 $this->handleStatus = true;
78 }
79 }
80
81 return $this;
82 }
83
88 private function getAttendeeStatus(string $userEmail): ?string
89 {
90 $attendees = $this->component->getEvent()->getAttendees();
91 if (is_iterable($attendees))
92 {
93 foreach ($attendees as $attendee)
94 {
95 if ($attendee->getParameterValueByName('email') === $userEmail
96 || $this->getMailTo($attendee->getValue()) === $userEmail)
97 {
98 $attendeeStatus = $attendee->getParameterValueByName('partstat');
99 if(array_key_exists($attendeeStatus,Dictionary::ATTENDEES_STATUS))
100 {
101 return Dictionary::ATTENDEES_STATUS[$attendeeStatus];
102 }
103 }
104 }
105 }
106
107 return null;
108 }
109
114 public function sendNotificationGuestReaction(array $event, string $attendeeStatus): void
115 {
116 CCalendarEvent::SetMeetingStatusEx([
117 'attendeeId' => $event['OWNER_ID'],
118 'eventId' => $event['ID'],
119 'status' => $attendeeStatus,
120 'personalNotification' => $event['MEETING_HOST'],
121 ]);
122 }
123
127 public function isSuccess(): bool
128 {
129 return $this->handleStatus;
130 }
131
140 public static function handleFromRequest(Event $event): bool
141 {
142 $attachments = $event->getParameter('attachments');
143 if (is_array($attachments))
144 {
145 foreach($attachments as $file)
146 {
147 if (in_array($file['type'], self::CONTENT_TYPES, true))
148 {
149 try
150 {
151 $fileObject = new File($file['tmp_name'], $event->getParameter('site_id'));
152 $fileContent = Encoding::convertEncoding($fileObject->getContents(), SenderInvitation::CHARSET, SITE_CHARSET);
153 }
154 catch (FileNotFoundException $e)
155 {
156 AddMessage2Log('File ics not found', 'calendar', 2);
157 die();
158 }
159
160 $icalComponent = InboxManager::createInstance($fileContent)
161 ->parseContent()
162 ->getComponent();
163
164 if ($icalComponent->getMethod() === Dictionary::METHOD['reply'])
165 {
166 return self::fromComponent($icalComponent)
167 ->handle()
168 ->isSuccess();
169 }
170 }
171 }
172 }
173
174 return false;
175 }
176}