• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/mysql/user_counter.php
  • Класс: CUserCounter
  • Вызов: CUserCounter::Clear
static function Clear($user_id, $code, $site_id = SITE_ID, $sendPull = true, $bMultiple = false, $cleanCache = true)
{
	global $CACHE_MANAGER;
	$connection = BitrixMainApplication::getConnection();
	$helper = $connection->getSqlHelper();

	$user_id = (int)$user_id;
	if (
		$user_id < 0
		|| $code == ''
	)
	{
		return false;
	}

	if (!is_array($site_id))
	{
		$site_id = [ $site_id ];
	}

	if ($bMultiple)
	{
		if($connection->lock('counter_delete'))
		{
			$siteToDelete = "";
			foreach ($site_id as $i => $site_id_tmp)
			{
				if ($i > 0)
				{
					$siteToDelete .= ",";
				}
				$siteToDelete .= "'".$helper->forSQL($site_id_tmp)."'";
			}

			$strDeleteSQL = "
				DELETE FROM b_user_counter
				WHERE
					USER_ID = ".$user_id."
					AND SITE_ID IN (".$siteToDelete.")
					AND CODE LIKE '".$helper->forSQL($code)."L%'
				";

			$connection->query($strDeleteSQL);

			foreach ($site_id as $i => $site_id_tmp)
			{
				$merge = $helper->prepareMerge('b_user_counter', ['USER_ID', 'SITE_ID', 'CODE'], [
					'USER_ID' => $user_id,
					'SITE_ID' => $site_id_tmp,
					'CODE' => $code,
					'CNT' => 0,
					'LAST_DATE' => new BitrixMainDBSqlExpression($helper->getCurrentDateTimeFunction()),
				], [
					'CNT' => 0,
					'LAST_DATE' => new BitrixMainDBSqlExpression($helper->getCurrentDateTimeFunction()),
				]);
				if ($merge[0])
				{
					$connection->query($merge[0]);
				}
			}

			$connection->unlock('counter_delete');
		}
	}
	else
	{
		foreach ($site_id as $i => $site_id_tmp)
		{
			$merge = $helper->prepareMerge('b_user_counter', ['USER_ID', 'SITE_ID', 'CODE'], [
				'USER_ID' => $user_id,
				'SITE_ID' => $site_id_tmp,
				'CODE' => $code,
				'CNT' => 0,
				'LAST_DATE' => new BitrixMainDBSqlExpression($helper->getCurrentDateTimeFunction()),
			], [
				'CNT' => 0,
				'LAST_DATE' => new BitrixMainDBSqlExpression($helper->getCurrentDateTimeFunction()),
			]);
			if ($merge)
			{
				$connection->Query($merge[0]);
			}
		}
	}

	if (self::$counters && self::$counters[$user_id])
	{
		foreach ($site_id as $site_id_tmp)
		{
			if ($site_id_tmp === self::ALL_SITES)
			{
				foreach (self::$counters[$user_id] as $key => $tmp)
				{
					self::$counters[$user_id][$key][$code] = 0;
				}
				break;
			}

			if (!isset(self::$counters[$user_id][$site_id_tmp]))
			{
				self::$counters[$user_id][$site_id_tmp] = array();
			}

			self::$counters[$user_id][$site_id_tmp][$code] = 0;
		}
	}

	if ($cleanCache)
	{
		$CACHE_MANAGER->Clean('user_counter' . $user_id, 'user_counter');
	}

	if ($sendPull)
	{
		self::SendPullEvent($user_id, $code);
	}

	return true;
}