• Модуль: forum
  • Путь к файлу: ~/bitrix/modules/forum/classes/general/topic.php
  • Класс: CAllForumTopic
  • Вызов: CAllForumTopic::GetMessageCount
static function GetMessageCount($forumID, $topicID, $approved = null)
{
	global $CACHE_MANAGER;
	static $arCacheCount = array();
	static $obCache = null;
	static $cacheLabel = 'forum_msg_count';
	static $notCached = 0;
	static $TTL = 3600000;

	if ($approved === true) $approved = "Y";
	if ($approved === false) $approved = "N";
	if ($approved === null) $approved = "A";

	if ($approved !== "Y" && $approved !== "N" && $approved !== "A")
		return false;

	if (isset($arCacheCount[$forumID][$topicID][$approved]))
	{
		return $arCacheCount[$forumID][$topicID][$approved];
	}

	if ($obCache === null)
		$obCache = new CPHPCache;

	$cacheID = md5($cacheLabel.$forumID);
	$cachePath = str_replace(array(":", "//"), "/", "/".SITE_ID."/".$cacheLabel."/");
	if ($obCache->InitCache($TTL, $cacheID, $cachePath))
	{
		$resCache = $obCache->GetVars();
		if (is_array($resCache['messages']))
			$arCacheCount[$forumID] = $resCache['messages'];
	}

	if (isset($arCacheCount[$forumID][$topicID][$approved]))
	{
		return $arCacheCount[$forumID][$topicID][$approved];
	}
	else
	{
		$bCount = true;
		if ($approved === "N" || $approved === "Y")
			$bCount = "cnt_not_approved";

		if (intval($topicID) > 0 || $topicID === 0)
			$arFilter = array("TOPIC_ID" => $topicID);
		else
		{
			$arRes = CForumTopic::GetByID($topicID);
			if ($arRes)
				$arFilter = array("TOPIC_ID" => $arRes['ID']);
			else
				return false;
		}
		$count = CForumMessage::GetList(null, $arFilter, $bCount);

		$result = 0;
		if ($approved === "N")
		{
			$result = intval($count['CNT_NOT_APPROVED']);
		}
		elseif ($approved === "Y")
		{
			$result = $count['CNT'] - $count['CNT_NOT_APPROVED'];
		}
		else
		{
			$result = intval($count);
		}
		$notCached++;
	}

	$arCacheCount[$forumID][$topicID][$approved] = $result;

	if ($notCached > 2)
	{
		$obCache->StartDataCache($TTL, $cacheID, $cachePath);
		CForumCacheManager::SetTag($cachePath, $cacheLabel.$forumID);
		$obCache->EndDataCache(array("messages" => $arCacheCount[$forumID]));
		$notCached = 0;
	}
	return $result;
}