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

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

//--------------------------------------------------=-=-=-=-=
//	Инициализируем категорию
//--------------------------------------------------=-=-=-=-=

if( $Board->DefinitionCat() )
	{
		if( $_REQUEST['type'] == "rss" ) include( ENGINE_DIR."/modules/board/rss.php" );
		
		$OnPage = $Board->Config['view_cat_on_page'] > 0 ? intval( $Board->Config['view_cat_on_page'] ) : 1;
		
		//--------------------------------------------------=-=-=-=-=
		//	Настройка метатитлов
		//--------------------------------------------------=-=-=-=-=
		
		$SpeedBarCat = $Board->SpeedBarCat( $Board->RequestCat );
		$ThreadCatForSql = $Board->SqlThreadCat( $Board->RequestCat );
		
		for( $i = count( $SpeedBarCat ); $i > 0; $i-- ) $module['title'][] = $SpeedBarCat[ ( $i - 1 ) ]['title'];
		for( $i = count( $SpeedBarCat ); $i > 0; $i-- ) $module['speedbar'][] = "<a href=\"".$SpeedBarCat[ ( $i - 1 ) ]['link']."\">".$SpeedBarCat[ ( $i - 1 ) ]['title']."</a>";
		
		//--------------------------------------------------=-=-=-=-=
		//	Смотрим, какую папку с шаблонами нам грузить
		//--------------------------------------------------=-=-=-=-=
		
		$RowCat = $Board->DB['category'][ $Board->RequestCat ];
		$Folder = $RowCat['template'] != "" ? totranslit( $RowCat['template'] ) : "default";
		$BoardCategory = $RowCat['id'];
		
		//--------------------------------------------------=-=-=-=-=
		//	Поиск при необходимости
		//--------------------------------------------------=-=-=-=-=
		
		if( $_REQUEST['BoardSearch'] == "---" ) $_REQUEST['BoardSearch'] = "";
		if( $_REQUEST['board_type'] == "---" ) $_REQUEST['board_type'] = "";
		if( ( $_REQUEST['BoardSearch'] != "" || $_REQUEST['board_type'] != "" ) && $Board->Config['fastsearch_on'] == "on" )
			{
				if( isset( $_REQUEST['submit_search'] ) ) $_REQUEST['page'] = 1;
				$SearchTextForm = $Board->FilterSearchText( $_REQUEST['BoardSearch'] );
				$Story = $db->safesql( $SearchTextForm );
				$BoardType = intval( $_REQUEST['board_type'] );
				$PageLink = $Board->ReturnLinkCategory( $Board->RequestCat );
				
				$SqlSearch = array();
				if( $Story ) $SqlSearch[] = "( `title` LIKE '%{$Story}%' OR `text` LIKE '%{$Story}%' )";
				if( $BoardType ) $SqlSearch[] = "`board_type`='{$BoardType}'";
				
				if( count( $SqlSearch ) > 0 )
					{
						$SqlSearch = "AND ".implode( " AND ", $SqlSearch );
						$NoNewsError = "По вашему запросу ничего не найдено. <a href=\"{$PageLink}\">Отменить поиск</a>";
						$SearchText = "По вашему запросу, найдено {posts} объявлений на {total} страницах. <a href=\"{$PageLink}\">Отменить поиск</a>";
						$PageLink = "{$PageLink}search/".( $SearchTextForm ? urlencode( htmlspecialchars( $SearchTextForm ) ) : "---" )."/".( $BoardType ? $BoardType : "---" )."/page/{page}";
						$SearchInfo = true;
					}
						else
					{
						$SqlSearch = "";	
					}
			}
		
		//--------------------------------------------------=-=-=-=-=
		//	Если нет поиска, то просто выводим объявления категории
		//--------------------------------------------------=-=-=-=-=
		
				else
			{
				$BoardCatMainTemplate = @file_get_contents( $tpl->dir."/board/{$Folder}/category/main.tpl" );
				$BoardCatListTemplate = @file_get_contents( $tpl->dir."/board/{$Folder}/category/list.tpl" );
				$cats = $Board->GenerateTemplateThreadCat( $BoardCatMainTemplate, $BoardCatListTemplate, $Board->RequestCat, $Board->Config['view_cat_max_columns'] );
				
				$PageLink = $Board->ReturnLinkCategory( $Board->RequestCat );
				$PageLink = "{$PageLink}page/{page}";
				$NoNewsError = "Объявленией не обнаружено.";
				$SearchInfo = false;
			}
			
		
		if( $Board->Config['rss_on'] == "yes" )
			{
				$rssLink = $Board->ReturnLinkCategory( $Board->RequestCat );
				$rssLink .= $Board->AltUrl === true ? "rss.xml" : "&type=rss";
			}
		
		//--------------------------------------------------=-=-=-=-=
		//	Генерация объявлений категории и её подкатегорий
		//--------------------------------------------------=-=-=-=-=
		
		$order = array();
		if( $Board->Config['echo_post_supervip'] != "no" ) $order[] = "`super_vip_date` DESC";
		if( $Board->Config['echo_post_vip'] != "no" ) $order[] = "`vip_date` DESC";
		$order[] = "`date` DESC";
		$order = implode( ", ", $order );
		
		$ThreadCatForSql = implode( ",", $ThreadCatForSql );
		$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"	=> "category/",
			"cashe_file"	=> "cashe_{$Board->RequestCat}.tmp",
			"cache"			=> $Board->Config['region_on'] == "on" ? false : true,
			"sql_count"		=> "SELECT COUNT(*) as count FROM ".PREFIX."_board_post WHERE category IN ({$ThreadCatForSql}) AND approve='1' {$SqlRegion} {$SqlSearch}",
			"sql_query"		=> "SELECT * FROM ".PREFIX."_board_post WHERE category IN ({$ThreadCatForSql}) AND approve='1' {$SqlRegion} {$SqlSearch} ORDER BY {$order} {limit}",
			"num"			=> $OnPage,
			"nav"			=> $PageLink,
			"search_info"	=> $SearchInfo,
			"search_text"	=> $SearchText,
			"no_news_error" => $NoNewsError,
			
		);
			
		require( ENGINE_DIR."/modules/board/list.post.php" );
		
		// Разрешаем вывод формы поиска
		$BoardLoadMainTpl = true;
		if( $Board->Config['fastsearch_on'] != "on" ) $BoardAllowSearch = false;
		
		// Ключевые слова и описание для поисковиков
		if( $RowCat['description'] ) $metatags['description'] = stripslashes( $RowCat['description'] );
		if( $RowCat['keywords'] ) $metatags['keywords'] = stripslashes( $RowCat['keywords'] );
	}

//--------------------------------------------------=-=-=-=-=
//	В случае ошибки, выводим информацию
//--------------------------------------------------=-=-=-=-=

		else
	{
		$tpl->Load_Template( "info.tpl" );
		$tpl->set( "{title}", "Ошибка" );
		$tpl->set( "{error}", "Категория объявлений не найдена" );
		$tpl->compile( "board" );
		$tpl->clear();
	}

?>