<?php
define('PROTECTOR', 1);

$path = '../'; //путь

$textl = 'Форум';
include($path.'files/db.php');
include($path.'files/auth.php');
include($path.'files/func.php');
include($path .'files/core.php');
include('head.php');
switch($_GET['act']){

###############################################################################
##                   Главная страница, список разделов                       ##
###############################################################################
default:

echo '<div class="m_bg">Разделы</div>';
$result = mysql_query("SELECT * FROM `forum_cats` WHERE `id_cat` = 0");

while($cat = mysql_fetch_assoc($result)){
	$topics = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_topics` WHERE `id_cat` = '".$cat['id']."'"), 0);
	$msg = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_msg` WHERE `id_cat` = '".$cat['id']."'"), 0);
	echo '<div class="m_bg"><img src="pic/forum.png"> <a href="index.php?act=cat_view&amp;id='.$cat['id'].'">'.$cat['name'].'</a></div>';


}

break;

###############################################################################
##                            Просмотр раздела                               ##
###############################################################################
case 'cat_view':

$id = intval($_GET['id']);

$row = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_cats` WHERE `id` = '$id'"));
if(isset($row['id'])){
	echo '<div class="m_bg">'.$row['name'].'</div>';
	$count_subcats = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_cats` WHERE `id_cat` = '$id'"), 0);
	if($count_subcats > 0){
		$result_subcats = mysql_query("SELECT * FROM `forum_cats` WHERE `id_cat` = '$id'");
		while($subcat = mysql_fetch_assoc($result_subcats)){
			echo '<div class="m_bg"><a href="?act=cat_view&amp;id='.$subcat['id'].'">'.$subcat['name'].'</a></div>';

		}

	}else{
		$count_topics = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` WHERE `id_cat` = '$id'"), 0);

if($count_topics > 0){
		$pages = ceil($count_topics/10);
		if(isset($_GET['page'])){
			$page = abs(intval($_GET['page']));
		}else{
			$page = 1;
		}
		$from = ($page-1)*10;



			$result_topics = mysql_query("SELECT * FROM `forum_topics` WHERE `id_cat` = '$id' ORDER BY `time` DESC LIMIT $from, 10");
			while($topic = mysql_fetch_assoc($result_topics)){
				$msg = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_msg` WHERE `id_topic` = '".$topic['id']."'"), 0);
				$last_user = mysql_fetch_assoc(mysql_query("SELECT `login` FROM `forum_msg` WHERE `id_topic` = '".$topic['id']."' ORDER BY `time` DESC LIMIT 1"));
				$last_page = ceil($msg/10);

echo'<div class="m_bg">';
				echo '<a href="topic.php?id='.$topic['id'].'">'.$topic['title'].'</a>['.$msg.']  <a href="../search.php?nick='.$last_user['login'].'&amp;go=go">'.$last_user['login'].'</a>  <a href="topic.php?id='.$topic['id'].'&amp;page='.$last_page.'">»</a><br/>';

echo'</div>';
			}



		}else{
			echo 'В этом разделе пока нет сообщений<br/>Щёлкните <b>Новая тема</b>, и ваше сообщение станет первым!<br/>';
		}

		echo '<div class="m_bg"><a href="posting.php?act=topic_add&amp;id='.$id.'" class="button">Новая тема</a><br />';

navig($page, 'index.php?act=cat_view&id='.$id.'&amp;', $pages);
echo'</div>';
	}
}else{
	echo'Такого раздела не существует!';
}

break;
}

include('end.php');
?>