<?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_theme=(int)$_GET['id'];

$q=mysql_query("SELECT `forum_themes`.* , `forum_categories`.`name` AS `category_name` , `forum_topics`.`name` AS `topic_name`
FROM `forum_themes` 
LEFT JOIN `forum_categories` ON `forum_categories`.`id` = `forum_themes`.`id_category`
LEFT JOIN `forum_topics` ON `forum_topics`.`id` = `forum_themes`.`id_topic`
WHERE `forum_themes`.`id` = '$id_theme' AND `forum_themes`.`group_show` <= '$user->group' 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;
}

$theme=mysql_fetch_assoc($q);



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






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





if ($theme['id_vote'])
{
$q=mysql_query("SELECT * FROM `forum_vote` WHERE `id` = '$theme[id_vote]' AND `group_view` <= '$user->group'");
if (mysql_num_rows($q))
{



$vote=mysql_fetch_assoc($q);

$vote_tpl = new design();
$vote_tpl->assign('name',$vote['name']);

$vote_accept=!@mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote_votes` WHERE `id_vote` = '$theme[id_vote]' AND `id_user` = '$user->id'"),0);
if (!$vote['active'])$vote_accept=false;

$q=mysql_query("SELECT `vote`, COUNT(*) as `count` FROM `forum_vote_votes` WHERE `id_vote` = '$theme[id_vote]' GROUP BY `vote`");
$countets = array(1=>0, 2=>0, 3=>0, 4=>0, 5=>0, 6=>0, 7=>0, 8=>0, 9=>0, 10=>0);
while($r=mysql_fetch_assoc($q))
{
$countets[$r['vote']]=$r['count'];
}

$all_votes=array_sum($countets);
$vote_tpl->assign('sum',$all_votes);

$votes = array();
for ($i=1;$i<=10;$i++) {
if ($vote['v'.$i])
{
$votes[]=array('name'=>$vote['v'.$i],'pc'=>@round($countets[$i]/$all_votes*100),'count'=>$countets[$i],'url'=>'theme.php?id='.$theme['id'].'&amp;page='.$pages->this_page.'&amp;vote='.$i);
}
}

if (!empty($_GET['vote']) && $user->group>=$vote['group_vote'] && $vote_accept)
{
$vote_add=(int)$_GET['vote'];
if ($vote['v'.$vote_add])
{

mysql_query("INSERT INTO `forum_vote_votes` (`id_vote`, `id_theme`, `id_user`, `vote`) VALUES ('$vote[id]','$theme[id]', '$user->id', '$vote_add')");

$doc->msg('Ваш голос успешно засчитан');
header('Refresh: 1; url=theme.php?id='.$theme['id'].'&page='.$pages->this_page);

$doc->ret('Вернуться в тему','theme.php?id='.$theme['id'].'&amp;page='.$pages->this_page);
exit;
}
}


$vote_tpl->assign('votes',$votes);

$vote_tpl->assign('is_add',$user->group >= $vote['group_vote'] && $vote_accept);

}
}


if (isset($vote_tpl))$vote_tpl->display('design.vote.tpl');



$q = mysql_query("SELECT * FROM `forum_messages` WHERE `id_theme` = '$theme[id]' AND `group_show` <= '$user->group' ORDER BY `id` ASC LIMIT {$pages->my_start()}, {$pages->items_per_page}");
echo mysql_error();
while ($messages=mysql_fetch_assoc($q)) {
$ank=new user($messages['id_user']);
$act=array();
$edit=false;
$hide=false;



if ($user->group)
{
$act[]=array('quote',"message.php?id_message=$messages[id]&amp;quote"); // цитирование
}

if ($user->group >= $messages['group_edit'])
{

if ($theme['group_show']<=1){

if ($messages['group_show']<=1)
{
$act[]=array('hide',"message.edit.php?id=$messages[id]&amp;return=".URL."&amp;act=hide&amp;".passgen()); // скрытие
}
else
{
$act[]=array('show',"message.edit.php?id=$messages[id]&amp;return=".URL."&amp;act=show&amp;".passgen()); // показ
$hide=true;
}
}

$act[]=array('edit',"message.edit.php?id=$messages[id]&amp;return=".URL); // редактирование
}
elseif ($user->id==$messages['id_user'] && TIME<$messages['time']+600)
{
// автору сообщения разрешается его редактировать в течении 10 минут
$act[]=array('edit',"message.edit.php?id=$messages[id]&amp;return=".URL); // редактирование
}

if ($ank->group <= $user->group && $user->id!=$ank->id)
{
if ($user->group >= 2)
// бан
$act[]=array('complaint',"/dpanel/user.ban.php?id_ank=$messages[id_user]&amp;return=".URL."&amp;link=".urlencode("/forum/message.php?id_message=$messages[id]"));
else
 // жалоба на сообщение
$act[]=array('complaint',"/complaint.php?id=$messages[id_user]&amp;return=".URL."&amp;link=".urlencode("/forum/message.php?id_message=$messages[id]"));
}


$t="<a href='message.php?id_message=$messages[id]'>$ank->nick</a> ".vremja($messages['time']);
$p=text::for_opis($messages['message'] );


if ($messages['edit_id_user'] && ($ank->group<$user->group || $ank->id==$user->id) )
{
$ank_edit=new user($messages['edit_id_user']);
$edit='<a href="message.history.php?id='.$messages['id'].'&amp;return='.URL.'">Изм.('.$messages['edit_count'].')</a> '.$ank_edit->login.' ('.vremja($messages['edit_time']).')<br />';
}

$post_files = array();
$post_dir_path = H.'/sys/files/.forum/'.$theme['id'].'/'.$messages['id'];
if (@is_dir($post_dir_path))
{
$dir = new files($post_dir_path);
$content = $dir -> getList('time_add:desc');
$files = &$content['files'];
$count = count($files);

for ($i=0;$i<$count;$i++){
$post = output_text($files[$i]->properties);

$post_files[]=array('icon' => $dcms -> browser_type=='wap'?$files[$i]->iconSmall():$files[$i]->icon(),
'title' => "<a href='/files".$files[$i]->getPath().".htm?order=time_add:desc'>".for_value($files[$i]->runame)."</a>", 'post' => $post);
}
}

$show = new design();
$show->assign('post',$post_files);
$files = $show->fetch('design.listing.tpl');


$posts[]=array('title'=>$t, 'icon'=>$ank->icon(), 'post'=>$p.$files, 'act'=>$act, 'edit'=>$edit, 'hide'=> $hide && $theme['group_show']<=1);
}
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('theme.php?id='.$theme['id'].'&amp;'); // вывод страниц


if ($theme['group_write']<=$user->group)$doc->act('Написать сообщение','message.new.php?id_theme='.$theme['id']."&amp;return=".URL);
if ($theme['group_edit']<=$user->group)$doc->act('Параметры темы','theme.edit.php?id='.$theme['id']."&amp;return=".URL);
if (!$theme['id_vote'] && $theme['group_write']<=$user->group && $user->group >= 2)
$doc->act('Создать голосование','vote.new.php?id_theme='.$theme['id']."&amp;return=".URL);
if ($theme['id_vote'] && $theme['group_write']<=$user->group && $user->group >= 2)
$doc->act('Голосование','vote.edit.php?id_theme='.$theme['id']."&amp;return=".URL);




if ($theme['group_edit'] <= $user->group && $user->group >= 2)$doc->act('Удаление сообщений','theme.posts.delete.php?id='.$theme['id']."&amp;page={$pages->this_page}");
if ($theme['group_edit'] <= $user->group && $user->group >= 2)$doc->act('Удаление темы','theme.delete.php?id='.$theme['id']);


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