<?php

$k_post = $db->super_query("SELECT COUNT(*) as count FROM `forum_themes` WHERE `name` LIKE '%" . $_SESSION['search_'] . "%' OR `description` LIKE '%" . $_SESSION['search_'] . "%'");
$k_post = $k_post['count'];
$k_page = k_page($k_post, $set['p_str']);
$page = page($k_page);
$start = $set['p_str'] * $page - $set['p_str'];

if (!isset($_POST['search']) && isset($_SESSION['search_'])) {
    $search = $_SESSION['search_'];
} else {
    $search = $db->safesql(trim($_POST['search']));
    $_SESSION['search_'] = mb_strtolower($search);
}

if ($k_post == 0) {
    $doc->NoResult();
}

$themes = $db->query("SELECT * FROM `forum_themes` WHERE `name` like '%" . $search_text . "%' OR `description` like '%" . $search_text . "%' LIMIT $start, $set[p_str]");

while ($theme = mysqli_fetch_object($themes)) {
    $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)) {
        $count_posts = $db->super_query("SELECT COUNT(*) as count FROM `forum_posts` WHERE `id_theme` = '$theme->id'");
        $count_posts = $count_posts['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'>";
        echo "<span style='float:right;'>";
        echo "<i class='fa fa-comment-o fa-fw'></i> $count_posts";
        echo "</span>";
        echo "<a href='/modules/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; ";

        echo "<span style = 'color: #9197a3;'>";
        echo toOutput(cutStr($theme->description, 150));
        echo "</span>";

        echo "</div>";
        echo "<div class='hr'></div>";
    } else {
        continue;
    }
}

if ($k_page > 1) {
    str("?search=forum&amp;", $k_page, $page);
}
