<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
///Подключаем шапку
$title->GetHeader();

$ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'].'/classes/level.class.ini');
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
$array = DB::$dbs->query("SELECT * FROM forum_topic WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
$raz = DB::$dbs->queryFetch("SELECT * FROM forum_razdel WHERE id = ? LIMIT 1", [$arr['rid']]);
$praz = DB::$dbs->queryFetch("SELECT * FROM forum_prazdel WHERE id = ? LIMIT 1", [$arr['prid']]);
$mess = DB::$dbs->querySingle("SELECT count(id) FROM forum_mess WHERE tid = ?", [$id]);
if ($mess > 10) {
$pg = ceil($mess / 10);
$st = '?page='.$pg;
} else {
$st = '';
}
$do = isset($_GET['do']) ? check($_GET['do']) : false;
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 typeof="v:Breadcrumb"><a href="/forum" rel="v:url" property="v:title">Форум</a></li>
<li typeof="v:Breadcrumb"><a href="viewrazd.php?id='.$raz['id'].'" rel="v:url" property="v:title">'.$raz['name'].'</a></li>
<li typeof="v:Breadcrumb"><a href="viewthem.php?id='.$praz['id'].'" rel="v:url" property="v:title">'.$praz['name'].'</a></li>
<li typeof="v:Breadcrumb"><a href="viewtopic.php?id='.$arr['id'].'" rel="v:url" property="v:title">'.$arr['name'].'</a></li>
<li class="active" typeof="v:Breadcrumb"><span property="v:title">Действие с сообщением</span></li></ol></div><div class="col-xs-12 col-sm-4 col-md-3 col-lg-3 text-right page_header_sidebar"></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">';

switch($do) {
default:
header('location: /');
break;
case 'rename':
$title->SetTitle(''.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Переименовать тему');
if ($b['level'] >= $ini['rename']) {
echo '<div class="catSides"> <a href="viewtopic.php?id='.$id.'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Переименовать тему</div>';
if (isset($_POST['add'])) {
$err = [];
$form = ['name' => isset($_POST['name']) ? check($_POST['name']) : null];
if (empty($form['name'])) {
$err[] = 'Введите новое название!';
}
if (empty($err)) {
DB::$dbs->query("UPDATE `forum_topic` SET `name` = ? WHERE `id` = ? LIMIT 1", [$form['name'],$id]);
header('location: /forum/thema'.$id.$st);
die();
} else {
echo '<div class="error">';
foreach($err as $error) {
echo '<b>'.$error.'</b><br/>';
}
echo '</div>';
}
}
echo '<form action="?do=rename&id='.$id.'" method="POST"><div class="row text_box"><div class="pull-right text-muted top_right_box"></div>Название:<br/><input type="text" name="name" value="'.$arr['name'].'"/><br/><br/><input type="submit" name="add" value="Изменить"/></div></form>';
} else {
header('location: /');
die();
}
break;
case 'closed':
if ($arr['type'] == 'closed') {
header('location: /');
die();
}
$title->SetTitle(''.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Закрыть тему');
echo '<div class="catSides"> <a href="viewtopic.php?id='.$id.'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Закрыть тему</div>';
if ($b['level'] >= $ini['closed'] OR $arr['user_id'] == $b['id'] AND $arr['type'] != 'closed') {
if (isset($_GET['ok'])) {
DB::$dbs->query("UPDATE forum_topic SET closed_who = ?, type = ? WHERE id = ? LIMIT 1", [$b['id'],'closed',$id]);
header('location: /forum/thema'.$id.$st);
die();
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div>Вы уверены, что хотите закрыть тему?<br/><a href="?do=closed&id='.$id.'&ok">Да</a> | <a href="viewtopic.php?id='.$id.$st.'">Нет</a></div>';
} else {
header('location: /');
die();
}
break;
case 'open':
if ($arr['type'] != 'closed') {
header('location: /');
die();
}
$title->SetTitle(''.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Открыть тему');

echo '<div class="catSides"> <a href="viewtopic.php?id='.$id.'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Открыть тему</div>';
if ($b['level'] >= $ini['open']) {
if (isset($_GET['ok'])) {
DB::$dbs->query("UPDATE forum_topic SET type = ? WHERE id = ? LIMIT 1", ['open',$id]);
header('location: /forum/thema'.$id.$st);
die();
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div>Вы уверены, что хотите открыть тему?<br/><a href="?do=open&id='.$id.'&ok">Да</a> | <a href="viewtopic.php?id='.$id.$st.'">Нет</a></div>';
} else {
header('location: /');
die();
}
break;
case 'delt':
$title->SetTitle(' '.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Удалить тему');
echo '<div class="catSides"> <a href="viewtopic.php?id='.$id.'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Удалить тему</div>';
if ($b['level'] >= $ini['delt']) {
if (isset($_GET['ok'])) {
DB::$dbs->query("DELETE FROM forum_topic WHERE id = ? LIMIT 1", [$id]);
header('location: /forum');
die();
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div>Вы уверены, что хотите удалить тему?<br/><a href="?do=delt&id='.$id.'&ok">Да</a> | <a href="viewtopic.php?id='.$id.$st.'">Нет</a></div>';
} else {
header('location: /');
die();
}
break;
case 'per':
$title->SetTitle(' '.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Перенести тему');
echo '<div class="catSides"> <a href="viewtopic.php?id='.$id.'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Перенести тему</div>';
if ($b['level'] >= $ini['per']) {
if (isset($_POST['ok'])) {
$nr = abs((int)$_POST['razd']);
if (empty($nr)) {
err('Вы не выбрали новый раздел..');
} elseif (DB::$dbs->query("SELECT count(id) FROM forum_prazdel WHERE id = ?", [$nr])==0) {
err('Данный подраздел не существует!');
} else {
$nrp = DB::$dbs->queryFetch("SELECT rid,name FROM forum_prazdel WHERE id = ? LIMIT 1", [$nr]);
$nrp2 = DB::$dbs->queryFetch("SELECT name FROM forum_prazdel WHERE id = ? LIMIT 1", [$arr['prid']]);
$nname = $arr['name'].' (перенесено)';
DB::$dbs->query("UPDATE forum_topic SET rid = ?, prid = ?, name = ?, last = ? WHERE id = ? LIMIT 1",array($nrp['rid'],$nr,$nname,time(),$id));
DB::$dbs->query("UPDATE forum_mess SET rid = ?, prid = ? WHERE tid = ? ",array($nrp['rid'],$nr,$id));
$tex = 'Тема перенесена в подраздел [b]'.$nrp['name'].'[/b]!';
DB::$dbs->query("INSERT INTO forum_mess SET rid = ?, prid = ?, user_id = ?, text = ?, tid = ?, time= ?",array($nrp['rid'],$nr,$b['id'],$tex,$id,time()));
header('location: /forum/thema'.$id.$st);
die();
}
}
echo '<form action="?do=per&id='.$id.'" method="post"><div class="row text_box"><div class="pull-right text-muted top_right_box"></div> <select class="form-control input-lg" size="1" name="razd">';
$razds = DB::$dbs->query("SELECT * from forum_prazdel order by id asc");
while($pr = $razds->fetch()){
$r = DB::$dbs->queryFetch("SELECT * from forum_razdel where id = ? limit 1",array($pr['rid']));
echo'<option value="'.$pr['id'].'">'.$pr['name'].' ('.$r['name'].')</option><br />';
}
echo'</select><br/><input type="submit" class="btn btn-primary" name="ok" value="Перенести"/></div></form>';
} else {
header('location: /');
die();
}
break;
case 'fasten':
$title->SetTitle($arr['name'].' - Закрепить тему');
if ($b['level'] < 2) {
header('location: /');
die();
}

echo '<div class="catSides"><a href="viewtopic.php?id='.$arr['id'].'"><i class="md-format-list-bulleted"></i>  '.$arr['name'].'</a> - Закрепить тему</div>';

$mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_mess WHERE tid = ?", array($arr['id']));
if ($mess > 10) {
$pg = ceil($mess / 10);
$st = '?page='.$pg;
} else {
$st = '';
}
if (isset($_GET['ok'])) {
if ($arr['fasten'] == 1) {
header('location: /');
die();
}
DB::$dbs->query("UPDATE forum_topic SET fasten = ? WHERE id = ? LIMIT 1", [1,$id]);
header('location: /forum/thema'.$arr['id'].$st);
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div>Действительно закрепить тему?<br/><a href="?do=fasten&id='.$id.'&ok"> Да</a> | <a href="/forum/thema'.$arr['id'].'?'.$st.'">Нет</a></div>';
break;
case 'unfasten':
$title->SetTitle($arr['name'].' | Открепить тему');
if ($b['level'] < 2) {
header('location: /');
die();
}

echo '<div class="catSides"><a href="viewtopic.php?id='.$arr['id'].'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Открепить тему</div>';
$mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_mess WHERE tid = ?", [$arr['id']]);
if ($mess > 10) {
$pg = ceil($mess / 10);
$st = '?page='.$pg;
} else {
$st = '';
}
if (isset($_GET['ok'])) {
if ($arr['fasten'] == 0) {
header('location: /');
die();
}
DB::$dbs->query("UPDATE forum_topic SET fasten = ? WHERE id = ? LIMIT 1", [0,$id]);
header('location: /forum/thema'.$arr['id'].$st);
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div>Действительно открепить тему?<br/><a href="?do=unfasten&id='.$id.'&ok"> Да</a> | <a href="/forum/thema'.$arr['id'].$st.'">Нет</a></div>';
break;
case 'addvote':
$title->SetTitle(''.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Добавить опрос');
echo '<div class="catSides"> <a href="/forum/thema'.$arr['id'].'"><i class="md-format-list-bulleted"></i>  '.$arr['name'].'</a> - Добавить опрос</div>';
if ($arr['user_id'] != $b['id'] && $b['level'] < 3) {
header('Location: /');
die();
} elseif (!empty($arr['quest'])) {
err('Опрос уже создан!');
} else {
if (isset($_POST['create'])) {
$quest = check($_POST['quest']);
$v1 = check($_POST['var'][0]);
$v2 = check($_POST['var'][1]);
$v3 = check($_POST['var'][2]);
$v4 = check($_POST['var'][3]);
if (empty($quest)) {
err('Вопрос не может быть пустым!');
} elseif (empty($v1) && empty($v2)) {
err('Должно быть хотя бы 2 первых варианта!');
} elseif (count(array_unique($_POST['var'])) < 4 && !empty($v3) && !empty($v4)) {
err('Варианты не должны совпадать!');
} elseif (count(array_unique($_POST['var'])) <= 2 && empty($v3) && empty($v4)) {
err('Варианты не должны совпадать!');
} else {
DB::$dbs->query("UPDATE `forum_topic` SET
`quest` = ?,
`var1` = ?,
`var2` = ?,
`var3` = ?, `var4` = ?
WHERE `id` = ? LIMIT 1", array($quest,$v1,$v2,$v3,$v4,$id));
header('Location: /forum/thema'.$id.$st);
}
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div><form method="post" action="?do=addvote&id='.$id.'">Вопрос:<br/><textarea name="quest" cols="50" rows="5" style="width: 99%;"></textarea><br/>Вариант #1:<br/><input class="form-control input-lg" type="text" name="var[]"/><br/>Вариант #2:<br/><input class="form-control input-lg" type="text" name="var[]"/><br/>Вариант #3:(необязательно)<br/><input class="form-control input-lg" type="text" name="var[]"/><br/>Вариант #4:(необязательно)<br/><input class="form-control input-lg" type="text" name="var[]"/><br/><input type="submit" name="create" value="Добавить"/></form></div>';
}
break;
case 'edit_vote':
$title->SetTitle(''.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Изменить опрос');
echo '<div class="catSides"> <a href="/forum/thema'.$arr['id'].'"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</a> - Изменить опрос</div>';
if ($arr['user_id'] != $b['id'] && $b['level'] < 3) {
header('Location: /');
die();
}
if (isset($_POST['create'])) {
$quest = check($_POST['quest']);
$v1 = check($_POST['var1']);
$v2 = check($_POST['var2']);
$v3 = check($_POST['var3']);
$v4 = check($_POST['var4']);
if (empty($quest)) {
err('Вопрос не может быть пустым!');
} elseif (empty($v1) && empty($v2)) {
err('Должно быть хотя бы 2 первых варианта!');
} else {
DB::$dbs->query("UPDATE `forum_topic` SET
`quest` = ?,
`var1` = ?,
`var2` = ?,
`var3` = ?, `var4` = ?
WHERE `id` = ? LIMIT 1", array($quest,$v1,$v2,$v3,$v4,$id));
header('Location: /forum/thema'.$id.$st);
}
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div><form method="post" action="?do=edit_vote&id='.$id.'">Вопрос:<br/><textarea name="quest" cols="50" rows="5" style="width: 99%;">'.$arr['quest'].'</textarea><br/>Вариант #1:<br/><input class="form-control input-lg" type="text" name="var1" value="'.$arr['var1'].'"/><br/>Вариант #2:<br/><input class="form-control input-lg" type="text" name="var2" value="'.$arr['var2'].'"/><br/>Вариант #3:(необязательно)<br/><input class="form-control input-lg" type="text" name="var3" value="'.$arr['var3'].'"/><br/>Вариант #4:(необязательно)<br/><input class="form-control input-lg" type="text" name="var4" value="'.$arr['var4'].'"/><br/><input class="form-control input-lg" type="submit" name="create" value="Изменить"/></form></div>';
break;
}
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();
?>