12 private static $isLiveFeedJobOn =
false;
16 $user_id = (int)$user_id;
36 $user_id = (int)$user_id;
42 if (!is_array($arLastDate))
49 $diff = CTimeZone::GetOffset();
52 if (!isset(self::$counters[$user_id][
$site_id]))
54 $arAll = self::getValuesFromDB($user_id);
58 foreach($arAll as $arItem)
62 || $arItem[
"SITE_ID"] === self::ALL_SITES
65 if (!isset(self::$counters[$user_id][
$site_id][$arItem[
"CODE"]]))
67 self::$counters[$user_id][
$site_id][$arItem[
"CODE"]] = 0;
69 self::$counters[$user_id][
$site_id][$arItem[
"CODE"]] += $arItem[
"CNT"];
71 if (!isset($arLastDate[$user_id]))
73 $arLastDate[$user_id] = [];
75 if (!isset($arLastDate[$user_id][
$site_id]))
77 $arLastDate[$user_id][
$site_id] = [];
80 if (isset($arItem[
"LAST_DATE_TS"]))
82 $arLastDate[$user_id][
$site_id][$arItem[
"CODE"]] = $arItem[
"LAST_DATE_TS"] - $diff;
89 return (self::$counters[$user_id][
$site_id] ?? []);
95 $user_id = (int)$user_id;
104 $res = CSite::GetList($by,
$order, Array(
"ACTIVE" =>
"Y"));
105 while ($row =
$res->Fetch())
110 $arAll = self::getValuesFromDB($user_id);
112 foreach ($arAll as $arItem)
114 if ($arItem[
'SITE_ID'] === self::ALL_SITES)
118 if (isset($arCounters[
$siteId][$arItem[
'CODE']]))
120 $arCounters[
$siteId][$arItem[
'CODE']] += (int)$arItem[
'CNT'];
124 $arCounters[
$siteId][$arItem[
'CODE']] = (int)$arItem[
'CNT'];
128 elseif (isset($arCounters[$arItem[
'SITE_ID']][$arItem[
'CODE']]))
130 $arCounters[$arItem[
'SITE_ID']][$arItem[
'CODE']] += (int)$arItem[
'CNT'];
134 $arCounters[$arItem[
'SITE_ID']][$arItem[
'CODE']] = (int)$arItem[
'CNT'];
141 private static function getValuesFromDB(
int $userId = 0)
146 CACHED_b_user_counter !==
false
155 SELECT CODE, SITE_ID, CNT, ' .
$DB->datetimeToTimestampFunction(
'LAST_DATE') .
' LAST_DATE_TS
161 while ($rowFields =
$res->fetch())
163 $rowFields[
'CODE'] = self::getGroupedCode($rowFields[
'CODE']);
169 if (CACHED_b_user_counter !==
false)
180 $cachedCounters = [];
186 if (!isset($cachedCounters[
$key]))
194 if (isset(
$counter[
'LAST_DATE_TS']))
196 $cachedCounters[
$key][
'LAST_DATE_TS'] =
$counter[
'LAST_DATE_TS'];
201 return array_values($cachedCounters);
208 $user_id = (int)$user_id;
209 if ($user_id < 0 ||
$code ==
'')
215 SELECT " .
$DB->DateToCharFunction(
"LAST_DATE") .
" LAST_DATE
217 WHERE USER_ID = ".$user_id.
"
218 AND (SITE_ID = '".
$site_id.
"' OR SITE_ID = '" . self::ALL_SITES .
"')
219 AND CODE = '" .
$DB->ForSql(
$code) .
"'
236 $user_id = (int)$user_id;
243 UPDATE b_user_counter SET
245 WHERE USER_ID = ".$user_id.
"
246 AND (SITE_ID = '".
$site_id.
"' OR SITE_ID = '".self::ALL_SITES.
"')";
253 unset(self::$counters[$user_id]);
258 unset(self::$counters[$user_id][
$site_id]);
275 if ($tag ==
'' ||
$code ==
'')
281 UPDATE b_user_counter SET
283 WHERE TAG = '" .
$DB->ForSQL($tag) .
"' AND CODE = '" .
$DB->ForSQL(
$code) .
"'
284 AND (SITE_ID = '" .
$site_id .
"' OR SITE_ID = '" . self::ALL_SITES .
"')";
287 self::$counters = [];
290 if ($sendPull && self::CheckLiveMode())
297 $res = CSite::GetList($by,
$order, Array(
"ACTIVE" =>
"Y"));
298 while ($row =
$res->Fetch())
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 .
"')";
315 while ($row =
$res->Fetch())
318 $row[
'CHANNEL_TYPE'] ===
'private'
320 $row[
'CHANNEL_TYPE'] ===
'shared'
321 && (
int)$row[
'USER_ID'] === 0
328 if ($row[
'SITE_ID'] === self::ALL_SITES)
332 if (isset($pullMessage[$row[
'CHANNEL_ID']][
$siteId][$row[
'CODE']]))
334 $pullMessage[$row[
'CHANNEL_ID']][
$siteId][$row[
'CODE']] += (int)$row[
'CNT'];
338 $pullMessage[$row[
'CHANNEL_ID']][
$siteId][$row[
'CODE']] = (int)$row[
'CNT'];
342 elseif (isset($pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][$row[
'CODE']]))
344 $pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][$row[
'CODE']] += (int)$row[
'CNT'];
348 $pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][$row[
'CODE']] = (int)$row[
'CNT'];
352 foreach ($pullMessage as
$channelId => $arMessage)
355 'module_id' =>
'main',
356 'command' =>
'user_counter',
358 'params' => $arMessage,
369 CModule::IncludeModule(
'pull')
370 && CPullOptions::GetNginxStatus()
376 $user_id = (int)$user_id;
382 if (self::CheckLiveMode())
389 $res = CSite::GetList($by,
$order, Array(
"ACTIVE" =>
"Y"));
390 while ($row =
$res->Fetch())
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
405 ?
" AND uc.CODE LIKE '".
$DB->ForSQL(
$code).
"%'"
406 :
" AND uc.CODE = '".$DB->ForSQL(
$code).
"'"
413 $pullMessage = Array();
414 while ($row =
$res->Fetch())
419 $row[
'CHANNEL_TYPE'] ===
'private'
421 $row[
'CHANNEL_TYPE'] ===
'shared'
422 && (
int)$row[
'USER_ID'] === 0
428 if ($row[
'SITE_ID'] === self::ALL_SITES)
432 if (isset($pullMessage[$row[
'CHANNEL_ID']][
$siteId][
$key]))
434 $pullMessage[$row[
'CHANNEL_ID']][
$siteId][
$key] += (int)$row[
'CNT'];
438 $pullMessage[$row[
'CHANNEL_ID']][
$siteId][
$key] = (int)$row[
'CNT'];
442 elseif (isset($pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][
$key]))
444 $pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][
$key] += (int)$row[
'CNT'];
448 $pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][
$key] = (int)$row[
'CNT'];
452 foreach ($pullMessage as
$channelId => $arMessage)
455 'module_id' =>
'main',
456 'command' =>
'user_counter',
458 'params' => $arMessage,
466 $code = self::getGroupedCode($row[
"CODE"]);
468 if ($row[
'SITE_ID'] === self::ALL_SITES)
472 if (isset($pullMessage[$row[
'CHANNEL_ID']][
$siteId][
$code]))
474 $pullMessage[$row[
'CHANNEL_ID']][
$siteId][
$code] += (int)$row[
'CNT'];
478 $pullMessage[$row[
'CHANNEL_ID']][
$siteId][
$code] = (int)$row[
'CNT'];
482 elseif (isset($pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][
$code]))
484 $pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][
$code] += (int)$row[
'CNT'];
488 $pullMessage[$row[
'CHANNEL_ID']][$row[
'SITE_ID']][
$code] = (int)$row[
'CNT'];
508 foreach ($records as
$code => $value)
525 private static function getGroupedCode(
$code)
529 if (mb_strpos(
$code, self::LIVEFEED_CODE) === 0)
565 $days = (int)Option::get(
'main',
'user_counter_days', 20);
567 $time =
$DB->CharToDateFunction(
GetTime(time()- $days*60*60*24,
"FULL"));
568 $DB->Query(
"DELETE FROM b_user_counter WHERE TIMESTAMP_X <= ".
$time);
570 return "CUserCounter::DeleteOld();";
577 $value = (int)$value;
578 $user_id = (int)$user_id;
579 if ($user_id < 0 ||
$code ==
'')
585 SELECT CNT FROM b_user_counter
586 WHERE USER_ID = ".$user_id.
"
591 if ($cntVal =
$rs->Fetch())
596 $ssql =
", TAG = '".$DB->ForSQL($tag).
"'";
599 if($cntVal[
'CNT'] != $value)
602 UPDATE b_user_counter SET
603 CNT = " . $value .
" " . $ssql .
",
605 WHERE USER_ID = " . $user_id .
"
607 AND CODE = '" .
$DB->ForSQL(
$code) .
"'
618 INSERT INTO b_user_counter
619 (CNT, USER_ID, SITE_ID, CODE, TAG)
621 (".$value.
", ".$user_id.
", '".
$DB->ForSQL(
$site_id).
"', '".
$DB->ForSQL(
$code).
"', '".
$DB->ForSQL($tag).
"')
625 if (self::$counters && isset(self::$counters[$user_id]))
629 foreach(self::$counters[$user_id] as
$key =>
$tmp)
631 self::$counters[$user_id][
$key][
$code] = $value;
636 if (!isset(self::$counters[$user_id][
$site_id]))
638 self::$counters[$user_id][
$site_id] = [];
661 $user_id = (int)$user_id;
662 if ($user_id < 0 ||
$code ==
'')
667 $increment = (int)$increment;
669 $merge = $helper->prepareMerge(
'b_user_counter', [
'USER_ID',
'SITE_ID',
'CODE'], [
670 'USER_ID' => $user_id,
675 'CNT' =>
new \
Bitrix\Main\DB\SqlExpression(
'b_user_counter.CNT + ' . $increment),
682 if (isset(self::$counters[$user_id]) && is_array(self::$counters[$user_id]))
686 foreach(self::$counters[$user_id] as
$key =>
$tmp)
688 if (isset(self::$counters[$user_id][
$key][
$code]))
690 self::$counters[$user_id][
$key][
$code] += $increment;
694 self::$counters[$user_id][
$key][
$code] = $increment;
700 if (!isset(self::$counters[$user_id][
$site_id]))
702 self::$counters[$user_id][
$site_id] = [];
740 $user_id = (int)$user_id;
741 if ($user_id < 0 ||
$code ==
'')
746 $decrement = (int)$decrement;
748 $merge = $helper->prepareMerge(
'b_user_counter', [
'USER_ID',
'SITE_ID',
'CODE'], [
749 'USER_ID' => $user_id,
752 'CNT' => -$decrement,
754 'CNT' =>
new \
Bitrix\Main\DB\SqlExpression(
'b_user_counter.CNT - ' . $decrement),
761 if (self::$counters && self::$counters[$user_id])
765 foreach (self::$counters[$user_id] as
$key =>
$tmp)
767 if (isset(self::$counters[$user_id][
$key][
$code]))
769 self::$counters[$user_id][
$key][
$code] -= $decrement;
773 self::$counters[$user_id][
$key][
$code] = -$decrement;
779 if (!isset(self::$counters[$user_id][
$site_id]))
781 self::$counters[$user_id][
$site_id] = [];
811 if ($sub_select <>
'')
823 $insertFields = [
'USER_ID',
'CNT',
'SITE_ID',
'CODE',
'SENT',
'TAG'];
826 $insertFields[] =
'TIMESTAMP_X';
828 $strSQL = $helper->prepareMergeSelect(
830 [
'USER_ID',
'SITE_ID',
'CODE'],
832 '(' . $sub_select .
')',
834 'CNT' =>
new \
Bitrix\Main\DB\SqlExpression(
'b_user_counter.CNT + ?v',
'CNT'),
835 'SENT' =>
new \
Bitrix\Main\DB\SqlExpression(
'?v',
'SENT'),
845 $insertFields = [
'USER_ID',
'CNT',
'SITE_ID',
'CODE',
'SENT'];
848 $insertFields[] =
'TIMESTAMP_X';
850 $strSQL = $helper->prepareMergeSelect(
852 [
'USER_ID',
'SITE_ID',
'CODE'],
854 '(' . $sub_select .
')',
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'),
863 $insertFields = [
'USER_ID',
'CNT',
'SITE_ID',
'CODE',
'SENT'];
866 $insertFields[] =
'TIMESTAMP_X';
868 $strSQL = $helper->prepareMergeSelect(
870 [
'USER_ID',
'SITE_ID',
'CODE'],
872 '(' . $sub_select .
')',
874 'CNT' =>
new \
Bitrix\Main\DB\SqlExpression(
"b_user_counter.CNT + ?v",
'CNT'),
875 'SENT' =>
new \
Bitrix\Main\DB\SqlExpression(
"?v",
'SENT'),
893 self::$counters = [];
902 $res = CSite::GetList($by,
$order, Array(
"ACTIVE" =>
"Y"));
903 while($row =
$res->Fetch())
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"]) .
")
926 $pullMessage = Array();
927 while($row =
$res->fetch())
932 $connection->Query(
"UPDATE b_user_counter SET SENT = '1' WHERE SENT = '0' AND CODE NOT LIKE '". self::LIVEFEED_CODE .
"L%'");
936 if (self::CheckLiveMode())
938 foreach ($pullMessage as
$channelId => $arMessage)
941 'module_id' =>
'main',
942 'command' =>
'user_counter',
944 'params' => $arMessage,
964 $user_id = (int)$user_id;
987 $siteToDelete .=
",";
989 $siteToDelete .=
"'".$helper->forSQL($site_id_tmp).
"'";
993 DELETE FROM b_user_counter
995 USER_ID = ".$user_id.
"
996 AND SITE_ID IN (".$siteToDelete.
")
997 AND CODE LIKE '".$helper->forSQL(
$code).
"L%'
1004 $merge = $helper->prepareMerge(
'b_user_counter', [
'USER_ID',
'SITE_ID',
'CODE'], [
1005 'USER_ID' => $user_id,
1006 'SITE_ID' => $site_id_tmp,
1009 'LAST_DATE' =>
new \
Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1012 'LAST_DATE' =>
new \
Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1027 $merge = $helper->prepareMerge(
'b_user_counter', [
'USER_ID',
'SITE_ID',
'CODE'], [
1028 'USER_ID' => $user_id,
1029 'SITE_ID' => $site_id_tmp,
1032 'LAST_DATE' =>
new \
Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1035 'LAST_DATE' =>
new \
Bitrix\Main\DB\SqlExpression($helper->getCurrentDateTimeFunction()),
1044 if (self::$counters && self::$counters[$user_id])
1048 if ($site_id_tmp === self::ALL_SITES)
1050 foreach (self::$counters[$user_id] as
$key =>
$tmp)
1052 self::$counters[$user_id][
$key][
$code] = 0;
1057 if (!isset(self::$counters[$user_id][$site_id_tmp]))
1059 self::$counters[$user_id][$site_id_tmp] =
array();
1062 self::$counters[$user_id][$site_id_tmp][
$code] = 0;
1068 $CACHE_MANAGER->Clean(
'user_counter' . $user_id,
'user_counter');
1088 $pullMessage = Array();
1089 $bPullEnabled =
false;
1094 str_starts_with(
$code, self::LIVEFEED_CODE)
1095 &&
$code !== self::LIVEFEED_CODE
1101 "DELETE FROM b_user_counter WHERE CODE = '".
$code.
"'"
1104 self::$counters = [];
1107 if (self::$isLiveFeedJobOn ===
false && self::CheckLiveMode())
1112 self::$isLiveFeedJobOn =
true;
1119 self::CheckLiveMode()
1123 $bPullEnabled =
true;
1129 while($row =
$res->Fetch())
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 .
"'";
1143 while($row =
$res->Fetch())
1149 $DB->Query(
"DELETE FROM b_user_counter WHERE CODE = '".
$code.
"'");
1151 self::$counters = [];
1159 if (self::CheckLiveMode())
1161 foreach ($pullMessage as
$channelId => $arMessage)
1164 'module_id' =>
'main',
1165 'command' =>
'user_counter',
1167 'params' => $arMessage,
1175 protected static function dbIF($condition, $yes, $no)
1177 return "CASE WHEN ".$condition.
" THEN ".$yes.
" ELSE ".$no.
"END ";
1199 $queryObject = CSite::getList($by,
$order, [
'ACTIVE' =>
'Y']);
1200 while ($row = $queryObject->fetch())
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 .
"%'
1217 $queryObject =
$DB->Query($strSQL);
1218 while($row = $queryObject->fetch())
1225 foreach ($pullMessage as
$channelId => $arMessage)
1228 'module_id' =>
'main',
1229 'command' =>
'user_counter',
1231 'params' => $arMessage,
if(empty( $fields)) foreach($fields as $field) $channelId
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
static getConnection($name="")
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
static addValueToPullMessage($row, $arSites=array(), &$pullMessage=[])
static Increment($user_id, $code, $site_id=SITE_ID, $sendPull=true, $increment=1)
static SendPullEvent($user_id, $code="", $bMultiple=false)
static getGroupedCounterRecords($records)
static ClearByUser($user_id, $site_id=SITE_ID, $code=self::ALL_SITES, $bMultiple=false, $sendPull=true)
static dbIF($condition, $yes, $no)
static GetLastDateByUserAndCode($user_id, $site_id=SITE_ID, $code=self::ALL_SITES)
static DeleteByCode($code)
static getGroupedCounters($counters)
static GetValueByUserID($user_id, $site_id=SITE_ID, $code=self::ALL_SITES)
static IncrementWithSelect($sub_select, $sendPull=true, $arParams=array())
static GetValue($user_id, $code, $site_id=SITE_ID)
static OnSocNetLogDelete($logId)
static Decrement($user_id, $code, $site_id=SITE_ID, $sendPull=true, $decrement=1)
static GetCodeValuesByUserID($user_id, $site_id=SITE_ID)
static GetLastDate($user_id, $code, $site_id=SITE_ID)
static ClearAll($user_id, $site_id=SITE_ID, $sendPull=true)
static GetAllValues($user_id)
static Clear($user_id, $code, $site_id=SITE_ID, $sendPull=true, $bMultiple=false, $cleanCache=true)
static Set($user_id, $code, $value, $site_id=SITE_ID, $tag='', $sendPull=true)
static ClearByTag($tag, $code, $site_id=SITE_ID, $sendPull=true)
static GetValues($user_id, $site_id=SITE_ID, &$arLastDate=[])
static sendLiveFeedPull()
static OnSocNetLogCommentDelete($commentId)
if(!is_array($prop["VALUES"])) $tmp
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
GetTime($timestamp, $type="SHORT", $site=false, $bSearchInSitesOnly=false)
MakeTimeStamp($datetime, $format=false)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key