<?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
*/
	$_GET['var1']=(!empty($_GET['var1']))?$_GET['var1']:0;
	$page=(!empty($_GET['var2']))?intval($_GET['var2']):0;

	$forum_info=array();
	switch($_GET['action'])
	{
		case('theme'):
		case('toggle'):
		case('delete_theme'):
		case('hide_theme'):
			if ($_GET['var1'])
			{
				$forum_info=Db::i()->selectRow("SELECT ft.forum_id, forum_name, descr, forum_total_posts, total_posts, ff.total_themes, closed, theme_id, IF (ft.author_id>0, login, author) as author, author_id, subject, UNIX_TIMESTAMP(last_post) as last_post, pinup
														FROM ?_forum_themes ft
														JOIN ?_forum_forums ff ON ft.forum_id=ff.forum_id
														LEFT JOIN ?_users u ON u.user_id=ft.author_id
														WHERE ft.theme_id=?", $_GET['var1']);
				if (!$forum_info && access('admin'))
				{
					$forum_info=Db::i()->selectRow("SELECT ft.forum_id, forum_name, descr, forum_total_posts, total_posts, ff.total_themes, closed, theme_id, IF (ft.author_id>0, u.login, author) as author, author_id, subject, UNIX_TIMESTAMP(last_post) as last_post, pinup, uh.login as hidden
														FROM ?_forum_hthemes ft
														JOIN ?_forum_forums ff ON ft.forum_id=ff.forum_id
														LEFT JOIN ?_users u ON u.user_id=ft.author_id
														LEFT JOIN ?_users uh ON ft.hidden=uh.user_id
														WHERE ft.theme_id=?", $_GET['var1']);
				}
			}
		break;
		
		case('reply'):
		case('post'):
		case('quote'):
		case('edit'):
		case('delete'):
		case('hide_post'):
			if ($_GET['var1']) $forum_info=Db::i()->selectRow("SELECT post_id, ft.theme_id, fp.user_id, IF (fp.user_id>0, login, user_name) as user_name, IF (fp.user_id>0, u.email, fp.email) as email, msg, UNIX_TIMESTAMP(putdate) as putdate, ip, ft.forum_id, author, subject, forum_name, pinup
														FROM ?_forum_posts fp
														JOIN ?_forum_themes ft ON fp.theme_id=ft.theme_id
														JOIN ?_forum_forums ff ON ft.forum_id=ff.forum_id
														LEFT JOIN ?_users u ON u.user_id=fp.user_id
														WHERE fp.post_id=?", $_GET['var1']);
		break;
		
		case('search'):
		case('new'):
		case('edit_forum'):
		case('delete_forum'):
		case(''):
			if ($_GET['var1']) $forum_info=Db::i()->selectRow("SELECT forum_id, forum_name, descr, forum_total_posts, ff.total_themes, closed
														FROM ?_forum_forums ff
														WHERE ff.forum_id=?", $_GET['var1']);
		break;
		case('unhide_post'):
		case('delete_hidden'):
			if ($_GET['var1'] && access('admin')) $forum_info=Db::i()->selectRow("SELECT post_id, ft.theme_id, fp.user_id, IF (fp.user_id>0, login, user_name) as user_name, IF (fp.user_id>0, u.email, fp.email) as email, msg, UNIX_TIMESTAMP(putdate) as putdate, ip, ft.forum_id, author, subject, forum_name, pinup
														FROM ?_forum_hposts fp
														JOIN ?_forum_themes ft ON fp.theme_id=ft.theme_id
														JOIN ?_forum_forums ff ON ft.forum_id=ff.forum_id
														LEFT JOIN ?_users u ON u.user_id=fp.user_id
														WHERE fp.post_id=?", $_GET['var1']);
		break;
		case('unhide_theme'):
		case('delete_theme_hidden'):
			if ($_GET['var1']) $forum_info=Db::i()->selectRow("SELECT ft.forum_id, forum_name, descr, forum_total_posts, total_posts, ff.total_themes, closed, theme_id, IF (ft.author_id>0, u.login, author) as author, author_id, subject, UNIX_TIMESTAMP(last_post) as last_post, pinup, uh.login as hidden
														FROM ?_forum_hthemes ft
														JOIN ?_forum_forums ff ON ft.forum_id=ff.forum_id
														LEFT JOIN ?_users u ON u.user_id=ft.author_id
														LEFT JOIN ?_users uh ON ft.hidden=uh.user_id
														WHERE ft.theme_id=?", $_GET['var1']);
		break;

	}
	
	if (!empty($forum_info['forum_name'])) $forum_info['forum_name_html']=getbb($forum_info['forum_name'], 'nolinks');
	if (!empty($forum_info['descr'])) $forum_info['descr_html']=getbb($forum_info['descr']);
	if (!empty($forum_info['subject'])) $forum_info['subject_html']=getbb($forum_info['subject'], 'nolinks');
	if (!empty($forum_info['msg'])) $forum_info['msg_html']=getbb($forum_info['msg']);

	//навигация "хлебные крошки"
	if (!empty($forum_info['forum_id'])) addBreadcrumb($forum_info['forum_name_html'], LabCMS::i()->url($forum_info['forum_id']), true);
	if (!empty($forum_info['theme_id'])) addBreadcrumb($forum_info['subject_html'], LabCMS::i()->url(array($forum_info['theme_id'],1), 'theme'), true);

		//селектор форумов
	$select_forum=Db::i()->selectCol("SELECT forum_id AS ARRAY_KEY, forum_name FROM ?_forum_forums");
	foreach($select_forum as $forum_id=>$forum){$select_forum[$forum_id]=preg_replace('/\[.+?\]/', '', $forum);}
	Tpl::i()->assign('select_forum', $select_forum);

	//если нет массива с просмотренными темами, то создаем его
	if (!isset($_SESSION['forum']['themes']) || !is_array($_SESSION['forum']['themes'])) $_SESSION['forum']['themes']=array();

	//прикручиваем права по умолчанию
	Tpl::i()->assign('access_admin', access("admin"));
	Tpl::i()->assign('access_moderator', (access("moderator") || access("admin")));
	Tpl::i()->assign('access_add', ((!empty($forum_info['closed']))?access("add_closed"):access("add")));

	Tpl::i()->append('feed_rss', array('path'=>LabCMS::i()->url(false, 'new_posts', 'xml'), 'name'=>'Новые сообещния форума '.LabConfig::get('site_name')));
	if (!empty($forum_info['forum_id'])) Tpl::i()->append('feed_rss', array('path'=>LabCMS::i()->url($forum_info['forum_id'], false, 'xml'), 'name'=>'Форум '.$forum_info['forum_name'].' - '.LabConfig::get('site_name')));
	if (!empty($forum_info['theme_id'])) Tpl::i()->append('feed_rss', array('path'=>LabCMS::i()->url($forum_info['theme_id'], 'theme', 'xml'), 'name'=>'Сообщения темы "'.$forum_info['subject'].'" - '.LabConfig::get('site_name')));

	
	switch (true)
	{
		case(!$_GET['action'] && !empty($forum_info['forum_id']) && $_GET['data_type']=='xml'):
			if (!access('read')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('read_closed')) LabCMS::i()->error403();
			include dirname(__FILE__)."/themes.xml.php";
		break;
		case(!$_GET['action'] && !empty($forum_info['forum_id'])):
			if (!access('read')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('read_closed')) LabCMS::i()->error403();
			include dirname(__FILE__)."/themes.php";
		break;
		
		case($_GET['action']=='theme' && $forum_info['theme_id'] && $_GET['data_type']=='xml'):
			if (!access('read')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('read_closed')) LabCMS::i()->error403();
			include dirname(__FILE__)."/posts.xml.php";
		break;
		case($_GET['action']=='theme' && $forum_info['theme_id']):
			if (!access('read')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('read_closed')) LabCMS::i()->error403();
			include dirname(__FILE__)."/posts.php";
		break;
	
		case($_GET['action']=='post' && $forum_info['post_id']):
			if (!access('read')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('read_closed')) LabCMS::i()->error403();
			include dirname(__FILE__)."/post.php";
		break;
		
		case($_GET['action']=='new_posts' && $_GET['data_type']=='xml'):
			if (!access('read')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('read_closed')) LabCMS::i()->error403();
			include dirname(__FILE__)."/last_posts.xml.php";
		break;

		case($_GET['action']=='edit' && (access('moderator') || (access("edit") && $forum_info['user_id']==User::i()->id))):
			$mode="edit";
			addBreadcrumb("редактирование сообщения", LabCMS::i()->url($forum_info['post_id'], 'edit'));
			include dirname(__FILE__)."/edit_post.php";
		break;

		case($_GET['action']=='delete' && (access('admin') || (access('delete') && $forum_info['user_id']==User::i()->id))):
			$mode="delete";
			include dirname(__FILE__)."/edit_post.php";
		break;
		
		case($_GET['action']=='delete_hidden' && access('admin')):
			$mode="delete_hidden";
			include dirname(__FILE__)."/edit_post.php";
		break;
		
		case($_GET['action']=='delete_theme' && access('admin')):
			$mode="delete_theme";
			include dirname(__FILE__)."/edit_post.php";
		break;
		
		case($_GET['action']=='delete_theme_hidden' && access('admin')):
			$mode="delete_theme_hidden";
			include dirname(__FILE__)."/edit_post.php";
		break;

		case($_GET['action']=='toggle' && access('moderator')):
			include dirname(__FILE__)."/theme_toggle.php";
		break;
	
		case($_GET['action']=='quote' && $forum_info['post_id']):
		case($_GET['action']=='reply' && $forum_info['post_id']):
			if (!access('add')) LabCMS::i()->error403();
			if (!empty($forum_info['closed']) && !access('add_closed')) LabCMS::i()->error403();
			$mode="reply";
			include dirname(__FILE__)."/edit_post.php";
			addBreadcrumb("ответ на сообщение", LabCMS::i()->url($forum_info['post_id'], $_GET['action']));
		break;

		case($_GET['action']=='new' && $forum_info['forum_id']):
			if (!access('add')) LabCMS::i()->error403();
			if ($forum_info['closed'] && !access('add_closed')) LabCMS::i()->error403();
			$mode="new";
			include dirname(__FILE__)."/edit_post.php";
			addBreadcrumb("добавление темы", LabCMS::i()->url($forum_info['forum_id'], 'new'));
		break;

		case($_GET['action']=='search' && access('read')):
			include dirname(__FILE__)."/search.php";
			addBreadcrumb("результаты поиска", LabCMS::i()->url(array(0, 1, $_GET['search']), 'search'));
		break;

		case($_GET['action']=='add_forum' && access('admin')):
			$mode="new";
			include dirname(__FILE__)."/edit_forum.php";
			addBreadcrumb("создание нового форума", LabCMS::i()->url(false, 'add_forum'));
		break;

		case($_GET['action']=='edit_forum' && access('admin')):
			$mode="edit";
			include dirname(__FILE__)."/edit_forum.php";
			addBreadcrumb("редактирование форума", LabCMS::i()->url($forum_info['forum_id'], 'edit_forum'));
		break;
		
		case($_GET['action']=='hide_post' && access('moderator')):
			$mode="hide_post";
			include dirname(__FILE__)."/hide.php";
		break;
		
		case($_GET['action']=='hide_theme' && access('moderator')):
			$mode="hide_theme";
			include dirname(__FILE__)."/hide.php";
		break;
		
		case($_GET['action']=='unhide_post' && access('admin')):
			$mode="unhide_post";
			include dirname(__FILE__)."/hide.php";
		break;
		
		case($_GET['action']=='unhide_theme' && access('admin')):
			$mode="unhide_theme";
			include dirname(__FILE__)."/hide.php";
		break;

		case($_GET['action']=='delete_forum' && access('admin')):
			$mode="delete";
			include dirname(__FILE__)."/edit_forum.php";
		break;
		
		default:
			include dirname(__FILE__)."/forums.php";
	}

	LabVars::$title=(!empty($forum_info['subject'])?$forum_info['subject']." / ":"").(!empty($forum_info['forum_name'])?$forum_info['forum_name']." / ":"").LabCMS::i()->title;
	Tpl::i()->assign('forum_info', $forum_info);