<?php

$k_post = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_themes` WHERE `name` LIKE "%' . $_SESSION['search_'] . '%" OR `description` LIKE "%' . $_SESSION['search_'] . '%"'), 0);
$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 = mysql_real_escape_string(trim($_POST['search']));
    $_SESSION['search_'] = mb_strtolower($search);
}

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

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

while ($theme = mysql_fetch_object($themes)) {
    $razdel = mysql_fetch_object(mysql_query('SELECT `id`, `id_forum` FROM `forum_razdels` WHERE `id` = ' . $theme->id_razdel));
    $forum = mysql_fetch_object(mysql_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 = mysql_result(mysql_query('SELECT COUNT(*) FROM `forum_posts` WHERE `id_theme` = ' . $theme->id), 0);
        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);
}
