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

echo '<div class="catSides"><i class="md-format-list-bulleted"></i> Правила сайта - Категории </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="row"><div class="col-xs-3 col-sm-1"><div class="user_brief"><a href="?do=view&id='.$a['id'].'"><img src="'.$img.'" alt="" />  '.$a['name'].' </a> '.$D.'</div></div></div>';
}
}
if ($b['level'] > 3) {
echo '<div class="catSides"> <i class="md-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="row text_box"><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 '<ul class="nav nav-pills nav-stacked"><li><a href="?do=add&id='.$id.'"><i class="md-forum"></i> Добавить правило</a></ul>';
}
$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="row text_box"><div class="pull-right text-muted top_right_box"></div><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="row_hard">Удалено!</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="row text_box"><div class="pull-right text-muted top_right_box"></div><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="row text_box"><div class="pull-right text-muted top_right_box"></div><form action="?do=add&id='.$id.'" method="POST">Название (пример: 1.2 / 1.3):<br/> <input class="form-control" type="text" name="name"/><br/>Содержание:<br/><textarea class="form-control" rows="3" 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['add'])) {
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="row text_box"><div class="pull-right text-muted top_right_box"></div><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/><textarea class="form-control" rows="3" name="text">'.$arr['text'].'</textarea><br/><input type="submit"class="btn btn-primary" name="add" value="Добавить"/></form></div>';
break;
}

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


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