<?php
/*
=====================================================
 Модуль создал: REZER (http://rezer.net)
=====================================================
 Файл: fullsearch.php
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Назначение: расширенный поиск объявлений
=====================================================
*/

if( !defined( "DATALIFEENGINE" ) ) die( "Hacking attempt!" );

$Folder = "main";

// Сколько выводить новостей на одну страницу
$OnPage = $Board->Config['view_cat_on_page'] > 0 ? intval( $Board->Config['view_cat_on_page'] ) : 1;
		
//	Настройка метатитлов
$module['title'][] = "Расширенный поиск";
$module['speedbar'][] = "Расширенный поиск";

//--------------------------------------------------=-=-=-=-=
//	Генерация запроса в БД
//--------------------------------------------------=-=-=-=-=

$SearchSQL = "";
if( isset( $_REQUEST['submit'] ) )
	{
		if( isset( $_POST['submit'] ) ) $_REQUEST['page'] = 1;
		$category = intval( $_REQUEST['category'] );
		$board_type = intval( $_REQUEST['board_type'] );
		$currency = intval( $_REQUEST['currency'] );
		$country = intval( $_REQUEST['country'] );
		$city = intval( $_REQUEST['city'] );
		$SearchPriceOt = intval( $_REQUEST['price_ot'] ) > 0 ? intval( $_REQUEST['price_ot'] ) : "";
		$SearchPriceDo = intval( $_REQUEST['price_do'] ) > 0 ? intval( $_REQUEST['price_do'] ) : "";
		$date = intval( $_REQUEST['date'] );
		$SearchOnPage = $_REQUEST['on_page'] > 0 ? intval( $_REQUEST['on_page'] ) : 10;
		$SearchTitle = htmlspecialchars( $Board->FilterSearchText( $_REQUEST['title'] ) );
		$SearchText = htmlspecialchars( $Board->FilterSearchText( $_REQUEST['text'] ) );
		$thread_cats = intval( $_REQUEST['thread_cats'] );
		$Board->LoadListCity( $country );
		
		$SearchSQL = array();
		if( $category )
			{
				if( $thread_cats )
					{
						$ThreadCatForSql = $Board->SqlThreadCat( $category );
						$SearchSQL[] = "`category` IN (".implode( ",", $ThreadCatForSql ).")";
					}
						else
					{
						$SearchSQL[] = "`category`='{$category}'";
					}
			}
		
		if( $SearchTitle ) $SearchSQL[] = "`title` LIKE '%".$db->safesql( $SearchTitle )."%'";
		if( $SearchText ) $SearchSQL[] = "`text` LIKE '%".$db->safesql( $SearchText )."%'";
		if( $board_type ) $SearchSQL[] = "`board_type`='{$board_type}'";
		if( $currency ) $SearchSQL[] = "`currency`='{$currency}'";
		if( $SearchPriceOt ) $SearchSQL[] = "`price`>='{$SearchPriceOt}'";
		if( $SearchPriceDo ) $SearchSQL[] = "`price`<='{$SearchPriceDo}'";
		if( $Board->Config['region_on'] == "on" )
			{
				if( $country ) $SearchSQL[] = "`country`='{$country}'";
				if( $city ) $SearchSQL[] = "`city`='{$city}'";
			}
			
		if( $date )
			{
				$added_time = time() + ( $config['date_adjust'] * 60 );
				$NowDate = date( "Y-m-d", $added_time );
				$NowDateTime = date( "Y-m-d H:i:s", $added_time );
				if( $date == 1 )
					{
						$SearchSQL[] = "`date`>='{$NowDate} 00:00:00'";
						$SearchSQL[] = "`date`<='{$NowDate} 23:59:59'";
					}
				elseif( $date == 2 )
					{
						$DateSql = ( time() + ( $config['date_adjust'] * 60 ) ) - 604800;
						$DateSql = date( "Y-m-d 00:00:00", $DateSql );
						$SearchSQL[] = "`date`>='{$DateSql}'";
						$SearchSQL[] = "`date`<='{$NowDate} 23:59:59'";
					}
				elseif( $date == 3 )
					{
						$DateSql = ( time() + ( $config['date_adjust'] * 60 ) ) - 2678400;
						$DateSql = date( "Y-m-d 00:00:00", $DateSql );
						$SearchSQL[] = "`date`>='{$DateSql}'";
						$SearchSQL[] = "`date`<='{$NowDate} 23:59:59'";	
					}
			}
			
		if( count( $SearchSQL ) > 0 )
			$SearchSQL = " AND ".implode( " AND ", $SearchSQL );
		else
			$SearchSQL = "";
			
		$PageLink = "/index.php?do=board&action=fullsearch&title=".urlencode( $SearchTitle )."&text=".urlencode( $SearchText )."&date={$date}&country={$country}&city={$city}&category={$category}&thread_cats={$thread_cats}&board_type={$board_type}&price_ot={$SearchPriceOt}&price_do={$SearchPriceDo}&currency={$currency}&on_page={$SearchOnPage}&submit=1&page={page}";
	}
		else
	{
		$Board->LoadListCity( $_SESSION['BoardCountry'] );
		$country = $_SESSION['BoardCountry'];
		$city = $_SESSION['BoardCity'];
		$SearchOnPage = 10;
		$thread_cats = 1;
	}
	
