<?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($path.'files/head.php');
include('zag.php');
switch($_GET['act']){

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

echo '<div class="m_bg">Разделы</div>';
$result = mysql_query("SELECT * FROM `forum_cats_clan` WHERE `id_cat` = '0' and `clan` = '$udata[clan]' ");
$on = mysql_num_rows(mysql_query("SELECT * FROM `forum_cats_clan` WHERE `id_cat` = 0 and `clan` = '$udata[clan]' "));
if($on >= 1){
while($cat = mysql_fetch_assoc($result)){
		$topics = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_topics_clan` WHERE `id_cat` = '".$cat['id']."' "), 0);
	$msg = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `forum_msg_clan` WHERE `id_cat` = '".$cat['id']."'  "), 0);
	echo '<div class="m_bg"><img src="../forum/pic/forum.png"><a href="index.php?act=cat_view&amp;id='.$cat['id'].'">'.$cat['name'].'</a></div>';


}
}
else { echo' <a href="index.php?act=create">Создать разделы</a>'; }


break;



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

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

$row = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_cats_clan` WHERE `id` = '$id' and `clan` = '$udata[clan]'"));
if(isset($row['id'])){
	echo '<div class="m_bg">'.$row['name'].'</div>';
	$count_subcats = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_cats_clan` WHERE `id_cat` = '$id' and `clan` = '$udata[clan]'"), 0);
	if($count_subcats > 0){
		$result_subcats = mysql_query("SELECT * FROM `forum_cats_clan` WHERE `id_cat` = '$id' and `clan` = '$udata[clan]'");
		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_clan` WHERE `id_cat` = '$id' and `clan` = '$udata[clan]'"), 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_clan` WHERE `id_cat` = '$id' and `clan` = '$udata[clan]' 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_clan` WHERE `id_topic` = '".$topic['id']."' and `clan` = '$udata[clan]'"), 0);
				$last_user = mysql_fetch_assoc(mysql_query("SELECT `login` FROM `forum_msg_clan` WHERE `id_topic` = '".$topic['id']."' and `clan` = '$udata[clan]' 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;

case 'create':
$result = mysql_query("SELECT * FROM `forum_cats_clan` WHERE `id_cat` = 0 and `clan` = '$udata[clan]' ");
$on = mysql_num_rows(mysql_query("SELECT * FROM `forum_cats_clan` WHERE `id_cat` = 0 and `clan` = '$udata[clan]' "));
if($on == 0){
mysql_query("INSERT INTO `forum_cats_clan` SET `id` = '', `id_cat` = '0', `clan` = '$udata[clan]', `name` = 'Новости клана'");
mysql_query("INSERT INTO `forum_cats_clan` SET `id` = '', `id_cat` = '0', `clan` = '$udata[clan]', `name` = 'Общение'");
mysql_query("INSERT INTO `forum_cats_clan` SET `id` = '', `id_cat` = '0', `clan` = '$udata[clan]', `name` = 'Вопросы");
mysql_query("INSERT INTO `forum_cats_clan` SET `id` = '', `id_cat` = '0', `clan` = '$udata[clan]', `name` = 'Предложения'");
mysql_query("INSERT INTO `forum_cats_clan` SET `id` = '', `id_cat` = '0', `clan` = '$udata[clan]', `name` = 'Для новичков'");
echo'Форумы успешно созданы';
echo'<br/><a href=index.php>Вернуться на форум</a>';
include('down.php');
exit;
}
break;


}

include('down.php');
?>