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

	if (access('admin'))
	{
		
		$count=Db::i()->selectCell("SELECT
							(SELECT COUNT(theme_id) FROM ?_forum_themes WHERE forum_id=?) +
							(SELECT COUNT(theme_id) FROM ?_forum_hthemes WHERE forum_id=?)", $forum_info['forum_id'], $forum_info['forum_id']);
		
		$pager=new Paginator($count, LabConfig::$mcfg['forum_themes_at_page'], $page);
		$pager=$pager->getPages();
		
		$themes=Db::i()->select("SELECT theme_id, author_id, IF(author_id, ua.login, author) as author, subject, total_posts, IF(u.user_id, u.login, last_user) as last_user, last_user_id, UNIX_TIMESTAMP(last_post) as last_post, IF(last_post>?,1,0) as new, pinup, NULL as hidden
														FROM ?_forum_themes ft
														LEFT JOIN ?_users u ON ft.last_user_id=u.user_id
														LEFT JOIN ?_users ua ON ft.author_id=ua.user_id
														WHERE forum_id=?
														UNION
														SELECT theme_id, author_id, IF(author_id, ua.login, author) as author, subject, total_posts, IF(u.user_id, u.login, last_user) as last_user, last_user_id, UNIX_TIMESTAMP(last_post) as last_post, IF(last_post>?,1,0) as new, pinup, uh.login as hidden
														FROM ?_forum_hthemes ft
														LEFT JOIN ?_users u ON ft.last_user_id=u.user_id
														LEFT JOIN ?_users ua ON ft.author_id=ua.user_id
														LEFT JOIN ?_users uh ON ft.hidden=uh.user_id
														WHERE forum_id=?
														ORDER BY pinup DESC, last_post DESC LIMIT ?d, ?d", User::i()->last_visit, $forum_info['forum_id'], User::i()->last_visit, $forum_info['forum_id'], $pager['sql_start'], $pager['sql_limit']);

	}
	else
	{
		$count=Db::i()->selectCell("SELECT COUNT(theme_id) FROM ?_forum_themes WHERE forum_id=?", $forum_info['forum_id']);
		
		$pager=new Paginator($count, LabConfig::$mcfg['forum_themes_at_page'], $page);
		$pager=$pager->getPages();

		$themes=Db::i()->select("SELECT theme_id, author_id, IF(author_id, ua.login, author) as author, subject, total_posts, IF(u.user_id, u.login, last_user) as last_user, last_user_id, UNIX_TIMESTAMP(last_post) as last_post, IF(last_post>?,1,0) as new, pinup
														FROM ?_forum_themes ft
														LEFT JOIN ?_users u ON ft.last_user_id=u.user_id
														LEFT JOIN ?_users ua ON ft.author_id=ua.user_id
														WHERE forum_id=?
														ORDER BY pinup DESC, last_post DESC LIMIT ?d, ?d", User::i()->last_visit, $forum_info['forum_id'], $pager['sql_start'], $pager['sql_limit']);
	}
													
	foreach($themes as $key=>$theme)
	{
		$themes[$key]['total_pages']	=	ceil($theme['total_posts']/LabConfig::$mcfg['forum_posts_at_page']);
		$themes[$key]['subject_html']		=	getbb($theme['subject'], 'nolinks');
	}
	Tpl::i()->assign('pager', $pager);
	Tpl::i()->assign('themes', $themes);
	Tpl::i()->assign("forum_id", $forum_info['forum_id']);
