<?
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="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 class="active" typeof="v:Breadcrumb"><span property="v:title">Форум</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">';
if (isset($b)) {
echo'<div class="catSides"><a href="/"><i class="md-format-list-bulleted"></i> Главная </a> - Форум</div>';

echo '<div class="row"><div class="col-xs-3 col-sm-1"><div class="user_brief">Новые: <a href="newtheme.php">Темы</a> | <a href="newposts.php">Сообщения</a></br>
 Мои: <a href="user_thems.php?id='.$b['id'].'">Темы</a> | <a href="user_posts.php?id='.$b['id'].'">Сообщения</a> | <a href="podpis.php">Наблюдаемые</a></div></div></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="row text_box"><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()) {
$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);
$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]);
echo'<div class="catSides"><i class="md-format-list-bulleted"></i><a href="viewrazd.php?id='.$a['id'].'"> '.$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>'; 
$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 '<ul class="nav nav-pills nav-stacked"><li><a href="viewthem.php?id='.$arr['id'].'"><i class="md-forum"></i> '.$arr['name'].' ('.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']]).')</a></ul>';
}
}
}
}
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="row text_box"><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="catSides"><i class="md-format-list-bulleted"></i> <a href="/rules">Правила</a> | <a href="/bbcodes"> Справка BBcode</a> | <a href="/smiles">Все смайлы</a> | <a href="search">Поиск</a> | <a href="wholly_new">Найти новые</a></div>'; 

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


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