Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
outcomingeventmanager.php
1<?php
2
3
5
6
16use Bitrix\Calendar\ICal\Basic\{Dictionary, ICalUtil};
17
18IncludeModuleLangFile($_SERVER['DOCUMENT_ROOT'] . BX_ROOT . '/modules/calendar/lib/ical/mailinvitation/senderrequestinvitation.php');
19
21{
22 const ATTACHMENT_NAME = 'invite.ics';
23 const CHARSET = 'utf-8';
24 const CONTENT_TYPE = 'text/calendar';
25 const MAX_SENT = 3;
26
30 private $method;
34 private $uid;
38 private $status;
42 private $eventFields;
43 private AttendeesCollection $attendees;
47 private $receiver;
51 private $sender;
52 private $answer;
56 private $changeFields;
57 private $counterInvitations;
58
59 public static function createInstance(array $params): OutcomingEventManager
60 {
61 return new self($params);
62 }
63
64 public function __construct(array $params)
65 {
66 $this->method = $params['icalMethod'];
67 $this->eventFields = $params['arFields'];
68 $this->attendees = $params['userIndex'];
69 $this->receiver = $params['receiver'];
70 $this->sender = $params['sender'];
71 $this->changeFields = $params['changeFields'];
72 $this->counterInvitations = 0;
73 $this->answer = $params['answer'];
74 }
75
76 public function __serialize(): array
77 {
78 return [
79 'method' => $this->method,
80 'eventFields' => $this->eventFields,
81 'attendees' => $this->attendees,
82 'receiver' => $this->receiver,
83 'sender' => $this->sender,
84 'changeFields' => $this->changeFields,
85 ];
86 }
87
88 public function __unserialize(array $data): void
89 {
90 $this->method = $data['method'];
91 $this->eventFields = $data['eventFields'];
92 $this->attendees = $data['attendees'];
93 $this->receiver = $data['receiver'];
94 $this->sender = $data['sender'];
95 $this->changeFields = $data['changeFields'];
96 }
97
99 {
100 $this->checkOrganizerEmail();
101 $filesContent = $this->getRequestContent();
102 $mailEventFields = $this->getRequestMailEventFields();
103 $files = $this->getFiles();
104 $this->status = \CEvent::sendImmediate('SEND_ICAL_INVENT', SITE_ID, $mailEventFields, "Y", "", $files, '', $filesContent);
105
106 return $this;
107 }
108
110 {
111 $filesContent = $this->getReplyContent();
112 $mailEventFields = $this->getReplyMailEventFields();
113 $files = $this->getFiles();
114 $this->status = \CEvent::sendImmediate('SEND_ICAL_INVENT', SITE_ID, $mailEventFields, "Y", "", $files, '', $filesContent);
115
116 return $this;
117 }
118
120 {
121 $filesContent = $this->getCancelContent();
122 $mailEventFields = $this->getCancelMailEventFields();
123 $files = [];
124 $this->status = \CEvent::sendImmediate('SEND_ICAL_INVENT', SITE_ID, $mailEventFields, "Y", "", $files, '', $filesContent);
125
126 return $this;
127 }
128
129 public function getUId(): string
130 {
131 return $this->uid;
132 }
133
135 {
136 $this->counterInvitations++;
137 }
138
139 public function getEventId()
140 {
141 return $this->eventFields['ID'];
142 }
143
144 public function getEvent()
145 {
146 return $this->eventFields;
147 }
148
149 public function getMethod()
150 {
151 return $this->method;
152 }
153
154 public function getCountInvitations()
155 {
156 return $this->counterInvitations;
157 }
158
159 public function getStatus(): string
160 {
161 return $this->status;
162 }
163
164 public function getReceiver()
165 {
166 return $this->receiver;
167 }
168
169 private function getSenderAddress(): string
170 {
171 return $this->eventFields['MEETING']['MAIL_FROM'] ?? $this->sender['EMAIL'];
172 }
173
174 private function getReceiverAddress(): string
175 {
176 if (isset($this->receiver['MAILTO']))
177 {
178 return $this->receiver['MAILTO'];
179 }
180
181 return $this->receiver['EMAIL'];
182 }
183
184 private function getBodyMessage(): string
185 {
186 return 'ical body message';
187 }
188
189 private function getSubjectMessage(): string
190 {
191 $result = '';
192 $siteName = \COption::GetOptionString("main", "site_name", '');
193 if ($siteName !== '')
194 {
195 $result = "[".$siteName."]";
196 }
197
198 switch ($this->method)
199 {
200 case 'request':
201 $result .= ' ' . Loc::getMessage("EC_CALENDAR_ICAL_MAIL_METHOD_REQUEST");
202 break;
203 case 'edit':
204 $result .= ' ' . Loc::getMessage("EC_CALENDAR_ICAL_MAIL_METHOD_EDIT");
205 break;
206 case 'cancel':
207 $result .= ' ' . Loc::getMessage("EC_CALENDAR_ICAL_MAIL_METHOD_CANCEL");
208 break;
209 case 'reply':
210 $answer = match ($this->answer)
211 {
212 IncomingInvitationRequestHandler::MEETING_STATUS_ACCEPTED_CODE => Loc::getMessage('EC_CALENDAR_ICAL_MAIL_METHOD_REPLY_ACCEPTED'),
213 IncomingInvitationRequestHandler::MEETING_STATUS_DECLINED_CODE => Loc::getMessage('EC_CALENDAR_ICAL_MAIL_METHOD_REPLY_DECLINED'),
214 default => throw new LogicException('Calendar. Ical. With the reply method, none of the statuses matched')
215 };
216 $result .= ' ' . $answer;
217 break;
218 }
219
220 $result .= ": ".$this->eventFields['NAME'];
221
222 return $result;
223 }
224
225 private function getFiles(): array
226 {
227 return [];
228 }
229
230 private function getRequestContent(): array
231 {
232 $attachmentManager = new OutcomingAttachmentManager ($this->eventFields, $this->attendees, $this->method);
233 $attachmentManager->prepareRequestAttachment();
234 $this->uid = $attachmentManager->getUid();
235 $fileContent = Encoding::convertEncoding($attachmentManager->getAttachment(), SITE_CHARSET, "utf-8");
236 return [[
237 'CONTENT' => $fileContent,
238 'CONTENT_TYPE' => self::CONTENT_TYPE,
239 'METHOD' => Dictionary::METHODS[$this->method],
240 'CHARSET' => self::CHARSET,
241 'NAME' => self::ATTACHMENT_NAME,
242 'ID' => Helper::getUniqId(),
243 ]];
244 }
245
246 private function getRequestMailEventFields(): array
247 {
248 return [];
249 }
250
251 private function getReplyMailEventFields()
252 {
253 return [
254 'EMAIL_FROM' => $this->getSenderAddress(),
255 'EMAIL_TO' => $this->getReceiverAddress(),
256// 'EMAIL_TO' => $this->getMailtoAddress(),
257 'MESSAGE_SUBJECT' => $this->getSubjectMessage(),
258 'MESSAGE_PHP' => $this->getReplyBodyMessage(),
259 'NAME' => $this->eventFields['NAME'],
260 'ANSWER' => $this->answer,
261 'DATE_FROM' => $this->eventFields['DATE_FROM'],
262 'DATE_TO' => $this->eventFields['DATE_TO'],
263 'FULL_DAY' => $this->eventFields['SKIP_TIME'] ? 'Y' : 'N',
264 'DESCRIPTION' => str_replace("\r\n", "#$&#$&#$&", $this->eventFields['DESCRIPTION']),
265 'ATTENDEES' => $this->getAttendeesList(),
266 'ATTENDEES_LIST' => $this->getAttendeesList(),
267 'ORGANIZER' => $this->getOrganizerName(),
268 'LOCATION' => $this->eventFields['LOCATION'],
269 'FILES_LINK' =>$this->getFilesLink(),
270 'METHOD' => $this->method,
271 ];
272 }
273
274 private function getCancelMailEventFields()
275 {
276 return [
277 "=Reply-To" => $this->getOrganizerName().' <'.$this->getReceiverAddress().'>',
278 "=From" => $this->getOrganizerName().' <'.$this->getSenderAddress().'>',
279 "=Message-Id" => $this->getMessageId(),
280 "=In-Reply-To" => $this->getMessageReplyTo(),
281 'EMAIL_FROM' => $this->getSenderAddress(),
282 'EMAIL_TO' => $this->getReceiverAddress(),
283 'MESSAGE_SUBJECT' => $this->getSubjectMessage(),
284 'MESSAGE_PHP' => $this->getBodyMessage(),
285 'DATE_FROM' => $this->eventFields['DATE_FROM'],
286 'DATE_TO' => $this->eventFields['DATE_TO'],
287 'NAME' => $this->eventFields['NAME'],
288 'DESCRIPTION' => str_replace("\r\n", "#$&#$&#$&", $this->eventFields['DESCRIPTION']),
289 'ATTENDEES' => $this->getAttendeesList(),
290 'ORGANIZER' => $this->getOrganizerName(),
291 'LOCATION' => $this->eventFields['LOCATION'],
292 'FILES_LINK' =>$this->getFilesLink(),
293 'METHOD' => $this->method,
294 ];
295 }
296
297 private function getAttendeesList(): string
298 {
299 if ($this->eventFields['MEETING']['HIDE_GUESTS'])
300 {
301 return (string)Loc::getMessage('EC_CALENDAR_ICAL_MAIL_HIDE_GUESTS_INFORMATION');
302 }
303
304 $attendees = [];
305
307 foreach ($this->attendees as $attendee)
308 {
309 if (!empty($attendee->getFullName()))
310 {
311 $attendees[] = $attendee->getFullName();
312 }
313 }
314
315 return implode(", ", $attendees);
316 }
317
318 private function getOrganizerName(): string
319 {
321 $organizer = $this->eventFields['ICAL_ORGANIZER'];
322 return $organizer->getFullName() . ' (' . $organizer->getEmail() .')';
323 }
324
325 private function getFilesLink()
326 {
327 $attaches = [];
328
329 foreach ($this->eventFields['ATTACHES'] as $attach)
330 {
331 $attaches[] = '<a href="'.$attach['link'].'">'.$attach['name'].'</a><br />' ;
332 }
333
334 return implode(" ", $attaches);
335 }
336
337 private function getMessageId(): string
338 {
339 return "<CALENDAR_EVENT_".$this->eventFields['PARENT_ID']."@".$GLOBALS["SERVER_NAME"].">";
340 }
341
342 private function getMessageReplyTo(): string
343 {
344 return $this->getMessageId();
345 }
346
347 private function getReplyContent(): array
348 {
349 $attachmentManager = new OutcomingAttachmentManager ($this->eventFields, $this->attendees, $this->method);
350 $attachmentManager->prepareReplyAttachment();
351 $fileContent = Encoding::convertEncoding($attachmentManager->getAttachment(), SITE_CHARSET, "utf-8");
352 return [[
353 'CONTENT' => $fileContent,
354 'CONTENT_TYPE' => self::CONTENT_TYPE,
355 'METHOD' => Dictionary::METHODS[$this->method],
356 'CHARSET' => self::CHARSET,
357 'NAME' => self::ATTACHMENT_NAME,
358 'ID' => Helper::getUniqId(),
359 ]];
360 }
361
362 private function getReplyBodyMessage()
363 {
364 return 'reply body message';
365 }
366
367 private function getCancelContent(): array
368 {
369 $attachmentManager = new OutcomingAttachmentManager ($this->eventFields, $this->attendees, $this->method);
370 $attachmentManager->prepareCancelAttachment();
371 $fileContent = Encoding::convertEncoding($attachmentManager->getAttachment(), SITE_CHARSET, "utf-8");
372 return [[
373 'CONTENT' => $fileContent,
374 'CONTENT_TYPE' => self::CONTENT_TYPE,
375 'METHOD' => Dictionary::METHODS[$this->method],
376 'CHARSET' => self::CHARSET,
377 'NAME' => self::ATTACHMENT_NAME,
378 'ID' => ICalUtil::getUniqId(),
379 ]];
380 }
381
382 private function getDateForTemplate()
383 {
384 $res = Util::getIcalTemplateDate([
385 'DATE_FROM' => $this->eventFields['DATE_FROM'],
386 'DATE_TO' => $this->eventFields['DATE_TO'],
387 'TZ_FROM' => $this->eventFields['TZ_FROM'],
388 'TZ_TO' => $this->eventFields['TZ_TO'],
389 'FULL_DAY' => $this->eventFields['SKIP_TIME'],
390 ]);
391 $offset = (Util::getDateObject(null, false, $this->eventFields['TZ_FROM']))->format('P');
392 $res .= ' (' . $this->eventFields['TZ_FROM'] . ', ' . 'UTC' . $offset . ')';
393
394 if (isset($this->eventFields['RRULE']['FREQ']) && $this->eventFields['RRULE']['FREQ'] !== 'NONE')
395 {
396 $rruleString = Util::getIcalTemplateRRule($this->eventFields['RRULE'],
397 [
398 'DATE_FROM' => $this->eventFields['DATE_FROM'],
399 'DATE_TO' => $this->eventFields['DATE_TO'],
400 'TZ_FROM' => $this->eventFields['TZ_FROM'],
401 'TZ_TO' => $this->eventFields['TZ_TO'],
402 'FULL_DAY' => $this->eventFields['SKIP_TIME'],
403 ]
404 );
405 $res .= ', (' . $rruleString . ')';
406 }
407
408 return $res;
409 }
410
411 private function getEditTitle()
412 {
413 if ($this->method !== 'edit')
414 {
415 return null;
416 }
417
418 if (count($this->changeFields) === 1)
419 {
420 switch ($this->changeFields[0]['fieldKey'])
421 {
422 case 'DATE_FROM':
423 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_CHANGE_FIELD_TITLE_DATE');
424 case 'LOCATION':
425 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_CHANGE_FIELD_TITLE_LOCATION');
426 case 'ATTENDEES':
427 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_CHANGE_FIELD_TITLE_ATTENDEES');
428 case 'RRULE':
429 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_CHANGE_FIELD_TITLE_RRULE');
430 case 'NAME':
431 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_CHANGE_FIELD_TITLE_NAME');
432 default:
433 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_METHOD_EDIT');
434 }
435 }
436
437 return Loc::getMessage('EC_CALENDAR_ICAL_MAIL_METHOD_EDIT');
438 }
439
440 private function getChangeFieldsString()
441 {
442 $res = [];
443 if (!empty($this->changeFields))
444 {
445 foreach ($this->changeFields as $changeField)
446 {
447 $res[] = $changeField['fieldKey'];
448 }
449 }
450 return implode(';', $res);
451 }
452
453 private function checkOrganizerEmail()
454 {
455 if (Loader::includeModule('mail'))
456 {
457 if (empty($this->sender['EMAIL']))
458 {
459 $boxes = \Bitrix\Mail\MailboxTable::getUserMailboxes($this->eventFields['MEETING_HOST']);
460 $email = array_shift($boxes)['EMAIL'];
461 $this->sender['EMAIL'] = $email;
462 $this->attendees[$this->eventFields['MEETING_HOST']]['EMAIL'] = $email;
463 }
464 }
465 }
466
467}
static getDateObject(string $date=null, ?bool $fullDay=true, ?string $tz='UTC')
Definition util.php:102
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
$GLOBALS['____1444769544']
Definition license.php:1