<?php

/* DCMS Special
 * Дата последнего редактирования 10.10.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(); // форма авторизации
#Навигация
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/foto/'>" . 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);

if (isset($_GET['go']) && $search != NULL) {
    $q_search = str_replace('%', '', $search);
    $q_search = str_replace(' ', '%', $q_search);
    
    $k_post = $db->super_query("SELECT COUNT(*) as count FROM `gallery_foto` where `name` like '%" . $db->safesql($q_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'];
    $q = $db->query("SELECT * FROM `gallery_foto` WHERE `name` like '%" . $db->safesql($q_search) . "%' LIMIT $start, $set[p_str]");

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

    if ($k_post > 0) {
        echo "<div class='list-group-item-null list-group-item-info'>\n";
        echo lang('Найдено результатов') . ": $k_post \n";
        echo "</div>\n";

        while ($post = $db->get_row($q)) {

            $f = $post['id'];
            $foto = $db->get_row($db->query("SELECT * FROM `gallery_foto` WHERE `id` = '" . $f . "'  LIMIT 1"));
            $komm = $db->super_query("SELECT COUNT(*) as count FROM `gallery_komm` WHERE `id_foto` = '$post[id]'");
            $komm = $komm['count'];
            $gallery = $db->get_row($db->query("SELECT * FROM `gallery` WHERE `id`='" . $foto['id_gallery'] . "'  LIMIT 1"));
            $ank_p = get_user($gallery['id_user']);

            echo '<div class="list-group-item-null">';

            echo toOutput($post['name']);
            echo " <br />\n";
            echo "<div class='photos'>";
            echo "<div class='photos_blok'><center><i class='fa fa-star fa-fw'></i> <b>" . $post['rating'] . "</b> <i class='fa fa-comments fa-fw'></i> <b>$komm</b></center></div>";
            echo "<a href='/modules/foto/$ank_p[id]/$gallery[id]/$post[id]/'>";
            echo "<img style='width:100px;height:100px;' src='/modules/foto/foto150/$foto[id].$foto[ras]' alt='Фото_$foto[id]'/>";
            echo "</a></div>";

            echo "  </div>\n";
        }
    }
    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 class='list-group-item-null' method=\"post\" action=\"?go\">";
$search = stripcslashes(htmlspecialchars($search));

$doc->Input('search', 'Введите запрос', 64, "$search");
echo "<br />\n";
$doc->Button('btn btn-success btn-sm', null, 'search', 'Поиск');
echo '</form>';

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