Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
chat.php
1
<?php
2
namespace
Bitrix\Landing\Chat
;
3
4
use \Bitrix\Landing\Manager;
5
use \Bitrix\Landing\File;
6
7
use \Bitrix\Main\Localization\Loc;
8
9
Loc::loadMessages
(__FILE__);
10
11
class
Chat
extends
\Bitrix\Landing\Internals\BaseTable
12
{
17
public
static
$internalClass
=
'ChatTable'
;
18
24
protected
static
function
getAvatarId
($avatarId)
25
{
26
$avatar =
File::getFileArray
($avatarId);
27
if
($avatar)
28
{
29
$avatarId = (int)$avatar[
'ID'
];
30
File::releaseFile
($avatarId);
31
}
32
else
33
{
34
$avatarId = 0;
35
}
36
37
return
$avatarId;
38
}
39
45
public
static
function
add
($fields)
46
{
47
if
(array_key_exists(
'CHAT_ID'
, $fields))
48
{
49
unset($fields[
'CHAT_ID'
]);
50
}
51
if
(array_key_exists(
'AVATAR'
, $fields))
52
{
53
$avatarId =
self::getAvatarId
($fields[
'AVATAR'
]);
54
}
55
else
56
{
57
$avatarId = 0;
58
}
59
60
// first create chat in th IM module
61
if
(
62
isset($fields[
'TITLE'
]) &&
63
\
Bitrix
\Main\Loader::includeModule(
'im'
)
64
)
65
{
66
$userId =
Manager::getUserId
();
67
$chat = new \CIMChat(0);
68
$chatId = $chat->add([
69
'TITLE'
=> $fields[
'TITLE'
],
70
'USERS'
=> [$userId],
71
'AVATAR_ID'
=> $avatarId,
72
'OWNER_ID'
=> $userId,
73
'ENTITY_TYPE'
=>
'LANDING'
74
]);
75
if
($chatId)
76
{
77
// welcome message
78
\CIMChat::addMessage([
79
'FROM_USER_ID'
=> $userId,
80
'SYSTEM'
=>
'Y'
,
81
'TO_CHAT_ID'
=> $chatId,
82
'MESSAGE'
=>
Loc::getMessage
(
'LANDING_CHAT_WELCOME_CREATE_MESSAGE'
),
83
]);
84
$fields[
'CHAT_ID'
] = $chatId;
85
// and create internal chat
86
$result = parent::add($fields);
87
if
($result->isSuccess())
88
{
89
\CIMChat::SetChatParams($chatId, [
90
'ENTITY_ID'
=> $result->getId()
91
]);
92
}
93
return
$result;
94
}
95
}
96
97
return
parent::add($fields);
98
}
99
105
public
static
function
getList
($params = [])
106
{
107
//@todo: make access filter
108
return
parent::getList($params);
109
}
110
116
public
static
function
getRow
($id): array
117
{
118
static
$chats = [];
119
120
if
(!array_key_exists($id, $chats))
121
{
122
$res =
self::getList
([
123
'filter'
=> [
124
'ID'
=> intval($id)
125
]
126
]);
127
$chats[$id] = $res->fetch();
128
}
129
130
return
$chats[$id];
131
}
132
139
public
static
function
update
($id, $fields = [])
140
{
141
$chatRow =
self::getRow
($id);
142
143
if
(!$chatRow)
144
{
145
$result = new \Bitrix\Main\Result;
146
$result->addErrors([
147
new
\
Bitrix
\Main\
Error
(
148
Loc::getMessage
(
'LANDING_CHAT_ERROR_CHAT_UPDATE'
),
149
'ERROR_CHAT_UPDATE'
150
)
151
]);
152
return
$result;
153
}
154
155
$result = parent::update($id, $fields);
156
157
// update IM chat
158
if
($result->isSuccess())
159
{
160
$chat = new \CIMChat(0);
161
// title
162
if
(
163
isset($fields[
'TITLE'
]) &&
164
$chatRow[
'TITLE'
] != $fields[
'TITLE'
]
165
)
166
{
167
$chat->rename(
168
$chatRow[
'CHAT_ID'
],
169
$fields[
'TITLE'
],
170
false
171
);
172
}
173
// avatar
174
if
(
175
isset($fields[
'AVATAR'
]) &&
176
$chatRow[
'AVATAR'
] != $fields[
'AVATAR'
]
177
)
178
{
179
$fields[
'AVATAR'
] =
self::getAvatarId
($fields[
'AVATAR'
]);
180
if
($fields[
'AVATAR'
])
181
{
182
File::deletePhysical
(
183
$chatRow[
'AVATAR'
]
184
);
185
$chat->setAvatarId(
186
$chatRow[
'CHAT_ID'
],
187
$fields[
'AVATAR'
]
188
);
189
}
190
}
191
}
192
193
return
$result;
194
}
195
201
public
static
function
getMembersId
(
int
$chatId): array
202
{
203
$ids = [];
204
205
if
($chatId && \
Bitrix
\Main\Loader::includeModule(
'im'
))
206
{
207
$chatRow =
self::getRow
($chatId);
208
$users = \Bitrix\Im\Chat::getUsers($chatRow[
'CHAT_ID'
]);
209
foreach
($users as $user)
210
{
211
$ids[] = $user[
'id'
];
212
}
213
}
214
215
return
$ids;
216
}
217
223
public
static
function
joinChat
(
int
$internalId): int
224
{
225
$chatId = 0;
226
227
$chatRow =
self::getRow
($internalId);
228
if
($chatRow && \
Bitrix
\Main\Loader::includeModule(
'im'
))
229
{
230
$chatId = $chatRow[
'CHAT_ID'
];
231
$userId =
Manager::getUserId
();
232
$users =
self::getMembersId
($internalId);
233
if
(!in_array($userId, $users))
234
{
235
$chat = new \CIMChat(0);
236
$chat->addUser($chatId, [$userId],
false
);
237
Binding::clearCache
($internalId);
238
}
239
}
240
241
return
$chatId;
242
}
243
}
Bitrix\Landing\Chat\Chat
Definition
chat.php:12
Bitrix\Landing\Chat\Chat\$internalClass
static $internalClass
Definition
chat.php:17
Bitrix\Landing\Chat\Chat\getList
static getList($params=[])
Definition
chat.php:105
Bitrix\Landing\Chat\Chat\add
static add($fields)
Definition
chat.php:45
Bitrix\Landing\Chat\Chat\getMembersId
static getMembersId(int $chatId)
Definition
chat.php:201
Bitrix\Landing\Chat\Chat\getRow
static getRow($id)
Definition
chat.php:116
Bitrix\Landing\Chat\Chat\joinChat
static joinChat(int $internalId)
Definition
chat.php:223
Bitrix\Landing\Chat\Chat\getAvatarId
static getAvatarId($avatarId)
Definition
chat.php:24
Bitrix\Landing\Chat\Chat\update
static update($id, $fields=[])
Definition
chat.php:139
Bitrix\Landing\Error
Definition
error.php:5
Bitrix\Landing\File\deletePhysical
static deletePhysical(int $fileId)
Definition
file.php:636
Bitrix\Landing\File\getFileArray
static getFileArray($fileId)
Definition
file.php:580
Bitrix\Landing\File\releaseFile
static releaseFile(int $fileId)
Definition
file.php:615
Bitrix\Landing\Internals\BaseTable
Definition
base.php:7
Bitrix\Landing\Manager\getUserId
static getUserId()
Definition
manager.php:107
Bitrix\Landing\Manager\clearCache
static clearCache()
Definition
manager.php:1377
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Landing\Chat
Definition
binding.php:2
Bitrix
modules
landing
lib
chat
chat.php
Создано системой
1.10.0