<?php
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';
$set['title']='Просмотр акции';
include_once '../sys/inc/thead.php';
if (isset($_GET['id']))$post['id']=intval($_GET['id']);else {header("Location: /index.php?".SID);exit;}
if(mysql_result(mysql_query("SELECT COUNT(*) FROM `stock` WHERE  `id` = '".intval($_GET['id'])."'"),0) ==0){header("Location: /index.php?".SID);exit;}
$post=mysql_fetch_array(mysql_query("select * from `stock` where `id`='".intval($_GET['id'])."';"));
title();
aut();
err();
if (isset($_POST['msg']) && isset($user))
{
$msg=$_POST['msg'];
if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024){$err='Сообщение слишком длинное';}
elseif (strlen2($msg)<2){$err='Короткое сообщение';}
elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_komm` WHERE `id_stock` = '".intval($_GET['id'])."' AND `id_user` = '$user[id]' AND `msg` = '".mysql_real_escape_string($msg)."' LIMIT 1"),0)!=0){$err='Ваше сообщение повторяет предыдущее';}
else{
mysql_query("INSERT INTO `stock_komm` (`id_user`, `time`, `msg`, `id_stock`) values('$user[id]', '$time', '".mysql_real_escape_string($msg)."', '".intval($_GET['id'])."')");
msg('Ваш комментарий успешно принят');
}
}

elseif (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_komm` WHERE `id` = '".intval($_GET['del'])."' && `id_stock` = '".intval($_GET['id'])."'"),0))
{
if (isset($user) && ($user['level']>=4) || isset($user) && ($user['id']=$post['id_user']))
{
mysql_query("DELETE FROM `stock_komm` WHERE `id` = '".intval($_GET['del'])."' LIMIT 1");
msg('Комментарий удален');
}

}
echo "<div class='gmenu'>";
if (isset($user) && mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_rating` WHERE `id_user` = '$user[id]' AND `id_stock` = '$post[id]'"), 0)==0){if (isset($_GET['rating']) && $_GET['rating']=='ok'){  mysql_query("UPDATE `stock` SET `rating` = '".($post['rating']+1)."' WHERE `id` = '$post[id]' LIMIT 1",$db); mysql_query("UPDATE `stock_rating` SET `rating` = '".($post['rating']+1)."' WHERE `id` = '$post[id]' LIMIT 1",$db);mysql_query("INSERT INTO `stock_rating` (`id_user`, `id_stock`) values('$user[id]', '$post[id]')",$db);msg ('Ваш отзыв принят');$post=mysql_fetch_assoc(mysql_query("SELECT * FROM `stock` WHERE `id` = $post[id] LIMIT 1")); }}
if (isset($user) && mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_rating` WHERE `id_user` = '$user[id]' AND `id_stock` = '$post[id]'"), 0)==0){if (isset($_GET['rating']) && $_GET['rating']=='no'){  mysql_query("UPDATE `stock` SET `rating` = '".($post['rating']-1)."' WHERE `id` = '$post[id]' LIMIT 1",$db); mysql_query("UPDATE `stock_rating` SET `rating` = '".($post['rating']-1)."' WHERE `id` = '$post[id]' LIMIT 1",$db);mysql_query("INSERT INTO `stock_rating` (`id_user`, `id_stock`) values('$user[id]', '$post[id]')",$db);msg ('Ваш отзыв принят');$post=mysql_fetch_assoc(mysql_query("SELECT * FROM `stock` WHERE `id` = $post[id] LIMIT 1")); }}
echo "<b>".output_text($post['name'])."</b> <br />\n";
echo " ".output_text($post['msg'])." <br />\n";
echo"<div class='hr'></div>";
$ank=get_user($post['id_user']);echo "Добавил: <a href='/info.php?id=$ank[id]'>$ank[nick]</a>";
echo "(".vremja($post['time']).")\n";echo"<div class=hr></div>";
if (isset($user) && mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_rating` WHERE `id_user` = '$user[id]' AND `id_stock` = '$post[id]'"), 0)==0){echo"Ваш отзыв:\n";echo " <a href=?id=$post[id]&amp;rating=ok><img src=plus.png></a> /";echo " <a href=?id=$post[id]&amp;rating=no><img src=minus.png></a>";echo"<br/>";}
echo"<img src=rating.png> Рейтинг: <font color=blue>$post[rating]</font><br/>";
if ($post[write]==1){echo"<b><font color=red> акция закрыта</font></b><br/>";}
echo"<div class=hr></div>";
if ($user[stock]==1){echo"[ <a href=edit.php?id=$post[id]>Редактировать</a>]";}

echo"</div><div class='foot'>Комментарии: ".mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_komm` WHERE `id_stock` = '$post[id]'"),0)."</div>";


$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `stock_komm` WHERE `id_stock` = '".intval($_GET['id'])."'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `stock_komm` WHERE `id_stock` = '".intval($_GET['id'])."' ORDER BY `id` DESC LIMIT $start, $set[p_str]");
if ($k_post==0)
{
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Нет комментариев\n";

echo "  </td>\n";
echo "   </tr>\n";
}
while ($post = mysql_fetch_assoc($q))
{
//$ank=mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
$ank=get_user($post['id_user']);


if($num==1){echo "<div class='gmenu'>";$num=0;}else{echo "<div class='gmenu'>";$num=1;}
echo "".online($ank['id'])." <a href='/info.php?id=$ank[id]'>$ank[nick]</a> (".vremja($post['time']).")<br/>\n";
echo output_text($post['msg'])."<br />\n";
if (isset($user)){echo"[<a href=otvet.php?id=$post[id]>Ответ</a>]";}
if ($user['stock']==1){
echo "<a href='?id=".intval($_GET['id'])."&amp;del=$post[id]'>Удалить</a><br />\n";

}
echo"</div>";
}


if ($k_page>1)str("?id=".intval($_GET['id']).'&amp;',$k_page,$page); // Вывод страниц



if (isset($user))
{
$post=mysql_fetch_array(mysql_query("select * from `stock` where `id`='".intval($_GET['id'])."';"));
if ($post[post]==1){echo"<div class=hr></div>";echo"Комментарии запрещены";}else{
echo "<form method=\"post\" name='message' action=\"?id=".intval($_GET['id'])."&amp;page=$page\">\n";
if ($set['web'] && is_file(H.'style/themes/'.$set['set_them'].'/altername_post_form.php'))
include_once H.'style/themes/'.$set['set_them'].'/altername_post_form.php';
else

echo "Сообщение:<br />\n<textarea name=\"msg\"></textarea><br />\n";
if ($user['set_translit']==1)echo "<label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label><br />\n";
echo "<input value=\"Отправить\" type=\"submit\" />\n";
echo "</form>\n";
}

}


echo"<div class='foot'>\n";
echo"<a href='index.php'>Назад</a><br />\n";
echo"</div>\n";
include_once '../sys/inc/tfoot.php';
?>