Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
FavoriteChat.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Chat
;
4
5
use
Bitrix\Im\Model\ChatTable
;
6
use
Bitrix\Im\Model\RelationTable
;
7
use
Bitrix\Im\V2\Chat
;
8
use
Bitrix\Im\V2\Entity\User\User
;
9
use
Bitrix\Im\V2\Result
;
10
use Bitrix\Im\Model\EO_Chat;
11
use
Bitrix\Im\V2\Service\Context
;
12
use
Bitrix\Im\V2\Service\Locator
;
13
use
Bitrix\Main\Localization\Loc
;
14
15
class
FavoriteChat
extends
PrivateChat
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
(): ?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
checkAccessWithoutCaching
(
int
$userId): bool
147
{
148
return
$this->getAuthorId() === $userId;
149
}
150
151
//endregion
152
158
public
function
add
(array $params, ?
Context
$context =
null
):
Result
159
{
160
$result =
new
Result
;
161
162
$paramsResult = $this->
prepareParams
($params);
163
if
(!$paramsResult->isSuccess())
164
{
165
return
$result->addErrors($paramsResult->getErrors());
166
}
167
168
$params = $paramsResult->getResult();
169
170
$chat = $this->getFavoriteChat($params[
'AUTHOR_ID'
] ??
null
);
171
172
if
(!$chat)
173
{
174
$chat =
new
static
($params);
175
$chat
176
->setTitle(
Loc::getMessage
(
'IM_CHAT_FAVORITE_TITLE_V3'
))
177
->setDescription(
Loc::getMessage
(
'IM_CHAT_FAVORITE_DESCRIPTION_V2'
))
178
->
save
()
179
;
180
181
$chat->sendBanner();
182
183
if
(!$chat->getChatId())
184
{
185
return
$result->addError(
new
ChatError
(
ChatError::CREATION_ERROR
));
186
}
187
188
if
($chat->getAuthorId() > 0)
189
{
190
RelationTable::add([
191
'CHAT_ID'
=> $chat->getChatId(),
192
'MESSAGE_TYPE'
=> \IM_MESSAGE_PRIVATE,
193
'USER_ID'
=> $chat->getAuthorId(),
194
'STATUS'
=> \IM_STATUS_READ,
195
]);
196
}
197
}
198
199
$result->setResult([
200
'CHAT_ID'
=> $chat->getChatId(),
201
'CHAT'
=> $chat,
202
]);
203
204
return
$result;
205
}
206
207
public
static
function
getTitlePhrase
(): string
208
{
209
return
Loc::getMessage
(
'IM_CHAT_FAVORITE_TITLE_V3'
) ??
''
;
210
}
211
212
protected
function
sendBanner
(): void
213
{
214
$messageText =
Loc::getMessage
(
'IM_CHAT_FAVORITE_CREATE_WELCOME'
);
215
\CIMMessage::Add([
216
'MESSAGE_TYPE'
=> $this->
getType
(),
217
'TO_CHAT_ID'
=> $this->getChatId(),
218
'FROM_USER_ID'
=> $this->getAuthorId(),
219
'MESSAGE'
=> $messageText,
220
'SYSTEM'
=>
'Y'
,
221
'PUSH'
=>
'N'
,
222
'PARAMS'
=> [
223
'COMPONENT_ID'
=>
'OwnChatCreationMessage'
,
224
],
225
]);
226
}
227
228
protected
function
prepareParams
(array $params = []):
Result
229
{
230
$result =
new
Result
();
231
232
if
(!isset($params[
'AUTHOR_ID'
]))
233
{
234
if
(isset($params[
'FROM_USER_ID'
]))
235
{
236
$params[
'AUTHOR_ID'
] = (int)$params[
'FROM_USER_ID'
];
237
}
238
else
239
{
240
$params[
'AUTHOR_ID'
] = 0;
241
}
242
}
243
244
if
($params[
'AUTHOR_ID'
] <= 0)
245
{
246
return
$result->addError(
new
ChatError
(
ChatError::WRONG_SENDER
));
247
}
248
249
$result->setResult($params);
250
251
return
$result;
252
}
253
254
private
function
getFavoriteChat(?
int
$userId =
null
): ?
FavoriteChat
255
{
256
if
(!$userId)
257
{
258
$context = $context ?? Locator::getContext();
259
$userId = $context->getUserId();
260
}
261
262
$chatResult =
self::find
([
'TO_USER_ID'
=> $userId]);
263
if
(!$chatResult->isSuccess() || !$chatResult->hasResult())
264
{
265
return
null
;
266
}
267
268
$result = $chatResult->getResult();
269
return
FavoriteChat::getInstance($result[
'ID'
]);
270
}
271
272
protected
function
addIndex
():
Chat
273
{
274
return
$this;
275
}
276
277
protected
function
updateIndex
():
Chat
278
{
279
return
$this;
280
}
281
}
Bitrix\Im\Chat
Definition
chat.php:19
Bitrix\Im\Chat\getType
static getType($chatData)
Definition
chat.php:41
Bitrix\Im\Model\ChatTable
Definition
chat.php:55
Bitrix\Im\Model\RelationTable
Definition
relation.php:51
Bitrix\Im\User
Definition
user.php:10
Bitrix\Im\V2\Chat\ChatError
Definition
ChatError.php:9
Bitrix\Im\V2\Chat\ChatError\CREATION_ERROR
const CREATION_ERROR
Definition
ChatError.php:25
Bitrix\Im\V2\Chat\ChatError\WRONG_RECIPIENT
const WRONG_RECIPIENT
Definition
ChatError.php:14
Bitrix\Im\V2\Chat\ChatError\WRONG_SENDER
const WRONG_SENDER
Definition
ChatError.php:13
Bitrix\Im\V2\Chat\FavoriteChat
Definition
FavoriteChat.php:16
Bitrix\Im\V2\Chat\FavoriteChat\getDefaultEntityType
getDefaultEntityType()
Definition
FavoriteChat.php:17
Bitrix\Im\V2\Chat\FavoriteChat\add
add(array $params, ?Context $context=null)
Definition
FavoriteChat.php:158
Bitrix\Im\V2\Chat\FavoriteChat\addIndex
addIndex()
Definition
FavoriteChat.php:272
Bitrix\Im\V2\Chat\FavoriteChat\getDialogId
getDialogId()
Definition
FavoriteChat.php:27
Bitrix\Im\V2\Chat\FavoriteChat\find
static find(array $params=[], ?Context $context=null)
Definition
FavoriteChat.php:108
Bitrix\Im\V2\Chat\FavoriteChat\getDialogContextId
getDialogContextId()
Definition
FavoriteChat.php:37
Bitrix\Im\V2\Chat\FavoriteChat\sendBanner
sendBanner()
Definition
FavoriteChat.php:212
Bitrix\Im\V2\Chat\FavoriteChat\updateIndex
updateIndex()
Definition
FavoriteChat.php:277
Bitrix\Im\V2\Chat\FavoriteChat\prepareParams
prepareParams(array $params=[])
Definition
FavoriteChat.php:228
Bitrix\Im\V2\Chat\FavoriteChat\load
load($source=null)
Definition
FavoriteChat.php:45
Bitrix\Im\V2\Chat\FavoriteChat\checkAccessWithoutCaching
checkAccessWithoutCaching(int $userId)
Definition
FavoriteChat.php:146
Bitrix\Im\V2\Chat\FavoriteChat\getTitlePhrase
static getTitlePhrase()
Definition
FavoriteChat.php:207
Bitrix\Im\V2\Chat\FavoriteChat\getCompanion
getCompanion(?int $userId=null)
Definition
FavoriteChat.php:22
Bitrix\Im\V2\Chat\FavoriteChat\save
save()
Definition
FavoriteChat.php:80
Bitrix\Im\V2\Chat\PrivateChat
Definition
PrivateChat.php:33
Bitrix\Im\V2\Entity\User\User
Definition
User.php:24
Bitrix\Im\V2\Result
Definition
Result.php:9
Bitrix\Im\V2\Service\Context
Definition
Context.php:8
Bitrix\Im\V2\Service\Locator
Definition
Locator.php:8
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Im\V2\Chat
Definition
ChannelChat.php:3
modules
im
lib
V2
Chat
FavoriteChat.php
Создано системой
1.10.0