1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
counter.php
См. документацию.
1<?php
2namespace Bitrix\Im;
3
4use Bitrix\Im\V2\Message\CounterServiceLegacy;
5use Bitrix\Main\EventResult;
6use Bitrix\Main\Localization\Loc;
7
8class Counter
9{
10 const CACHE_TTL = 86400; // 1 month
11 const CACHE_NAME = 'counter_v3'; // 1 month
12 const CACHE_PATH = '/bx/im/counter/';
13
14 const TYPE_MESSENGER = 'messenger';
15 const TYPE_NOTIFY = 'notify';
16 const MODULE_ID = 'im';
17
18 public static function get($userId = null, $options = [])
19 {
20 if (isset($options['JSON']))
21 {
22 return \Bitrix\Im\Common::toJson((new CounterServiceLegacy($userId))->get());
23 }
24 return (new CounterServiceLegacy($userId))->get();
25 /*
26 $result = [
27 'TYPE' => [
28 'ALL' => 0,
29 'NOTIFY' => 0,
30 'DIALOG' => 0,
31 'CHAT' => 0,
32 'LINES' => 0,
33 ],
34 'DIALOG' => [],
35 'DIALOG_UNREAD' => [],
36 'CHAT' => [],
37 'CHAT_MUTED' => [],
38 'CHAT_UNREAD' => [],
39 'LINES' => [],
40 ];
41
42 $userId = Common::getUserId($userId);
43 if ($userId <= 0)
44 {
45 return $result;
46 }
47
48 $cache = \Bitrix\Main\Data\Cache::createInstance();
49 if ($cache->initCache(self::CACHE_TTL, self::CACHE_NAME.'_'.$userId, self::CACHE_PATH))
50 {
51 $result = $cache->getVars();
52 if (isset($options['JSON']))
53 {
54 $result = \Bitrix\Im\Common::toJson($result);
55 }
56 return $result;
57 }
58
59 $query = "
60 SELECT
61 R1.CHAT_ID,
62 R1.MESSAGE_TYPE,
63 CASE WHEN RC.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."' THEN RC.ITEM_ID ELSE 0 END AS PRIVATE_USER_ID,
64 U.ACTIVE AS PRIVATE_USER_ACTIVE,
65 R1.COUNTER,
66 R1.NOTIFY_BLOCK MUTED,
67 CASE WHEN RC.USER_ID > 0 THEN 'Y' ELSE 'N' END AS IN_RECENT,
68 RC.UNREAD
69 FROM b_im_relation R1
70 LEFT JOIN b_im_recent RC ON RC.ITEM_RID = R1.ID
71 LEFT JOIN b_user U ON RC.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."' AND U.ID = RC.ITEM_ID
72 WHERE R1.USER_ID = ".intval($userId)." AND (R1.STATUS <> ".IM_STATUS_READ." OR RC.UNREAD = 'Y')
73 ";
74 $counters = \Bitrix\Main\Application::getInstance()->getConnection()->query($query)->fetchAll();
75
76 foreach ($counters as $entity)
77 {
78 if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM)
79 {
80 $result['TYPE']['ALL'] += (int)$entity['COUNTER'];
81 $result['TYPE']['NOTIFY'] += (int)$entity['COUNTER'];
82 }
83 else
84 {
85 if ($entity['IN_RECENT'] == 'N')
86 {
87 continue;
88 }
89 if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
90 {
91 if ($entity['PRIVATE_USER_ACTIVE'] === 'N')
92 {
93 continue;
94 }
95
96 if ($entity['COUNTER'] > 0)
97 {
98 $result['TYPE']['ALL'] += (int)$entity['COUNTER'];
99 $result['TYPE']['DIALOG'] += (int)$entity['COUNTER'];
100 $result['DIALOG'][$entity['PRIVATE_USER_ID']] = (int)$entity['COUNTER'];
101 }
102 else if ($entity['UNREAD'] === 'Y')
103 {
104 $result['TYPE']['ALL']++;
105 $result['TYPE']['DIALOG']++;
106 $result['DIALOG_UNREAD'][] = (int)$entity['PRIVATE_USER_ID'];
107 }
108 }
109 else if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
110 {
111 $result['TYPE']['ALL'] += (int)$entity['COUNTER'];
112 $result['TYPE']['LINES'] += (int)$entity['COUNTER'];
113 $result['LINES'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
114 }
115 else
116 {
117 if ($entity['COUNTER'] > 0)
118 {
119 if ($entity['MUTED'] === 'N')
120 {
121 $result['TYPE']['ALL'] += (int)$entity['COUNTER'];
122 $result['TYPE']['CHAT'] += (int)$entity['COUNTER'];
123 $result['CHAT'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
124 }
125 else
126 {
127 $result['CHAT_MUTED'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
128 }
129 }
130 else if ($entity['UNREAD'] === 'Y')
131 {
132 if ($entity['MUTED'] === 'N')
133 {
134 $result['TYPE']['ALL']++;
135 $result['TYPE']['CHAT']++;
136 }
137 $result['CHAT_UNREAD'][] = (int)$entity['CHAT_ID'];
138 }
139
140 }
141 }
142 }
143
144 $cache->startDataCache();
145 $cache->endDataCache($result);
146
147 if (isset($options['JSON']))
148 {
149 $result = \Bitrix\Im\Common::toJson($result);
150 }
151
152 return $result;
153 */
154 }
155
156 public static function clearCache($userId = null)
157 {
158 $cache = \Bitrix\Main\Data\Cache::createInstance();
159 if ($userId)
160 {
161 $cache->clean(self::CACHE_NAME.'_'.$userId, self::CACHE_PATH);
162 }
163 else
164 {
165 $cache->cleanDir(self::CACHE_PATH);
166 }
167
168 return true;
169 }
170
171 public static function getChatCounter($chatId, $userId = null)
172 {
173 $chatId = intval($chatId);
175
176 if ($chatId <= 0 || $userId <= 0)
177 {
178 return false;
179 }
180
181 $counters = self::get($userId);
182
183 return intval($counters['CHAT'][$chatId]);
184 }
185
186 public static function getDialogCounter($opponentUserId, $userId = null)
187 {
189 $opponentUserId = intval($opponentUserId);
190 if ($userId <= 0 || $opponentUserId <= 0)
191 {
192 return false;
193 }
194
195 $counters = self::get($userId);
196
197 return intval($counters['DIALOG'][$opponentUserId]);
198 }
199
200 public static function getNotifyCounter($userId = null)
201 {
203 if ($userId <= 0)
204 {
205 return false;
206 }
207
208 $counters = self::get($userId);
209
210 return intval($counters['TYPE']['NOTIFY']);
211 }
212
213 public static function countingLostCountersAgent($notifyRelationId = 0, $chatRelationId = 0)
214 {
215 return '';
216
217 /*
218 $foundNotify = false;
219 $foundChat = false;
220
221 $notifyStartId = intval($notifyRelationId);
222
223 if ($notifyStartId >= 0)
224 {
225 $query = "
226 SELECT COUNT(1) CNT, R.ID, R.USER_ID
227 FROM b_im_message M
228 INNER JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND R.COUNTER = 0
229 WHERE M.NOTIFY_READ <> 'Y' AND R.ID > ".$notifyStartId."
230 GROUP BY R.ID, R.USER_ID
231 HAVING CNT > 0
232 ";
233 $cursor = \Bitrix\Main\Application::getInstance()->getConnection()->query($query);
234
235 $count = 0;
236 while ($row = $cursor->fetch())
237 {
238 $notifyRelationId = $row['ID'];
239 $foundNotify = true;
240
241 \Bitrix\Im\Model\RelationTable::update($row['ID'], Array(
242 'STATUS' => IM_STATUS_UNREAD,
243 "MESSAGE_STATUS" => IM_MESSAGE_STATUS_RECEIVED,
244 'COUNTER' => $row['CNT'],
245 ));
246
247 $count++;
248 if ($count > 100)
249 {
250 break;
251 }
252 }
253 }
254
255 $chatRelationId = intval($chatRelationId);
256
257 if ($chatRelationId >= 0)
258 {
259 $query = "
260 SELECT R.ID, R.COUNTER PREVIOUS_COUNTER, (
261 SELECT COUNT(1) FROM b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.ID > R.LAST_ID
262 ) COUNTER
263 FROM b_im_relation R
264 WHERE R.STATUS <> ".IM_STATUS_READ." AND R.COUNTER = 0 AND R.ID > ".$chatRelationId."
265 ORDER BY R.ID ASC
266 LIMIT 0, 100;
267 ";
268 $cursor = \Bitrix\Main\Application::getInstance()->getConnection()->query($query);
269
270 while ($row = $cursor->fetch())
271 {
272 $chatRelationId = $row['ID'];
273 $foundChat = true;
274
275 if ($row['COUNTER'] == 0)
276 {
277 $update = Array(
278 'STATUS' => IM_STATUS_READ,
279 "MESSAGE_STATUS" => IM_MESSAGE_STATUS_RECEIVED,
280 );
281 }
282 else if ($row['PREVIOUS_COUNTER'] == $row['COUNTER'])
283 {
284 continue;
285 }
286 else
287 {
288 $update = Array(
289 'COUNTER' => $row['COUNTER']
290 );
291 }
292 \Bitrix\Im\Model\RelationTable::update($row['ID'], $update);
293 }
294 }
295
296 if ($foundNotify || $foundChat)
297 {
298 return '\Bitrix\Im\Counter::countingLostCountersAgent('.($foundNotify? $notifyRelationId: -1).', '.($foundChat? $chatRelationId: -1).');';
299 }
300 else
301 {
302 return '';
303 }
304 */
305 }
306
308 {
309 return new EventResult(
310 EventResult::SUCCESS,
311 [
312 self::TYPE_MESSENGER => [
313 'NAME' => Loc::getMessage('IM_COUNTER_TYPE_MESSENGER'),
314 'DEFAULT' => true
315 ],
316 self::TYPE_NOTIFY => [
317 'NAME' => Loc::getMessage('IM_COUNTER_TYPE_NOTIFY'),
318 'DEFAULT' => false
319 ],
320 ],
321 self::MODULE_ID
322 );
323 }
324
325 public static function onGetMobileCounter(\Bitrix\Main\Event $event)
326 {
327 $params = $event->getParameters();
328
329 $counters = self::get($params['USER_ID']);
330
331 $messengerCounter = $counters['TYPE']['MESSENGER'] ?? $counters['TYPE']['ALL'];
332 $notifyCounter = isset($counters['TYPE']['MESSENGER']) ? $counters['TYPE']['NOTIFY'] : 0;
333
334 $mobileCounters = [
335 [
336 'TYPE' => self::TYPE_MESSENGER,
337 'COUNTER' => $messengerCounter,
338 ],
339 [
340 'TYPE' => self::TYPE_NOTIFY,
341 'COUNTER' => $notifyCounter,
342 ],
343 ];
344
345 return new EventResult(EventResult::SUCCESS, $mobileCounters, self::MODULE_ID);
346 }
347}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static countingLostCountersAgent($notifyRelationId=0, $chatRelationId=0)
Определения counter.php:213
const CACHE_PATH
Определения counter.php:12
const CACHE_NAME
Определения counter.php:11
static clearCache($userId=null)
Определения counter.php:156
const MODULE_ID
Определения counter.php:16
const TYPE_NOTIFY
Определения counter.php:15
static onGetMobileCounterTypes(\Bitrix\Main\Event $event)
Определения counter.php:307
static getDialogCounter($opponentUserId, $userId=null)
Определения counter.php:186
static getNotifyCounter($userId=null)
Определения counter.php:200
const TYPE_MESSENGER
Определения counter.php:14
static getChatCounter($chatId, $userId=null)
Определения counter.php:171
static onGetMobileCounter(\Bitrix\Main\Event $event)
Определения counter.php:325
const CACHE_TTL
Определения counter.php:10
$options
Определения commerceml2.php:49
$event
Определения prolog_after.php:141
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$counters
Определения options.php:100