Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mail.php
1<?php
3
9
10class Mail extends Service
11{
12 public const MEETING_STATUS_CREATED = 'created';
13 public const MEETING_STATUS_CANCELLED = 'cancelled';
14
19 public function notifyAboutMeetingStatus(string $to): bool
20 {
21 if ($this->doSendMeetingCancelled())
22 {
23 Sharing\SharingEventManager::setCanceledTimeOnSharedLink($this->event->getId());
24 Sharing\SharingEventManager::reSaveEventWithoutAttendeesExceptHostAndSharingLinkOwner($this->eventLink);
25 return $this->notifyAboutMeetingCancelled($to);
26 }
27
28 return false;
29 }
30
31 protected function doSendMeetingCancelled(): bool
32 {
33 $ownerStatus = $this->getOwner()['STATUS'];
34 return $ownerStatus === 'N';
35 }
36
37 public function notifyAboutMeetingCreated(string $to): bool
38 {
39 Sharing\Helper::setSiteLanguage();
40
41 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CREATED');
42 $ownerName = $this->getOwner()['NAME'];
43 $mailParams = $this->getBaseMailParams($ownerName);
44 $arParams = [
45 'STATUS' => self::MEETING_STATUS_CREATED,
46 'CANCEL_LINK' => $this->eventLink->getUrl() . Sharing\Helper::ACTION_CANCEL,
47 'ICS_LINK' => $this->eventLink->getUrl() . Sharing\Helper::ACTION_ICS,
48 'VIDEOCONFERENCE_LINK' => $this->eventLink->getUrl() . Sharing\Helper::ACTION_CONFERENCE,
49 ];
50 $arParams = array_merge($arParams, $mailParams);
51
52 $files = null;
53 if ($icsFileId = $this->getIcsFileId($ownerName, $arParams['EVENT_NAME']))
54 {
55 $files = [$icsFileId];
56 }
57
58 return $this->sendMessage($to, $arParams, $subject, $files);
59 }
60
61 public function notifyAboutMeetingCancelled(string $to): bool
62 {
63 Sharing\Helper::setSiteLanguage();
64
65 $ownerName = $this->getOwner()['NAME'];
66
67 $gender = $this->getOwner()['GENDER'];
68 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CANCELLED', ['#NAME#' => $ownerName]);
69 if ($gender === 'M')
70 {
71 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CANCELLED_M', ['#NAME#' => $ownerName]);
72 }
73 if ($gender === 'F')
74 {
75 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CANCELLED_F', ['#NAME#' => $ownerName]);
76 }
77
78 $mailParams = $this->getBaseMailParams($ownerName);
79
80 $arParams = [
81 'STATUS' => self::MEETING_STATUS_CANCELLED,
82 'CALENDAR_LINK' => $this->getCalendarLink(),
83 'WHO_CANCELLED' => $ownerName,
84 'WHEN_CANCELLED' => $this->getWhenCancelled(),
85 ];
86 $arParams = array_merge($arParams, $mailParams);
87
88 return $this->sendMessage($to, $arParams, $subject);
89 }
90
91 public function notifyAboutSharingEventEdit(string $to): bool
92 {
93 //TODO waiting for mail template
94 Sharing\Helper::setSiteLanguage();
95 $ownerName = $this->getOwner()['NAME'];
96
97 $gender = $this->getOwner()['GENDER'];
98 //TODO add REAL phrases
99 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CANCELLED', ['#NAME#' => $ownerName]);
100 if ($gender === 'M')
101 {
102 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CANCELLED_M', ['#NAME#' => $ownerName]);
103 }
104 else if ($gender === 'F')
105 {
106 $subject = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_SUBJECT_CANCELLED_F', ['#NAME#' => $ownerName]);
107 }
108 $mailParams = $this->getBaseMailParams($ownerName);
109 $arParams = [
110 'STATUS' => self::MEETING_STATUS_CANCELLED,
111 'CALENDAR_LINK' => $this->getCalendarLink(),
112 'WHO_EDITED' => $ownerName,
113// 'WHEN_EDITED' => $this->getWhenCancelled(),
114 ];
115 $arParams = array_merge($arParams, $mailParams);
116
117 //TODO uncomment
118 return true;
119// return $this->sendMessage($to, $arParams, $subject);
120 }
121
122 protected function sendMessage(string $to, array $arParams, string $subject, ?array $files = null): bool
123 {
124 $fields = [
125 'EMAIL_TO' => $to,
126 'SUBJECT' => $subject,
127 ];
128
129 if (\CCalendar::IsBitrix24())
130 {
131 $fields['DEFAULT_EMAIL_FROM'] = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_BITRIX24_FROM');
132 }
133
134 return \CEvent::SendImmediate(
135 'CALENDAR_SHARING',
136 SITE_ID,
137 array_merge($fields, $arParams),
138 'Y',
139 '',
140 $files,
141 ) === 'Y';
142 }
143
144 protected function getBaseMailParams(string $ownerName): array
145 {
146 $arParams = [
147 'EVENT_NAME' => Sharing\SharingEventManager::getSharingEventNameByUserName($ownerName),
148 'EVENT_DATE' => $this->getFormattedEventDateFirstLine(),
149 'EVENT_TIME' => $this->getFormattedEventDateSecondLine(),
150 'TIMEZONE' => $this->getEventTimezone(),
151 'CALENDAR_MONTH_NAME' => $this->getCalendarMonthName(),
152 'CALENDAR_DAY' => $this->getCalendarDay(),
153 'CALENDAR_TIME' => $this->getCalendarTime(),
154 'ABUSE_LINK' => $this->getAbuseLink(),
155 'BITRIX24_LINK' => $this->getBitrix24Link(),
156 ];
157
158 $parentLink = $this->getParentLink();
159 if (!is_null($parentLink))
160 {
161 $arParams['AVATARS'] = [];
162 foreach ($parentLink->getMembers() as $member)
163 {
164 $arParams['AVATARS'][] = $member->getAvatar();
165 }
166 }
167
168 return $arParams;
169 }
170
171 protected function getParentLink(): ?Sharing\Link\Joint\JointLink
172 {
173 $link = Sharing\Link\Factory::getInstance()->getLinkByHash($this->eventLink->getParentLinkHash());
174 if ($link instanceof Sharing\Link\Joint\JointLink)
175 {
176 return $link;
177 }
178
179 return null;
180 }
181
182 protected function getFormattedEventDateFirstLine(): string
183 {
184 $timestampUTCWithServerOffset = Sharing\Helper::getUserDateTimestamp($this->event->getStart());
185 $culture = Main\Application::getInstance()->getContext()->getCulture();
186 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
187 $dayMonth = FormatDate($dayMonthFormat, $timestampUTCWithServerOffset);
188 if ($this->isStartAndEndInDifferentDays())
189 {
190 $timeStart = $this->formatTime($this->event->getStart());
191 $result = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_EVENT_START', [
192 '#DATE#' => "$dayMonth $timeStart"
193 ]);
194 }
195 else
196 {
197 $weekDayFormat = 'l';
198 $weekDay = mb_strtolower(FormatDate($weekDayFormat, $timestampUTCWithServerOffset));
199 $result = "$dayMonth, $weekDay";
200 }
201
202 return $result;
203 }
204
205 protected function isStartAndEndInDifferentDays(): bool
206 {
207 $culture = Main\Application::getInstance()->getContext()->getCulture();
208 $shortDateFormat = Main\Type\Date::convertFormatToPhp($culture->get('SHORT_DATE_FORMAT'));
209
210 $startTimestampUTCWithServerOffset = Sharing\Helper::getUserDateTimestamp($this->event->getStart());
211 $endTimestampUTCWithServerOffset = Sharing\Helper::getUserDateTimestamp($this->event->getEnd());
212
213 $start = FormatDate($shortDateFormat, $startTimestampUTCWithServerOffset);
214 $end = FormatDate($shortDateFormat, $endTimestampUTCWithServerOffset);
215
216 return $start !== $end;
217 }
218
219 protected function getFormattedEventDateSecondLine(): string
220 {
221 if ($this->event->isFullDayEvent())
222 {
223 $result = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_EVENT_FULL_DAY');
224 }
225 elseif ($this->isStartAndEndInDifferentDays())
226 {
227 $timestampUTCWithServerOffset = Sharing\Helper::getUserDateTimestamp($this->event->getEnd());
228 $culture = Main\Application::getInstance()->getContext()->getCulture();
229 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
230 $dayMonth = FormatDate($dayMonthFormat, $timestampUTCWithServerOffset);
231 $timeEnd = $this->formatTime($this->event->getEnd());
232 $result = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_EVENT_END', [
233 '#DATE#' => "$dayMonth $timeEnd"
234 ]);
235 }
236 else
237 {
238 $result = $this->getFormattedEventTimeInterval();
239 }
240
241 return $result;
242 }
243
244 protected function getFormattedEventTimeInterval(): string
245 {
246 $timeStart = $this->formatTime($this->event->getStart());
247 $timeEnd = $this->formatTime($this->event->getEnd());
248 return "$timeStart - $timeEnd";
249 }
250
251 protected function getEventTimezone(): string
252 {
253 $result = '';
254 if (!$this->event->isFullDayEvent())
255 {
256 $result = Sharing\Helper::formatTimezone($this->event->getStartTimeZone());
257 }
258
259 return $result;
260 }
261
265 protected function getCalendarMonthName(): string
266 {
267 $timestampUTCWithServerOffset = Sharing\Helper::getUserDateTimestamp($this->event->getStart());
268 return FormatDate('f', $timestampUTCWithServerOffset);
269 }
270
274 protected function getCalendarDay(): string
275 {
276 return $this->event->getStart()->format('j');
277 }
278
279 protected function getCalendarTime(): string
280 {
281 if ($this->event->isFullDayEvent())
282 {
283 $result = Loc::getMessage('EC_CALENDAR_SHARING_MAIL_EVENT_FULL_DAY');
284 }
285 else
286 {
287 $result = $this->formatTime($this->event->getStart());
288 }
289
290 return $result;
291 }
292
293 protected function getWhenCancelled(): string
294 {
295 $timestamp = $this->eventLink->getCanceledTimestamp();
296
297 $culture = Main\Application::getInstance()->getContext()->getCulture();
298 $dayMonthFormat = Main\Type\Date::convertFormatToPhp($culture->get('DAY_MONTH_FORMAT'));
299 $timeFormat = $culture->get('SHORT_TIME_FORMAT');
300 $formatLong = "$dayMonthFormat $timeFormat";
301
302 return FormatDate($formatLong, $timestamp);
303 }
304
305 protected function formatTime(Date $date): string
306 {
307 $culture = Main\Application::getInstance()->getContext()->getCulture();
308 $timeFormat = $culture->get('SHORT_TIME_FORMAT');
309 return $date->format($timeFormat);
310 }
311
316 protected function getIcsFileId(string $organizerName, string $eventName): ?int
317 {
318 try
319 {
320 $icsManager = IcsManager::getInstance();
321 $fileId = $icsManager->createIcsFile($this->event->setName($eventName), [
322 'eventUrl' => Sharing\Helper::getShortUrl($this->eventLink->getUrl()),
323 'conferenceUrl' => Sharing\Helper::getShortUrl($this->eventLink->getUrl() . Sharing\Helper::ACTION_CONFERENCE),
324 'organizer' => [
325 'name' => $organizerName,
326 'email' => $this->getOrganizerEmail(),
327 ],
328 ]);
329 }
330 catch (\Exception $e)
331 {
332 return null;
333 }
334
335 return $fileId;
336 }
337
338 protected function getOrganizerEmail(): string
339 {
340 $region = Main\Application::getInstance()->getLicense()->getRegion();
341 if ($region === 'ru')
342 {
343 return 'no-reply@bitrix24.ru';
344 }
345
346 return 'no-reply@bitrix24.com';
347 }
348
349 protected function getAbuseLink(): ?string
350 {
351 $ownerId = $this->eventLink->getOwnerId();
352 $calendarLink = $this->getCalendarLink() ?? $this->eventLink->getUrl();
353
354 return Sharing\Helper::getEmailAbuseLink($ownerId, $calendarLink);
355 }
356
357 protected function getBitrix24Link(): ?string
358 {
359 return Sharing\Helper::getBitrix24Link();
360 }
361
362}
format(string $format=null)
Definition date.php:107
sendMessage(string $to, array $arParams, string $subject, ?array $files=null)
Definition mail.php:122
getIcsFileId(string $organizerName, string $eventName)
Definition mail.php:316
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29