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

if (!isset($_GET['id']) || $_GET['id'] == 0)
{
	header('Location: index.php');
	exit;
}

$ID = intval($_GET['id']);

$poll = mysql_fetch_assoc(mysql_query("SELECT * FROM `poll` WHERE `id` = '$ID' LIMIT 1"));

if (!isset($poll['id']))
{
	header('Location: index.php');
	exit;
}

$ank = get_user($poll['id_user']);

$set['title']='✔ Опрос от ' . $ank['nick'] . ' ツ';

// Опросы :: Голосуем

if (isset($user) && isset($_GET['vote']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_votes` WHERE `id_user` = '$user[id]' AND `id_poll` = '$poll[id]'"), 0) == 0 && mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_variant` WHERE `id` = '" . intval($_GET['vote']) . "' AND `id_poll` = '$poll[id]'"), 0) == 1)
{
	mysql_query("INSERT INTO `poll_votes` (`id_otvet`, `id_user`, `id_poll`, `time`) values('" . intval($_GET['vote']) . "', '$user[id]', '$ID', '$time')");
	
				/*
			===================================
			Лента
			===================================
			*/
			
			$q = mysql_query("SELECT * FROM `frends` WHERE `user` = '" . $user['id'] . "' AND `i` = '1'");
			
			while ($f = mysql_fetch_array($q))
			{
				$a = get_user($f['frend']);

				$lentaSet = mysql_fetch_array(mysql_query("SELECT * FROM `tape_set` WHERE `id_user` = '".$a['id']."' LIMIT 1")); // Общая настройка ленты
				if ($a['id'] != $ank['id'] && $f['lenta_poll_vote'] == 1 && $lentaSet['lenta_poll_vote'] == 1 && $user['id'] != $ank['id']
				&& mysql_result(mysql_query("SELECT COUNT(*) FROM `tape` WHERE `id_user` = '$a[id]' AND `id_file` = '$poll[id]' AND `type` = 'poll_like' LIMIT 1"),0) == 0)
				mysql_query("INSERT INTO `tape` (`id_user`,`ot_kogo`,  `avtor`, `type`, `time`, `id_file`) values('$a[id]', '$user[id]', '$poll[id_user]', 'poll_vote', '$time', '$ID')"); 

			}
	

	
	$_SESSION['message'] = 'Ваш голос успешно принят';
	header('Location: ?id=' . $ID);
	exit;
}elseif (!isset($user) && isset($_GET['vote'])) $err = 'Вы должны сначала авторизоваться';


// Голосуем за опрос

if (isset($_GET['like']))
{
		if ($user['id'] != $ank['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_like` WHERE `id_poll` = '$poll[id]' AND `id_user` = '$user[id]' LIMIT 1"),0)==0){
			
			mysql_query("INSERT INTO `poll_like` (`id_user`, `id_poll`) values('$user[id]', '$poll[id]')");

			/*
			===================================
			Лента
			===================================
			*/
			
			$q = mysql_query("SELECT * FROM `frends` WHERE `user` = '" . $user['id'] . "' AND `i` = '1'");
			
			while ($f = mysql_fetch_array($q))
			{
				$a = get_user($f['frend']);

				$lentaSet = mysql_fetch_array(mysql_query("SELECT * FROM `tape_set` WHERE `id_user` = '".$a['id']."' LIMIT 1")); // Общая настройка ленты
				if ($a['id'] != $ank['id'] && $f['lenta_poll_like'] == 1 && $lentaSet['lenta_poll_like'] == 1 && $user['id'] != $ank['id']
				&& mysql_result(mysql_query("SELECT COUNT(*) FROM `tape` WHERE `id_user` = '$a[id]' AND `id_file` = '$poll[id]' AND `type` = 'poll' LIMIT 1"),0)==0)
				mysql_query("INSERT INTO `tape` (`id_user`,`ot_kogo`,  `avtor`, `type`, `time`, `id_file`) values('$a[id]', '$user[id]', '$poll[id_user]', 'poll_like', '$time', '$ID')"); 

			}
		}
}

// Удаляем опрос

if (isset($user) && ($user['id'] == $ank['id'] || $user['level'] > 1) && isset($_GET['delete']))
{
	mysql_query("DELETE FROM `poll` WHERE `id` = '$ID' LIMIT 1");
	mysql_query("DELETE FROM `poll_variant` WHERE `id_poll` = '$ID'");
	mysql_query("DELETE FROM `poll_votes` WHERE `id_poll` = '$ID'");
	mysql_query("DELETE FROM `poll_like` WHERE `id_poll` = '$ID'");
	mysql_query("DELETE FROM `poll_komm` WHERE `id_poll` = '$ID'");
	
	if ($user['id'] != $ank['id'])
	{
		$msg = $user['group_name'] . ' [url=/info.php?id=' . $user['id'] . ']' . $user['nick'] . '[/url] удалил ваш опрос: [br][green]' . $poll['msg'] . '[/green][br][red]Старайтесь больше не нарушать правила нашего сайта![/red]';
		
		mysql_query("INSERT INTO `mail` (`id_user`, `id_kont`, `msg`, `time`) values('0', '$ank[id]', '".my_esc($msg)."', '$time')");
	}
	
	$_SESSION['message'] = 'Опрос успешно удален';
	header('Location: index.php');
	exit;
}


include_once '../../sys/inc/thead.php';
title();
aut(); // форма авторизации
err();

?>
<style>
.raquo {
  display: inline-block;
  height: 14px;
  width: 14px;
  border: 1px solid #7f7f7f;
  border-radius: 20px;
  -moz-border-radius: 20px;
  background-color: #ffffff;
  vertical-align: middle;
  text-align: center;
  line-height: 100%;
}

.raquo:hover {
  border: 1px solid #757575;
  background-color: #e9e9e9;
}
</style>
<?

echo '<div class="foot">';
echo '<img src="/style/icons/str2.gif" alt="*"> <a href="index.php">Опросы</a><br />';
echo '</div>';

echo '<div class="nav1">';
echo user::avatar($poll['id_user']) . user::nick($poll['id_user'], 1, 1, 1);
echo '<div class="st_1"></div>';

echo '<div class="st_2">';
echo output_text($poll['msg']);
echo '</div></div>';


// Like к опросу
$l = mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_like` WHERE `id_poll` = '$poll[id]'"),0);

echo '<div class="main2">';
if (isset($user) && $user['id'] != $ank['id']) {
	
	if ($user['id']!=$ank['id'] && mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_like` WHERE `id_poll` = '$poll[id]' AND `id_user` = '$user[id]' LIMIT 1"),0)==0){
		echo " <a href='?id=$poll[id]&like'><img src='/style/icons/like.gif' alt=''/>Класс!</a> • ";
		$like = $l;
	} else {
		echo " <img src='/style/icons/like.gif' alt=''/> Вы и ";
		$like = $l - 1;
	}
} else {
	echo " <img src='/style/icons/like.gif' alt=''/> ";
	$like = $l;
}

echo "$like чел.";
echo '</div>';

$q = mysql_query("SELECT * FROM `poll_variant` WHERE `id_poll` = '" . $ID . "' ORDER BY `num` ASC");

while ($var = mysql_fetch_assoc($q))
{
	$num++;
	$color = $num.$num.$num; 
	$color = $color.$color + $color + $color - ($color / 2);
	
	echo '<table style="border-bottom: 1px #b2b2b2 solid; padding: 2px; background:#d8d8d8;">';
	echo '<tr>';
	echo '<td>';
	
	if (mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_votes` WHERE `id_user` = '$user[id]' AND `id_poll` = '$poll[id]'"), 0) == 0)
	{
    echo '<a href="?id=' . $poll['id'] . '&amp;vote=' . $var['id'] . '"><span class="raquo"></span> <font color="#' . $color . '">' . text($var['otvet']) . '</font></a>';
  } elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_votes` WHERE `id_user` = '$user[id]' AND `id_otvet` = '$var[id]' AND `id_poll` = '$poll[id]'"), 0) == 1) {
    echo '<span class="raquo"><img src="/style/icons/ok.gif" alt="ok" /></span> <font color="#' . $color . '">' . text($var['otvet']) . '</font>';
  } else {
    echo '<span class="raquo"><img src="/style/icons/delete.gif" alt="ok" /></span> <font color="#' . $color . '">' . text($var['otvet']) . '</font>';
  }
	echo '</td>';
	
	echo '<td style="text-align:right;">';
	echo '(' . mysql_result(mysql_query("SELECT COUNT(*) FROM `poll_votes` WHERE `id_otvet` = '$var[id]' AND `id_poll` = '$poll[id]'"), 0) . ')';
	echo '</td>';
	echo '</tr>';
	echo '</table>';
}

if (isset($user) && ($user['id'] == $ank['id'] || $user['level'] > 1))
{
	echo '<div class="foot">';
	echo '<img src="/style/icons/str.gif" alt="*"> <a href="?id=' . $ID . '&amp;delete">Удалить опрос</a><br />';
	echo '</div>';
}

echo '<div class="foot">';
echo '<img src="/style/icons/str2.gif" alt="*"> <a href="index.php">Опросы</a><br />';
echo '</div>';

include_once '../../sys/inc/tfoot.php';
?>
