1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
user_counter.php
См. документацию.
1<?php
2
4
6{
7 public const ALL_SITES = '**';
8 public const LIVEFEED_CODE = '**';
9 public const SYSTEM_USER_ID = 0;
10
11 protected static $counters = [];
12 private static $isLiveFeedJobOn = false;
13
14 public static function GetValue($user_id, $code, $site_id = SITE_ID)
15 {
16 $user_id = (int)$user_id;
17
18 if ($user_id < 0)
19 {
20 return false;
21 }
22
24 if (isset($arCodes[$code]))
25 {
26 return (int)$arCodes[$code];
27 }
28
29 return 0;
30 }
31
32 public static function GetValues($user_id, $site_id = SITE_ID, &$arLastDate = [])
33 {
34 static $diff;
35
36 $user_id = (int)$user_id;
37 if ($user_id < 0)
38 {
39 return [];
40 }
41
42 if (!is_array($arLastDate))
43 {
44 $arLastDate = [];
45 }
46
47 if ($diff === false)
48 {
49 $diff = CTimeZone::GetOffset();
50 }
51
52 if (!isset(self::$counters[$user_id][$site_id]))
53 {
54 $arAll = self::getValuesFromDB($user_id);
55
56 if (is_array($arAll))
57 {
58 foreach($arAll as $arItem)
59 {
60 if (
61 $arItem["SITE_ID"] == $site_id
62 || $arItem["SITE_ID"] === self::ALL_SITES
63 )
64 {
65 if (!isset(self::$counters[$user_id][$site_id][$arItem["CODE"]]))
66 {
67 self::$counters[$user_id][$site_id][$arItem["CODE"]] = 0;
68 }
69 self::$counters[$user_id][$site_id][$arItem["CODE"]] += $arItem["CNT"];
70
71 if (!isset($arLastDate[$user_id]))
72 {
73 $arLastDate[$user_id] = [];
74 }
75 if (!isset($arLastDate[$user_id][$site_id]))
76 {
77 $arLastDate[$user_id][$site_id] = [];
78 }
79
80 if (isset($arItem["LAST_DATE_TS"]))
81 {
82 $arLastDate[$user_id][$site_id][$arItem["CODE"]] = $arItem["LAST_DATE_TS"] - $diff;
83 }
84 }
85 }
86 }
87 }
88
89 return (self::$counters[$user_id][$site_id] ?? []);
90 }
91
92 public static function GetAllValues($user_id)
93 {
94 $arCounters = [];
95 $user_id = (int)$user_id;
96 if ($user_id < 0)
97 {
98 return $arCounters;
99 }
100
101 $arSites = Array();
102 $by = '';
103 $order = '';
104 $res = CSite::GetList($by, $order, Array("ACTIVE" => "Y"));
105 while ($row = $res->Fetch())
106 {
107 $arSites[] = $row['ID'];
108 }
109
110 $arAll = self::getValuesFromDB($user_id);
111
112 foreach ($arAll as $arItem)
113 {
114 if ($arItem['SITE_ID'] === self::ALL_SITES)
115 {
116 foreach ($arSites as $siteId)
117 {
118 if (isset($arCounters[$siteId][$arItem['CODE']]))
119 {
120 $arCounters[$siteId][$arItem['CODE']] += (int)$arItem['CNT'];
121 }
122 else
123 {
124 $arCounters[$siteId][$arItem['CODE']] = (int)$arItem['CNT'];
125 }
126 }
127 }
128 elseif (isset($arCounters[$arItem['SITE_ID']][$arItem['CODE']]))
129 {
130 $arCounters[$arItem['SITE_ID']][$arItem['CODE']] += (int)$arItem['CNT'];
131 }
132 else
133 {
134 $arCounters[$arItem['SITE_ID']][$arItem['CODE']] = (int)$arItem['CNT'];
135 }
136 }
137
138 return $arCounters;
139 }
140
141 private static function getValuesFromDB(int $userId = 0)
142 {
143 global $CACHE_MANAGER, $DB;
144
145 if (
146 CACHED_b_user_counter !== false
147 && $CACHE_MANAGER->read(CACHED_b_user_counter, 'user_counter' . $userId, 'user_counter')
148 )
149 {
150 $result = $CACHE_MANAGER->get('user_counter' . $userId);
151 }
152 else
153 {
154 $strSQL = '
155 SELECT CODE, SITE_ID, CNT, ' . $DB->datetimeToTimestampFunction('LAST_DATE') . ' LAST_DATE_TS
156 FROM b_user_counter
157 WHERE USER_ID = ' . $userId;
158
159 $res = $DB->query($strSQL);
160 $result = [];
161 while ($rowFields = $res->fetch())
162 {
163 $rowFields['CODE'] = self::getGroupedCode($rowFields['CODE']);
164 $result[] = $rowFields;
165 }
166
167 $result = self::getValuesForCache($result);
168
169 if (CACHED_b_user_counter !== false)
170 {
171 $CACHE_MANAGER->set('user_counter' . $userId, $result);
172 }
173 }
174
175 return $result;
176 }
177
178 private static function getValuesForCache(array $counters = []): array
179 {
180 $cachedCounters = [];
181
182 foreach ($counters as $counter)
183 {
184 $key = $counter['CODE'] . '_' . $counter['SITE_ID'];
185
186 if (!isset($cachedCounters[$key]))
187 {
188 $cachedCounters[$key] = $counter;
189 $cachedCounters[$key]['CNT'] = (int)$counter['CNT'];
190 }
191 else
192 {
193 $cachedCounters[$key]['CNT'] += (int)$counter['CNT'];
194 if (isset($counter['LAST_DATE_TS']))
195 {
196 $cachedCounters[$key]['LAST_DATE_TS'] = $counter['LAST_DATE_TS'];
197 }
198 }
199 }
200
201 return array_values($cachedCounters);
202 }
203
204 public static function GetLastDate($user_id, $code, $site_id = SITE_ID)
205 {
206 global $DB;
207
208 $user_id = (int)$user_id;
209 if ($user_id < 0 || $code == '')
210 {
211 return 0;
212 }
213
214 $strSQL = "
215 SELECT " . $DB->DateToCharFunction("LAST_DATE") . " LAST_DATE
216 FROM b_user_counter
217 WHERE USER_ID = ".$user_id."
218 AND (SITE_ID = '".$site_id."' OR SITE_ID = '" . self::ALL_SITES . "')
219 AND CODE = '" . $DB->ForSql($code) . "'
220 ";
221
222 $result = 0;
223 $dbRes = $DB->Query($strSQL);
224 if ($arRes = $dbRes->Fetch())
225 {
226 $result = MakeTimeStamp($arRes["LAST_DATE"]);
227 }
228
229 return $result;
230 }
231
232 public static function ClearAll($user_id, $site_id = SITE_ID, $sendPull = true)
233 {
234 global $DB, $CACHE_MANAGER;
235
236 $user_id = (int)$user_id;
237 if ($user_id < 0)
238 {
239 return false;
240 }
241
242 $strSQL = "
243 UPDATE b_user_counter SET
244 CNT = 0
245 WHERE USER_ID = ".$user_id."
246 AND (SITE_ID = '".$site_id."' OR SITE_ID = '".self::ALL_SITES."')";
247 $DB->Query($strSQL);
248
249 if ($site_id === self::ALL_SITES)
250 {
251 if (self::$counters)
252 {
253 unset(self::$counters[$user_id]);
254 }
255 }
256 elseif (self::$counters)
257 {
258 unset(self::$counters[$user_id][$site_id]);
259 }
260
261 $CACHE_MANAGER->Clean("user_counter" . $user_id, "user_counter");
262
263 if ($sendPull)
264 {
265 self::SendPullEvent($user_id);
266 }
267
268 return true;
269 }
270
271 public static function ClearByTag($tag, $code, $site_id = SITE_ID, $sendPull = true)
272 {
273 global $DB, $CACHE_MANAGER;
274
275 if ($tag == '' || $code == '')
276 {
277 return false;
278 }
279
280 $strSQL = "
281 UPDATE b_user_counter SET
282 CNT = 0
283 WHERE TAG = '" . $DB->ForSQL($tag) . "' AND CODE = '" . $DB->ForSQL($code) . "'
284 AND (SITE_ID = '" . $site_id . "' OR SITE_ID = '" . self::ALL_SITES . "')";
285 $DB->Query($strSQL);
286
287 self::$counters = [];
288 $CACHE_MANAGER->CleanDir("user_counter");
289
290 if ($sendPull && self::CheckLiveMode())
291 {
292 global $DB;
293
294 $arSites = Array();
295 $by = '';
296 $order = '';
297 $res = CSite::GetList($by, $order, Array("ACTIVE" => "Y"));
298 while ($row = $res->Fetch())
299 {
300 $arSites[] = $row['ID'];
301 }
302
303 $helper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
304
305 $strSQL = "
306 SELECT uc.USER_ID as CHANNEL_ID, 'private' as CHANNEL_TYPE, uc.USER_ID, uc.SITE_ID, uc.CODE, uc.CNT
307 FROM b_user_counter uc
308 INNER JOIN b_user u ON u.ID = uc.USER_ID AND (CASE WHEN u.EXTERNAL_AUTH_ID IN ('" . implode("', '", \Bitrix\Main\UserTable::getExternalUserTypes())."') THEN 'Y' ELSE 'N' END) = 'N' AND u.LAST_ACTIVITY_DATE > " . $helper->addSecondsToDateTime('(-3600)')."
309 WHERE TAG = '".$DB->ForSQL($tag)."' AND CODE = '".$DB->ForSQL($code)."'
310 AND (SITE_ID = '".$site_id."' OR SITE_ID = '" . self::ALL_SITES . "')";
311
312 $res = $DB->Query($strSQL);
313
314 $pullMessage = [];
315 while ($row = $res->Fetch())
316 {
317 if (!(
318 $row['CHANNEL_TYPE'] === 'private'
319 || (
320 $row['CHANNEL_TYPE'] === 'shared'
321 && (int)$row['USER_ID'] === 0
322 )
323 ))
324 {
325 continue;
326 }
327
328 if ($row['SITE_ID'] === self::ALL_SITES)
329 {
330 foreach ($arSites as $siteId)
331 {
332 if (isset($pullMessage[$row['CHANNEL_ID']][$siteId][$row['CODE']]))
333 {
334 $pullMessage[$row['CHANNEL_ID']][$siteId][$row['CODE']] += (int)$row['CNT'];
335 }
336 else
337 {
338 $pullMessage[$row['CHANNEL_ID']][$siteId][$row['CODE']] = (int)$row['CNT'];
339 }
340 }
341 }
342 elseif (isset($pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$row['CODE']]))
343 {
344 $pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$row['CODE']] += (int)$row['CNT'];
345 }
346 else
347 {
348 $pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$row['CODE']] = (int)$row['CNT'];
349 }
350 }
351
352 foreach ($pullMessage as $channelId => $arMessage)
353 {
355 'module_id' => 'main',
356 'command' => 'user_counter',
357 'expiry' => 3600,
358 'params' => $arMessage,
359 ));
360 }
361 }
362
363 return true;
364 }
365
366 public static function CheckLiveMode()
367 {
368 return (
369 CModule::IncludeModule('pull')
370 && CPullOptions::GetNginxStatus()
371 );
372 }
373
374 protected static function SendPullEvent($user_id, $code = "", $bMultiple = false)
375 {
376 $user_id = (int)$user_id;
377 if ($user_id < 0)
378 {
379 return false;
380 }
381
382 if (self::CheckLiveMode())
383 {
384 global $DB;
385
386 $arSites = Array();
387 $by = '';
388 $order = '';
389 $res = CSite::GetList($by, $order, Array("ACTIVE" => "Y"));
390 while ($row = $res->Fetch())
391 {
392 $arSites[] = $row['ID'];
393 }
394
395 $helper = \Bitrix\Main\Application::getConnection()->getSqlHelper();
396 $strSQL = "
397 SELECT uc.USER_ID as CHANNEL_ID, 'private' as CHANNEL_TYPE, uc.USER_ID, uc.SITE_ID, uc.CODE, uc.CNT
398 FROM b_user_counter uc
399 INNER JOIN b_user u ON u.ID = uc.USER_ID AND (CASE WHEN u.EXTERNAL_AUTH_ID IN ('" . implode("', '", \Bitrix\Main\UserTable::getExternalUserTypes())."') THEN 'Y' ELSE 'N' END) = 'N' AND u.LAST_ACTIVITY_DATE > ".$helper->addSecondsToDateTime('(-3600)')."
400 WHERE uc.USER_ID = " . (int)$user_id
401 .(
402 $code <> ''
403 ? (
404 $bMultiple
405 ? " AND uc.CODE LIKE '".$DB->ForSQL($code)."%'"
406 : " AND uc.CODE = '".$DB->ForSQL($code)."'"
407 )
408 : ""
409 )."
410 ";
411 $res = $DB->Query($strSQL);
412
413 $pullMessage = Array();
414 while ($row = $res->Fetch())
415 {
416 $key = ($code <> '' ? $code : $row['CODE']);
417
418 if (!(
419 $row['CHANNEL_TYPE'] === 'private'
420 || (
421 $row['CHANNEL_TYPE'] === 'shared'
422 && (int)$row['USER_ID'] === 0
423 )
424 ))
425 {
426 continue;
427 }
428 if ($row['SITE_ID'] === self::ALL_SITES)
429 {
430 foreach ($arSites as $siteId)
431 {
432 if (isset($pullMessage[$row['CHANNEL_ID']][$siteId][$key]))
433 {
434 $pullMessage[$row['CHANNEL_ID']][$siteId][$key] += (int)$row['CNT'];
435 }
436 else
437 {
438 $pullMessage[$row['CHANNEL_ID']][$siteId][$key] = (int)$row['CNT'];
439 }
440 }
441 }
442 elseif (isset($pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$key]))
443 {
444 $pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$key] += (int)$row['CNT'];
445 }
446 else
447 {
448 $pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$key] = (int)$row['CNT'];
449 }
450 }
451
452 foreach ($pullMessage as $channelId => $arMessage)
453 {
455 'module_id' => 'main',
456 'command' => 'user_counter',
457 'expiry' => 3600,
458 'params' => $arMessage,
459 ));
460 }
461 }
462 }
463
464 public static function addValueToPullMessage($row, $arSites = array(), &$pullMessage = [])
465 {
466 $code = self::getGroupedCode($row["CODE"]);
467
468 if ($row['SITE_ID'] === self::ALL_SITES)
469 {
470 foreach($arSites as $siteId)
471 {
472 if (isset($pullMessage[$row['CHANNEL_ID']][$siteId][$code]))
473 {
474 $pullMessage[$row['CHANNEL_ID']][$siteId][$code] += (int)$row['CNT'];
475 }
476 else
477 {
478 $pullMessage[$row['CHANNEL_ID']][$siteId][$code] = (int)$row['CNT'];
479 }
480 }
481 }
482 elseif (isset($pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$code]))
483 {
484 $pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$code] += (int)$row['CNT'];
485 }
486 else
487 {
488 $pullMessage[$row['CHANNEL_ID']][$row['SITE_ID']][$code] = (int)$row['CNT'];
489 }
490 }
491
492 public static function getGroupedCounters($counters)
493 {
494 $result = array();
495
496 foreach ($counters as $siteId => $data)
497 {
499 }
500
501 return $result;
502 }
503
504 public static function getGroupedCounterRecords($records)
505 {
506 $result = array();
507
508 foreach ($records as $code => $value)
509 {
510 $code = self::getGroupedCode($code);
511
512 if (isset($result[$code]))
513 {
514 $result[$code] += $value;
515 }
516 else
517 {
518 $result[$code] = $value;
519 }
520 }
521
522 return $result;
523 }
524
525 private static function getGroupedCode($code)
526 {
527 $result = $code;
528
529 if (mb_strpos($code, self::LIVEFEED_CODE) === 0)
530 {
531 $result = self::LIVEFEED_CODE;
532 }
533
534 return $result;
535 }
536
537 public static function OnSocNetLogCommentDelete($commentId)
538 {
539 CUserCounter::DeleteByCode(self::LIVEFEED_CODE . 'LC' . (int)$commentId);
540 }
541
542 public static function OnSocNetLogDelete($logId)
543 {
544 CUserCounter::DeleteByCode(self::LIVEFEED_CODE . 'L' . (int)$logId);
545 }
546
547 // legacy function
548 public static function GetValueByUserID($user_id, $site_id = SITE_ID, $code = self::ALL_SITES)
549 {
550 return self::GetValue($user_id, $code, $site_id);
551 }
552 public static function GetCodeValuesByUserID($user_id, $site_id = SITE_ID)
553 {
554 return self::GetValues($user_id, $site_id);
555 }
556 public static function GetLastDateByUserAndCode($user_id, $site_id = SITE_ID, $code = self::ALL_SITES)
557 {
558 return self::GetLastDate($user_id, $code, $site_id);
559 }
560
561 public static function DeleteOld()
562 {
563 global $DB;
564
565 $days = (int)Option::get('main', 'user_counter_days', 20);
566
567 $time = $DB->CharToDateFunction(GetTime(time()- $days*60*60*24, "FULL"));
568 $DB->Query("DELETE FROM b_user_counter WHERE TIMESTAMP_X <= ".$time);
569
570 return "CUserCounter::DeleteOld();";
571 }
572
573 public static function Set($user_id, $code, $value, $site_id = SITE_ID, $tag = '', $sendPull = true)
574 {
575 global $DB, $CACHE_MANAGER;
576
577 $value = (int)$value;
578 $user_id = (int)$user_id;
579 if ($user_id < 0 || $code == '')
580 {
581 return false;
582 }
583
584 $rs = $DB->Query("
585 SELECT CNT FROM b_user_counter
586 WHERE USER_ID = ".$user_id."
587 AND SITE_ID = '".$DB->ForSQL($site_id)."'
588 AND CODE = '".$DB->ForSQL($code)."'
589 ");
590
591 if ($cntVal = $rs->Fetch())
592 {
593 $ssql = "";
594 if ($tag != "")
595 {
596 $ssql = ", TAG = '".$DB->ForSQL($tag)."'";
597 }
598
599 if($cntVal['CNT'] != $value)
600 {
601 $DB->Query("
602 UPDATE b_user_counter SET
603 CNT = " . $value . " " . $ssql . ",
604 SENT = 0
605 WHERE USER_ID = " . $user_id . "
606 AND SITE_ID = '" . $DB->ForSQL($site_id) . "'
607 AND CODE = '" . $DB->ForSQL($code) . "'
608 ");
609 }
610 else
611 {
612 $sendPull = false;
613 }
614 }
615 else
616 {
617 $DB->Query("
618 INSERT INTO b_user_counter
619 (CNT, USER_ID, SITE_ID, CODE, TAG)
620 VALUES
621 (".$value.", ".$user_id.", '".$DB->ForSQL($site_id)."', '".$DB->ForSQL($code)."', '".$DB->ForSQL($tag)."')
622 ", true);
623 }
624
625 if (self::$counters && isset(self::$counters[$user_id]))
626 {
627 if ($site_id === self::ALL_SITES)
628 {
629 foreach(self::$counters[$user_id] as $key => $tmp)
630 {
631 self::$counters[$user_id][$key][$code] = $value;
632 }
633 }
634 else
635 {
636 if (!isset(self::$counters[$user_id][$site_id]))
637 {
638 self::$counters[$user_id][$site_id] = [];
639 }
640
641 self::$counters[$user_id][$site_id][$code] = $value;
642 }
643 }
644
645 $CACHE_MANAGER->Clean("user_counter" . $user_id, "user_counter");
646
647 if ($sendPull)
648 {
649 self::SendPullEvent($user_id, $code);
650 }
651
652 return true;
653 }
654
655 public static function Increment($user_id, $code, $site_id = SITE_ID, $sendPull = true, $increment = 1)
656 {
657 global $CACHE_MANAGER;
659 $helper = $connection->getSqlHelper();
660
661 $user_id = (int)$user_id;
662 if ($user_id < 0 || $code == '')
663 {
664 return false;
665 }
666
667 $increment = (int)$increment;
668
669 $merge = $helper->prepareMerge('b_user_counter', ['USER_ID', 'SITE_ID', 'CODE'], [
670 'USER_ID' => $user_id,
671 'SITE_ID' => $site_id,
672 'CODE' => $code,
673 'CNT' => $increment,
674 ], [
675 'CNT' => new \Bitrix\Main\DB\SqlExpression('b_user_counter.CNT + ' . $increment),
676 ]);
677 if ($merge[0])
678 {
679 $connection->query($merge[0]);
680 }
681
682 if (isset(self::$counters[$user_id]) && is_array(self::$counters[$user_id]))
683 {
684 if ($site_id === self::ALL_SITES)
685 {
686 foreach(self::$counters[$user_id] as $key => $tmp)
687 {
688 if (isset(self::$counters[$user_id][$key][$code]))
689 {
690 self::$counters[$user_id][$key][$code] += $increment;
691 }
692 else
693 {
694 self::$counters[$user_id][$key][$code] = $increment;
695 }
696 }
697 }
698 else
699 {
700 if (!isset(self::$counters[$user_id][$site_id]))
701 {
702 self::$counters[$user_id][$site_id] = [];
703 }
704
705 if (isset(self::$counters[$user_id][$site_id][$code]))
706 {
707 self::$counters[$user_id][$site_id][$code] += $increment;
708 }
709 else
710 {
711 self::$counters[$user_id][$site_id][$code] = $increment;
712 }
713 }
714 }
715 $CACHE_MANAGER->Clean("user_counter".$user_id, "user_counter");
716
717 if ($sendPull)
718 {
719 self::SendPullEvent($user_id, $code);
720 }
721
722 return true;
723 }
724
734 public static function Decrement($user_id, $code, $site_id = SITE_ID, $sendPull = true, $decrement = 1)
735 {
736 global $CACHE_MANAGER;
738 $helper = $connection->getSqlHelper();
739
740 $user_id = (int)$user_id;
741 if ($user_id < 0 || $code == '')
742 {
743 return false;
744 }
745
746 $decrement = (int)$decrement;
747
748 $merge = $helper->prepareMerge('b_user_counter', ['USER_ID', 'SITE_ID', 'CODE'], [
749 'USER_ID' => $user_id,
750 'SITE_ID' => $site_id,
751 'CODE' => $code,
752 'CNT' => -$decrement,
753 ], [
754 'CNT' => new \Bitrix\Main\DB\SqlExpression('b_user_counter.CNT - ' . $decrement),
755 ]);
756 if ($merge[0])
757 {
758 $connection->query($merge[0]);
759 }
760
761 if (self::$counters && self::$counters[$user_id])
762 {
763 if ($site_id === self::ALL_SITES)
764 {
765 foreach (self::$counters[$user_id] as $key => $tmp)
766 {
767 if (isset(self::$counters[$user_id][$key][$code]))
768 {
769 self::$counters[$user_id][$key][$code] -= $decrement;
770 }
771 else
772 {
773 self::$counters[$user_id][$key][$code] = -$decrement;
774 }
775 }
776 }
777 else
778 {
779 if (!isset(self::$counters[$user_id][$site_id]))
780 {
781 self::$counters[$user_id][$site_id] = [];
782 }
783
784 if (isset(self::$counters[$user_id][$site_id][$code]))
785 {
786 self::$counters[$user_id][$site_id][$code] -= $decrement;
787 }
788 else
789 {
790 self::$counters[$user_id][$site_id][$code] = -$decrement;
791 }
792 }
793 }
794
795 $CACHE_MANAGER->Clean("user_counter".$user_id, "user_counter");
796
797 if ($sendPull)
798 {
799 self::SendPullEvent($user_id, $code);
800 }
801
802 return true;
803 }
804
805 public static function IncrementWithSelect($sub_select, $sendPull = true, $arParams = array())
806 {
807 global $CACHE_MANAGER;
809 $helper = $connection->getSqlHelper();
810
811 if ($sub_select <> '')
812 {
813 $pullInclude = (
814 $sendPull
816 );
817
818 if (
819 is_array($arParams)
820 && isset($arParams["TAG_SET"])
821 )
822 {
823 $insertFields = ['USER_ID', 'CNT', 'SITE_ID', 'CODE', 'SENT', 'TAG'];
824 if (is_array($arParams) && isset($arParams["SET_TIMESTAMP"]))
825 {
826 $insertFields[] = 'TIMESTAMP_X';
827 }
828 $strSQL = $helper->prepareMergeSelect(
829 'b_user_counter',
830 ['USER_ID', 'SITE_ID', 'CODE'],
831 $insertFields,
832 '(' . $sub_select . ')',
833 [
834 'CNT' => new \Bitrix\Main\DB\SqlExpression('b_user_counter.CNT + ?v', 'CNT'),
835 'SENT' => new \Bitrix\Main\DB\SqlExpression('?v', 'SENT'),
836 'TAG' => $arParams["TAG_SET"],
837 ]
838 );
839 }
840 elseif (
841 is_array($arParams)
842 && isset($arParams["TAG_CHECK"])
843 )
844 {
845 $insertFields = ['USER_ID', 'CNT', 'SITE_ID', 'CODE', 'SENT'];
846 if (is_array($arParams) && isset($arParams["SET_TIMESTAMP"]))
847 {
848 $insertFields[] = 'TIMESTAMP_X';
849 }
850 $strSQL = $helper->prepareMergeSelect(
851 'b_user_counter',
852 ['USER_ID', 'SITE_ID', 'CODE'],
853 $insertFields,
854 '(' . $sub_select . ')',
855 [
856 'CNT' => new \Bitrix\Main\DB\SqlExpression("CASE WHEN b_user_counter.TAG = '" . $helper->forSQL($arParams["TAG_CHECK"]) . "' THEN b_user_counter.CNT ELSE b_user_counter.CNT + ?v END", 'CNT'),
857 'SENT' => new \Bitrix\Main\DB\SqlExpression("CASE WHEN b_user_counter.TAG = '" . $helper->forSQL($arParams["TAG_CHECK"]) . "' THEN b_user_counter.SENT ELSE ?v END", 'SENT'),
858 ]
859 );
860 }
861 else
862 {
863 $insertFields = ['USER_ID', 'CNT', 'SITE_ID', 'CODE', 'SENT'];
864 if (is_array($arParams) && isset($arParams["SET_TIMESTAMP"]))
865 {
866 $insertFields[] = 'TIMESTAMP_X';
867 }
868 $strSQL = $helper->prepareMergeSelect(
869 'b_user_counter',
870 ['USER_ID', 'SITE_ID', 'CODE'],
871 $insertFields,
872 '(' . $sub_select . ')',
873 [
874 'CNT' => new \Bitrix\Main\DB\SqlExpression("b_user_counter.CNT + ?v", 'CNT'),
875 'SENT' => new \Bitrix\Main\DB\SqlExpression("?v", 'SENT'),
876 ]
877 );
878 }
879
880 $connection->query($strSQL);
881
882 if (
883 !is_array($arParams)
884 || (
885 !isset($arParams["TAG_SET"])
886 && (
887 !isset($arParams["CLEAN_CACHE"])
888 || $arParams["CLEAN_CACHE"] != "N"
889 )
890 )
891 )
892 {
893 self::$counters = [];
894 $CACHE_MANAGER->CleanDir("user_counter");
895 }
896
897 if ($pullInclude)
898 {
899 $arSites = Array();
900 $by = '';
901 $order = '';
902 $res = CSite::GetList($by, $order, Array("ACTIVE" => "Y"));
903 while($row = $res->Fetch())
904 {
905 $arSites[] = $row['ID'];
906 }
907
908 if (
909 !empty($arParams["USERS_TO_PUSH"])
910 && is_array($arParams["USERS_TO_PUSH"])
911 )
912 {
913 if($connection->lock('pull'))
914 {
915 $helper = $connection->getSqlHelper();
916
917 $strSQL = "
918 SELECT uc.USER_ID as CHANNEL_ID, uc.USER_ID, uc.SITE_ID, uc.CODE, uc.CNT
919 FROM b_user_counter uc
920 INNER JOIN b_user u ON u.ID = uc.USER_ID AND (CASE WHEN u.EXTERNAL_AUTH_ID IN ('" . implode("', '", \Bitrix\Main\UserTable::getExternalUserTypes())."') THEN 'Y' ELSE 'N' END) = 'N' AND u.LAST_ACTIVITY_DATE > " . $helper->addSecondsToDateTime('(-3600)')."
921 WHERE uc.SENT = '0' AND uc.USER_ID IN (" . implode(", ", $arParams["USERS_TO_PUSH"]) . ")
922 ";
923
924 $res = $connection->Query($strSQL);
925
926 $pullMessage = Array();
927 while($row = $res->fetch())
928 {
929 self::addValueToPullMessage($row, $arSites, $pullMessage);
930 }
931
932 $connection->Query("UPDATE b_user_counter SET SENT = '1' WHERE SENT = '0' AND CODE NOT LIKE '". self::LIVEFEED_CODE . "L%'");
933
934 $connection->unlock('pull');
935
936 if (self::CheckLiveMode())
937 {
938 foreach ($pullMessage as $channelId => $arMessage)
939 {
941 'module_id' => 'main',
942 'command' => 'user_counter',
943 'expiry' => 3600,
944 'params' => $arMessage,
945 ));
946 }
947 }
948 }
949 }
950 else
951 {
953 }
954 }
955 }
956 }
957
958 public static function Clear($user_id, $code, $site_id = SITE_ID, $sendPull = true, $bMultiple = false, $cleanCache = true)
959 {
960 global $CACHE_MANAGER;
962 $helper = $connection->getSqlHelper();
963
964 $user_id = (int)$user_id;
965 if (
966 $user_id < 0
967 || $code == ''
968 )
969 {
970 return false;
971 }
972
973 if (!is_array($site_id))
974 {
975 $site_id = [ $site_id ];
976 }
977
978 if ($bMultiple)
979 {
980 if($connection->lock('counter_delete'))
981 {
982 $siteToDelete = "";
983 foreach ($site_id as $i => $site_id_tmp)
984 {
985 if ($i > 0)
986 {
987 $siteToDelete .= ",";
988 }
989 $siteToDelete .= "'".$helper->forSQL($site_id_tmp)."'";
990 }
991
992 $strDeleteSQL = "
993 DELETE FROM b_user_counter
994 WHERE
995 USER_ID = ".$user_id."
996 AND SITE_ID IN (".$siteToDelete.")
997 AND CODE LIKE '".$helper->forSQL($code)."L%'
998 ";
999
1000 $connection->query($strDeleteSQL);
1001
1002 foreach ($site_id as $i => $site_id_tmp)
1003 {
1004 $merge = $helper->prepareMerge('b_user_counter', ['USER_ID', 'SITE_ID', 'CODE'], [
1005 'USER_ID' => $user_id,
1006 'SITE_ID' => $site_id_tmp,
1007 'CODE' => $code,
1008 'CNT' => 0,
1009 'LAST_DATE' => new \Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1010 ], [
1011 'CNT' => 0,
1012 'LAST_DATE' => new \Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1013 ]);
1014 if ($merge[0])
1015 {
1016 $connection->query($merge[0]);
1017 }
1018 }
1019
1020 $connection->unlock('counter_delete');
1021 }
1022 }
1023 else
1024 {
1025 foreach ($site_id as $i => $site_id_tmp)
1026 {
1027 $merge = $helper->prepareMerge('b_user_counter', ['USER_ID', 'SITE_ID', 'CODE'], [
1028 'USER_ID' => $user_id,
1029 'SITE_ID' => $site_id_tmp,
1030 'CODE' => $code,
1031 'CNT' => 0,
1032 'LAST_DATE' => new \Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1033 ], [
1034 'CNT' => 0,
1035 'LAST_DATE' => new \Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1036 ]);
1037 if ($merge)
1038 {
1039 $connection->Query($merge[0]);
1040 }
1041 }
1042 }
1043
1044 if (self::$counters && self::$counters[$user_id])
1045 {
1046 foreach ($site_id as $site_id_tmp)
1047 {
1048 if ($site_id_tmp === self::ALL_SITES)
1049 {
1050 foreach (self::$counters[$user_id] as $key => $tmp)
1051 {
1052 self::$counters[$user_id][$key][$code] = 0;
1053 }
1054 break;
1055 }
1056
1057 if (!isset(self::$counters[$user_id][$site_id_tmp]))
1058 {
1059 self::$counters[$user_id][$site_id_tmp] = array();
1060 }
1061
1062 self::$counters[$user_id][$site_id_tmp][$code] = 0;
1063 }
1064 }
1065
1066 if ($cleanCache)
1067 {
1068 $CACHE_MANAGER->Clean('user_counter' . $user_id, 'user_counter');
1069 }
1070
1071 if ($sendPull)
1072 {
1073 self::SendPullEvent($user_id, $code);
1074 }
1075
1076 return true;
1077 }
1078
1079 public static function DeleteByCode($code)
1080 {
1081 global $DB, $CACHE_MANAGER;
1082
1083 if ($code == '')
1084 {
1085 return false;
1086 }
1087
1088 $pullMessage = Array();
1089 $bPullEnabled = false;
1090
1092
1093 $isLiveFeed = (
1094 str_starts_with($code, self::LIVEFEED_CODE)
1095 && $code !== self::LIVEFEED_CODE
1096 );
1097
1098 if ($isLiveFeed)
1099 {
1100 $DB->Query(
1101 "DELETE FROM b_user_counter WHERE CODE = '".$code."'"
1102 );
1103
1104 self::$counters = [];
1105 $CACHE_MANAGER->CleanDir("user_counter");
1106
1107 if (self::$isLiveFeedJobOn === false && self::CheckLiveMode())
1108 {
1110 $application && $application->addBackgroundJob([__CLASS__, 'sendLiveFeedPull']);
1111
1112 self::$isLiveFeedJobOn = true;
1113 }
1114
1115 return true;
1116 }
1117
1118 if (
1119 self::CheckLiveMode()
1120 && $connection->lock('pull')
1121 )
1122 {
1123 $bPullEnabled = true;
1124
1125 $arSites = [];
1126 $by = '';
1127 $order = '';
1128 $res = CSite::GetList($by, $order, array("ACTIVE" => "Y"));
1129 while($row = $res->Fetch())
1130 {
1131 $arSites[] = $row['ID'];
1132 }
1133
1134 $helper = $connection->getSqlHelper();
1135 $strSQL = "
1136 SELECT uc.USER_ID as CHANNEL_ID, uc.USER_ID, uc.SITE_ID, uc.CODE, uc.CNT
1137 FROM b_user_counter uc
1138 INNER JOIN b_user u ON u.ID = uc.USER_ID AND (CASE WHEN u.EXTERNAL_AUTH_ID IN ('" . implode("', '", \Bitrix\Main\UserTable::getExternalUserTypes()) . "') THEN 'Y' ELSE 'N' END) = 'N' AND u.LAST_ACTIVITY_DATE > ".$helper->addSecondsToDateTime('(-3600)')."
1139 WHERE uc.CODE = '" . $code . "'";
1140
1141 $res = $DB->Query($strSQL);
1142
1143 while($row = $res->Fetch())
1144 {
1145 self::addValueToPullMessage($row, $arSites, $pullMessage);
1146 }
1147 }
1148
1149 $DB->Query("DELETE FROM b_user_counter WHERE CODE = '".$code."'");
1150
1151 self::$counters = [];
1152 $CACHE_MANAGER->CleanDir("user_counter");
1153
1154 if ($bPullEnabled)
1155 {
1156 $connection->unlock('pull');
1157 }
1158
1159 if (self::CheckLiveMode())
1160 {
1161 foreach ($pullMessage as $channelId => $arMessage)
1162 {
1164 'module_id' => 'main',
1165 'command' => 'user_counter',
1166 'expiry' => 3600,
1167 'params' => $arMessage,
1168 ));
1169 }
1170 }
1171
1172 return null;
1173 }
1174
1175 protected static function dbIF($condition, $yes, $no)
1176 {
1177 return "CASE WHEN ".$condition." THEN ".$yes." ELSE ".$no."END ";
1178 }
1179
1180 // legacy function
1181 public static function ClearByUser($user_id, $site_id = SITE_ID, $code = self::ALL_SITES, $bMultiple = false, $sendPull = true)
1182 {
1183 return self::Clear($user_id, $code, $site_id, $sendPull, $bMultiple);
1184 }
1185
1186 public static function sendLiveFeedPull()
1187 {
1188 global $DB;
1189
1190 $pullMessage = [];
1191
1193
1194 $connection->lock('pull');
1195
1196 $sites = [];
1197 $by = '';
1198 $order = '';
1199 $queryObject = CSite::getList($by, $order, ['ACTIVE' => 'Y']);
1200 while ($row = $queryObject->fetch())
1201 {
1202 $sites[] = $row['ID'];
1203 }
1204
1205 $helper = $connection->getSqlHelper();
1206
1207 $strSQL = "
1208 SELECT uc.USER_ID as CHANNEL_ID, uc.USER_ID, uc.SITE_ID, uc.CODE, uc.CNT
1209 FROM b_user_counter uc
1210 INNER JOIN b_user u ON u.ID = uc.USER_ID AND (CASE WHEN u.EXTERNAL_AUTH_ID IN ('"
1211 . implode("', '", \Bitrix\Main\UserTable::getExternalUserTypes())
1212 . "') THEN 'Y' ELSE 'N' END) = 'N' AND u.LAST_ACTIVITY_DATE > "
1213 .$helper->addSecondsToDateTime('(-3600)')."
1214 WHERE uc.CODE LIKE '" . self::LIVEFEED_CODE . "%'
1215 ";
1216
1217 $queryObject = $DB->Query($strSQL);
1218 while($row = $queryObject->fetch())
1219 {
1220 self::addValueToPullMessage($row, $sites, $pullMessage);
1221 }
1222
1223 $connection->unlock('pull');
1224
1225 foreach ($pullMessage as $channelId => $arMessage)
1226 {
1228 'module_id' => 'main',
1229 'command' => 'user_counter',
1230 'expiry' => 3600,
1231 'params' => $arMessage,
1232 ]);
1233 }
1234 }
1235}
1236
1238{
1239}
$arParams
Определения access_dialog.php:21
$connection
Определения actionsdefinitions.php:38
$arSites
Определения options.php:15
if(empty( $fields)) foreach($fields as $field) $channelId
Определения push.php:23
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getInstance()
Определения application.php:98
static getConnection($name="")
Определения application.php:638
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
Определения user_counter.php:6
static addValueToPullMessage($row, $arSites=array(), &$pullMessage=[])
Определения user_counter.php:464
static Increment($user_id, $code, $site_id=SITE_ID, $sendPull=true, $increment=1)
Определения user_counter.php:655
static SendPullEvent($user_id, $code="", $bMultiple=false)
Определения user_counter.php:374
static DeleteOld()
Определения user_counter.php:561
const ALL_SITES
Определения user_counter.php:7
static getGroupedCounterRecords($records)
Определения user_counter.php:504
static ClearByUser($user_id, $site_id=SITE_ID, $code=self::ALL_SITES, $bMultiple=false, $sendPull=true)
Определения user_counter.php:1181
static dbIF($condition, $yes, $no)
Определения user_counter.php:1175
static GetLastDateByUserAndCode($user_id, $site_id=SITE_ID, $code=self::ALL_SITES)
Определения user_counter.php:556
static DeleteByCode($code)
Определения user_counter.php:1079
static getGroupedCounters($counters)
Определения user_counter.php:492
static GetValueByUserID($user_id, $site_id=SITE_ID, $code=self::ALL_SITES)
Определения user_counter.php:548
static IncrementWithSelect($sub_select, $sendPull=true, $arParams=array())
Определения user_counter.php:805
static GetValue($user_id, $code, $site_id=SITE_ID)
Определения user_counter.php:14
static OnSocNetLogDelete($logId)
Определения user_counter.php:542
static Decrement($user_id, $code, $site_id=SITE_ID, $sendPull=true, $decrement=1)
Определения user_counter.php:734
static GetCodeValuesByUserID($user_id, $site_id=SITE_ID)
Определения user_counter.php:552
static GetLastDate($user_id, $code, $site_id=SITE_ID)
Определения user_counter.php:204
static ClearAll($user_id, $site_id=SITE_ID, $sendPull=true)
Определения user_counter.php:232
static GetAllValues($user_id)
Определения user_counter.php:92
static $counters
Определения user_counter.php:11
const LIVEFEED_CODE
Определения user_counter.php:8
const SYSTEM_USER_ID
Определения user_counter.php:9
static Clear($user_id, $code, $site_id=SITE_ID, $sendPull=true, $bMultiple=false, $cleanCache=true)
Определения user_counter.php:958
static CheckLiveMode()
Определения user_counter.php:366
static Set($user_id, $code, $value, $site_id=SITE_ID, $tag='', $sendPull=true)
Определения user_counter.php:573
static ClearByTag($tag, $code, $site_id=SITE_ID, $sendPull=true)
Определения user_counter.php:271
static GetValues($user_id, $site_id=SITE_ID, &$arLastDate=[])
Определения user_counter.php:32
static sendLiveFeedPull()
Определения user_counter.php:1186
static OnSocNetLogCommentDelete($commentId)
Определения user_counter.php:537
Определения user_counter.php:1238
static setNewEvent()
Определения user_counter_page.php:28
$sites
Определения clear_component_cache.php:15
global $CACHE_MANAGER
Определения clear_component_cache.php:7
if(!is_array($prop["VALUES"])) $tmp
Определения component_props.php:203
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
global $DB
Определения cron_frame.php:29
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$application
Определения bitrix.php:23
$arCodes
Определения options.php:154
$siteId
Определения ajax.php:8
GetTime($timestamp, $type="SHORT", $site=false, $bSearchInSitesOnly=false)
Определения tools.php:1890
MakeTimeStamp($datetime, $format=false)
Определения tools.php:538
$order
Определения payment.php:8
$time
Определения payment.php:61
$counter
Определения options.php:5
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$i
Определения factura.php:643
$arRes
Определения options.php:104
$site_id
Определения sonet_set_content_view.php:9
const SITE_ID
Определения sonet_set_content_view.php:12
$rs
Определения action.php:82
$dbRes
Определения yandex_detail.php:168