1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
notificationservice.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Sharing\Notification;
4
5use Bitrix\Calendar\Sharing;
6use Bitrix\Main\Loader;
7use Bitrix\Notifications;
8use Bitrix\Main\PhoneNumber;
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 if ($this->initiatorId === $this->eventLink->getHostId())
49 {
50 return false;
51 }
52
53 $owner = $this->getOwner();
54 $templateCode = $this->getTemplateCode($owner);
55 $calendarLink = null;
56 if ($templateCode === self::TEMPLATE_N)
57 {
58 $calendarLink = $this->getShortCalendarLink();
59 if (is_null($calendarLink))
60 {
61 $templateCode = self::TEMPLATE_N_NO_LINK;
62 }
63 }
64 $placeholders = $this->getPlaceholders($templateCode, $owner, $calendarLink);
65
66 return $this->sendMessage($to, $templateCode, $placeholders);
67 }
68
69 public function notifyAboutSharingEventEdit(string $to): bool
70 {
71 //TODO: add logic
72 return true;
73 }
74
79 public function sendCrmSharingAutoAccepted(string $to): bool
80 {
81 $manager = Sharing\Helper::getOwnerInfo($this->crmDealLink->getOwnerId());
82 $placeholders = [
83 // for whatsapp
84 'MANAGER_NAME' => Sharing\Helper::getPersonFullNameLoc($manager['name'], $manager['lastName']),
85 'DATE' => Sharing\Helper::formatDate($this->event->getStart()),
86 'EVENT_URL' => Sharing\Helper::getShortUrl($this->eventLink->getUrl()),
87
88 // for sms
89 'DATE_SHORT' => Sharing\Helper::formatDateShort($this->event->getStart()),
90 ];
91
92 return $this->sendMessage($to, self::TEMPLATE_CRM_SHARING_AUTO_ACCEPTED, $placeholders);
93 }
94
101 protected function sendMessage(string $phoneNumber, string $templateCode, array $placeholders): bool
102 {
103 if (!self::includeNotificationsModule())
104 {
105 return false;
106 }
107
108 $parsedPhone = PhoneNumber\Parser::getInstance()->parse($phoneNumber);
109 $countryCode = mb_strtolower($parsedPhone->getCountry());
110 if (!$parsedPhone->isValid() || in_array($countryCode, self::ALLOWED_COUNTRY_CODES, true))
111 {
112 return false;
113 }
114 $phoneNumberE164 = $parsedPhone->format(PhoneNumber\Format::E164);
115
116 return Notifications\Model\Message::create([
117 'PHONE_NUMBER' => $phoneNumberE164,
118 'TEMPLATE_CODE' => $templateCode,
119 'LANGUAGE_ID' => LANGUAGE_ID,
120 'PLACEHOLDERS' => $placeholders,
121 ])->enqueue()->isSuccess();
122 }
123
128 protected function getTemplateCode(array $owner): string
129 {
130 $templateCode = self::TEMPLATE_Q;
131
132 if ($owner['STATUS'] === 'Y')
133 {
134 $templateCode = self::TEMPLATE_Y;
135 }
136
137 if ($owner['STATUS'] === 'N')
138 {
139 $templateCode = self::TEMPLATE_N;
140 }
141
142 return $templateCode;
143 }
144
151 protected function getPlaceholders(string $templateCode, array $owner, ?string $calendarLink): array
152 {
154 $eventDateTime = $this->getEventFormattedDateTime();
155 $calendarOwner = $owner['NAME'];
156 $eventLink = Sharing\Helper::getShortUrl($this->eventLink->getUrl());
157
158 if ($templateCode === self::TEMPLATE_Q)
159 {
160 return [
161 'EVENT_NAME' => $eventName,
162 'DATE' => $eventDateTime,
163 'NAME' => $calendarOwner,
164 'URL' => $eventLink,
165 'URL_EVENT' => $eventLink, // sms parameter
166 ];
167 }
168 if ($templateCode === self::TEMPLATE_Y)
169 {
170 return [
171 'EVENT_NAME' => $eventName,
172 'DATE' => $eventDateTime,
173 'NAME' => $calendarOwner,
174 'URL' => $eventLink,
175 'URL_EVENT' => $eventLink, // sms parameter
176 ];
177 }
178 if ($templateCode === self::TEMPLATE_N)
179 {
180 return [
181 'EVENT_NAME' => $eventName,
182 'DATE' => $eventDateTime,
183 'NAME' => $calendarOwner,
184 'URL' => $calendarLink,
185 'URL_EVENT' => $eventLink, // sms parameter
186 ];
187 }
188 if ($templateCode === self::TEMPLATE_N_NO_LINK)
189 {
190 return [
191 'EVENT_NAME' => $eventName,
192 'DATE' => $eventDateTime,
193 'NAME' => $calendarOwner,
194 'URL_EVENT' => $eventLink, // sms parameter
195 ];
196 }
197 return [];
198 }
199
203 protected function getShortCalendarLink(): ?string
204 {
205 $calendarLink = $this->getCalendarLink();
206 if (!is_null($calendarLink))
207 {
209 }
210
211 return null;
212 }
213}
static formatDate(Date $date)
Определения helper.php:195
static getOwnerInfo(int $id)
Определения helper.php:139
static formatDateShort(Date $date)
Определения helper.php:235
static getShortUrl(string $url)
Определения helper.php:106
static getPersonFullNameLoc(?string $name, ?string $lastName)
Определения helper.php:123
sendMessage(string $phoneNumber, string $templateCode, array $placeholders)
Определения notificationservice.php:101
getPlaceholders(string $templateCode, array $owner, ?string $calendarLink)
Определения notificationservice.php:151
static getSharingEventNameByUserName(?string $userName)
Определения sharingeventmanager.php:264
const E164
Определения format.php:7
static getInstance()
Определения parser.php:78
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$manager
Определения office365push.php:39