<?php

/* DCMS Special
 * Дата последнего редактирования 09.10.2016
 * Модифицировал densnet
 * Автор yadalay
 */

$set['title'] = lang('Поиск по форуму');
require_once H . 'sys/inc/thead.php';

aut();

#Навигация
echo "<div class='list-group-item-null list-group-item-grey'><small>";
echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
echo "<a href='" . DIR_FORUM . "'>" . lang('Форум') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Поиск');
echo "</small></div><br />";

if (isset($_POST['search']) || isset($_SESSION['search'])) {
    if (!isset($_POST['search']) && isset($_SESSION['search'])) {
        $search = $_SESSION['search'];
    } else {
        $search = $db->safesql(trim($_POST['text']));
        $_SESSION['search'] = mb_strtolower($search);
    }
    if (mb_strlen($search) < 3) {
        echo "<div class='alert alert-danger'>" . lang('Слишком короткий текст') . ".</div>";
    } else {
        $q = $db->super_query("SELECT COUNT(*) as count FROM forum_themes WHERE name LIKE '%" . $_SESSION['search'] . "%' OR description LIKE '%" . $_SESSION['search'] . "%'");
        $k_post = $q['count'];
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

        if ($k_post == 0) {
            echo "<div class='list-group-item-null list-group-item-warning'>" . lang('По Вашему запросу ничего не найдено') . ".</div>";
        } else {
            echo "<div class='list-group-item-null list-group-item-grey'>" . lang('Вы ввели') . ": <b>" . toOutput($_SESSION['search']) . "</b></div>";

            $themes = $db->query('SELECT * FROM `forum_themes` WHERE `name` LIKE "%' . $_SESSION['search'] . '%" OR `description` LIKE "%' . $_SESSION['search'] . '%" ORDER BY `time_post` ASC LIMIT ' . $start . ', ' . $set['p_str']);
            while ($theme = mysqli_fetch_object($themes)) {
                $theme->name = str_replace($_SESSION['search'], $_SESSION['search'], mb_strtolower($theme->name));
                $theme->description = str_replace($_SESSION['search'], $_SESSION['search'], mb_strtolower($theme->description));
                $razdel = mysqli_fetch_object($db->query('SELECT `id`, `id_forum` FROM `forum_razdels` WHERE `id` = ' . $theme->id_razdel));
                $forum = mysqli_fetch_object($db->query('SELECT `id`, `access` FROM `forum` WHERE `id` = ' . $razdel->id_forum));

                if ($forum->access == 0 || (isset($user) && $forum->access == 1 && $user['group_access'] > 7) || (isset($user) && $forum->access == 2 && $user['group_access'] > 1)) {
                    $q = $db->super_query("SELECT COUNT(*) as count FROM `forum_posts` WHERE `id_theme` = '$theme->id'");
                    $count_posts = $q['count'];

                    if ($theme->reason_close != NULL) {
                        $type = "<i class='fa fa-file-text fa-fw'></i> " . toOutput($theme->name, 1, 1, 0, 0, 0) . " <i class='fa fa-lock fa-fw'></i>";
                    } elseif ($theme->type == 1) {
                        $type = "<i class='fa fa-file-text fa-fw'></i> " . toOutput($theme->name, 1, 1, 0, 0, 0) . " <i class='fa fa-thumb-tack fa-fw'></i>";
                    } else {
                        $type = "<i class='fa fa-file-text fa-fw'></i> " . toOutput($theme->name, 1, 1, 0, 0, 0) . "";
                    }

                    echo "<div class='list-group-item-null list-group-item-grey dop'>";
                    echo "<span style='float:right;'>";
                    echo "<i class='fa fa-comments fa-fw'></i> $count_posts";
                    echo "</span>";
                    echo "<a href='" . DIR_FORUM . "$forum->id/$razdel->id/$theme->id.html'>$type</a>";
                    echo "  &nbsp; <span style='color: #9197a3;' title='" . date::time($theme->time) . "'>" . date::timek($theme->time) . "</span><br />";
                    echo user($theme->id_user) . " &nbsp; ";


                    $continue = (mb_strlen($theme->description) > 150) ? '...' : NULL;
                    echo "<br /><span style = 'color: #9197a3;'>";
                    echo toOutput($theme->description);
                    echo "</span>";

                    echo "</div>";
                } else {
                    continue;
                }
                echo "</td></tr></table>";
            }

            if ($k_page > 1) {
                str(DIR_FORUM . 'search/', $k_page, $page);
            }
        }
    }
}

echo "<form action='" . DIR_FORUM . "search.html' method='post' class='list-group-item-null'>";
echo lang('Введите текст') . "<br />";
echo "<input name='text' type='text' value='" . ((isset($_SESSION['search'])) ? toOutput($_SESSION['search']) : NULL) . "' class='form-control'><br />";

$doc->Button('btn btn-primary btn-sm', 'search', 'search', 'Поиск');

echo "</form>";

