Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
notificationservice.php
1<?php
2
4
7use Bitrix\Notifications;
9
11{
12 private const ALLOWED_COUNTRY_CODES = ['ru'];
13
14 private const TEMPLATE_Q = 'SHARING_EVENT_INVITE';
15 private const TEMPLATE_Y = 'SHARING_EVENT_ACCEPTED';
16 private const TEMPLATE_N = 'SHARING_EVENT_DECLINED';
17 private const TEMPLATE_N_NO_LINK = 'SHARING_EVENT_DECLINED_2';
18 private const TEMPLATE_CRM_SHARING_AUTO_ACCEPTED = 'CRM_SHARING_AUTO_ACCEPTED';
19
27 public static function includeNotificationsModule(): bool
28 {
29 if (\CCalendar::IsBitrix24() && in_array(\CBitrix24::getPortalZone(), self::ALLOWED_COUNTRY_CODES))
30 {
31 return false;
32 }
33
34 if (!Loader::includeModule('notifications'))
35 {
36 return false;
37 }
38
39 return Notifications\Account::isServiceAvailable() && Notifications\Account::isConnected();
40 }
41
46 public function notifyAboutMeetingStatus(string $to): bool
47 {
48 $owner = $this->getOwner();
49 $templateCode = $this->getTemplateCode($owner);
50 $calendarLink = null;
51 if ($templateCode === self::TEMPLATE_N)
52 {
53 $calendarLink = $this->getShortCalendarLink();
54 if (is_null($calendarLink))
55 {
56 $templateCode = self::TEMPLATE_N_NO_LINK;
57 }
58 }
59 $placeholders = $this->getPlaceholders($templateCode, $owner, $calendarLink);
60
61 return $this->sendMessage($to, $templateCode, $placeholders);
62 }
63
64 public function notifyAboutSharingEventEdit(string $to): bool
65 {
66 //TODO: add logic
67 return true;
68 }
69
74 public function sendCrmSharingAutoAccepted(string $to): bool
75 {
76 $manager = Sharing\Helper::getOwnerInfo($this->crmDealLink->getOwnerId());
77 $placeholders = [
78 // for whatsapp
79 'MANAGER_NAME' => Sharing\Helper::getPersonFullNameLoc($manager['name'], $manager['lastName']),
80 'DATE' => Sharing\Helper::formatDate($this->event->getStart()),
81 'EVENT_URL' => Sharing\Helper::getShortUrl($this->eventLink->getUrl()),
82
83 // for sms
84 'DATE_SHORT' => Sharing\Helper::formatDateShort($this->event->getStart()),
85 ];
86
87 return $this->sendMessage($to, self::TEMPLATE_CRM_SHARING_AUTO_ACCEPTED, $placeholders);
88 }
89
96 protected function sendMessage(string $phoneNumber, string $templateCode, array $placeholders): bool
97 {
98 if (!self::includeNotificationsModule())
99 {
100 return false;
101 }
102
103 $parsedPhone = PhoneNumber\Parser::getInstance()->parse($phoneNumber);
104 $countryCode = mb_strtolower($parsedPhone->getCountry());
105 if (!$parsedPhone->isValid() || in_array($countryCode, self::ALLOWED_COUNTRY_CODES, true))
106 {
107 return false;
108 }
109 $phoneNumberE164 = $parsedPhone->format(PhoneNumber\Format::E164);
110
111 return Notifications\Model\Message::create([
112 'PHONE_NUMBER' => $phoneNumberE164,
113 'TEMPLATE_CODE' => $templateCode,
114 'LANGUAGE_ID' => LANGUAGE_ID,
115 'PLACEHOLDERS' => $placeholders,
116 ])->enqueue()->isSuccess();
117 }
118
123 protected function getTemplateCode(array $owner): string
124 {
125 $templateCode = self::TEMPLATE_Q;
126
127 if ($owner['STATUS'] === 'Y')
128 {
129 $templateCode = self::TEMPLATE_Y;
130 }
131
132 if ($owner['STATUS'] === 'N')
133 {
134 $templateCode = self::TEMPLATE_N;
135 }
136
137 return $templateCode;
138 }
139
146 protected function getPlaceholders(string $templateCode, array $owner, ?string $calendarLink): array
147 {
148 $eventName = Sharing\SharingEventManager::getSharingEventNameByUserName($owner['NAME']);
149 $eventDateTime = $this->getEventFormattedDateTime();
150 $calendarOwner = $owner['NAME'];
151 $eventLink = Sharing\Helper::getShortUrl($this->eventLink->getUrl());
152
153 if ($templateCode === self::TEMPLATE_Q)
154 {
155 return [
156 'EVENT_NAME' => $eventName,
157 'DATE' => $eventDateTime,
158 'NAME' => $calendarOwner,
159 'URL' => $eventLink,
160 'URL_EVENT' => $eventLink, // sms parameter
161 ];
162 }
163 if ($templateCode === self::TEMPLATE_Y)
164 {
165 return [
166 'EVENT_NAME' => $eventName,
167 'DATE' => $eventDateTime,
168 'NAME' => $calendarOwner,
169 'URL' => $eventLink,
170 'URL_EVENT' => $eventLink, // sms parameter
171 ];
172 }
173 if ($templateCode === self::TEMPLATE_N)
174 {
175 return [
176 'EVENT_NAME' => $eventName,
177 'DATE' => $eventDateTime,
178 'NAME' => $calendarOwner,
179 'URL' => $calendarLink,
180 'URL_EVENT' => $eventLink, // sms parameter
181 ];
182 }
183 if ($templateCode === self::TEMPLATE_N_NO_LINK)
184 {
185 return [
186 'EVENT_NAME' => $eventName,
187 'DATE' => $eventDateTime,
188 'NAME' => $calendarOwner,
189 'URL_EVENT' => $eventLink, // sms parameter
190 ];
191 }
192 return [];
193 }
194
198 protected function getShortCalendarLink(): ?string
199 {
200 $calendarLink = $this->getCalendarLink();
201 if (!is_null($calendarLink))
202 {
203 return Sharing\Helper::getShortUrl($this->getCalendarLink());
204 }
205
206 return null;
207 }
208}
sendMessage(string $phoneNumber, string $templateCode, array $placeholders)
getPlaceholders(string $templateCode, array $owner, ?string $calendarLink)