<?php
session_start();
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();

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

echo '<div class="catSides"><i class="md-format-list-bulleted"></i> Форум  - Поиск</div>';
if(empty($_GET[zx]) or !empty($_POST['text'])){
unset($_SESSION['text']);
unset($_SESSION['sear']);
unset($_SESSION['order']);
}else{
	$_POST['text']= $_SESSION['text'];
}
if (isset($_GET['search'])) {
$text = str_replace('%','',$_POST['text']);
$text = str_replace("'",'',$_POST['text']);
$text = str_replace('"','',$_POST['text']);
$text = str_replace('-','',$_POST['text']);
$text = check($text);
$_SESSION['text'] = $text;
$text = (!empty($_SESSION['text']) ? $_SESSION['text'] : NULL);
if (isset($_POST['sear'])) $sear = intval($_POST['sear']);
if (isset($_SESSION['sear'])) $sear = intval($_SESSION['sear']);

if (isset($_POST['order'])) $order = intval($_POST['order']);
if (isset($_SESSION['order'])) $order = intval($_SESSION['order']);
$_SESSION['sear'] = $sear;
$_SESSION['order'] = $order;

$ord = ($order == 1 ? 'DESC':'ASC');
if ($sear == 1) {
$look = DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_topic WHERE name LIKE '%$text%'");
$num = 10;
$k_page = k_page($look, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM forum_topic WHERE name LIKE '%$text%' ORDER BY last ".$ord." LIMIT $start,$num", [$id]);
if ($sql -> rowCount() == 0) {
err('Тем ещё нет!');
} else {
while($a = $sql->fetch()) {
if ($a['type'] != 'closed') {
$imt = '<i class="md-forum"></i>';
} elseif ($a['type'] == 'closed') {
$imt = '<i class="md-forum"></i>';
} elseif ($a['fasten'] == 1) {
$imt = '<i class="md-forum"></i>';
}
$us_mess = DB::$dbs->queryFetch("SELECT user_id,tid FROM forum_mess WHERE tid = ? ORDER BY time DESC LIMIT $start,$num", [$a['id']]);
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_mess WHERE tid = ?", [$a['id']]);
if ($k_mess > 10) {
$pg = ceil($k_mess / 10);
$st = ' <a href="/forum/thema'.$a['id'].'?page='.$pg.'">>></a>';
} else {
$st = '';
}
echo '<div class="row text_box"><div class="pull-right text-muted top_right_box"></div> <a href="/forum/thema'.$a['id'].'">'.$imt.' '.$a['name'].' ('.$k_mess.') '.nick($us_mess['user_id']).' '.$st.'</a></div>';
}
}
} else {
$look = DB::$dbs -> querySingle("SELECT COUNT(id) FROM forum_mess WHERE text LIKE '%$text%'");
$num = 10;
$k_page = k_page($look, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM forum_mess WHERE text LIKE '%$text%' ORDER BY time ".$ord." LIMIT $start,$num", [$id]);
if ($sql -> rowCount() == 0) {
err('Пусто');
} else {
while($a = $sql->fetch()) {
$rid = DB::$dbs->queryFetch("SELECT name,id FROM forum_razdel WHERE id = ? LIMIT 1", [$a['rid']]);
$prid = DB::$dbs->queryFetch("SELECT name,id FROM forum_prazdel WHERE id = ? LIMIT 1", [$a['prid']]);
$topic = DB::$dbs->queryFetch("SELECT name,id FROM forum_topic WHERE id = ? LIMIT 1", [$a['tid']]);
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM forum_mess WHERE tid = ?", array($topic['id']));
if ($k_mess > 10) {
$pg = ceil($k_mess / 10);
$st = ' <a href="/forum/thema'.$a['tid'].'?page='.$pg.'">>></a>';
} else {
$st = '';
}
echo '<div class="row"><div class="col-xs-3 col-sm-1"><div class="user_brief"><a href="/forum/viewrazd.php?id='.$rid['id'].'"><i class="md-forum"></i> '.$rid['name'].'</a> > <a href="/forum/viewthem.php?id='.$prid['id'].'">'.$prid['name'].'</a> > <a href="/forum/thema'.$topic['id'].'"> '.$topic['name'].'</a> ['.$k_mess.']<br/>'.Nick($a['user_id']).' ('.vr($a['time']).')<br/> '.text($a['text']).'</a></div></div></div>';
}
}
}
if ($k_page > 1) {
str('?zx=1&search&amp;',$k_page,$page);
}
}
echo '<div class="row"><div class="col-xs-3 col-sm-1"><div class="user_brief"><form action="?zx=1&search" method="POST">Что ищем? <br/><input class="form-control " type="text" name="text" placeholder="Запрос"><br/>Искать:<br/><select class="form-control " name="sear"><option value="1">В темах</option><option value="2">В сообщениях</option></select><br/> Сортировать:<br/><select class="form-control " name="order"><option value="1">По возрастанию</option><option value="2">По убыванию</option></select><br/> <input type="submit" name="search" value="Вперед"/></form></div></div></div>';
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();
?>