Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
recentchatprovider.php
1<?php
3
10
12{
13 protected const ITEM_TYPE_CHAT = 'chat';
14 protected const ITEM_TYPE_USER = 'user';
15
16 public function __construct(array $options = [])
17 {
18 parent::__construct();
19
20 if (isset($options['limit']) && is_int($options['limit']))
21 {
22 $this->options['limit'] = $options['limit'];
23 }
24 }
25
26 public function isAvailable(): bool
27 {
28 return $GLOBALS['USER']->isAuthorized();
29 }
30
31 public function fillDialog(Dialog $dialog): void
32 {
33 $recentList = \Bitrix\Im\Recent::getList(null, [
34 'LIMIT' => $this->getOption('limit', 50),
35 'OFFSET' => 0,
36 ]);
37
38 $items = (is_array($recentList) && isset($recentList['ITEMS']) && is_array($recentList['ITEMS']))
39 ? $recentList['ITEMS']
40 : []
41 ;
42
43 $dialog->addRecentItems($this->makeRecentChatItems($items));
44 }
45
46 public function makeRecentChatItems(array $items, array $options = []): array
47 {
48 return self::makeItems($items, array_merge($this->getOptions(), $options));
49 }
50
51 public function getItems(array $ids): array
52 {
53 return [];
54 }
55
56 public function getSelectedItems(array $ids): array
57 {
58 return [];
59 }
60
61 public static function makeItems(array $items, array $options = []): array
62 {
63 $result = [];
64 foreach ($items as $item)
65 {
66 $result[] = static::makeItem($item, $options);
67 }
68
69 return $result;
70 }
71
72 public static function makeItem(array $item, array $options = []): Item
73 {
74 $itemOptions = [
75 'id' => $item['ID'],
76 'entityId' => self::getEntityIdByItem($item),
77 'entityType' => self::getEntityTypeByItem($item),
78 'title' => $item['TITLE'],
79 'avatar' => $item['AVATAR']['URL'],
80 'customData' => self::getCustomDataByItem($item),
81 'saveable' => false,
82 ];
83
84 return new Item($itemOptions);
85 }
86
87 protected static function getEntityIdByItem(array $item): string
88 {
89 if ($item['TYPE'] === self::ITEM_TYPE_USER && !$item['USER']['BOT'])
90 {
91 return $item['TYPE'];
92 }
93
94 if ($item['TYPE'] === self::ITEM_TYPE_USER && $item['USER']['BOT'])
95 {
96 return 'im-bot';
97 }
98
99 return 'im-' . $item['TYPE'];
100 }
101
102 protected static function getEntityTypeByItem(array $item): string
103 {
104 if ($item['TYPE'] === self::ITEM_TYPE_CHAT)
105 {
106 return self::getEntityTypeByChat($item['CHAT']);
107 }
108
109 if ($item['TYPE'] === self::ITEM_TYPE_USER)
110 {
111 return self::getEntityTypeByUser($item['USER']);
112 }
113
114 return '';
115 }
116
117 protected static function getCustomDataByItem(array $item): array
118 {
119 $customData = [];
120 if ($item['TYPE'] === self::ITEM_TYPE_CHAT)
121 {
122 $customData['imChat'] = $item['CHAT'] ?: [];
123
124 return $customData;
125 }
126
127 if ($item['TYPE'] === self::ITEM_TYPE_USER)
128 {
129 $customData['imUser'] = $item['USER'] ?: [];
130
131 if ($item['USER'] && $item['USER']['BOT'])
132 {
133 $customData['imBot'] = \Bitrix\Im\Bot::getCache($item['ID']) ?: [];
134 }
135 }
136
137 return $customData;
138 }
139
140 protected static function getEntityTypeByChat(array $chat): string
141 {
142 $entityType = $chat['ENTITY_TYPE'];
143 if ($entityType !== '' && $entityType !== null)
144 {
145 return $entityType;
146 }
147
148 $type = $chat['MESSAGE_TYPE'];
149 switch ($type)
150 {
151 case \Bitrix\Im\Chat::TYPE_GROUP:
152 return 'GROUP';
153
154 case \Bitrix\Im\Chat::TYPE_OPEN:
155 return 'CHANNEL';
156 }
157
158 return '';
159 }
160
161 protected static function getEntityTypeByUser(array $user): string
162 {
163 if (!$user['ACTIVE'])
164 {
165 $type = 'inactive';
166 }
167 else if ($user['EXTERNAL_AUTH_ID'] === 'email')
168 {
169 $type = 'email';
170 }
171 else if ($user['EXTERNAL_AUTH_ID'] === 'replica')
172 {
173 $type = 'network';
174 }
175 else if (!in_array($user['EXTERNAL_AUTH_ID'], UserTable::getExternalUserTypes(), true))
176 {
177 if (ModuleManager::isModuleInstalled('intranet'))
178 {
179 if (UserProvider::isIntegrator($user['ID']))
180 {
181 $type = 'integrator';
182 }
183 else
184 {
185 $ufDepartment = $user['DEPARTMENTS'];
186 if (
187 empty($ufDepartment)
188 || (
189 is_array($ufDepartment)
190 && count($ufDepartment) === 1
191 && (int)$ufDepartment[0] === 0
192 )
193 )
194 {
195 $type = 'extranet';
196 }
197 else
198 {
199 $type = 'employee';
200 }
201 }
202 }
203 else
204 {
205 $type = 'user';
206 }
207 }
208 else
209 {
210 $type = 'unknown';
211 }
212
213 return $type;
214 }
215}
static isModuleInstalled($moduleName)
getOption(string $option, $defaultValue=null)
$GLOBALS['____1444769544']
Definition license.php:1