<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$title->SetTitle(homeurl.' - Свежие новости');
$title->GetHeader();
//echo'<div class="row"><div class="col-sm-6">';
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> :: Свежие новости</div></div></div>';

echo'<div class="list-group-item media"><a href="search.html"><i class="zmdi zmdi-search"></i> Поиск статей</a></div>';
echo'<div class="t-body"><div class="list-group lg-alt">';
$look = DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_article");
if ($look == 0) {
err('Записей нет!');
} else {
$sql = DB::$dbs->query("SELECT * FROM gazeta_article ORDER BY time DESC LIMIT 5");
while($a = $sql->fetch()) {
echo '<a class="list-group-item media" href="news.html?id='.$a['id'].'"><div class="pull-left"><img class="img-circle pull-left" src="/'.(user_inf($a['user_id'], 'ava') != NULL ? user_inf($a['user_id'], 'ava') : 'assets/img/4.jpg').'" alt="" width="40" height="40"></div><div class="media-body"><div class="list-group-item-heading">  '.$a['name'].'  </div><small class="list-group-item-text">('.vr($a['time']).')</small></div></a>';
}
}
echo'</div></div>';

$id = isset($_GET['red']) ? abs((int)$_GET['red']) : false;

// Переимен
if (isset($_GET['red'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
$inf = DB::$dbs->queryFetch("SELECT name FROM `gazeta_cat` WHERE `id` = ? LIMIT 1", [$id]);
if (isset($_POST['save'])) {
$err = [];
$name = check($_POST['name']);
if (empty($name)) {
$err[] = 'Заполните название!';
}
if (empty($err)) {
DB::$dbs->query("UPDATE gazeta_cat SET name = ? WHERE id = ? LIMIT 1", [$name,$id]);
echo '<div class="alert alert-success"role="alert"">Успешно сохранено!</div>'; } else {
echo '<div class="alert alert-danger" role="alert"">';
foreach($err as $error) {
echo $error.'<br/>';
}
echo '</div>';
}
}
echo '<div class="list-group-item media"><form method="post" action="?red='.$id.'">Название:<br/> 
<input type="text" class="form-control m-b-10" name="name" value="'.$inf['name'].'"/> 
<input type="submit" name="save" class="btn btn-primary" value="Сохранить"/></div></form>'; 
}
// Удаление категории
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM gazeta_cat WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: ?');
die();
}
// Вывод разделов 
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> :: Список разделов</div></div></div>';
$looks = DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_cat");
if ($looks ==0) {
err('Категорий нет!');
} else {
$array = DB::$dbs->query("SELECT * FROM gazeta_cat ORDER BY id ASC");
while($a = $array->fetch()) {
$D = ($b['level'] > 4 ? '<a href="?red='.$a['id'].'">[Изм]</a> <a href="?del='.$a['id'].'">[Удл]</a> ':NULL);
echo '<div class="list-group-item media"><i class="zmdi zmdi-folder"></i> <a href="category.html?id='.$a['id'].'"><i class="md-folder"></i> '.$a['name'].' ('.DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_article WHERE cid = ?", [$a['id']]).(!empty($_new) ? '<font color="red">+'.$_new.'</font>':NULL).')</a> '.$D.' </div>';
}
}
if ($b['level'] > 4) {
if (isset($_POST['add'])) {
if (empty($_POST['name'])) {
err('Введите название!');
} else {
DB::$dbs->query("INSERT INTO gazeta_cat SET name = ?", [check($_POST['name'])]);
header('location: ?');
}
}
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i> Новая категория</div></div></div>';
echo '<div class="list-group-item media"><form action="?" method="POST"> <input type="text" class="form-control" name="name"/> <input type="submit" name="add" class="btn btn-primary" value="Добавить"/></form></div>';
}

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