<?php
include_once '../sys/inc/start.php';
$doc = new document();

$doc->title='Форум';
if (!isset($_GET['id']) || !is_numeric($_GET['id']))
{
header('Refresh: 1; url=./');
$doc->err('Ошибка выбора раздела');

exit;
}
$id_top=(int)$_GET['id'];

$q=mysql_query("SELECT `forum_topics`.*, `forum_categories`.`name` AS `category_name` FROM `forum_topics`
LEFT JOIN `forum_categories` ON `forum_categories`.`id` = `forum_topics`.`id_category`
WHERE `forum_topics`.`id` = '$id_top' AND `forum_topics`.`group_show` <= '$user->group' AND `forum_categories`.`group_show` <= '$user->group'");

if (!mysql_num_rows($q))
{
header('Refresh: 1; url=./');
$doc->err('Раздел не доступен');

exit;
}

$topic=mysql_fetch_assoc($q);



$doc->title.=' - '.$topic['name'];



$posts = array();
$pages = new pages;
$pages->posts = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_themes` WHERE `id_topic` = '$topic[id]' AND `group_show` <= '$user->group'"), 0); // количество категорий форума
$pages->this_page(); // получаем текущую страницу


$q = mysql_query("SELECT `forum_themes`.* , COUNT(`forum_messages`.`id`) AS `count` FROM `forum_themes`
LEFT JOIN `forum_messages` ON `forum_messages`.`id_theme` = `forum_themes`.`id` AND `forum_messages`.`group_show` <= '$user->group'
 WHERE `forum_themes`.`id_topic` = '$topic[id]' AND `forum_themes`.`group_show` <= '$user->group'
 GROUP BY `forum_themes`.`id`
 ORDER BY `forum_themes`.`top`, `forum_themes`.`time_last` DESC LIMIT {$pages->my_start()}, {$pages->items_per_page}");
while ($themes=mysql_fetch_assoc($q)) {
$is_top=$themes['top'];
$is_open=(int)($themes['group_write']<=1);
$i=array('size'=>'small','src'=>"/sys/images/icons/forum.theme.$is_top.$is_open.png");
$t="<a href='theme.php?id={$themes['id']}'>$themes[name]</a> ($themes[count])";

if($themes['count']>$pages->items_per_page)$t.=" <a href='theme.php?id={$themes['id']}&amp;page=end'>&gt;&gt;</a>";

$autor=new user($themes['id_autor']);
$last_msg=new user($themes['id_last']);

$p=($autor->id!=$last_msg->id?$autor->nick.'/'.$last_msg->nick:$autor->nick).' ('.vremja($themes['time_last']).')';
$posts[]=array('title'=>$t,'icon'=>$i,'post'=>$p);
}
if (!$posts)$posts[]=array('icon'=>array('size'=>'small','src'=>'/sys/images/icons/empty.png'),'title'=>'Доступных Вам тем нет');

$show = new design();
$show->assign('post',$posts);
$show->display('design.listing.tpl');

$pages->listing('topic.php?id='.$topic['id'].'&amp;'); // вывод страниц



if ($topic['group_write']<=$user->group)$doc->act('Начать новую тему','theme.new.php?id_topic='.$topic['id']."&amp;return=".URL);

if ($topic['group_edit']<=$user->group)$doc->act('Параметры раздела','topic.edit.php?id='.$topic['id']."&amp;return=".URL);

$doc->ret(empty($topic['category_name'])?'В категорию':$topic['category_name'],'category.php?id='.$topic['id_category']);
$doc->ret('Форум','./');
?>
