<?php
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;
$array = DB::$dbs->query("SELECT * FROM news WHERE id = ? LIMIT 1", [$id]);
if ($array-> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();

$title->SetTitle('Новости - Комментарии к '.$arr['name'].'');

///Подключаем шапку
$title->GetHeader();
echo '<div class="container-fluid page_header"><div class="container"><div class="row"><div class="col-xs-12 col-sm-8 col-md-9 col-lg-9"><h1>Новости - '.$arr['name'].' </h1><ol class="breadcrumb" xmlns:v="http://'.homeurl.'/#"><li typeof="v:Breadcrumb"><a href="/" rel="v:url" property="v:title">'.homeurl.'</a></li><li typeof="v:Breadcrumb"><a href="/news" rel="v:url" property="v:title">Новости</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title"> '.$arr['name'].'</span></li></ol></div></div></div></div>';
echo'<div class="container page_body"><div class="row"><div class="col-xs-12 page_content col-lg-9"><div class="navbar page_content_navbar collapse hidden-lg"><div class="container-fluid"><div class="navbar-collapse"></div></div></div><div class="text_box hidden-lg pr_block_small hidden"></div><div class="container-fluid cols_table">';

echo '<div class="catSides"><a href="/"><i class="md-format-list-bulleted"></i> Главная </a> - '.$arr['name'].' - Комментарии</div>';
echo'<div class="row text_box">'.text($arr['txt']).'<br/>Автор: '.Nick($arr['user']).'</div>';
echo '<div class="catSides"><i class="md-comment"></i> Обсуждение новости</div>';
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM news_comm WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: ?id='.$id);
die();
}
if (isset($_GET['otv'])) {
$t = DB::$dbs->queryFetch("SELECT * FROM blogger WHERE id = ?", [abs((int)$_GET['otv'])]);
if (empty($t['id'])) {
header('location: /');
}
$reply = $t['nick'].', ';
} else {
$reply = '';
}
$otv = isset($_GET['otv']) ? abs((int)$_GET['otv']) : false;
$look = DB::$dbs->querySingle("SELECT COUNT(id) FROM news_comm WHERE id_news = ?", [$id]);
$num = 10;
$k_page = k_page($look, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM news_comm WHERE id_news = ? ORDER BY time DESC LIMIT $start,$num", [$id]);
if ($sql->rowCount() == 0) {
err('Комментов нет');
} else {
while($a = $sql -> fetch()) {
$D = ($b['level'] > 2 ? '[<a href="?id='.$id.'&del='.$a['id'].'">Удл</a>] ':NULL);
echo '<div class="row text_box">'.$D.Nick($a['user']).' ('.vr($a['time']).') '.(($a['user'] != $b['id']) ? ' <a href="?id='.$id.'&otv='.$a['user'].'">[отв]</a>':NULL).'</br>'.text($a['txt']).'</div>';
}
}
if ($k_page>1) {
str('?id='.$id.'&', $k_page,$page);
}
if ($b == true) {
echo '<div class="catSides"><i class="md-comment"></i> Добавить комментарий</div>';
if (isset($_POST['add'])) {
$text = check($_POST['text']);
if (empty($text)) {
err('Введите текст!');
} elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM news_comm WHERE txt = ? AND id_news = ?", [$text,$id]) > 0) {
err('Такой коммент уже есть :)');
} else {
DB::$dbs->query("UPDATE `blogger` SET `gears` = `gears` + ? WHERE id = ?", [1,$b['id']]);
DB::$dbs->query("INSERT INTO news_comm SET txt = ?, time = ?, user = ?, id_news = ?", [$text,time(),$b['id'],$id]);
if (!empty($otv) && $otv != $b['id']) {
$mess = 'Пользователь [b]'.$b['nick'].'[/b] ответил на ваш комментарий к новости [url=http://'.$_SERVER['HTTP_HOST'].'/news/comm.html?id='.$id.']'.$arr['name'].'[/url]';
jurnal($otv,$mess,3);
}
header('location: ?id='.$id);
die();
}
}
$o = (!empty($otv) ? '&otv='.$otv : NULL);
echo '<form name="form" method="post" action="?do=comm&id='.$id.$o.'"><div class="row text_box">Комментарий:<br/><br/>';
quickpaste('text');
quickpanel();
echo'</br></br><textarea name="text" class="form-control" rows="3">'.$reply.'</textarea></br> <input type="submit" name="add" class="btn btn-primary" value="Добавить"/></div></form>';
}

///Подключаем меню
$title->GetMenu();


///Подключаем футер
$title->GetFooter();
?>
