<?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="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/forum"style="text-decoration:none; color:white;"> Форум</a> :: Действие с сообщением</div></div></div>';
echo '<div class="list-group-item media"><a href="viewrazd.php?id='.$raz['id'].'"><i class="zmdi zmdi-comments"></i> '.$raz['name'].'</a> :: <a href="viewthem.php?id='.$praz['id'].'">'.$praz['name'].'</a> :: <a href="viewtopic.php?id='.$arr['id'].'">'.$arr['name'].'</a> :: Действие с сообщением</div>';
switch($do) {
default:
header('location: /');
break;
case 'rename':
$title->SetTitle(''.$raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Переименовать тему');
if ($b['level'] >= $ini['rename']) {
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Переименовать тему </div></div></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 '<div class="list-group-item media"><form action="?do=rename&id='.$id.'" method="POST">Название:<br/><input type="text" class="form-control"  name="name" value="'.$arr['name'].'"/><br/><input type="submit" class="btn btn-primary" 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="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Закрыть тему </div></div></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="list-group-item media">Вы уверены, что хотите закрыть тему?<br/><a href="?do=closed&id='.$id.'&ok" class="btn btn-primary">Да</a> :: <a href="viewtopic.php?id='.$id.$st.'" class="btn btn-primary">Нет</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="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Открыть тему </div></div></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="list-group-item media">Вы уверены, что хотите открыть тему?<br/><a href="?do=open&id='.$id.'&ok"class="btn btn-primary">Да</a> :: <a href="viewtopic.php?id='.$id.$st.'"class="btn btn-primary">Нет</a></div>';
} else {
header('location: /');
die();
}
break;
case 'delt':
$title->SetTitle($raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Удалить тему');
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Удалить тему </div></div></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="list-group-item media">Вы уверены, что хотите удалить тему?<br/><a href="?do=delt&id='.$id.'&ok"class="btn btn-primary">Да</a> :: <a href="viewtopic.php?id='.$id.$st.'"class="btn btn-primary">Нет</a></div>';
} else {
header('location: /');
die();
}
break;
case 'per':
$title->SetTitle($raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Перенести тему');
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Перенести </div></div></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",[$nrp['rid'],$nr,$nname,time(),$id]);
DB::$dbs->query("UPDATE forum_mess SET rid = ?, prid = ? WHERE tid = ? ",[$nrp['rid'],$nr,$id]);
$tex = 'Тема перенесена в подраздел [b]'.$nrp['name'].'[/b]!';
DB::$dbs->query("INSERT INTO forum_mess SET rid = ?, prid = ?, user_id = ?, text = ?, tid = ?, time= ?",[$nrp['rid'],$nr,$b['id'],$tex,$id,time()]);
header('location: /forum/thema'.$id.$st);
die();
}
}
echo '<div class="list-group-item media"><form action="?do=per&id='.$id.'" method="post"><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",[$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="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Закрепить тему</div></div></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'] == 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="list-group-item media">Действительно закрепить тему?<br/><a href="?do=fasten&id='.$id.'&ok"class="btn btn-primary"> Да</a> :: <a href="/forum/thema'.$arr['id'].'?'.$st.'"class="btn btn-primary">Нет</a></div>';
break;
case 'unfasten':
$title->SetTitle($arr['name'].' | Открепить тему');
if ($b['level'] < 2) {
header('location: /');
die();
}
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Открепить тему </div></div></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="list-group-item media">Действительно открепить тему?<br/><a href="?do=unfasten&id='.$id.'&ok"class="btn btn-primary"> Да</a> :: <a href="/forum/thema'.$arr['id'].$st.'"class="btn btn-primary">Нет</a></div>';
break;
case 'addvote':
$title->SetTitle($raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Добавить опрос');
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Добавить опрос </div></div></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", [$quest,$v1,$v2,$v3,$v4,$id]);
header('Location: /forum/thema'.$id.$st);
}
}
echo '<div class="list-group-item media"><form method="post" action="?do=addvote&id='.$id.'">Вопрос:<br/><textarea name="quest" class="form-control" rows="8"></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" class="btn btn-primary" value="Добавить"/></form></div>';
}
break;
case 'edit_vote':
$title->SetTitle($raz['name'].' - '.$praz['name'].' - '.$arr['name'].' - Изменить опрос');
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="viewtopic.php?id='.$id.'"style="text-decoration:none; color:white;"> '.$arr['name'].'</a> :: Изменить опрос </div></div></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", [$quest,$v1,$v2,$v3,$v4,$id]);
header('Location: /forum/thema'.$id.$st);
}
}
echo '<div class="list-group-item media"><form method="post" action="?do=edit_vote&id='.$id.'">Вопрос:<br/><textarea name="quest" class="form-control" rows="8">'.$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="btn btn-primary" type="submit" name="create" value="Изменить"/></form></div>';
break;
}
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-format-list-bulleted"></i>  <a href="/rules"style="text-decoration:none; color:white;">Правила</a> | <a href="/bbcodes"style="text-decoration:none; color:white;"> Справка BBcode</a> | <a href="/smiles"style="text-decoration:none; color:white;">Все смайлы</a> | <a href="search"style="text-decoration:none; color:white;">Поиск</a></div></div></div>'; 
$title->GetMenu();
$title->GetFooter();
?>