<?php
/**
 * @package     JohnCMS
 * @link        http://johncms.com
 * @copyright   Copyright (C) 2008-2015 JohnCMS Community
 * @license     LICENSE.txt (see attached file)
 * @version     VERSION.txt (see attached file)
 * @author      http://johncms.com/about
 */

/*
-----------------------------------------------------------------
Функция подсветки результатов запроса
-----------------------------------------------------------------
*/
function ReplaceKeywords($search, $text)
{
    $search = str_replace('*', '', $search);

    return mb_strlen($search) < 3 ? $text : preg_replace('|(' . preg_quote($search, '/') . ')|siu', '<span style="background-color: #FFFF33">$1</span>', $text);
}

/*
-----------------------------------------------------------------
Принимаем данные, выводим форму поиска
-----------------------------------------------------------------
*/
$search_post = isset($_POST['search']) ? trim($_POST['search']) : false;
$search_get = isset($_GET['search']) ? rawurldecode(trim($_GET['search'])) : false;
$search = $search_post ? $search_post : $search_get;
$search_t = isset($_REQUEST['t']);
echo '<div class="menu"><a href="/"><img src="/ico/home.png"></a> » <a href="/library/qidir.php">Kutubxonadan qidirish</a></div>'; 
echo '<div class="phdr"><center>Kutubxonadan qidirish </center></div>'
. '<div class="menu"><form action="?act=search" method="post"><div>'
    . '<input type="text" value="' . ($search ? functions::checkout($search) : '') . '" name="search" />'
    . '<input type="submit" value="' . $lng['search'] . '" name="submit" /><br />'
    . '</div></form></div>';

/*
-----------------------------------------------------------------
Проверям на ошибки
-----------------------------------------------------------------
*/
$error = false;
if ($search && (mb_strlen($search) < 2 || mb_strlen($search) > 64))
    $error = $lng['error_search_length'];

if ($search && !$error) {
    /*
    -----------------------------------------------------------------
    Выводим результаты запроса
    -----------------------------------------------------------------
    */
    $array = explode(' ', $search);
    $count = count($array);
    $query = mysql_real_escape_string($search);
    $total = mysql_result(mysql_query("
        SELECT COUNT(*) FROM `library_texts`
        WHERE MATCH (`" . ($search_t ? 'name' : 'text') . "`) AGAINST ('" . $query . "' IN BOOLEAN MODE)"), 0);
        
    
    if ($total > $kmess)
echo '<div class="menu"><center>' . functions::display_pagination('?act=search&amp;' . ($search_t ? 't=1&amp;' : '') . 'search=' . urlencode($search) . '&amp;', $start, $total, $kmess) . '</center></div>';
    if ($total) {
        $req = mysql_query("
            SELECT *, MATCH (`" . ($search_t ? 'name' : 'text') . "`) AGAINST ('" . $query . "' IN BOOLEAN MODE) AS `rel`
            FROM `library_texts`
            WHERE MATCH (`" . ($search_t ? 'name' : 'text') . "`) AGAINST ('" . $query . "' IN BOOLEAN MODE)
            ORDER BY `rel` DESC
            LIMIT " . $start . ", " . $kmess
        );
        $i = 0;
        while (($res = mysql_fetch_assoc($req)) !== false) {
            echo $i % 2 ? '<div class="list2">' : '<div class="list1">';
            foreach ($array as $srch) {
                if (($pos = mb_strpos(strtolower($res['text']), strtolower(str_replace('*', '', $srch)))) !== false) {
                    break;
                }
            }
            if (!isset($pos) || $pos < 100) {
                $pos = 100;
            }
            $name = functions::checkout($res['name']);
            $text = functions::checkout(mb_substr($res['text'], ($pos - 100), 400), 1);
            if ($search_t) {
                foreach ($array as $val) {
                    $name = ReplaceKeywords($val, $name);
                }
            } else {
                foreach ($array as $val) {
                    $text = ReplaceKeywords($val, $text);
                }
            }
            echo '<strong><a href="index.php?id=' . $res['id'] . '">' . $name . '</a></strong><br />' . $text
                . ' <div class="sub"><span class="gray">' . $lng_lib['added'] . ':</span> ' . functions::checkout($res['author'])
                . ' <span class="gray">(' . functions::display_date($res['time']) . ')</span><br />'
                . '<span class="gray">' . $lng_lib['reads'] . ':</span> ' . $res['count_views']
                . '</div></div>';
            ++$i;
        }
    } else {
        echo '<div class="rmenu"><p>' . $lng['search_results_empty'] . '</p></div>';
    }
    if ($total > $kmess) {
echo '<div class="menu"><center>' . functions::display_pagination('?act=search&amp;' . ($search_t ? 't=1&amp;' : '') . 'search=' . urlencode($search) . '&amp;', $start, $total, $kmess) . '</center></div>';
    }
} else {
    if ($error) {
        echo functions::display_error($error);
    }
echo '<div class="gmenu"><small>Salom bu bo\'lim orqali Yoqimli.Uz saytining kutubxonasidan malumotlar qidirib topishingiz mumkin. Buning uchun o\'zingizga kerak malumotni yuqorida yozib qidirishni bosing. Siz izlagan malumot kutubxonamizda bo\'lsa albatta sizga ko\'rsatiladi.</small></div>';
}
