<?
if(!defined('DATALIFEENGINE')){die("Hacking attempt!");}

$word = 40;
$limit = $config['newslist_num'];

if (isset ( $_SESSION['dle_sort_newslist'] )) $news_sort_by = $_SESSION['dle_sort_newslist']; else $news_sort_by = $config['news_sort'];
if (isset ( $_SESSION['dle_direction_newslist'] )) $news_direction_by = $_SESSION['dle_direction_newslist']; else $news_direction_by = $config['news_msort'];

if (isset ( $_REQUEST['category'] )) {
	if (substr ( $_GET['category'], - 1, 1 ) == '/') $_GET['category'] = substr ( $_GET['category'], 0, - 1 );
	$category = explode ( '/', $_GET['category'] );
	$category = end ( $category );
	$category = $db->safesql ( strip_tags ( $category ) );
    $link_cat = $category ."/";
} else $category = '';

$news_query = "WHERE approve='1' AND allow_main = '1'";
$cat_news_number = "WHERE approve='1' AND allow_main = '1'";

if($category){$cat_name = "WHERE alt_name='".$category."'";}

$result_category = $db->query("SELECT name,alt_name,id,descr,keywords FROM ".PREFIX."_category $cat_name");
$name_category = $db->get_row($result_category);

$meta_description = stripslashes($name_category['descr']);
$meta_keywords = stripslashes($name_category['keywords']);

if($category){
    $cat = get_sub_cats( $name_category['id'] );
    $cat_news_number = "WHERE `category` regexp '[[:<:]]({$cat})[[:>:]]' AND approve='1'";
    $news_query = "WHERE `category` regexp '[[:<:]]({$cat})[[:>:]]' AND approve='1'";
}

/* Делаем запрос на подсчёт количества новостей */
$sql_page = "SELECT COUNT(*) as count FROM " . PREFIX . "_post $cat_news_number ORDER BY $news_sort_by $news_direction_by";
$temp = $db->super_query($sql_page);
$posts = $temp['count'];

if($posts > 0){
    $page = intval( $_REQUEST['page'] );
    $total = intval( ( ( $posts - 1 ) / $limit ) + 1 );
	if( $page <= 0 ) $page = 1;
	if( $page > $total ) $page = $total;
    $start = $page * $limit - $limit;
	$i = $start;

$result = $db->query("SELECT id,autor,title,category,alt_name,date,comm_num,rating,news_read FROM ".PREFIX."_post $news_query ORDER BY $news_sort_by $news_direction_by LIMIT " . $start . "," . $limit);
$tpl->load_template('newslist/newslist.tpl');

while($row = $db->get_row($result)){
    $i++;

    if (strlen($row['title']) > $word) $title = stripcslashes(substr ($row['title'], 0, $word))." ..."; else $title = stripcslashes($row['title']);
    $date_news = strtotime($row['date']);
    $link = $config['http_home_url'].$row['id']."-".$row['alt_name'].".html";

    $date = date("d.m.Y",strtotime($row['date']));
    $tpl->set("{date}", $date);

    $tpl->set('{spoiler}',"<img style=\"cursor: pointer;\" src=\"/templates/{$config['skin']}/images/newslist_plus.gif\" onclick=\"newslist('".$row['id']."', 'plus'); return false;\" align=\"absmiddle\" border=\"0\" title=\"Показать краткое содержание статьи\"></div>");
    $tpl->set('{story}','');

$metatags['titles'] = "Все новости » ".$name_category['name'];

$tpl->copy_template = "<div id=\"news-id-".$row['id']."\">".$tpl->copy_template."</div>";
$tpl->set_block("'\\[ajax\\](.*?)\\[/ajax\\]'si","");
$tpl->set("[not-ajax]", "");
$tpl->set("[/not-ajax]", "");

				$set = array(
			 		'{link}'		=> $link,
			 		'{rating}'	    => $row['rating'],
					'{title}'	    => $title,
					'{coments}'	    => $row['comm_num'],
					'{prosmotr}'	=> $row['news_read'],
					'{author}'	    => "<a href=\"/user/".$row['autor']."/\">".stripcslashes($row['autor'])."</a>",
					'{id}'	    => $row['id'],					
                     );

				$tpl->set('', $set);
				$tpl->compile('fileslist');
}
		$list = $tpl->result['fileslist'];
		$tpl->load_template('newslist/main.tpl');
		$tpl->set('{list}', $list);
		$tpl->set('[newslist]','');
        $tpl->set('[/newslist]','');

$tpl->set ( '{sort}', news_sort ( $do ) );
$tpl->set('{description}', "Просмотр категории: ".$name_category['name']);

/* Указываем что всё это нужно грузить в тег {content} в main.tpl */
$tpl->set_block("'\[allcat\](.*?)\[/allcat\]'si","");
$tpl->compile('content'); 
$tpl->clear();

$nav = "/newslist/".$link_cat."page/{page}/";
CreateNavigation( $nav, $page, $total );

$db->free();
}else{
    $tpl->load_template('info.tpl');
    $metatags['titles'] = "Все новости » Категория пуста";
    $tpl->set('{error}', "К сожалению эта категория ещё не имеет ни одну статью, попробуйте зайти позже.");
    $tpl->set('{title}', "Категория пуста");
    $tpl->compile('content');
    $tpl->clear();
    }
?>
