<?php

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

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

$doc->Title('Поиск видео');

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

aut();

$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);

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

if (isset($_GET['go']) && $search != NULL) {
    $search_a = explode(' ', $search);

    for ($i = 0; $i < count($search_a); $i++) {
        $search_a2[$i] = '<span class="search_c">' . stripcslashes(htmlspecialchars($search_a[$i])) . '</span>';
        $search_a[$i] = stripcslashes(htmlspecialchars($search_a[$i]));
    }

    $q_search = str_replace('%', '', $search);
    $q_search = str_replace(' ', '%', $q_search);

    $k_post = $db->super_query("SELECT COUNT(*) as count FROM `videos` 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'];

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

    $q = $db->query("SELECT * FROM `videos` WHERE `name` like '%" . $db->safesql($q_search) . "%'  ORDER BY `name` ASC LIMIT $start, $set[p_str] ");
    $i = 0;

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

        $k = $db->super_query("SELECT COUNT(*) as count FROM `videos_like` WHERE `id_videos` = '" . $res['id'] . "'  LIMIT 1");
        $like = $k['count'];
        $k = $db->super_query("SELECT COUNT(*) as count FROM `videos_views` WHERE `id_videos` = '" . $res['id'] . "'  LIMIT 1");
        $pokz = $k['count'];
        $k = $db->super_query("SELECT COUNT(*) as count FROM `videos_komm` WHERE `id_videos` = '" . $res['id'] . "'  LIMIT 1");
        $comm = $k['count'];

        echo "<table class='list-group-item-null'><tr><td class='icon14'>";
        echo '<img src="http://i.ytimg.com/vi/' . $res['kod'] . '/1.jpg" width="70" alt="screen" />';
        echo "</td><td class='null'>";
        echo '<a href="video.php?id=' . $res['id'] . '"> ' . $res['name'] . '</a><br/>';

        echo "<i class='fa fa-heart fa-fw'></i> $like ";
        echo "<i class='fa fa-eye fa-fw'></i> $pokz ";
        echo "<i class='fa fa-comments fa-fw'></i> $comm ";

        echo "</td></tr></table>";
        echo "<div class='hr'></div>";
    }
    if ($k_page > 1) {
        str("search.php?go&amp;", $k_page, $page);
    } // Вывод страниц
} else {
    echo "<div class='list-group-item-null list-group-item-grey'>";
    echo lang('Поиск') . ":";
    echo "</div>";
}

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

echo "<input type='text' required class='form-control' name='search' placeholder='" . lang('Ваш запрос') . "' value='$search' />";
echo "<br />";
$doc->Button('btn btn-success btn-sm', null, 'search', 'Искать');

echo "</form>\n";

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