<?
include '../config.php';
include '../func.php';
head();
ban();
/////////////////////////////
$razdel = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_razdel` WHERE `id` = '".num($_GET['razdel'])."'"));
$podforum = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_podrazdel` WHERE `id` = '".num($_GET['pod_forum'])."'"));
$theme = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_themes` WHERE `id` = '".num($_GET['theme'])."'"));

if ( $id_session )
mysql_query("UPDATE `profile` SET `online` = 'theme".$theme['id']."' WHERE `id` = '".$id_session."'");

if ( $_GET['mod'] == 'edit_good' ) echo link1 . 'Тема успешно отредактирована' . div;
if ( $_GET['mod'] == 'edit_error' ) echo link1 . 'Возникла ошибка' . div;
if ( $_GET['mod'] == 'editp_good' ) echo link1 . 'Пост успешно отредактирован' . div;
if ( $_GET['mod'] == 'editp_error' ) echo link1 . 'Возникла ошибка' . div;

if ( !empty($id_session) AND $theme['user'] == $id_session OR $level_forum_session >= 1 OR $level_session >= 2  ) {
if ( $theme['status'] == 1 ) $status = '<b><a href="status.php?mod=1&theme='.$theme['id'].'">Открыть</a></b>';
else $status = '<b><a href="status.php?mod=2&theme='.$theme['id'].'">Закрыть</a></b>';
}

if ( $level_forum_session >= 1 OR $level_session >= 2  ) {
if ( $theme['fiz'] == 1 ) $fix = '<b><a href="fix.php?mod=1&theme='.$theme['id'].'">Открепить</a></b>';
else $fix = '<b><a href="fix.php?mod=2&theme='.$theme['id'].'">Закрепить</a></b>';
}

echo head . "<a href=\"index.php?act=pod_forum&id=".$razdel['id']."\">".$razdel['name']."</a> | <a href=\"themes.php?razdel=".$razdel['id']."&pod_forum=".$podforum['id']."\">".$podforum['name']."</a> | <b>".$theme['name']."</b>" . div;  

if ( empty ( $theme['id'] ) ) {
    
    echo 'Данной темы не существует.<br/>';
    pod_navig();
    foot();
    exit();
    
}


echo "<div class=\"link2\">";
echo 'Название темы: <b>'.$theme['name'].'</b><br/>';
echo ''.smile(adminbb(parsebb(nl2br($theme['msg'])))).'<br/ ><br/ >';
echo 'Автор: ' . user($theme['user_id']) . ' | ' . date2($theme['date']) . '<br/>'; 

if ($theme['user_id'] == $id_session OR $level_forum_session >= 1)
echo "<br/ >Тема: <b><a href=\"edit.php?mod=theme&theme=".$theme['id']."\">Edit</a></b> ";
if ($level_forum_session >= 3 OR $level_session >= 3) echo " <b><a href=\"delete.php?mod=theme&theme=".$theme['id']."\">Delete</a></b> ";
echo $status . ' ' .$fix;
echo '</div>'; 

if ( $theme['status'] == 0 )
echo link1 . "<a href=\"add.php?theme=".$theme['id']."&podforum=".$podforum['id']."&razdel=".$razdel['id']."\">Написать</a> | <a href=\"theme.php?razdel=".$theme['razdel_id']."&pod_forum=".$theme['pod_forum']."&theme=".$theme['id']."\">Обновить</a>" . div;
else
echo link2 . '<b>Тема закрыта для обсуждения</b>' . div;


$all = mysql_result(mysql_query("SELECT count(*) FROM `forum_msg` WHERE `theme` = '".$theme['id']."'"), 0);
if ( $all == 0) {
    echo link2 . 'Еще никто ничего не писал' . div;
    pod_navig();
    foot();
    exit();
    
}

echo '<div class="link2">';
if ($forum_abc_session == 0) { //Сортировка
    echo '<b>Сортировка:</b> [123] | <a href="theme.php?theme='.$theme['id'].'&pod_forum='.$podforum['id'].'&razdel='.$razdel['id'].'&mod=sort">[321]</a>';
    $order = '';
} else {
    echo '<b>Сортировка:</b> [321] | <a href="theme.php?theme='.$theme['id'].'&pod_forum='.$podforum['id'].'&razdel='.$razdel['id'].'&mod=sort">[123]</a></div>';
    $order = 'order by id desc';
}

if ($_GET['mod']=='sort') {
    
    if ($forum_abc_session == 0) {
        $result = 1;
    } else {
        $result = 0;
    }
    mysql_query("UPDATE `profile` SET `forum_abc` = '".$result."' WHERE `id` = '".$id_session."'");
    header('theme.php?theme='.(int)$_GET['theme'].'&podforum='.(int)$_GET['pod_forum'].'&razdel='.(int)$_GET['razdel']);
}


//echo div. kub . '<img src="../style/img/kub.gif" alt="" />' . div;


$onpage = $settforum_postov_session;
if (empty($settforum_postov_session)) $onpage = 10;

$allpage = ceil($all/$onpage);
$this_page = (isset($_GET['p'])) ? intval($_GET['p']) : 1 ;
if($this_page < 1 || $this_page > $allpage)$this_page = 1;
$start = $this_page*$onpage - $onpage;

$result = mysql_query("SELECT * FROM forum_msg WHERE `theme` = '".$theme['id']."' ".$order." limit ".num($start).",".num($onpage)."");

$i = 0;
$cols = 0;

while($msg = mysql_fetch_assoc($result)){
    ++$cols;
    $num = ($start + $cols);
    
echo '<div class="'.($i++ % 2 ? 'link1' : 'link2').'">';
$res = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$msg['user_id']."'"));

if ( $res['level_forum'] == 0 ) $login = ''.$res['login'].'';
if ( $res['level_forum'] == 1 ) $login = '<span style="color: #66ff00">'.$res['login'].'</span>';
if ( $res['level_forum'] == 2 ) $login = '<span style="color: #0000ff">'.$res['login'].'</span>';
if ( $res['level_forum'] == 3 ) $login = '<span style="color: #990000">'.$res['login'].'</span>';

//Редактирование поста
if ($msg['user_id'] == $id_session OR $level_forum_session >= 1 OR $level_session >= 3)
$edit = ' <a href="edit.php?post='.$msg['id'].'&mod=post">[ред]</a> ';
else
$edit = '';

//Удаление поста
if ($msg['user_id'] == $id_session OR $level_forum_session >= 2 OR $level_session >= 3)
$delete = ' <a href="delete.php?a=post&post='.$msg['id'].'">[del]</a> ';
else
$delete = '';

$us = mysql_fetch_assoc(mysql_query("SELECT * FROM `profile` WHERE `id` = '".$msg['edit_user']."'"));
if ( ($msg['edit'] > 0) AND (!empty($msg['edit_user'])) ) {
    
    $edites = '<br/><small>Изменен '.$msg['edit'].' раз, пользователем <a href="../'.$us['id'].'"><b>'.$us['login'].'</b></a></small><br/>';
} else {
    $edites = '';
}
//

if ($settforum_photo_session == 0) {
if($res['upload_ava'])
    $ava = '<img src="../resize.php?img=upload_ava/'.$res['upload_ava'].'&width=40&height=40" alt=""/></a>';
else
    $ava = '<img src="../resize.php?img=noava.png&width=40&height=40" alt=""/></a>';
} else {
    $ava = '';
}

$time = $msg['date'];
echo $ava. " <b>" .$num. "</b>. <a href=\"inslide.php?theme=".$theme['id']."&user=".$res['id']."\"><b>".$login."</b></a> <a href=\"inslide.php?theme=".$theme['id']."&user=".$res['id']."&mod=post&post=".$msg['id']."\">[цит]</a> <a href=\"inslide.php?theme=".$theme['id']."&user=".$res['id']."&post=".$msg['id']."\">[отв]</a>".$edit." ".$delete."<hr> ".forum_text(smile(adminbb(parsebb(mat(nl2br($msg['msg']))))), $msg['id'], $theme['id'])."<br/><br/><small>".date2($time)."</small>".$edites.div.div;
}


echo link1 . '<b>Страницы:</b> ';
navigation($allpage,"theme.php?razdel=".$razdel['id']."&pod_forum=".$podforum['id']."&theme=".$theme['id']."&p={p}",1,1);
echo div;

if ($settforum_forma_session == 0) {
echo link2;
echo '<form action="add.php?podforum='.$podforum['id'].'&razdel='.$razdel['id'].'&theme='.$theme['id'].'" method="post" name="form">';

echo '<textarea id="tarea" name="msg" cols="30" rows="5" style="width: 99%;"></textarea><br/>';

echo '<input name="submit" type="submit" class="button" value="Отправить" /></form></div>';
}

echo "<div class='link2'><b><a href=\"../info.php?act=bb\">BB-Коды</a></b> <a href=\"../smiles.php?\">Смайлы</a> | " ;


$online = mysql_result(mysql_query("SELECT count(*) FROM `profile` WHERE `time` > '".time()."' AND `online` = 'theme".$theme['id']."'"), 0);

echo '<a href="themeonl.php?theme='.$theme['id'].'">В теме: '.$online.' чел</a>';

echo '</div>';
pod_navig();
foot();
?>