//--------------------------------------------------=-=-=-=-=
//	Генерация формы поиска
//--------------------------------------------------=-=-=-=-=

$thread_cats_checked = $thread_cats == 1 ? "checked=\"checked\"" : "";
$SelectDate = $Board->SelectList( array( 0 => "Без разницы", 1 => "За сегодня", 2 => "За неделю", 3 => "За месяц" ), $date );
$SelectBoardType = $Board->ReturnSelectBoardType( $board_type );
$SelectCurrency = $Board->ReturnSelectCurrency( $currency );
$SelectCategory = $Board->SelectCategory( $category, "", "", "", true );
$SelectCountry = $Board->ReturnSelectCountry( $country );
$SelectCity = $Board->ReturnSelectCity( $country, $city );

$tpl->Load_Template( "/board/main/full.search.tpl" );
$tpl->set( "{title}", $SearchTitle );
$tpl->set( "{text}", $SearchText );
$tpl->set( "{price_ot}", $SearchPriceOt );
$tpl->set( "{price_do}", $SearchPriceDo );
$tpl->set( "{on_page}", $SearchOnPage );
$tpl->set( "{date}", "<select name=\"date\">{$SelectDate}</select>" );
$tpl->set( "{category}", "<select name=\"category\">{$SelectCategory}</select>" );
$tpl->set( "{board_type}", "<select name=\"board_type\">{$SelectBoardType}</select>" );
$tpl->set( "{currency}", "<select name=\"currency\">{$SelectCurrency}</select>" );
$tpl->set( "{country}", "<select name=\"country\" id=\"SelectListCountryAdd\" onchange=\"SelectCountry( 'Add', 'Выберите' );\"><option value=\"\">- Выберите -</option>{$SelectCountry}</select>" );
$tpl->set( "{city}", "<span id=\"FormSelectCityAdd\"><select name=\"city\" id=\"SelectCityAdd\"><option value=\"\">- Выберите -</option>{$SelectCity}</select> <span id=\"FormSelectCityStatusAdd\"></span></span>" );
$tpl->set( "{thread_cats}", "<input type=\"checkbox\" name=\"thread_cats\" id=\"thread_cats\" {$thread_cats_checked} value=\"1\" />" );
$tpl->copy_template = "<form method=\"post\" action=\"\">{$tpl->copy_template}<input type=\"hidden\" name=\"do\" value=\"board\" /><input type=\"hidden\" name=\"action\" value=\"fullsearch\" /></form>";
$tpl->compile( "board_search" );
$tpl->clear();

//--------------------------------------------------=-=-=-=-=
//	Вывод найденных объявлений
//--------------------------------------------------=-=-=-=-=

if( $SearchSQL )
	{
		$NewsList = array(
		
			"template"		=> "board/{$Folder}/board.short.tpl",
			"template_nav"	=> "board/{$Folder}/navigation.tpl",
			"template_info"	=> "board/{$Folder}/info.tpl",
			"tpl_compile"	=> "board_content",
			"cashe_folder"	=> "",
			"cashe_file"	=> "",
			"cache"			=> false,
			"sql_count"		=> "SELECT COUNT(*) as count FROM ".PREFIX."_board_post WHERE approve='1' {$SearchSQL}",
			"sql_query"		=> "SELECT * FROM ".PREFIX."_board_post WHERE approve='1' {$SearchSQL} ORDER BY date DESC {limit}",
			"num"			=> $SearchOnPage,
			"nav"			=> $PageLink,
			"search_info"	=> true,
			"no_news_error" => "По вашему запросу ничего не найдено.",
			
		);
			
		require( ENGINE_DIR."/modules/board/list.post.php" );
	}

// Совмещаем форму поиска и список объявлений
$tpl->result['board_content'] = $tpl->result['board_search'].$tpl->result['board_content'];

// Разрешаем вывод формы поиска
$BoardLoadMainTpl = true;
$BoardAllowSearch = false;

?>