<?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="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>';

$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
$do = isset($_GET['do']) ? check($_GET['do']) : false;
switch($do) {
default:
$look = DB::$dbs->querySingle("SELECT COUNT(id) FROM rules_cat");
if ($look ==0) {
err('Категорий нет!');
} else {
$array = DB::$dbs->query("SELECT * FROM rules_cat ORDER BY id ASC");
while($a = $array->fetch()) {
$D = ($b['level'] > 3 ? '[<a href="?do=ren&id='.$a['id'].'">EDIT</a> | <a href="?do=delr&id='.$a['id'].'">DELETE</a>] ':NULL);
echo '<div class="list-group-item media"><a href="?do=view&id='.$a['id'].'"><img src="'.$img.'" alt="" />  '.$a['name'].' </a> '.$D.'</div>';
}
}
if ($b['level'] > 3) {
echo '<div class="list-group-item media"> <i class="zmdi zmdi-format-list-bulleted"></i><a href="?new"> Добавить категорию</a></div>';
if (isset($_POST['add'])) {
if (empty($_POST['name'])) {
err('Введите название!');
} else {
DB::$dbs->query("INSERT INTO rules_cat SET name = ?", [check($_POST['name'])]);
header('location: ?');
}
}
if (isset($_GET['new'])) {
echo '<div class="list-group-item media"><div class="pull-right text-muted top_right_box"></div><form action="?" method="POST">Новая категория:<br/> <input class="form-control" type="text" name="name"/></br> <input type="submit" class="btn btn-primary" name="add" value="Добавить"/></form></div>';
}
}
break;
case 'view':
$array = DB::$dbs->query("SELECT * FROM rules_cat 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 rules_view WHERE id = ?", [intval($_GET['del'])]);
}
if ($b['level'] > 2) {
echo '<div class="tile"><ul class="nav nav-pills nav-stacked"><li><a href="?do=add&id='.$id.'"><i class="zmdi zmdi-comments"></i> Добавить правило</a></ul></div>';
}
$look = DB::$dbs->querySingle("SELECT COUNT(id) FROM rules_view WHERE cid = ?", [$id]);
if ($look == 0) {
err('В данной категории нет записей!');
}
$num = 10;
$k_page = k_page($look, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM rules_view WHERE cid = ? ORDER BY id ASC LIMIT $start,$num",[$id]);
while($a = $sql->fetch()) {
echo '<div class="list-group-item media"><b>'.$a['name'].' </b><br/>'.text($a['text']).'<br/>'.($b['level'] > 3 ? '<a href="?do=edit&id='.$a['id'].'">[Редактировать]</a> | <a href="?do=view&id='.$id.'&del='.$a['id'].'">[Удалить]</a>':NULL).'</div>';
}
if ($k_page > 1) {
str('?do=view&id='.$id.'&',$k_page,$page);
}
break;
case 'delr':
$array = DB::$dbs->query("SELECT * FROM rules_cat WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
if ($b['level'] < 4) {
header('location: ?');
die();
}
$arr_art = DB::$dbs->query("SELECT * FROM rules_view WHERE cid = ? ORDER BY id DESC", [$id]);
while($art = $arr_art->fetch()) {
DB::$dbs->query("DELETE FROM rules_view WHERE cid = ?", [$art['id']]);
}
DB::$dbs->query("DELETE FROM rules_cat WHERE id = ?", [$id]);
echo '<div class="alert alert-danger" role="alert">Успешно удалено!</div>';
break;
case 'ren':
if ($b['level'] < 3) {
header('location: /');
die();
}
$array = DB::$dbs->query("SELECT * FROM rules_cat WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
if (isset($_POST['add'])) {
if (empty($_POST['name'])) {
err('Введите название!');
} else {
DB::$dbs->query("UPDATE rules_cat SET name = ? WHERE id = ?", [check($_POST['name']),$id]);
header('location: ?');
}
}
echo '<div class="list-group-item media"><form action="?do=ren&id='.$id.'" method="POST">Название:<br/> <input type="text" class="form-control" name="name" value="'.$arr['name'].'"/> <input type="submit" class="btn btn-primary"name="add" value="Добавить"/></form></div>';
break;
case 'add':
if ($b['level'] < 2) { 
header('location: /');
die();
}
$array = DB::$dbs->query("SELECT * FROM rules_cat WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
if (isset($_POST['add'])) {
if (empty($_POST['name']) OR empty($_POST['text'])) {
err('Введите название и содержание!');
} else {
DB::$dbs->query("INSERT INTO rules_view SET name = ?, text = ?, cid = ?", [check($_POST['name']),check($_POST['text']),$id]);
header('location: ?do=view&id='.$id);
}
}
echo '<div class="list-group-item media"><form action="?do=add&id='.$id.'" method="POST">Название (пример: 1.2 / 1.3):<br/> <input class="form-control" type="text" name="name"/><br/>Содержание:<br/><br/>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea class="form-control" rows="8" name="text"></textarea><br/><input type="submit" class="btn btn-primary" name="add" value="Добавить"/></form></div>';
break;
case 'edit':
if ($b['level'] < 3) {
header('location: /');
die();
}
$array = DB::$dbs->query("SELECT * FROM rules_view WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
if (isset($_POST['fadd'])) {
if (empty($_POST['name']) OR empty($_POST['text'])) {
err('Введите название и содержание!');
} else {
DB::$dbs->query("UPDATE rules_view SET name = ?, text = ? WHERE id = ?", [check($_POST['name']),check($_POST['text']),$id]);
header('location: ?do=view&id='.$id);
}
}
echo '<div class="list-group-item media"><form action="?do=edit&id='.$id.'" method="POST">Название (прим.: 1 / 1.2 / 1.3):<br/> <input class="form-control" type="text" name="name" value="'.$arr['name'].'"/><br/>Содержание:<br/><br/>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea class="form-control" rows="8" name="text">'.$arr['text'].'</textarea><br/><input type="submit"class="btn btn-primary" name="fadd" value="Добавить"/></form></div>';
break;
}

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