<?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 codes_code WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
DB::$dbs->query("UPDATE `codes_code` SET `chit` = `chit` + ? WHERE id = ?", [1,$id]);
$cat = DB::$dbs->queryFetch("SELECT id,name FROM codes_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
$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>Полезные куски кодов </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="/codes" rel="v:url" property="v:title">Полезные коды</a></li><li typeof="v:Breadcrumb"><a href="codlist.php?id='.$cat['id'].'" rel="v:url" property="v:title">'.$cat['name'].'</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="/codes"><i class="md-format-list-bulleted"></i> Полезные коды </a> - <a href="codlist.php?id='.$cat['id'].'">'.$cat['name'].'</a>  - '.$arr['name'].'</div>';
if ($b['level'] < 2) {
if ($arr['check'] == 0) {
echo '<div class="error">Код на модерации!</div>'; 
///Подключаем меню
$title->GetMenu();

///Подключаем футер
$title->GetFooter();
exit;
}
}
echo '<div class="row text_box"><b>Описание: </b> ';
echo text($arr['text']); echo'</div><div class="row text_box"><div class="citat" style="word-wrap:break-word;"><b>Код:</b></br>';
echo text($arr['code']);
echo '</div></div>';
echo '<div class="row text_box"><i class="md-group"></i> Добавил: '.Nick($arr['user_id']).' ('.vr($arr['time']).')</div>';

echo'<div class="catSides"><i class="md-comment"></i> Обсуждение кода ('.DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_comm WHERE cid = ?", [$id]).')</div>';
$array = DB::$dbs->query("SELECT * FROM codes_code WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}

$arr = $array -> fetch();
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM codes_comm WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: code.php?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;
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_comm WHERE cid = ?", [$id]);
$num = 10;
$k_page = k_page($k_mess, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM codes_comm WHERE cid = ? ORDER BY time DESC LIMIT $start,$num", [$id]);
if ($sql->rowCount() == 0) {
err('Комментариев нет!');
} else {
while($a = $sql -> fetch()) {
$D = ($b['level'] > 1 ? '[<a href=code.php?id='.$id.'&del='.$a['id'].'>Удл</a>] ':NULL);
echo '<div class="row text_box">'.$D.Nick($a['user_id']).' ('.vr($a['time']).') '.(($a['user_id'] != $b['id']) ? ' <a href="code.php?id='.$id.'&otv='.$a['user_id'].'">[отв]</a>':NULL).'</br>'.text($a['text']).'</div>';
}
}
if ($k_page>1) {
str('code.php?id='.$id.'&', $k_page,$page);
}
echo '<div class="catSides"><i class="md-comment"></i> Добавить комментарий</div>';
if ($b == true){
if (isset($_POST['add'])) {
$text = check($_POST['text']);
if (empty($text)) {
err('Введите текст!');
} elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM codes_comm WHERE text = ? AND cid = ?", [$text,$id]) > 0) {
err('Такой коммент уже есть :)');
} else {
DB::$dbs->query("INSERT INTO codes_comm SET text = ?, time = ?, user_id = ?, cid= ?", [$text,time(),$b['id'],$id]);
header('location: code.php?id='.$id);
die();
}
}
$o = (!empty($otv) ? '&otv='.$otv : NULL);
echo '<form name="form" method="post" action="code.php?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>';
} else {echo'<div class="row text_box">Только зарегистрированные пользователи могут оставлять комментарии. <a href= "/login" >Войдите</a>, пожалуйста.</div>';}

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


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

?>