<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$title->SetTitle('Дата-центр - Админ форум');
$title->GetHeader();

if ($b['level'] == 0) {
header('location:/');
die();
}

$do = isset($_GET['do']) ? check($_GET['do']) : false;
switch($do) {
default:
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> :: <a href="/administration/"style="text-decoration:none; color:white;"> Дата-центр</a> :: Админ форум</div></div></div><div class="tile">';
echo '<ul class="nav nav-pills nav-stacked"><li><a href="rulls.html"><i class="zmdi zmdi-comments"></i>  Правила модераторов <strong><span style="color:#ff0000;">[Обязательно!]</span></strong></a></ul></div>';
echo '<div class="list-group-item media"><i class="zmdi zmdi-shuffle"></i> <a href="?do=add">Создать</a> | <a href=javascript:document.location.reload()>Обновить</a></div>';
echo'<div class="t-body"><div class="list-group lg-alt">';
$k_topic = DB::$dbs->querySingle("SELECT COUNT(id) FROM adm_topic");
$num = 10;
$k_page = k_page($k_topic, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM adm_topic ORDER BY last DESC LIMIT $start,$num");
if ($sql -> rowCount() == 0) {
err('Тем ещё не нет!');
} else {
while($a = $sql->fetch()) {
$us_mess = DB::$dbs->queryFetch("SELECT user_id,tid,time FROM adm_mess WHERE tid = ? ORDER BY time DESC", [$a['id']]);
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM adm_mess WHERE tid = ?", array($a['id']));
if ($k_mess > 10) {
$pg = ceil($k_mess / 10);
$st = ' <a href="?do=view&id='.$a['id'].'&page='.$pg.'">></a>';
} else {
$st = '';
}
echo'<div class="list-group-item media"><a href="?do=view&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['type'] == 1 ? '#':NULL).' '.$a['name'].' ('.$k_mess.')</div></a><small class="list-group-item-text">'.Nick($us_mess['user_id']).' написал '.vr($us_mess['time']).'</small></div></div>';
}
}
if ($k_page>1) {
str('?', $k_page,$page);
}
break;
case 'add':
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> :: <a href="/administration/"style="text-decoration:none; color:white;"> Дата-центр</a> :: Создать тему</div></div></div>';
if (isset($_POST['add'])) {
$err = [];
$form = ['name' => isset($_POST['name']) ? check($_POST['name']) : null, 'text' => isset($_POST['text']) ? check($_POST['text']) : null];
if (empty($form['name']) OR empty($form['text'])) {
$err[] = 'Заполните название темы и её содержание!';
}
if (mb_strlen($form['name']) < 3 OR mb_strlen($form['name']) > 45) {
$err[] = 'Ошибка названия темы! Мин. 3 / макс. 45 символов.';
}
if (DB::$dbs->querySingle("SELECT COUNT(id) FROM adm_topic WHERE name = ?", [$form['name']]) > 0) {
$err[] = 'Такая тема уже есть!';
}
if (empty($err)) {
DB::$dbs->query("INSERT INTO adm_topic SET name = ?, user_id = ?, time = ?, last = ?", [$form['name'],$b['id'],time(),time()]);
$last = DB::$dbs->lastInsertId();
DB::$dbs->query("INSERT INTO adm_mess SET text = ?, user_id = ?, time = ?, tid = ?", [$form['text'],$b['id'],time(),$last]);
header('location: ?do=view&id='.$last);
die();
} else {
echo '<div class="alert alert-danger" role="alert">';
foreach($err as $error) {
echo '<b>'.$error.'</b><br/>';
}
echo '</div>';
}
}
echo '<div class="list-group-item media"><form action="?do=add" method="POST"> Название:<br/><input type="text" class="form-control" name="name"/><br/>Содержание:<br/><textarea class="form-control" rows="3" name="text"></textarea><br/><input type="submit" name="add" class="btn btn-primary" value="Отправить"/></div></form>';
break;
case 'view':
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
$array = DB::$dbs->query("SELECT * FROM adm_topic WHERE id = ? LIMIT 1", [$id]);

if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
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> :: <a href="/administration/"style="text-decoration:none; color:white;"> Дата-центр</a> :: '.$arr['name'].'</div></div></div>';

echo '<div class="list-group-item media"><i class="zmdi zmdi-shuffle"></i>  <a href=javascript:document.location.reload()>Обновить</a> |'.($b['level'] > 2 ? ($arr['type'] == 1 ? '<a href="?do=view&id='.$id.'&closed"> Открыть</a>':'<a href="?do=view&id='.$id.'&closed"> Закрыть</a>').'':NULL).' '.($b['level'] > 4 ? '| <a href="?do=view&id='.$id.'">Удалить тему</a>':NULL).'</div>';

$mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM adm_mess WHERE tid = ?", array($id));
if ($mess > 10) {
$pg = ceil($mess / 10);
$st = '&page='.$pg;
} else {
$st = '';
}
if (isset($_GET['delp'])) {

if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM adm_mess WHERE id = ?", [abs((int)$_GET['delp'])]);
header('location: ?do=view&id='.$id.$st);
die();
}
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM adm_mess WHERE tid = ?", [$id]);
DB::$dbs->query("DELETE FROM adm_topic WHERE id = ?", [$id]);
header('location: ?');
die();
}
if (isset($_GET['closed'])) {
if ($b['level'] < 3) {
header('location: ?do=view&id='.$id.$st);
die();
}
if ($arr['type'] != 1) {
DB::$dbs->query("UPDATE adm_topic SET type = ? WHERE id = ? LIMIT 1", [1,$id]);
header('location: ?do=view&id='.$id.$st);
die();
} else {
DB::$dbs->query("UPDATE adm_topic SET type = ? WHERE id = ? LIMIT 1", [0,$id]);
header('location: ?do=view&id='.$id.$st);
die();
}
}
if ($arr['type'] != 1) {
if (isset($_GET['otv'])) {
$post = DB::$dbs->queryFetch("SELECT user_id,id FROM adm_mess WHERE id = ? LIMIT 1", [abs((int)$_GET['otv'])]);
$user = DB::$dbs->queryFetch("SELECT nick,id FROM blogger WHERE id = ? LIMIT 1", [$post['user_id']]);

if (empty($user['id']) OR empty($post['id'])) {
header('location: ?do=view&id='.$id.$st);
die();
}
$reply = $user['nick'].', ';
} else {
$reply = '';
}

if (isset($_POST['add'])) {
$err = [];
$form = ['text' => isset($_POST['text']) ? check($_POST['text']) : null];

if (empty($form['text'])) {
$err[] = 'Заполните сообщение!';
}
if (DB::$dbs->querySingle("SELECT COUNT(id) FROM adm_mess WHERE text = ?", [$form['text']]) > 0) {
$err[] = 'Такое сообщение уже есть!';
}
if (empty($err)) {
DB::$dbs->query("INSERT INTO adm_mess SET text = ?, user_id = ?, time = ?, tid = ?", [$form['text'],$b['id'],time(),$id]);
DB::$dbs->query("UPDATE adm_topic SET last = ? WHERE id = ? LIMIT 1", [time(),$id]);   
                                        
header('location: ?do=view&id='.$id.'&sort='.$sorts.'&'.$st);
die();
} else {
echo '<div class="alert alert-danger" role="alert">';
foreach($err as $error) {
echo '<b>'.$error.'</b><br/>';
}
echo '</div>';
}
}
echo '<div class="list-group-item media"><form name="form" method="post" action="?do=view&id='.$id.'" method="POST">';
quickpaste('text');
quickpanel();
echo'</br></br><textarea class="form-control" rows="8" name="text">'.$reply.'</textarea><br/><input type="submit" class="btn btn-primary" name="add" value="Написать"/></div></form>';
}
else err('Эта тема закрыта, вы не можете писать ответы и редактировать сообщения.');
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM adm_mess WHERE tid = ?", [$id]);
$num = 10;
$k_page = k_page($k_mess, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM adm_mess WHERE tid = ? ORDER BY time ASC LIMIT $start,$num", [$id]);
if ($sql -> rowCount() == 0) {
err('Сообщений ещё нет!');
} else {
while($a = $sql->fetch()) {
$us = DB::$dbs->queryFetch("SELECT last,id FROM blogger WHERE id = ? LIMIT 1", [$a['user_id']]);
echo '<div class="list-group-item media"><div class="media-body"><div class="list-group-item-heading">'.($b['level'] > 4 ? '[<a href="?do=view&id='.$id.'sort='.$sorts.'&delp='.$a['id'].'">Удл</a>] ':NULL) .Nick($a['user_id']).'  ('.vr($a['time']).') '. (($arr['type'] != 1 && $a['user_id'] != $b['id']) ? ' <a href="?do=view&id='.$id.'&sort='.$sorts.'&otv='.$a['id'].'">[Отв]</a><br/>':'<br/>') . text($a['text']).'</div></div></div>';
}
}
if ($k_page>1) {
str('?do=view&id='.$id.'&sort='.$sort.'&', $k_page,$page);
}
break;
}

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