<?
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>';


$topic_vote = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_vote` WHERE `topic`='$id'"), 0);
if ($topic_vote != 0) {
echo '<b>В этой теме уже есть опрос!</b><br /> ';
echo '<a href="' . htmlspecialchars(getenv("HTTP_REFERER")) . '">Вернуться</a>';
foot();
exit;
}
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) && !empty ($_POST[0]) && !empty ($_POST[1]) && !empty ($_POST['count_vote'])) {

mysql_query("INSERT INTO `forum_vote` SET `name`='" . check($vote_name) . "', `time`='" . time() . "', `type` = '1', `topic`='" . $id . "';");
mysql_query("UPDATE `forum_topic` SET  `realid` = '1'  WHERE `id` = '$id'");
$vote_count = abs(intval($_POST['count_vote']));
if ($vote_count > 8)
$vote_count = 8;
else
if ($vote_count < 2)
$vote_count = 2;

for ($vote = 0; $vote < $vote_count; $vote++) {
$msg = iconv('utf-8', 'windows-1251', $msg);
$text = substr(trim($_POST[$vote]), 0, 30);
$msg = iconv('windows-1251', 'utf-8', $msg);
if (empty ($text)) {
continue;
}

mysql_query("INSERT INTO `forum_vote` SET `name`='" . check($text) . "',  `type` = '2', `topic`='" . $id . "';");
}
echo 'Опрос добавлен<br /><a href="'.F.'tema/'.$id.'">Продолжить</a>';
header("Location: ".F."tema/$id/end");
}
else
echo '<b>Ошибка добавления опроса!</b><br /><a href="addvote/'.$id.'">Повторить</a>';
}
else {
echo '<form action="?" method="post">';
echo '<br />Опрос(max. 150):<br/><input type="text" size="20" maxlength="150" name="name_vote" value="' . htmlentities($_POST['name_vote'], ENT_QUOTES, 'UTF-8') . '"/><br/>';
if (isset ($_POST['plus']))
++$_POST['count_vote'];
elseif (isset ($_POST['minus']))
--$_POST['count_vote'];
if ($_POST['count_vote'] < 2 || empty ($_POST['count_vote']))
$_POST['count_vote'] = 2;
elseif ($_POST['count_vote'] > 8)
$_POST['n'] = 8;
for ($vote = 0; $vote < $_POST['count_vote']; $vote++) {
echo 'Ответ ' . ($vote + 1) . '(max. 50): <br/><input type="text" name="' . $vote . '" value="' . htmlentities($_POST[$vote], ENT_QUOTES, 'UTF-8') . '"/><br/>';
}
echo '<input type="hidden" name="count_vote" value="' . abs(intval($_POST['count_vote'])) . '"/>';
echo ($_POST['count_vote'] < 8) ? '<br/><input type="submit" class="ibutton" name="plus" value="Доб. отв."/>' : '';
echo $_POST['count_vote'] > 2 ? '|<input type="submit" class="ibutton" name="minus" value="Уд. посл."/><br/>' : '<br/>';
echo '<br /><input type="submit" class="ibutton" name="submit" value="Добавить"/></form>';
echo '<br /><a href="'.htmlspecialchars(getenv("HTTP_REFERER")).'">Вернуться в тему</a>';
}

?>