1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
InvitationService.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Collab\Control\Invite;
6
7use Bitrix\Main\Config\Option;
8use Bitrix\Main\DI\ServiceLocator;
9use Bitrix\Main\Error;
10use Bitrix\Main\Localization\Loc;
11use Bitrix\Main\Result;
12use Bitrix\Main\Validation\ValidationService;
13use Bitrix\Socialnetwork\Collab\Collab;
14use Bitrix\Socialnetwork\Collab\Control\Invite\Command\InvitationCommand;
15use Bitrix\Socialnetwork\Collab\Permission\UserRole;
16use Bitrix\Socialnetwork\Collab\Registry\CollabRegistry;
17use Bitrix\Socialnetwork\Collab\User\User;
18use Bitrix\Socialnetwork\UserToGroupTable;
19use CIMNotify;
20
22{
23 protected const MAX_NOTIFY_LENGTH = 150;
24 protected const REQUEST_URL = '/company/personal/user/#USER_ID#/requests/';
25
27 protected User $sender;
28 protected User $recipient;
29 protected Collab $collab;
30 protected string $serverName;
31 protected int $relationId;
32
33 public function __construct()
34 {
35 $this->init();
36 }
37
38 public function send(InvitationCommand $command): Result
39 {
40 $result = new Result();
41
42 $validationResult = $this->validationService->validate($command);
43 if (!$validationResult->isSuccess())
44 {
45 return $validationResult;
46 }
47
48 $this->sender = new User($command->getInitiatorId());
49 $this->recipient = new User($command->getRecipientId());
50 $collab = CollabRegistry::getInstance()->get($command->getCollabId());
51
52 if ($collab === null)
53 {
54 return $result;
55 }
56
57 $this->collab = $collab;
58 $this->relationId = $command->hasRelationId() ? $command->getRelationId() : $this->getRelationId();
59
60 if ($this->relationId <= 0)
61 {
62 $result->addError(new Error('No relation', 'INVITE_NO_RELATION'));
63
64 return $result;
65 }
66
68
69 $messageResult = CIMNotify::Add($messageFields);
70 if ($messageResult === false)
71 {
72 global $APPLICATION;
73 $error = $APPLICATION->GetException();
74
75 if ($error)
76 {
77 $result->addError(new Error($error->msg, $error->id));
78 }
79 else
80 {
81 $result->addError(new Error('Invite is not sent', 'INVITE_NOT_SENT'));
82 }
83 }
84
85 return $result;
86 }
87
88 protected function getNotifyMessageOut(): callable
89 {
90 return function(?string $languageId = null): string {
91
92 $title = Loc::getMessage('SONET_COLLAB_INVITE_TEXT', [
93 '#NAME#' => $this->collab->getName(),
94 ], $languageId);
95
96 $confirm = Loc::getMessage('SONET_COLLAB_INVITE_CONFIRM', null, $languageId)
97 . ': '
98 . $this->getActionLink();
99
100 $reject = Loc::getMessage('SONET_COLLAB_INVITE_REJECT', null, $languageId)
101 . ': '
102 . $this->getActionLink(false);
103
104 return "{$title}\n\n{$confirm}\n\n{$reject}";
105 };
106 }
107
108 protected function getActionLink(bool $confirm = true): string
109 {
110 $confirmValue = $confirm ? 'Y' : 'N';
111
112 return "{$this->getRequestUrl()}?INVITE_GROUP={$this->relationId}&CONFIRM={$confirmValue}";
113 }
114
115 protected function getMessageFields(): array
116 {
117 return [
118 "MESSAGE_TYPE" => IM_MESSAGE_SYSTEM,
119 "TO_USER_ID" => $this->recipient->getId(),
120 "FROM_USER_ID" => $this->sender->getId(),
121 "NOTIFY_TYPE" => IM_NOTIFY_CONFIRM,
122 "NOTIFY_MODULE" => "socialnetwork",
123 "NOTIFY_EVENT" => "invite_group_btn",
124 "NOTIFY_TAG" => $this->getNotifyTag(),
125 "NOTIFY_TITLE" => $this->getNotifyTitle(),
126 "NOTIFY_MESSAGE" => $this->getNotifyMessage(),
127 "NOTIFY_BUTTONS" => $this->getButtons(),
128 'NOTIFY_MESSAGE_OUT' => $this->getNotifyMessageOut(),
129 ];
130 }
131
132 protected function getNotifyTag(): string
133 {
134 return "SOCNET|INVITE_GROUP|{$this->recipient->getId()}|{$this->relationId}";
135 }
136
137 protected function getNotifyTitle(): callable
138 {
139 return fn(?string $languageId = null): string => Loc::getMessage('SONET_COLLAB_INVITE_TEXT', [
140 '#NAME#' => rtrim(mb_substr($this->collab->getName(), 0, static::MAX_NOTIFY_LENGTH), '.') . '...',
141 ], $languageId);
142 }
143
144 protected function getNotifyMessage(): callable
145 {
146 return fn(?string $languageId = null): string => Loc::getMessage('SONET_COLLAB_INVITE_TEXT', [
147 '#NAME#' => $this->collab->getName(),
148 ], $languageId);
149 }
150
151 protected function getButtons(): array
152 {
153 return [
154 [
155 'TITLE' => static fn(?string $languageId = null): string => Loc::getMessage(
156 'SONET_COLLAB_INVITE_CONFIRM',
157 null,
158 $languageId,
159 ),
160 'VALUE' => 'Y',
161 'TYPE' => 'accept',
162 ],
163 [
164 'TITLE' => static fn(?string $languageId = null): string => Loc::getMessage(
165 'SONET_COLLAB_INVITE_REJECT',
166 null,
167 $languageId,
168 ),
169 'VALUE' => 'N',
170 'TYPE' => 'cancel',
171 ],
172 ];
173 }
174
175 protected function getRequestUrl(): string
176 {
177 $requestUrl = Option::get(
178 'socialnetwork',
179 'user_request_page',
180 static::REQUEST_URL,
181 );
182
183 return str_replace(["#USER_ID#", "#user_id#"], (string)$this->recipient->getId(), $requestUrl);
184 }
185
186 protected function getRelationId(): int
187 {
188 $row = UserToGroupTable::query()
189 ->setSelect(['ID'])
190 ->where('GROUP_ID', $this->collab->getId())
191 ->where('USER_ID', $this->recipient->getId())
192 ->where('ROLE', UserRole::REQUEST)
193 ->exec()
194 ->fetch();
195
196 if (empty($row))
197 {
198 return 0;
199 }
200
201 return (int)$row['ID'];
202 }
203
204 protected function init(): void
205 {
206 $this->validationService = ServiceLocator::getInstance()->get('main.validation.service');
207 }
208}
$messageFields
Определения callback_ednaru.php:22
global $APPLICATION
Определения include.php:80
get(string $id)
Определения servicelocator.php:126
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
const IM_MESSAGE_SYSTEM
Определения include.php:21
const IM_NOTIFY_CONFIRM
Определения include.php:36
$title
Определения pdf.php:123
$error
Определения subscription_card_product.php:20