<?
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="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"><span class="label label-danger">Новые</span> <a href="newtheme.php">Темы</a> | <a href="newposts.php">Сообщения</a></br>';
if (isset($b)) {
echo'</br><span class="label label-danger">Мои</span> <a href="user_thems.php?id='.$b['id'].'">Темы</a> | <a href="user_posts.php?id='.$b['id'].'">Сообщения</a> | <a href="podpis.php">Наблюдаемые</a>';
}
echo'</div>';
$id = isset($_GET['red']) ? abs((int)$_GET['red']) : false;

// Переимен
if (isset($_GET['red'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
if (empty($id)) {
header('location: /');
die();
}
$inf = DB::$dbs->queryFetch("SELECT name FROM `forum_razdel` 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 forum_razdel 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 forum_razdel WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: ?');
die();
}

$Chainsaw = DB::$dbs->query("SELECT * FROM forum_razdel ORDER BY id ASC");
if ($Chainsaw -> rowCount() == 0) {
err('Разделы ещё не созданы!');
} else {
while($a = $Chainsaw->fetch()) {
$teme = DB::$dbs->querySingle("SELECT COUNT(id) FROM `forum_topic` WHERE `rid` = ? AND `time` > ?", [$a['id'],time()-86400]);
$mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM `forum_mess` WHERE `rid` = ? AND `time` > ?", [$a['id'],time()-86400]);
$D = ($b['level'] > 2 ? '<a href="?red='.$a['id'].'"style="text-decoration:none; color:white;">[Изм]</a> <a href="?del='.$a['id'].'"style="text-decoration:none; color:white;">[Удл]</a> ':NULL);
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-comments"></i><a href="viewrazd.php?id='.$a['id'].'"style="text-decoration:none; color:white;">  '.$a['name'].'</a>  ('.DB::$dbs->querySingle("SELECT COUNT(id) FROM `forum_topic` WHERE `rid` = ?", [$a['id']]).'/'.DB::$dbs->querySingle("SELECT COUNT(id) FROM `forum_mess` WHERE `rid` = ?", [$a['id']]).') [<font color="red"><b>+'.($teme == 0 ? '0' : $teme).'</b></font> | <font color="green"><b>+'.($mess == 0 ? '0' : $mess).'</b></font>] '.$D.' </div></div></div><div class="t-body"><div class="list-group lg-alt">';
$array = DB::$dbs->query("SELECT * FROM forum_prazdel WHERE rid = ? ORDER BY id ASC", [$a['id']]);
if ($array -> rowCount() == 0) {
err('Подразделы ещё не созданы!');
} else {
while($arr = $array->fetch()) {
echo '<a href="viewthem.php?id='.$arr['id'].'" class="list-group-item media"><div class="pull-left"><img class="img-circle pull-left" src="/assets/img/2.jpg" alt="" width="40" height="40"></div><div class="media-body"><div class="list-group-item-heading">'.$arr['name'].'</div><small class="list-group-item-text">Топиков: '.DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_topic WHERE prid = ?", [$arr['id']]).' :: Сообщений: '.DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_mess WHERE prid = ?", [$arr['id']]).'</small></div></a>';
}
}
}
}
echo' </div></div>';
if ($b['level']>2) {
if (isset($_POST['add'])) {
if (empty($_POST['name'])) {
err('Введите название!');
} else {
DB::$dbs->query("INSERT INTO forum_razdel SET name = ?", [check($_POST['name'])]);
header('location: ?');
}
}
echo '<div class="list-group-item media"><form action="?" method="POST">Новый раздел:<br/> <input type="text" class="form-control m-b-10"name="name"/> <input type="submit" name="add" class="btn btn-primary"value="Добавить"/></form></div>';
}
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-format-list-bulleted"></i>  <a href="/rules"style="text-decoration:none; color:white;">Правила</a> | <a href="/bbcodes"style="text-decoration:none; color:white;"> Справка BBcode</a> | <a href="/smiles"style="text-decoration:none; color:white;">Все смайлы</a> | <a href="search"style="text-decoration:none; color:white;">Поиск</a></div></div></div>'; 

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