<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
	$mod_template="news.tpl";

	$page=intval($_GET['var1']);
	if ($page < 1) $page = 1;
	
	if (LabCMS::i()->is_cached($mod_template, 60*60, User::i()->group_id.'-'.$page)) return;

	if (!access('edit')) $tmp_sql=" AND `news_date`<=NOW()"; else $tmp_sql="";
	
	$total=Db::i()->selectCell("SELECT COUNT(news_id) FROM ?_news WHERE lang_id=? $tmp_sql", LabCMS::i()->lang['id']);
	
	if ($total)
	{
		$pg=new Paginator($total, LabConfig::$mcfg['news_at_page'], $page);
		$pager=$pg->getPages();
		
		$news=Db::i()->select("SELECT news_id, news_text, news_title, news_date FROM ?_news WHERE lang_id=? $tmp_sql ORDER BY news_date DESC LIMIT ?d, ?d", LabCMS::i()->lang['id'], $pager['sql_start'], $pager['sql_limit']);
	
		
		if (is_array($news))
		{
			if (LabConfig::$mcfg['use_comments'])
			{
				$ids=array();
				foreach($news as $new){$ids[]=$new['news_id'];}
				$c_count=LabComments::getCommentsCounters($ids);
			}
			foreach($news as $key=>$new)
			{
				//если присутствует тег [more]
				if ($anonce=mb_stristr($new['news_text'], '[more]', true))
				{
					$news[$key]['news_text']=$anonce;
					$news[$key]['anonce']=1;
				}
				elseif (LabConfig::$cfg['news_anonce_length'])
				{
					$text_wo_tags=strip_tags($new['news_text']);
					if (strlen($text_wo_tags)>LabConfig::$cfg['news_anonce_length'])
					{
						preg_match('/^(.{1,'.LabConfig::$cfg['news_anonce_length'].'})\s/s', $text_wo_tags, $anonce);
						$news[$key]['news_text']=$anonce[1].'...';
						$news[$key]['anonce']=1;
					}
				}
				if (LabConfig::$mcfg['use_comments']) $news[$key]['comments']=intval($c_count[$new['news_id']]);
			}
		}
		
		Tpl::i()->assign("news", $news);
		Tpl::i()->assign("pager", $pager);
	}
