Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
lastsearch.php
1<?php
9namespace Bitrix\Im;
10
13
15{
16 const LIMIT = 30;
17 const CACHE_TTL = 31536000;
18 const CACHE_PATH = '/bx/im/search/last/';
19
20 public static function add($dialogId, $userId = null)
21 {
22 $userId = \Bitrix\Im\Common::getUserId($userId);
23 if (!$userId)
24 {
25 return false;
26 }
27
28 if (!$dialogId || $dialogId == 'chat0')
29 {
30 return false;
31 }
32
33 if (!\Bitrix\Im\Common::isDialogId($dialogId))
34 {
35 return false;
36 }
37
38 if (\Bitrix\Im\Common::isChatId($dialogId))
39 {
40 $chatId = \Bitrix\Im\Dialog::getChatId($dialogId);
41 $relations = Chat::getRelation($chatId, ['WITHOUT_COUNTERS' => 'Y']);
42 if (!$relations[$userId])
43 {
44 return false;
45 }
46
47 $relationId = $relations[$userId]['ID'];
48 }
49 else
50 {
51 $relations = \Bitrix\Im\Dialog::getRelation($userId, $dialogId, ['WITHOUT_COUNTERS' => 'Y']);
52 if (!$relations[$userId])
53 {
54 return false;
55 }
56
57 $chatId = $relations[$userId]['CHAT_ID'];
58 $relationId = $relations[$userId]['ID'];
59 }
60
61 $orm = \Bitrix\Im\Model\LastSearchTable::getList(Array(
62 'filter' => Array('USER_ID' => $userId, 'DIALOG_ID' => $dialogId),
63 'order' => Array('ID' => 'DESC')
64 ));
65 if ($orm->fetch())
66 {
67 return true;
68 }
69
70 $result = \Bitrix\Im\Model\LastSearchTable::add(Array(
71 'USER_ID' => $userId,
72 'DIALOG_ID' => $dialogId,
73 'ITEM_CID' => $chatId,
74 'ITEM_RID' => $relationId,
75 ));
76
77 if (!$result->isSuccess())
78 return false;
79
80 $count = 0;
81 $delete = Array();
82
83 $orm = \Bitrix\Im\Model\LastSearchTable::getList(Array(
84 'filter' => Array('USER_ID' => $userId),
85 'order' => Array('ID' => 'DESC')
86 ));
87 while ($row = $orm->fetch())
88 {
89 $count++;
90
91 if ($count > self::LIMIT)
92 {
93 $delete[] = $row['ID'];
94 }
95 }
96
97 foreach ($delete as $id)
98 {
99 \Bitrix\Im\Model\LastSearchTable::delete($id);
100 }
101
102 self::clearCache($userId);
103
104 return $result->getId();
105 }
106
107 public static function delete($dialogId, $userId = null)
108 {
109 $userId = \Bitrix\Im\Common::getUserId($userId);
110 if (!$userId)
111 {
112 return false;
113 }
114
115 if (!$dialogId || $dialogId == 'chat0')
116 {
117 return false;
118 }
119
120 if (!\Bitrix\Im\Common::isDialogId($dialogId))
121 {
122 return false;
123 }
124
125 $orm = \Bitrix\Im\Model\LastSearchTable::getList(Array(
126 'filter' => Array(
127 '=USER_ID' => $userId,
128 '=DIALOG_ID' => $dialogId
129 )
130 ));
131 $row = $orm->fetch();
132 if (!$row)
133 {
134 return false;
135 }
136
137 \Bitrix\Im\Model\LastSearchTable::delete($row['ID']);
138
139 self::clearCache($userId);
140
141 return true;
142 }
143
144 public static function get($userId = null, $options = array())
145 {
146 $userId = \Bitrix\Im\Common::getUserId($userId);
147 if (!$userId)
148 {
149 return false;
150 }
151
152 $result = array();
153
154 $cacheId = 'list_v2_'.$userId.'_'.Color::isEnabled();
155 $cachePath = self::CACHE_PATH.\Bitrix\Im\Common::getCacheUserPostfix($userId);
156
157 $cache = \Bitrix\Main\Application::getInstance()->getCache();
158 $taggedCache = \Bitrix\Main\Application::getInstance()->getTaggedCache();
159 if($cache->initCache(self::CACHE_TTL, $cacheId, $cachePath))
160 {
161 $result = $cache->getVars();
162 }
163 else
164 {
165 $generalChatId = \CIMChat::GetGeneralChatId();
166
167 $select = Array(
168 '*',
169 'RELATION_USER_ID' => 'RELATION.USER_ID',
170 'RELATION_NOTIFY_BLOCK' => 'RELATION.NOTIFY_BLOCK',
171 'CHAT_TITLE' => 'CHAT.TITLE',
172 'CHAT_TYPE' => 'CHAT.TYPE',
173 'CHAT_AVATAR' => 'CHAT.AVATAR',
174 'CHAT_LAST_MESSAGE_STATUS' => 'CHAT.LAST_MESSAGE_STATUS',
175
176 'CHAT_AUTHOR_ID' => 'CHAT.AUTHOR_ID',
177 'CHAT_EXTRANET' => 'CHAT.EXTRANET',
178 'CHAT_COLOR' => 'CHAT.COLOR',
179 'CHAT_ENTITY_TYPE' => 'CHAT.ENTITY_TYPE',
180 'CHAT_ENTITY_ID' => 'CHAT.ENTITY_ID',
181 'CHAT_ENTITY_DATA_1' => 'CHAT.ENTITY_DATA_1',
182 'CHAT_ENTITY_DATA_2' => 'CHAT.ENTITY_DATA_2',
183 'CHAT_ENTITY_DATA_3' => 'CHAT.ENTITY_DATA_3',
184 'CHAT_DATE_CREATE' => 'CHAT.DATE_CREATE',
185 );
186
187 $orm = \Bitrix\Im\Model\LastSearchTable::getList(Array(
188 'select' => $select,
189 'filter' => Array('=USER_ID' => $userId),
190 'order' => Array('ID' => 'DESC')
191 ));
192 while ($row = $orm->fetch())
193 {
194 $isUser = mb_strpos($row['DIALOG_ID'], 'chat') !== 0;
195 $id = $row['DIALOG_ID'];
196
197 $item = Array(
198 'ID' => $isUser? (int)$id: $id,
199 'TYPE' => $isUser? 'user': 'chat',
200 'AVATAR' => Array(),
201 'TITLE' => Array(),
202 );
203
204 if ($isUser)
205 {
206 $item['USER'] = Array(
207 'ID' => (int)$row['DIALOG_ID'],
208 );
209 }
210 else
211 {
212 $avatar = \CIMChat::GetAvatarImage($row['CHAT_AVATAR'], 200, false);
213 $color = $row['CHAT_COLOR'] <> ''? Color::getColor($row['CHAT_COLOR']): Color::getColorByNumber($row['ITEM_ID']);
214 $chatType = \Bitrix\Im\Chat::getType($row);
215
216 if ($generalChatId == $row['ITEM_ID'])
217 {
218 $row["CHAT_ENTITY_TYPE"] = 'GENERAL';
219 }
220
221 $muteList = Array();
222 if ($row['RELATION_NOTIFY_BLOCK'] == 'Y')
223 {
224 $muteList = Array($row['RELATION_USER_ID'] => true);
225 }
226
227 $item['AVATAR'] = Array(
228 'URL' => $avatar,
229 'COLOR' => $color
230 );
231 $item['TITLE'] = $row['CHAT_TITLE'];
232 $item['CHAT'] = Array(
233 'ID' => (int)$row['ITEM_CID'],
234 'NAME' => $row['CHAT_TITLE'],
235 'OWNER' => (int)$row['CHAT_AUTHOR_ID'],
236 'EXTRANET' => $row['CHAT_EXTRANET'] == 'Y',
237 'AVATAR' => $avatar,
238 'COLOR' => $color,
239 'TYPE' => $chatType,
240 'ENTITY_TYPE' => (string)$row['CHAT_ENTITY_TYPE'],
241 'ENTITY_ID' => (string)$row['CHAT_ENTITY_ID'],
242 'ENTITY_DATA_1' => (string)$row['CHAT_ENTITY_DATA_1'],
243 'ENTITY_DATA_2' => (string)$row['CHAT_ENTITY_DATA_2'],
244 'ENTITY_DATA_3' => (string)$row['CHAT_ENTITY_DATA_3'],
245 'MUTE_LIST' => $muteList,
246 'DATE_CREATE' => $row['CHAT_DATE_CREATE'],
247 'MESSAGE_TYPE' => $row["CHAT_TYPE"],
248 );
249 }
250
251 $result[$id] = $item;
252 }
253
254 $taggedCache->startTagCache($cachePath);
255 $taggedCache->registerTag("USER_NAME");
256 $taggedCache->endTagCache();
257
258 $cache->startDataCache();
259 $cache->endDataCache($result);
260 }
261
262 foreach ($result as $id => $item)
263 {
264 if ($options['SKIP_OPENLINES'] == 'Y')
265 {
266 if ($item['TYPE'] == 'chat' && $item['CHAT']['TYPE'] == 'lines')
267 {
268 unset($result[$id]);
269 continue;
270 }
271 }
272 if ($options['SKIP_CHAT'] == 'Y')
273 {
274 if ($item['TYPE'] == 'chat' && $item['CHAT']['TYPE'] != 'lines')
275 {
276 unset($result[$id]);
277 continue;
278 }
279 }
280 if ($options['SKIP_DIALOG'] == 'Y')
281 {
282 if ($item['TYPE'] == 'user')
283 {
284 unset($result[$id]);
285 continue;
286 }
287 }
288
289 if ($item['USER']['ID'] > 0)
290 {
291 $user = User::getInstance($item['USER']['ID'])->getArray();
292 if (!$user)
293 {
294 $user = Array('ID' => 0);
295 }
296 else if ($item['TYPE'] == 'user')
297 {
298 $item['AVATAR'] = Array(
299 'URL' => $user['AVATAR'],
300 'COLOR' => $user['COLOR']
301 );
302 $item['TITLE'] = $user['NAME'];
303 }
304
305 $item['USER'] = $user;
306
307 $result[$id] = $item;
308 }
309 }
310
311 $result = array_values($result);
312
313 if ($options['JSON'])
314 {
315 foreach ($result as $index => $item)
316 {
317 foreach ($item as $key => $value)
318 {
319 if ($value instanceof \Bitrix\Main\Type\DateTime)
320 {
321 $item[$key] = date('c', $value->getTimestamp());
322 }
323 else if (is_array($value))
324 {
325 foreach ($value as $subKey => $subValue)
326 {
327 if ($subValue instanceof \Bitrix\Main\Type\DateTime)
328 {
329 $value[$subKey] = date('c', $subValue->getTimestamp());
330 }
331 else if (is_string($subValue) && $subValue && in_array($subKey, Array('URL', 'AVATAR')) && mb_strpos($subValue, 'http') !== 0)
332 {
333 $value[$subKey] = \Bitrix\Im\Common::getPublicDomain().$subValue;
334 }
335 else if (is_array($subValue))
336 {
337 $value[$subKey] = array_change_key_case($subValue, CASE_LOWER);
338 }
339 }
340 $item[$key] = array_change_key_case($value, CASE_LOWER);
341 }
342 }
343 $result[$index] = array_change_key_case($item, CASE_LOWER);
344 }
345 }
346
347 return $result;
348 }
349
350
351 public static function clearCache($userId = null)
352 {
353 $cache = Application::getInstance()->getCache();
354 $cache->cleanDir(self::CACHE_PATH.($userId? Common::getCacheUserPostfix($userId): ''));
355 }
356}
static getRelation($chatId, $params=[])
Definition chat.php:84
static getColorByNumber($number)
Definition color.php:144
static isEnabled()
Definition color.php:50
static getColor($code)
Definition color.php:121
static isChatId($id)
Definition common.php:64
static getCacheUserPostfix($id)
Definition common.php:59
static isDialogId($id)
Definition common.php:69
static clearCache($userId=null)
static add($dialogId, $userId=null)
static getInstance($userId=null)
Definition user.php:44