1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
FavoriteChat.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
5use Bitrix\Im\Model\ChatTable;
6use Bitrix\Im\Model\RelationTable;
7use Bitrix\Im\V2\Chat;
8use Bitrix\Im\V2\Entity\User\User;
9use Bitrix\Im\V2\Result;
10use Bitrix\Im\Model\EO_Chat;
11use Bitrix\Im\V2\Service\Context;
12use Bitrix\Im\V2\Service\Locator;
13use Bitrix\Main\Localization\Loc;
14
16{
17 protected function getDefaultEntityType(): string
18 {
19 return self::ENTITY_TYPE_FAVORITE;
20 }
21
22 public function getCompanion(?int $userId = null): User
23 {
24 return User::getInstance($this->getAuthorId());
25 }
26
27 public function getDialogId(?int $contextUserId = null): ?string
28 {
29 if ($this->dialogId || !$this->getChatId())
30 {
31 return $this->dialogId;
32 }
33
34 return $this->getAuthorId();
35 }
36
37 public function getDialogContextId(): ?string
38 {
39 return $this->getAuthorId() . ':' .$this->getAuthorId();
40 }
41
45 public function load($source = null): Result
46 {
47 $chatId = -1;
48 $authorId = -1;
49
50 if (is_numeric($source))
51 {
52 $chatId = (int)$source;
53 }
54 elseif ($source instanceof EO_Chat)
55 {
56 $chatId = $source->getId();
57 $authorId = $source->getAuthorId();
58 }
59 elseif (is_array($source))
60 {
61 $chatId = (int)$source['ID'];
62 $authorId = (int)$source['AUTHOR_ID'];
63 }
64
65 if ($chatId <= 0)
66 {
67 $chat = $this->getFavoriteChat($authorId);
68 if ($chat)
69 {
70 $source = $chat->getChatId();
71 }
72 }
73
74 return parent::load($source);
75 }
76
80 public function save(): Result
81 {
82 $saveResult = parent::save();
83 if (
84 $saveResult->isSuccess()
85 && $this->getChatId()
86 )
87 {
88 $authorId = $this->getAuthorId();
89 if (!$authorId)
90 {
91 $authorId = $this->getContext()->getUserId();
92 }
93 }
94
95 return $saveResult;
96 }
97
108 public static function find(array $params = [], ?Context $context = null): Result
109 {
110 $result = new Result;
111
112 if (empty($params['TO_USER_ID']))
113 {
114 $context = $context ?? Locator::getContext();
115 $params['TO_USER_ID'] = $context->getUserId();
116 }
117
118 if ($params['TO_USER_ID'] <= 0)
119 {
120 return $result->addError(new ChatError(ChatError::WRONG_RECIPIENT));
121 }
122
123 $row = ChatTable::query()
124 ->setSelect(['ID', 'TYPE', 'ENTITY_TYPE', 'ENTITY_ID'])
125 ->where('TYPE', self::IM_TYPE_PRIVATE)
126 ->where('ENTITY_TYPE', self::ENTITY_TYPE_FAVORITE)
127 ->where('AUTHOR_ID', (int)$params['TO_USER_ID'])
128 ->fetch()
129 ;
130
131 if ($row)
132 {
133 $result->setResult([
134 'ID' => (int)$row['ID'],
135 'TYPE' => $row['TYPE'],
136 'ENTITY_TYPE' => $row['ENTITY_TYPE'],
137 'ENTITY_ID' => $row['ENTITY_ID'],
138 ]);
139 }
140
141 return $result;
142 }
143
144 //region Access & Permissions
145
146 protected function checkAccessInternal(int $userId): Result
147 {
148 $result = new Result();
149
150 if ($this->getAuthorId() !== $userId)
151 {
153 }
154
155 return $result;
156 }
157
158 //endregion
159
165 public function add(array $params, ?Context $context = null): Result
166 {
167 $result = new Result;
168
169 $paramsResult = $this->prepareParams($params);
170 if (!$paramsResult->isSuccess())
171 {
172 return $result->addErrors($paramsResult->getErrors());
173 }
174
175 $params = $paramsResult->getResult();
176
177 $chat = $this->getFavoriteChat($params['AUTHOR_ID'] ?? null);
178
179 if (!$chat)
180 {
181 $chat = new static($params);
182 $chat
183 ->setTitle(Loc::getMessage('IM_CHAT_FAVORITE_TITLE_V3'))
184 ->setDescription(Loc::getMessage('IM_CHAT_FAVORITE_DESCRIPTION_MSGVER_1'))
185 ->save()
186 ;
187
188 $chat->sendBanner();
189
190 if (!$chat->getChatId())
191 {
192 return $result->addError(new ChatError(ChatError::CREATION_ERROR));
193 }
194
195 if ($chat->getAuthorId() > 0)
196 {
197 RelationTable::add([
198 'CHAT_ID' => $chat->getChatId(),
199 'MESSAGE_TYPE' => \IM_MESSAGE_PRIVATE,
200 'USER_ID' => $chat->getAuthorId(),
201 'STATUS' => \IM_STATUS_READ,
202 ]);
203 }
204 }
205
206 $result->setResult([
207 'CHAT_ID' => $chat->getChatId(),
208 'CHAT' => $chat,
209 ]);
210 $chat->isFilledNonCachedData = false;
211
212 return $result;
213 }
214
215 public static function getTitlePhrase(): string
216 {
217 return Loc::getMessage('IM_CHAT_FAVORITE_TITLE_V3') ?? '';
218 }
219
220 protected function sendBanner(): void
221 {
222 $messageText = Loc::getMessage('IM_CHAT_FAVORITE_CREATE_WELCOME_MSGVER_1');
224 'MESSAGE_TYPE' => $this->getType(),
225 'TO_CHAT_ID' => $this->getChatId(),
226 'FROM_USER_ID' => $this->getAuthorId(),
227 'MESSAGE' => $messageText,
228 'SYSTEM' => 'Y',
229 'PUSH' => 'N',
230 'PARAMS' => [
231 'COMPONENT_ID' => 'OwnChatCreationMessage',
232 ],
233 ]);
234 }
235
236 protected function prepareParams(array $params = []): Result
237 {
238 $result = new Result();
239
240 if (!isset($params['AUTHOR_ID']))
241 {
242 if (isset($params['FROM_USER_ID']))
243 {
244 $params['AUTHOR_ID'] = (int)$params['FROM_USER_ID'];
245 }
246 else
247 {
248 $params['AUTHOR_ID'] = 0;
249 }
250 }
251
252 if ($params['AUTHOR_ID'] <= 0)
253 {
254 return $result->addError(new ChatError(ChatError::WRONG_SENDER));
255 }
256
257 $result->setResult($params);
258
259 return $result;
260 }
261
262 private function getFavoriteChat(?int $userId = null): ?FavoriteChat
263 {
264 if (!$userId)
265 {
266 $context = $context ?? Locator::getContext();
267 $userId = $context->getUserId();
268 }
269
270 $chatResult = self::find(['TO_USER_ID' => $userId]);
271 if (!$chatResult->isSuccess() || !$chatResult->hasResult())
272 {
273 return null;
274 }
275
276 $result = $chatResult->getResult();
277 return FavoriteChat::getInstance($result['ID']);
278 }
279
280 protected function addIndex(): Chat
281 {
282 return $this;
283 }
284
285 protected function updateIndex(): Chat
286 {
287 return $this;
288 }
289}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getType($chatData, bool $camelCase=true)
Определения chat.php:45
const CREATION_ERROR
Определения ChatError.php:23
const WRONG_RECIPIENT
Определения ChatError.php:14
const ACCESS_DENIED
Определения ChatError.php:19
const WRONG_SENDER
Определения ChatError.php:13
checkAccessInternal(int $userId)
Определения FavoriteChat.php:146
add(array $params, ?Context $context=null)
Определения FavoriteChat.php:165
static find(array $params=[], ?Context $context=null)
Определения FavoriteChat.php:108
prepareParams(array $params=[])
Определения FavoriteChat.php:236
load($source=null)
Определения FavoriteChat.php:45
getDialogId(?int $contextUserId=null)
Определения FavoriteChat.php:27
static getTitlePhrase()
Определения FavoriteChat.php:215
getCompanion(?int $userId=null)
Определения FavoriteChat.php:22
static getInstance()
Определения application.php:98
Определения result.php:20
fetch(\Bitrix\Main\Text\Converter $converter=null)
Определения result.php:179
static Add($arFields)
Определения im_message.php:28
</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_STATUS_READ
Определения include.php:42
const IM_MESSAGE_PRIVATE
Определения include.php:22
$context
Определения csv_new_setup.php:223
Определения culture.php:9
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799