Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sms.php
1<?php
3
6use Bitrix\Notifications;
8
9class Sms extends Service
10{
11 private const TEMPLATE_Q = 'SHARING_EVENT_INVITE';
12 private const TEMPLATE_Y = 'SHARING_EVENT_ACCEPTED';
13 private const TEMPLATE_N = 'SHARING_EVENT_DECLINED';
14 private const TEMPLATE_N_NO_LINK = 'SHARING_EVENT_DECLINED_2';
15
21 public function includeNotificationsModule(): bool
22 {
23 if (!Loader::includeModule('notifications'))
24 {
25 return false;
26 }
27
28 return Notifications\Account::isServiceAvailable() && Notifications\Account::isConnected();
29 }
30
31 public function notifyAboutMeetingStatus(string $to): void
32 {
33 if (!$this->includeNotificationsModule())
34 {
35 return;
36 }
37
38 $parsedPhone = PhoneNumber\Parser::getInstance()->parse($to);
39 if (!$parsedPhone->isValid() || mb_strtolower($parsedPhone->getCountry()) !== 'ru')
40 {
41 return;
42 }
43 $phoneNumberE164 = $parsedPhone->format(PhoneNumber\Format::E164);
44
45 $owner = $this->getOwner();
46
47 $templateCode = $this->getTemplateCode($owner);
48 $calendarLink = null;
49 if ($templateCode === self::TEMPLATE_N)
50 {
51 $calendarLink = $this->getShortCalendarLink();
52 if (is_null($calendarLink))
53 {
54 $templateCode = self::TEMPLATE_N_NO_LINK;
55 }
56 }
57
58 Notifications\Model\Message::create([
59 'PHONE_NUMBER' => $phoneNumberE164,
60 'TEMPLATE_CODE' => $templateCode,
61 'LANGUAGE_ID' => LANGUAGE_ID,
62 'PLACEHOLDERS' => $this->getPlaceholders($templateCode, $owner, $calendarLink),
63 ])->enqueue();
64 }
65
66 protected function getTemplateCode(array $owner): string
67 {
68 $templateCode = self::TEMPLATE_Q;
69
70 if ($owner['STATUS'] === 'Y')
71 {
72 $templateCode = self::TEMPLATE_Y;
73 }
74
75 if ($owner['STATUS'] === 'N')
76 {
77 $templateCode = self::TEMPLATE_N;
78 }
79
80 return $templateCode;
81 }
82
83 protected function getPlaceholders(string $templateCode, array $owner, ?string $calendarLink): array
84 {
85 $eventName = $this->event->getName();
86 $eventDateTime = $this->getEventFormattedDateTime();
87 $calendarOwner = $owner['NAME'];
88 $eventLink = Sharing\Helper::getShortUrl($this->eventLink->getUrl());
89
90 if ($templateCode === self::TEMPLATE_Q)
91 {
92 return [
93 'EVENT_NAME' => $eventName,
94 'DATE' => $eventDateTime,
95 'NAME' => $calendarOwner,
96 'URL' => $eventLink,
97 'URL_EVENT' => $eventLink, // sms parameter
98 ];
99 }
100 if ($templateCode === self::TEMPLATE_Y)
101 {
102 return [
103 'EVENT_NAME' => $eventName,
104 'DATE' => $eventDateTime,
105 'NAME' => $calendarOwner,
106 'URL' => $eventLink,
107 'URL_EVENT' => $eventLink, // sms parameter
108 ];
109 }
110 if ($templateCode === self::TEMPLATE_N)
111 {
112 return [
113 'EVENT_NAME' => $eventName,
114 'DATE' => $eventDateTime,
115 'NAME' => $calendarOwner,
116 'URL' => $calendarLink,
117 'URL_EVENT' => $eventLink, // sms parameter
118 ];
119 }
120 if ($templateCode === self::TEMPLATE_N_NO_LINK)
121 {
122 return [
123 'EVENT_NAME' => $eventName,
124 'DATE' => $eventDateTime,
125 'NAME' => $calendarOwner,
126 'URL_EVENT' => $eventLink, // sms parameter
127 ];
128 }
129 return [];
130 }
131
132 protected function getShortCalendarLink(): ?string
133 {
134 $calendarLink = $this->getCalendarLink();
135 if (!is_null($calendarLink))
136 {
137 return Sharing\Helper::getShortUrl($this->getCalendarLink());
138 }
139
140 return null;
141 }
142}
getPlaceholders(string $templateCode, array $owner, ?string $calendarLink)
Definition sms.php:83
static includeModule($moduleName)
Definition loader.php:69