Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chat.php
1<?php
3
4use \Bitrix\Landing\Block\Cache;
5use \Bitrix\Landing\Manager;
6use \Bitrix\Main\Localization\Loc;
7
8Loc::loadMessages(__FILE__);
9
11{
15 const CHAT_TYPE_PERSONAL = 'private';
16
20 const CHAT_TYPE_GROUP = 'group';
21
26 protected function getInitData(): array
27 {
28 if (!\Bitrix\Main\Loader::includeModule('im'))
29 {
30 return [];
31 }
32
33 $return = [];
34 $blockSave = false;
35 $type = null;
36 $filter = $this->getSettingsValue('additional');
38 $block = $this->getOptionsValue('block');
39 $dom = $block->getDom();
40
41 // chat type
42 if (
43 isset($filter['type']) &&
44 is_string($filter['type'])
45 )
46 {
47 $type = mb_strtolower(trim($filter['type']));
48 }
49
50 // ID of user chat or local ID of group chat
51 if (
52 isset($filter['attributeData']) &&
53 is_string($filter['attributeData']) &&
54 mb_strpos($filter['attributeData'], '@')
55 )
56 {
57 [$attrSelector, $attrCode] = explode('@', $filter['attributeData']);
58 $attrCode = mb_strtolower($attrCode);
59 $resultNode = $dom->querySelector($attrSelector);
60 if ($resultNode)
61 {
62 if ($type == self::CHAT_TYPE_PERSONAL)
63 {
64 $return['CHAT_ID'] = (int) $resultNode->getAttribute($attrCode);
65 // by default we set current user id
66 if (!$return['CHAT_ID'])
67 {
68 $return['CHAT_ID'] = Manager::getUserId();
69 $resultNode->setAttribute($attrCode, $return['CHAT_ID']);
70 $blockSave = true;
71 }
72 }
73 else if ($type == self::CHAT_TYPE_GROUP)
74 {
75 $return['CHAT_ID'] = trim($resultNode->getAttribute($attrCode));
76 // create new one
77 if (preg_match('/[^\d]+/', $return['CHAT_ID']))
78 {
79 $chantEntity = json_decode(htmlspecialcharsback($return['CHAT_ID']), true);
80 if (is_array($chantEntity))
81 {
82 if (isset($chantEntity['ID']))
83 {
84 $chatId = $chantEntity['ID'];
85 unset($chantEntity['ID']);
86 $res = \Bitrix\Landing\Chat\Chat::update(
87 $chatId,
88 $chantEntity
89 );
90 }
91 else
92 {
93 $res = \Bitrix\Landing\Chat\Chat::add(
94 $chantEntity
95 );
96 }
97 if ($res->isSuccess())
98 {
99 $return['CHAT_ID'] = $res->getId();
100 $resultNode->setAttribute(
101 $attrCode, $return['CHAT_ID']
102 );
103 $blockSave = true;
104 }
105 }
106 }
107 else if (!$return['CHAT_ID'])
108 {
109 unset($return['CHAT_ID']);
110 }
111 $return['CHAT_ID'] = (int)$return['CHAT_ID'];
112 if ($return['CHAT_ID'])
113 {
114 \Bitrix\Landing\Chat\Binding::bindingBlock(
115 $return['CHAT_ID'], $block->getId()
116 );
117 }
118 }
119 }
120 }
121
122 // button title we recive from attribute too
123 if (
124 $return &&
125 isset($filter['attributeButton']) &&
126 is_string($filter['attributeButton']) &&
127 mb_strpos($filter['attributeButton'], '@')
128 )
129 {
130 [$attrSelector, $attrCode] = explode('@', $filter['attributeButton']);
131 $attrCode = mb_strtolower($attrCode);
132 $resultNode = $dom->querySelector($attrSelector);
133 if ($resultNode)
134 {
135 $return['SEND_TITLE'] = $resultNode->getAttribute($attrCode);
136 if (!$return['SEND_TITLE'])
137 {
138 $return['SEND_TITLE'] = Loc::getMessage('LANDING_SUBTYPE_BUTTON_SEND');
139 $resultNode->setAttribute($attrCode, $return['SEND_TITLE']);
140 $blockSave = true;
141 }
142 }
143 }
144
145 // chat type
146 if ($return && $type)
147 {
148 $return['TYPE'] = $type;
149 }
150
151 // save attributes to the block if necessary
152 if ($blockSave)
153 {
154 $block->saveContent($dom->saveHTML());
155 $block->save();
156 }
157
158 return $return;
159 }
160
166 protected function getUserList(array $initData): array
167 {
168 $data = [];
169 $userFilter = ['=ACTIVE' => 'Y'];
170 $chatId = $initData['CHAT_ID'];
171 $chatType = $initData['TYPE'];
172 $sendButton = [
173 'href' => '#',
174 'text' => $initData['SEND_TITLE']
175 ];
176
177 // for private chat chat Id = user Id
178 if ($chatType == $this::CHAT_TYPE_PERSONAL)
179 {
180 $sendButton['href'] = '#chat' . $chatId;
181 $userFilter['ID'] = $chatId;
182 }
183 else if ($chatType == $this::CHAT_TYPE_GROUP)
184 {
185 $sendButton['href'] = '#join' . $chatId;
186 $userFilter['ID'] = \Bitrix\Landing\Chat\Chat::getMembersId(
187 $chatId
188 );
189 if (!$userFilter['ID'])
190 {
191 $userFilter['ID'] = -1;
192 }
193 //{"TITLE":"Test chat 666","AVATAR":55639,"ID":24}
194 }
195 else
196 {
197 return [];
198 }
199
200 // select users
201 $res = \Bitrix\Main\UserTable::getList([
202 'select' => [
203 'ID', 'LOGIN', 'NAME', 'LAST_NAME', 'SECOND_NAME',
204 'WORK_POSITION', 'PERSONAL_PHOTO'
205 ],
206 'filter' => $userFilter
207 ]);
208 while ($user = $res->fetch())
209 {
210 if (Cache::isCaching())
211 {
212 Manager::getCacheManager()->registerTag(
213 'intranet_user_' . $user['ID']
214 );
215 }
216 $data[] = [
217 'ID' => $user['ID'],
218 'WORK_POSITION' => \htmlspecialcharsbx($user['WORK_POSITION']),
219 'NAME' => $name = \htmlspecialcharsbx(\CUser::formatName(
220 \CSite::getNameFormat(),
221 $user, true, false
222 )),
223 'AVATAR' => [
224 'src' => \CIMChat::getAvatarImage($user['PERSONAL_PHOTO']),
225 'alt' => $name
226 ],
227 'SEND' => $sendButton
228 ];
229 }
230
231 return $data;
232 }
233
238 public function getElementListData()
239 {
240 $initData = $this->getInitData();
241 if (!$initData)
242 {
243 return [];
244 }
245
246 return $this->getUserList($initData);
247 }
248
254 public function getElementData($element)
255 {
256 return [[]];
257 }
258}
getUserList(array $initData)
Definition chat.php:166
static getCacheManager()
Definition manager.php:89
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29