<?
require_once'home.php';

$q1 = @mysql_query( @"select  *  from  `forum_topic` where `id`='".$id."' ;" );
$topic = @mysql_fetch_array(@$q1);
if (mysql_affected_rows() == 0){
echo $fs1;
echo '<b>Такой темы не существует!</b>';
echo $fs2;
foot();
exit;
}
$f = $topic ['section'];
$sf = $topic ['subfor'];

$que = @mysql_query( "select * from  `forum_subfor` where id='$sf';" );
$dtm = @mysql_fetch_array( @$que );
$s_name = $dtm['name'];

$que = @mysql_query( "select * from  `forum_sections` where id='$f';" );
$dtm = @mysql_fetch_array( @$que );
$f_name = $dtm['name'];

echo '<dl><dt></dt></dl><a href="'.F.'">Форумы</a>|<a href="'.F.'subfs/'.$f.'">'.$f_name.'</a>';
echo '|<a href="'.F.'temes/'.$sf.'\">'.$s_name.'</a>|<a href="'.F.'tema/'.$id.'/'.$page.'">'.$topic ['name'].'</a><br /><dl><dt></dt></dl>';

if (($topic['login']!=$row['id'])&&($row['level']<5)){
echo '<b>У вас нет прав доступа сюда!</b><br /> ';
echo '<a href="'.htmlspecialchars(getenv("HTTP_REFERER")).'">Вернуться</a>';
foot();
exit;
}


$topic_vote = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote` WHERE `type`='1' AND `topic`='$id'"), 0);
if ($topic_vote != 1) {
echo '<b>В этой теме нет опроса!</b><br /> ';
echo '<a href="'.htmlspecialchars(getenv("HTTP_REFERER")).'">Вернуться</a>';
foot();
exit;
}

    if (isset ($_GET['delvote']) && !empty ($_GET['vote'])) {
        $vote = abs(intval($_GET['vote']));
        $totalvote = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote` WHERE `type`='2' AND `id`='$vote' AND `topic` = '$id'"), 0);
        $countvote = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote` WHERE `type`='2' AND `topic`='".$id."'"), 0);
        if ($countvote <= 2)
            header('location: ?');
        if ($totalvote != 0) {
            if (isset ($_GET['yes'])) {
                mysql_query("DELETE FROM `forum_vote` WHERE `id` = '$vote'");
                $countus = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote_us` WHERE `vote` = '".$vote."' AND `topic`='".$id."'"), 0);
                $topic_vote = mysql_fetch_array(mysql_query("SELECT `count` FROM `forum_vote` WHERE `type`='1' AND `topic`='$id' LIMIT 1"));
                $totalcount = $topic_vote['count'] - $countus;
                mysql_query("UPDATE `forum_vote` SET  `count`='".$totalcount."'   WHERE `type` = '1' AND `topic` = '".$id."'");
                mysql_query("DELETE FROM `forum_vote_us` WHERE `vote` = '$vote'");
                header('location: ?');
            }
            else {
                echo '<b>Вы действительно хотите удалить вариант ответа?</b><br />';
                echo '<a href="?vote='.$vote.'&amp;delvote&amp;yes">Удалить</a>|';
                echo '<a href="'.htmlspecialchars(getenv("HTTP_REFERER")).'">Отмена</a>';
            }
        }
        else {
            header('location: ?');
        }
    }
    else
        if (isset ($_POST['submit'])) {
        $msg = iconv('utf-8', 'windows-1251', $msg);
            $vote_name = substr(trim($_POST['name_vote']), 0, 50);
            $msg = iconv('windows-1251', 'utf-8', $msg);
            if (!empty ($vote_name))
                mysql_query("UPDATE `forum_vote` SET  `name`='".mysql_real_escape_string($vote_name)."'  WHERE `topic` = '$id' AND `type` = '1'");
            $vote_result = mysql_query("SELECT `id` FROM `forum_vote` WHERE `type`='2' AND `topic`='".$id."'");
            while ($vote = mysql_fetch_array($vote_result)) {
                if (!empty ($_POST[$vote['id'].'vote'])) {
                $msg = iconv('utf-8', 'windows-1251', $msg);
                    $text = substr(trim($_POST[$vote['id'].'vote']), 0, 30);
                    $msg = iconv('windows-1251', 'utf-8', $msg);
                    mysql_query("UPDATE `forum_vote` SET  `name`='".mysql_real_escape_string($text)."'  WHERE `id` = '".$vote['id']."'");
                }
            }
            $countvote = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote` WHERE `type`='2' AND `topic`='".$id."'"), 0);
            for ($vote = $countvote; $vote < 8; $vote++) {
                if (!empty ($_POST[$vote])) {
                $msg = iconv('utf-8', 'windows-1251', $msg);
                    $text = substr(trim($_POST[$vote]), 0, 30);
                    $msg = iconv('windows-1251', 'utf-8', $msg);
                    mysql_query("INSERT INTO `forum_vote` SET `name`='".mysql_real_escape_string($text)."',  `type` = '2', `topic`='".$id."';");
                }
            }
            //echo 'Опрос изменен<br /><a href="?id='.$id.'">Продолжить</a>';
            header("Location: ".F."tema/$id/end");
        }
        else {
            $countvote = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote` WHERE `type`='2' AND `topic`='".$id."'"), 0);
            $topic_vote = mysql_fetch_array(mysql_query("SELECT `name` FROM `forum_vote` WHERE `type`='1' AND `topic`='$id' LIMIT 1"));
            echo '<form action="?" method="post">';
            echo '<br />Опрос(max. 150):<br/><input type="text" size="20" maxlength="150" name="name_vote" value="'.htmlentities($topic_vote['name'], ENT_QUOTES, 'UTF-8').'"/><br/>';
            $vote_result = mysql_query("SELECT `id`, `name` FROM `forum_vote` WHERE `type`='2' AND `topic`='".$id."'");
            while ($vote = mysql_fetch_array($vote_result)) {
                echo 'Ответ '.($i + 1).'(max. 50): <br/><input type="text" name="'.$vote['id'].'vote" value="'.htmlentities($vote['name'], ENT_QUOTES, 'UTF-8').'"/>';
                if ($countvote > 2)
                    echo '[<a href="?vote='.$vote['id'].'&amp;delvote">del</a>]';
                echo '<br/>';
                ++$i;
            }
            for ($vote = $i; $vote < 8; $vote++) {
                echo 'Ответ '.($vote + 1).'(max. 50): <br/><input type="text" name="'.$vote.'"/><br/>';
            }
            echo '<br /><input type="submit" class="ibutton" name="submit" value="Изменить"/></form>';
            echo '<br /><a href="'.htmlspecialchars(getenv("HTTP_REFERER")).'">Вернуться в тему</a>';
    }


?>