<?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="posts.tpl";

	if (!isset($_GET['search'])) $_GET['search']=isset($_GET['var3'])?$_GET['var3']:'';

	$pager=new Paginator($forum_info['total_posts'], LabConfig::$mcfg['forum_posts_at_page'], $page);
	$pager=$pager->getPages();


	if (access('admin'))
	{
		$posts=Db::i()->select("SELECT fp.user_id, fp.post_id, IF (u.user_id>0, login, fp.user_name) as user_name, fp.msg, IF (u.user_id>0,u.email, fp.email) as email, UNIX_TIMESTAMP(putdate) as putdate, INET_NTOA(fp.ip) as ip, avatar, edited, NULL as hidden
                        FROM ?_forum_posts fp 
                        LEFT JOIN ?_users u ON fp.user_id=u.user_id
                        WHERE fp.theme_id=?
						UNION
						SELECT fp.user_id, fp.post_id, IF (u.user_id>0, login, fp.user_name) as user_name, fp.msg, IF (u.user_id>0,u.email, fp.email) as email, UNIX_TIMESTAMP(putdate) as putdate, INET_NTOA(fp.ip) as ip, avatar, edited, true as hidden 
                        FROM ?_forum_hposts fp 
                        LEFT JOIN ?_users u ON fp.user_id=u.user_id
                        WHERE fp.theme_id=?
						ORDER BY post_id
						LIMIT ?d, ?d", $forum_info['theme_id'], $forum_info['theme_id'], $pager['sql_start'], $pager['sql_limit']);
						
		
	}	
	else
	{
		$posts=Db::i()->select("SELECT fp.user_id, fp.post_id, IF (u.user_id>0, login, fp.user_name) as user_name, fp.msg, IF (u.user_id>0,u.email, fp.email) as email, UNIX_TIMESTAMP(putdate) as putdate, INET_NTOA(fp.ip) as ip, avatar, edited 
						FROM ?_forum_posts fp 
						LEFT JOIN ?_users u ON fp.user_id=u.user_id
						WHERE fp.theme_id=?
						ORDER BY post_id
						LIMIT ?d, ?d", $forum_info['theme_id'], $pager['sql_start'], $pager['sql_limit']);
	}
	

	$first_post=($pager['current']==1);
	foreach($posts as $key=>$post)
	{
		$posts[$key]['access_edit']=(($post['user_id']==User::i()->id && access("edit")) || access("moderator")) ? 1 : 0;
		$posts[$key]['access_hide']=(!$first_post && access("moderator, admin")) ? 1 : 0;
		$posts[$key]['access_delete']=(!$first_post && (($post['user_id']==User::i()->id && access("delete")) || access("admin"))) ? 1 : 0;
		$posts[$key]['online']=($post['user_id'] && User::i()->isOnline($post['user_id']));
		//подсвечиваем найденные слова
		if (strlen($_GET['search']) > 0) 
		{
			$stemmer = new Stem_Ru();
			
			$words = explode(" ", trim($_GET['search'])); 
			$query_text=array();
			foreach($words as $word)
			{
				if (mb_strlen($word)>2) $query_text[]=preg_quote($stemmer->stem_word($word), '/');
			}

			$query_text=implode('|', $query_text);
			$post['msg']=preg_replace('/((?:'.$query_text.')[0-9a-zA-Zа-яА-ЯёЁ\-]*)/isu', '[b][i]\1[/i][/b]', $post['msg']);
		}
		$posts[$key]['msg_html']=getbb($post['msg']);
		$first_post=0;
	}

	$_SESSION['forum']['themes'][$forum_info['theme_id']]=$forum_info['last_post'];

	Tpl::i()->assign('posts', $posts); 
	Tpl::i()->assign("pager", $pager);
