<?php
/*
=============================================================================
SEARCH FX - Хак по доп полям
=============================================================================
Автор хака: Gameer
-----------------------------------------------------
URL: http://gameer.name
-----------------------------------------------------
email: gameer@mail.ua
-----------------------------------------------------
skype: gameerblog
=============================================================================
Файл:  search_fx.php
=============================================================================
Версия хака : .1 Stable Release
=============================================================================
/*
* Что может:
* - То что описано в теме
*
*/
$val = trim(strip_tags(stripslashes($val)));
$cache = trim(strip_tags(stripslashes($cache)));
$xf_name = trim(strip_tags(stripslashes($xf)));
$fx_name = trim(strip_tags(stripslashes($fx)));
$allow_cache = ($config['version_id'] >= '10.2') ? $config['allow_cache'] == '1' : $config['allow_cache'] == "yes";
$is_change = false;
if ($cache == 'y' && !$allow_cache) // если кэш не включен включаем принудительно
{
    if(!$allow_cache)
    {
        if ($config['version_id'] >= '10.2')
            $config['allow_cache'] = '1';
        else
             $config['allow_cache'] = "yes";
        $is_change = true;
    }
}
if($cache == 'y')
{
    $search_xf = false;
    $search_xf = get_cache( "news_fx_search", $config['skin'] . $val . $xf_name . $fx_name);
    if($search_xf !== false)
    {
        echo $search_xf;
        return;
    }
}
$sql = $db->query("SELECT xfields FROM " . PREFIX . "_post WHERE SUBSTRING_INDEX( SUBSTRING_INDEX( xfields,  '{$xf_name}|', -1 ) ,  '||', 1 ) LIKE '%{$val}%'");
$count_rows = $sql->num_rows;
$tpl->load_template('search_xf.tpl');
if($count_rows > 0)
{
    $arr_two = array();
    while ($row = $db->get_row($sql))
    {
        $arr = xfieldsdataload($row['xfields']);
        $arr_two[] = $arr[$fx_name];
    }
 
    unset($arr);
    $arr_all = array();
    for($i = 0; $i < count($arr_two); $i++)
    {
        if(count(explode(',',$arr_two[$i])) >= 2)
        {
            $expl_all = explode(',',$arr_two[$i]);
            foreach($expl_all as $data)
               $arr_all[] = $data
        }
        else
            $arr_all[] = $arr_two[$i];
    }
    unset($arr_two);
    $count_arr = array_count_values($arr_all);
    unset($arr_all);
    $tpl->copy_template = str_replace("{val}", $val, $tpl->copy_template);
    $tpl->copy_template = preg_replace( "'\\[val\\](.*?)\\[/val\\]'si", "\\1", $tpl->copy_template);
    foreach($count_arr as $key => $value)
    {
        $tpl->copy_template = str_replace("{value}", trim(strip_tags(stripslashes($key))), $tpl->copy_template);
        $tpl->copy_template = str_replace("{count}", intval($value), $tpl->copy_template);
        $tpl->copy_template = preg_replace( "'\\[val\\](.*?)\\[/val\\]'si", "", $tpl->copy_template);
        $tpl->compile('search_xf');
    }
    $tpl->clear();
    $search_xf = $tpl->result['search_xf'];
}
else
    $search_xf = "Ничего не найдено.";
if($cache == 'y' && $is_change)
    create_cache( "news_fx_search", $search_xf, $config['skin'] . $val . $xf_name . $fx_name, true );
echo $search_xf;
function get_cache($name, $id)
{
    global $config, $is_logged, $member_id;
    if( $is_logged ) $end_file = $member_id['user_group'];
    else $end_file = "0";
    $id = md5( $id );
    $name = $name . "_" . $id . "_" . $end_file;
    $buffer = @file_get_contents( ENGINE_DIR . "/cache/" . $name . ".tmp" );
    if ( $buffer !== false AND $config['clear_cache'] )
    {
        $file_date = @filemtime( ENGINE_DIR . "/cache/" . $name . ".tmp" );
        $file_date = time()-$file_date;
        if ( $file_date > ( $config['clear_cache'] * 60 ) )
        {
            $buffer = false;
            @unlink( ENGINE_DIR . "/cache/" . $name . ".tmp" );
        }
        return $buffer;
    }
    else
        return $buffer;
}
?>