<?php

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

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

$set['title'] = lang('Блоги - Поиск');

require_once H . 'sys/inc/thead.php';

aut();
err();

#Навигация
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='/modules/diary/'>" . lang('Блоги') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
echo lang('Поиск');
echo "</small></div><br />";

$search = NULL;
if (isset($_SESSION['search'])) {
    $search = $_SESSION['search'];
}
if (isset($_POST['search'])) {
    $search = $_POST['search'];
}
$_SESSION['search'] = $search;

$search = preg_replace("#( ){2,}#", " ", $search);
$search = preg_replace("#^( ){1,}|( ){1,}$#", "", $search);

$where = NULL;
if (isset($_SESSION['where'])) {
    $where = $_SESSION['where'];
}
if (isset($_POST['where']) && ($_POST['where'] == 1 || $_POST['where'] == 2)) {
    $where = intval($_POST['where']);
}
$_SESSION['where'] = $where;

if (isset($_GET['go']) && $search != NULL && $where != NULL) {
    $q_search = str_replace('%', '', $search);
    $q_search = str_replace(' ', '%', $q_search);
    if ($where == 1) {
        $where1 = 'name';
    } else {
        $where1 = 'msg';
    }

    $q = $db->super_query("SELECT COUNT(*) as count FROM `diary` WHERE `$where1` like '%" . $db->safesql($q_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) {
        $doc->NoResult();
    }
    $q = $db->query("SELECT * FROM `diary` WHERE `$where1` like '%" . $db->safesql($q_search) . "%' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

    while ($post = $db->get_row($q)) {
        $us = get_user($post['id_user']);
        $post['tags'] = str_replace("$tag", "<span style='color:red'>$tag</span>", $post['tags']);

        echo '<div class="list-group-item-null">';
        echo "<a href='/modules/diary/$post[name]/'><i class='fa fa-book fa-fw'></i> $post[name]</a> <small style='color: grey;'>" . date::time($post['time']) . "</small><br />";

        echo '<i class="fa fa-eye fa-fw"></i> Просмотров: ' . $post['viewings'] . ' | ';
        echo '<i class="fa fa-star fa-fw"></i> Рейтинг: ' . $post['rating'] . '<br/>';
        echo '<i class="fa fa-tags fa-fw"></i> Метки: ' . $post['tags'] . '';

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

    if ($k_page > 1) {
        str("?go&", $k_page, $page);
    }
} else {
    echo '<div class="list-group-item-null list-group-item-grey">';
    echo lang('Введите текст для поиска') . '<br/>';
    echo '</div>';
}

echo'<form method="post" action="?go" class="list-group-item-null">';
$search = stripcslashes(htmlspecialchars($search));

$doc->Input('search', 'Что ищем?', 64, "$search");

echo lang('Искать') . '<br />';
echo '<select name="where" class="form-control">';
echo '<option value="2"' . ($where == 2 ? ' selected="selected"' : null) . '>В тексте</option>';
echo '<option value="1"' . ($where == 1 ? ' selected="selected"' : null) . '>В названии</option>';
echo '</select><br/>';

$doc->Button('btn btn-success btn-sm', null, 'search', 'Поиск');
echo '</form>';

require_once H . 'sys/inc/tfoot.php';
