1C-Bitrix
25.700.0
Загрузка...
Поиск...
Не найдено
ChatAvatar.php
См. документацию.
1
<?php
2
3
namespace
Bitrix\Im\V2\Entity\File
;
4
5
use
Bitrix\Im\Common
;
6
use
Bitrix\Im\V2\Chat
;
7
use
Bitrix\Im\V2\Result
;
8
use
Bitrix\Main\File\Image
;
9
use
Bitrix\Main\Loader
;
10
use
Bitrix\Pull\Event
;
11
use CFile;
12
13
class
ChatAvatar
14
{
15
protected
Chat
$chat
;
16
17
public
function
__construct
(
Chat
$chat
)
18
{
19
$this->chat =
$chat
;
20
}
21
22
public
function
get
(
bool
$addBlankPicture =
false
,
bool
$withDomain =
false
): string
23
{
24
$url
= $addBlankPicture ?
'/bitrix/js/im/images/blank.gif'
:
''
;
25
26
if
($this->chat->getAvatarId() > 0)
27
{
28
$avatar = $this->
getResizeAvatar
();
29
if
(!empty($avatar[
'src'
]))
30
{
31
$url
= $avatar[
'src'
];
32
}
33
}
34
35
if
($withDomain &&
$url
)
36
{
37
return
Common::getPublicDomain
() .
$url
;
38
}
39
40
return
$url
;
41
}
42
43
public
function
update
(
44
int
|
string
$avatar,
45
bool
$withMessage =
true
,
46
bool
$skipRecent =
false
,
47
bool
$withoutSaveInChat =
false
48
):
Result
49
{
50
if
(!is_numeric($avatar))
51
{
52
$avatar =
self::saveAvatarByString
((
string
)$avatar) ?? 0;
53
}
54
55
return
$this->
updateById
(
56
(
int
)$avatar,
57
$withMessage,
58
$skipRecent,
59
$withoutSaveInChat
60
);
61
}
62
63
protected
function
updateById
(
64
int
$avatarId,
65
bool
$withMessage =
true
,
66
bool
$skipRecent =
false
,
67
bool
$withoutSaveInChat =
false
68
):
Result
69
{
70
$result
=
new
Result
();
71
72
$oldAvatarId = $this->chat->getAvatarId();
73
$this->chat->setAvatarId($avatarId);
74
75
if
(!$withoutSaveInChat)
76
{
77
$result
= $this->chat->save();
78
if
(!
$result
->isSuccess())
79
{
80
return
$result
;
81
}
82
}
83
84
if
(isset($oldAvatarId) && $oldAvatarId > 0)
85
{
86
CFile::Delete($oldAvatarId);
87
}
88
89
$avatar = $this->
getResizeAvatar
();
90
if
(
91
!empty($avatar[
'src'
])
92
&& Loader::includeModule(
"pull"
)
93
)
94
{
95
$this->
sendPush
($avatar);
96
}
97
98
if
($withMessage)
99
{
100
$this->chat->sendMessageUpdateAvatar($skipRecent);
101
}
102
103
return
$result
->setResult($avatarId);
104
}
105
106
protected
function
sendPush
(
array
$avatar): void
107
{
108
$pushMessage = [
109
'module_id'
=>
'im'
,
110
'command'
=>
'chatAvatar'
,
111
'params'
=> [
112
'chatId'
=> $this->chat->getChatId(),
113
'avatar'
=> $avatar[
'src'
],
114
],
115
'extra'
=>
Common::getPullExtra
()
116
];
117
118
Event::add($this->chat->getRelations()->getUserIds(), $pushMessage);
119
if
($this->chat->needToSendPublicPull())
120
{
121
\CPullWatch::AddToStack(
'IM_PUBLIC_'
. $this->chat->getId(), $pushMessage);
122
}
123
}
124
125
protected
function
getResizeAvatar
():
array
126
{
127
$avatar = \CFile::ResizeImageGet(
128
$this->chat->getAvatarId() ?? 0,
129
[
'width'
=> 200,
'height'
=> 200],
130
BX_RESIZE_IMAGE_EXACT
,
131
false
,
132
false
,
133
true
134
);
135
if
(empty($avatar))
136
{
137
return
[];
138
}
139
140
return
$avatar;
141
}
142
143
public
static
function
saveAvatarByString
(
string
$avatarBase64): ?int
144
{
145
$avatar = \CRestUtil::saveFile($avatarBase64);
146
$imageCheck = (
new
Image
($avatar[
"tmp_name"
]))->getInfo();
147
148
if
(
149
!$imageCheck
150
|| !$imageCheck->getWidth() || $imageCheck->getWidth() > 5000
151
|| !$imageCheck->getHeight() || $imageCheck->getHeight() > 5000
152
)
153
{
154
return
null
;
155
}
156
157
if
(!$avatar || mb_strpos($avatar[
'type'
],
"image/"
) !== 0)
158
{
159
return
null
;
160
}
161
162
$avatar = CFile::saveFile($avatar,
'im'
);
163
164
return
is_numeric($avatar) && $avatar > 0 ? $avatar :
null
;
165
}
166
}
Bitrix\Im\Common
Определения
common.php:7
Bitrix\Im\Common\getPullExtra
static getPullExtra()
Определения
common.php:127
Bitrix\Im\Common\getPublicDomain
static getPublicDomain()
Определения
common.php:8
Bitrix\Im\V2\Entity\File\ChatAvatar
Определения
ChatAvatar.php:14
Bitrix\Im\V2\Entity\File\ChatAvatar\update
update(int|string $avatar, bool $withMessage=true, bool $skipRecent=false, bool $withoutSaveInChat=false)
Определения
ChatAvatar.php:43
Bitrix\Im\V2\Entity\File\ChatAvatar\saveAvatarByString
static saveAvatarByString(string $avatarBase64)
Определения
ChatAvatar.php:143
Bitrix\Im\V2\Entity\File\ChatAvatar\__construct
__construct(Chat $chat)
Определения
ChatAvatar.php:17
Bitrix\Im\V2\Entity\File\ChatAvatar\$chat
Chat $chat
Определения
ChatAvatar.php:15
Bitrix\Im\V2\Entity\File\ChatAvatar\sendPush
sendPush(array $avatar)
Определения
ChatAvatar.php:106
Bitrix\Im\V2\Entity\File\ChatAvatar\updateById
updateById(int $avatarId, bool $withMessage=true, bool $skipRecent=false, bool $withoutSaveInChat=false)
Определения
ChatAvatar.php:63
Bitrix\Im\V2\Entity\File\ChatAvatar\getResizeAvatar
getResizeAvatar()
Определения
ChatAvatar.php:125
Bitrix\Main\DB\Result
Определения
result.php:20
Bitrix\Main\Event
Определения
event.php:5
Bitrix\Main\Loader
Определения
loader.php:13
array
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения
file_new.php:804
$result
$result
Определения
get_property_values.php:14
BX_RESIZE_IMAGE_EXACT
const BX_RESIZE_IMAGE_EXACT
Определения
constants.php:12
Bitrix\Im\V2\Chat
Bitrix\Im\V2\Entity\File
Определения
ChatAvatar.php:3
Bitrix\Main\File\Image
Определения
Color.php:9
$url
$url
Определения
iframe.php:7
bitrix
modules
im
lib
V2
Entity
File
ChatAvatar.php
Создано системой
1.14.0