<?php
session_start();
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
if (empty($id)) {
header ('location: /');
exit;
}
$query = DB :: $dbs -> query ("SELECT * FROM `downs` WHERE `id`= ?", [$id]);
if ($query -> rowCount () == 0) {
header ('location: /');
exit;
}
$file = $query -> fetch();
$raz = DB :: $dbs -> queryFetch("SELECT * FROM `down_r` WHERE `id`= ?", [$file['rid']]);
$praz = DB :: $dbs -> queryFetch("SELECT * FROM `down_pr` WHERE `id`= ?", [$file['prid']]);
$url = $_SERVER['DOCUMENT_ROOT'].'/'.$file['url'];
$ext = pathinfo($url);
$ext = $ext['extension'];
$title->SetTitle('Голосование за файл '.$file['name']);
$title->GetHeader();
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/"style="text-decoration:none; color:white;"> Главная</a> :: <a href="/downs"style="text-decoration:none; color:white;"> Загрузки</a> :: <a href="show_dir.html?id='.$raz['id'].'"style="text-decoration:none; color:white;">'.$raz['name'].'</a> :: <a href="show_kat.html?id='.$praz['id'].'"style="text-decoration:none; color:white;">'.$praz['name'].'</a> :: '.$file['name'].'</div></div></div>';

if ($file['user_id'] == $b['id'] || $b['level'] > 1) {
echo '<div class="list-group-item media"><a href="admin.php?id='.$id.'"><i class="zmdi zmdi-settings"></i>  <a href="admin.php?id='.$id.'">[Изменить]</a> <a href="admin.php?do=screen&id='.$id.'">[Скриншоты]</a></div>';
}
$vote = check($_GET['vote']);
if (empty($vote)) {
header('location: file.html?id='.$id);
die();
}
if (DB::$dbs->querySingle("SELECT COUNT(id) FROM `down_votes` WHERE `user_id` = ? AND `id_file` = ?", [$b['id'],$id]) > 0) {
header('location: file.html?id='.$id);
die();
}
if (isset($_POST['vote'])) {
$text = check($_POST['text']);
if (empty($text)) {
header('location: file.html?id='.$id);
die();
}

if ($vote = 'za') {
DB::$dbs->query("INSERT INTO `down_votes` SET `user_id` = ?, `id_file` = ?, `text` = ?, `type` = ?, `time` = ?", [$b['id'],$id,$text,'za',time()]);
DB::$dbs->query("INSERT INTO `log_votes` SET `user_id` = ?, `id_file` = ?, `text` = ?, `type` = ?, `time` = ?", [$b['id'],$id,$text,'za',time()]);
} elseif ($vote = 'protiv') {
DB::$dbs->query("INSERT INTO `down_votes` SET `user_id` = ?, `id_file` = ?, `text` = ?, `type` = ?, `time` = ?", [$b['id'],$id,$text,'protiv',time()]);
DB::$dbs->query("INSERT INTO `log_votes` SET `user_id` = ?, `id_file` = ?, `text` = ?, `type` = ?, `time` = ?", [$b['id'],$id,$text,'protiv',time()]);
}
$mess = Nick($b['id']).' оценил [b]'.($vote = 'za' ? 'положительно':'отрицатально').'[/b] Ваш файл <a href="/downs/file.html?id='.$id.'">'.$file['name'].'</a>';
jurnal($file['user_id'],$mess,4);
header('location: file.html?id='.$id);
die();
}
echo '<div class="list-group-item media"><form action="?id='.$id.'&vote='.$vote.'" method="POST">Комментарий (max.: 250):<br/></br>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea class="form-control" rows="8" name="text"></textarea><br/><input name="vote" type="submit" class="btn btn-primary" value="Дальше"/></form></div>';
echo '<div class="tile"><ul class="nav nav-pills nav-stacked"><li><a href="file.html?id='.$id.'"><i class="zmdi zmdi-home"></i> Вернуться к файлу</a></li></ul></div>';

$title->GetMenu();
$title->GetFooter();
?>