Количество новостей в категории, для DLE 11.1 и младше

Количество новостей в категории, для DLE 11.1 и младше
Данная статья представляет собой простую адаптацию функционала актуальной версии DLE для более старых.

С помощью данного хака появится возможность с помощью тега:

{catnewscount id="id_категории"}

В любом месте шаблона выводить количество новостей в заданной категории.

Установка
Открыть файл engine/init.php
Найти строку:

$banned_info = get_vars ( "banned" );

Выше нее вставить:

$news_count_in_array = dle_cache ( "news", "newscountcacheincats" );
if ($news_count_in_array) {
	$news_count_in_array = json_decode($news_count_in_array, true);
	if ( !is_array($news_count_in_array) ) $news_count_in_array = array();
} else {
	$news_count_in_array = array();
	if( $config['no_date'] AND !$config['news_future'] ) {
		$thisdate = date( "Y-m-d H:i:s", $_TIME );
		$where_date = " AND date < '" . $thisdate . "'";
	} else $where_date = "";

	$db->query( "SELECT category, COUNT(*) AS count FROM " . PREFIX . "_post WHERE approve=1" . $where_date . " GROUP BY category" );
	while ( $row = $db->get_row() ) {
		if(!$row['category']) continue;
		$cat_array = $temp_cat_array = explode(",", $row['category']);
		foreach ( $temp_cat_array as $value ) {
			if(!isset($news_count_in_array[$value])) $news_count_in_array[$value] = $row['count'];
			else $news_count_in_array[$value] = $news_count_in_array[$value] + $row['count'];
			if( $config['show_sub_cats']) {
				$temp_parent = $cat_info[$value]['parentid'];
				while ( $temp_parent ) {
					if( !in_array($temp_parent, $cat_array) ) {
						if(!isset($news_count_in_array[$temp_parent])) $news_count_in_array[$temp_parent] = $row['count'];
						else $news_count_in_array[$temp_parent] = $news_count_in_array[$temp_parent] + $row['count'];
						$cat_array[] = $temp_parent;
					}
					$temp_parent = $cat_info[$temp_parent]['parentid'];
				}
			}
		}
	}
	create_cache("news", json_encode($news_count_in_array), "newscountcacheincats");
	unset($temp_parent, $temp_cat_array, $cat_array);
}

foreach ($cat_info as $key => $value) {
	$cat_info[$key]['newscount'] = (int)$news_count_in_array[$key];
}
unset($news_count_in_array);

Открыть файл engine/classes/template.class.php
Найти строку:

$this->template = file_get_contents( $this->dir . "/" . $tpl_name );

Ниже нее вставить:

		if (strpos ( $this->template, "{catnewscount" ) !== false) {
			$this->template = preg_replace_callback ( "#\{catnewscount id=['"](.+?)['"]\}#i", array( &$this, 'catnewscount'), $this->template );
		}

Найти строку:

$template = file_get_contents( $templatefile );

Ниже нее вставить:

		if (strpos ( $template, "{catnewscount" ) !== false) {
			$template = preg_replace_callback ( "#\{catnewscount id=['"](.+?)['"]\}#i", array( &$this, 'catnewscount'), $template );
		}

Найти строку:

	function compile($tpl) {

Выше нее вставить:

	function catnewscount($matches = array()) {
		global $cat_info;
		$id = (int)$matches[1];
		return (int)$cat_info[$id]['newscount'];
	}

С уважением,
Олег Александрович a.k.a. Sander
Источник: sandev.pro

Просмотров:

Добавить комментарий