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

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

$AllowApprove = array( "on", "off" );

$STitle = htmlspecialchars( trim( stripslashes( CharsetConvert( $_REQUEST['title'] ) ) ) );
$SBoardType = intval( $_REQUEST['board_type'] );
$SCurrency = intval( $_REQUEST['currency'] );
$SPriceOt = intval( $_REQUEST['price_ot'] ) > 0 ? intval( $_REQUEST['price_ot'] ) : "";
$SPriceDo = intval( $_REQUEST['price_do'] ) > 0 ? intval( $_REQUEST['price_do'] ) : "";
$SDateOt = htmlspecialchars( trim( stripslashes( $_REQUEST['date_ot'] ) ) );
$SDateDo = htmlspecialchars( trim( stripslashes( $_REQUEST['date_do'] ) ) );
$SEndDateOt = htmlspecialchars( trim( stripslashes( $_REQUEST['end_date_ot'] ) ) );
$SEndDateDo = htmlspecialchars( trim( stripslashes( $_REQUEST['end_date_do'] ) ) );
$SOnPage = intval( $_REQUEST['on_page'] ) > 0 ? intval( $_REQUEST['on_page'] ) : 50;
$SCountry = intval( $_REQUEST['country'] );
$SCity = intval( $_REQUEST['city'] );
if( $SCountry )
	{
		$Board->LoadListCity( $SCountry );
		$SelectCity = $Board->ReturnSelectCity( $SCountry, $SCity );
	}
	
$SCategory = intval( $_REQUEST['category'] );
$SApprove = in_array( $_REQUEST['approve'], $AllowApprove ) ? $_REQUEST['approve'] : "";

$SelectBoardType = $Board->ReturnSelectBoardType( $SBoardType );
$SelectCurrency = $Board->ReturnSelectCurrency( $SCurrency );
$SelectCountry = $Board->ReturnSelectCountry( $SCountry );
$SelectCategory = $Board->SelectCategory( $SCategory );
$SelectApprove = SelectList( array( "on" => "Да", "off" => "Нет" ), $SApprove );

opentable();
tableheader( "Поиск объявлений" );
echo <<<HTML

<form method="post" action="" name="SearchBoards" onsubmit="formSubmit( 'SearchBoards', 'list.update.php', 'main', 'status.gif', 'status_search_board', 'off' ); return false;">
	<div style="padding: 5px;">
		<table width="100%" border="0">
			<tr height="23px">
				<td width="15%">
					Название:
				</td>
				<td width="35%">
					<input type="text" name="title" value="{$STitle}" class="edit" style="width: 60%;" />
				</td>
				<td width="15%">
					Тип объявления:
				</td>
				<td width="35%">
					<select name="board_type">
						<option value="">---</option>
						{$SelectBoardType}
					</select>
				</td>
			</tr>
			<tr height="23px">
				<td width="15%">
					Стоимость:
					<div style="float: right; padding: 0px 5px 0px 0px;">от</div>
				</td>
				<td width="35%">
					<input type="text" name="price_ot" value="{$SPriceOt}" class="edit" style="width: 90px; text-align: center;" />
					&nbsp;&nbsp;&nbsp;до&nbsp;&nbsp;&nbsp;
					<input type="text" name="price_do" value="{$SPriceDo}" class="edit" style="width: 90px; text-align: center;" />
				</td>
				<td width="15%">
					Валюта:
				</td>
				<td width="35%">
					<select name="currency">
						<option value="">---</option>
						{$SelectCurrency}
					</select>
				</td>
			</tr>
			<tr height="23px">
				<td width="15%">
					Дата создания:
					<div style="float: right; padding: 0px 5px 0px 0px;">от</div>
				</td>
				<td width="35%">
					<input type="text" class="edit" name="date_ot" value="{$SDateOt}" style="width: 90px; text-align: center;" id="date_ot" />
					<img src="engine/inc/board/style/images/calendar.gif" border="0" style="cursor: pointer;" id="date_ot_calendar" align="absmiddle" />
					&nbsp;до <input type="text" class="edit" name="date_do" value="{$SDateDo}" style="width: 90px; text-align: center;" id="date_do" />
					<img src="engine/inc/board/style/images/calendar.gif" border="0" style="cursor: pointer;" id="date_do_calendar" align="absmiddle" />
				</td>
				<td width="15%">
					Дата окончания:
					<div style="float: right; padding: 0px 5px 0px 0px;">от</div>
				</td>
				<td width="35%">
					<input type="text" class="edit" name="end_date_ot" value="{$SEndDateOt}" style="width: 90px; text-align: center;" id="end_date_ot" />
					<img src="engine/inc/board/style/images/calendar.gif" border="0" style="cursor: pointer;" id="end_date_ot_calendar" align="absmiddle" />
					&nbsp;до <input type="text" class="edit" name="end_date_do" value="{$SEndDateDo}" style="width: 90px; text-align: center;" id="end_date_do" />
					<img src="engine/inc/board/style/images/calendar.gif" border="0" style="cursor: pointer;" id="end_date_do_calendar" align="absmiddle" />
				</td>
			</tr>			
			<tr height="23px">
HTML;

if( $Board->Config['region_on'] == "on" ) echo <<<HTML
			
				<td width="15%">
					Город:
				</td>
				<td width="35%">
					<select name="country" id="SelectListCountry" onkeyup="SelectCountry();" onkeydown="SelectCountry();" onchange="SelectCountry();">
						<option value="">- Без разницы -</option>
						{$SelectCountry}
					</select>
					<span id="FormSelectCity">
						<select name="city" id="SelectCity">
							<option value="">- Без разницы -</option>
							{$SelectCity}
						</select>
						<span id="FormSelectCityStatus"></span>
					</span>
				</td>
HTML;
if( $Board->Config['region_on'] != "on" ) echo <<<HTML
			
				<td width="15%">
					Город:
				</td>
				<td width="35%">
					Страны и города отключены
				</td>
HTML;
echo <<<HTML
				<td width="15%">
					На страницу:
				</td>
				<td width="35%">
					<input type="text" class="edit" name="on_page" value="{$SOnPage}" style="width: 90px; text-align: center;" />
					объявлений
				</td>
			</tr>
			<tr height="23px">
				<td width="15%">
					Категория:
				</td>
				<td width="35%">
					<select name="category">
						<option value="">- Без разницы -</option>
						{$SelectCategory}
					</select>
				</td>
				<td width="15%">
					Модерация:
				</td>
				<td width="35%">
					<select name="approve">
						<option value="">- Без разницы -</option>
						{$SelectApprove}
					</select>
				</td>
			</tr>
		</table>
	</div>
	<div class="hr_line"></div>
	<div style="padding: 5px;">
		<table width="100%" border="0">
			<tr>
				<td>
					<input type="submit" name="submit" value="Начать поиск" class="edit" />
					<input type="reset" value="Сбросить" class="edit" />
					<input type="hidden" name="action" value="main" />
					<input type="hidden" name="sort" id="board_hidden_sort" value="" />
					<span id="status_search_board" style="padding: 0px 0px 0px 5px;"></span>
				</td>
				<td align="right">
					<input type="button" value="Отменить поиск" class="edit" onclick="UpdateUrlMainData = ''; UpdateList( 'main' ); this.disabled = true; return false;" />
				</td>
			</tr>
		</table>
	</div>
</form>
HTML;
closetable();

$sql = array();
if( $STitle ) $sql[] = "`title` LIKE '%".$db->safesql( $STitle )."%'";
if( $SBoardType ) $sql[] = "`board_type`='{$SBoardType}'";
if( $SPriceOt ) $sql[] = "`price` >= '{$SPriceOt}'";
if( $SPriceDo ) $sql[] = "`price` <= '{$SPriceDo}'";
if( $SCurrency ) $sql[] = "`currency`='{$SCurrency}'";
if( $Board->Config['region_on'] == "on" )
	{
		if( $SCountry ) $sql[] = "`country`='{$SCountry}'";
		if( $SCity ) $sql[] = "`city`='{$SCity}'";
	}
	
if( $SCategory ) $sql[] = "`category`='{$SCategory}'";
if( $SDateOt ) $sql[] = "`date` >= '".$db->safesql( $SDateOt )."'";
if( $SDateDo ) $sql[] = "`date` <= '".$db->safesql( $SDateDo )."'";
if( $SEndDateOt ) $sql[] = "`end_date` >= '".$db->safesql( $SEndDateOt )."'";
if( $SEndDateDo ) $sql[] = "`end_date` <= '".$db->safesql( $SEndDateDo )."'";

if( $SApprove )
	{
		$SqlApprove = $SApprove == "on" ? 1 : 0;
		$sql[] = "`approve`='{$SqlApprove}'";
	}

if( count( $sql ) > 0 )
	$sql = "AND ".implode( " AND", $sql );
else
	$sql = "";
	
$temp = $db->super_query( "SELECT COUNT(*) as count FROM ".PREFIX."_board_post WHERE id!='' {$sql}" ); 
$posts = $temp['count'];
if( $posts > 0 )
	{
		$page = intval( $_REQUEST['page'] );
		$total = ( ( $posts - 1 ) / $SOnPage  ) + 1;
		$total =  intval( $total );
		if( $page <= 0 ) $page = 1;
		if( $page > $total ) $page = $total;
		$start = $page * $SOnPage - $SOnPage;
		$limit ="LIMIT {$start}, {$SOnPage}";
		
		if( !empty( $_REQUEST['sort'] ) )
			{
				$order_board = totranslit( $_REQUEST['sort'] );
				$array_sort = array(
				  "id_d" => array( "id", true ),
				  "id_k" => array( "id", false ),
				  "title_d" => array( "title", true ),
				  "title_k" => array( "title", false ),
				  "date_d" => array( "date", true ),
				  "date_k" => array( "date", false ),
				  "approve_d" => array( "approve", true ),
				  "approve_k" => array( "approve", false ),
				  "author_d" => array( "author", true ),
				  "author_k" => array( "author", false ),  
				  "category_d" => array( "category", true ),
				  "category_k" => array( "category", false ),  
				);
		
				foreach( $array_sort as $name_sort => $value )
					{    
						if( $name_sort == $order_board )
							{
								$new_sort = $value[0];
								$desc_sort = $value[1];
								if( $desc_sort ) $desc_sort = "DESC";
								$sql_order = "ORDER BY `{$new_sort}` {$desc_sort}";
								echo "<script language=\"Javascript\">document.getElementById( \"board_hidden_sort\" ).value=\"{$order_board}\";</script>";
							}
					}
			}
				else
			{
				$_REQUEST['sort'] = "date_d";
				$sql_order = "ORDER BY `date` DESC";
				$order_board = totranslit( $_REQUEST['sort'] );
			}
		
		$list = "";
		$result = $db->query( "SELECT * FROM ".PREFIX."_board_post WHERE id!='' {$sql} {$sql_order} {$limit}" );
		if( $db->num_rows( $result ) > 0 )
			{
				$list .= showStart();
				while( $row = $db->get_row( $result ) )
					{
						$id = $row['id'];
						$alt_name = htmlspecialchars( stripslashes( $row['alt_name'] ) );
						$title = htmlspecialchars( stripslashes( $row['title'] ) );
						$price = intval( $row['price'] );
						$date = date( "Y-m-d", strtotime( $row['date'] ) );
						$currency = intval( $row['currency'] );
						$board_type = intval( $row['board_type'] );
						$country = intval( $row['country'] );
						$city = intval( $row['city'] );
						$category = intval( $row['category'] );
						$approve = intval( $row['approve'] );
						$vip_date = intval( $row['vip_date'] );
						$super_vip_date = intval( $row['super_vip_date'] );
						$color = htmlspecialchars( stripslashes( $row['color'] ) );
						$color_date = intval( $row['color_date'] );
						$author = htmlspecialchars( stripslashes( $row['author'] ) );
						$author_id = intval( $row['author_id'] );
						$Board->LoadListCity( $country );
						$comm_num = intval( $row['comm_num'] );
						$photos = $row['photos'] != "" ? "<img src=\"engine/inc/board/style/images/photo.png\" alt=\"Имеются фотографии\" title=\"Имеются фотографии\" align=\"absmiddle\" />" : "";
						
						$vip_date = $vip_date > 0 ? date( "Y-m-d H:i:s", ( $vip_date + ( $config['date_adjust'] * 60 ) ) ) : "";
						$super_vip_date = $super_vip_date > 0 ? date( "Y-m-d H:i:s", ( $super_vip_date + ( $config['date_adjust'] * 60 ) ) ) : "";
						$color_date = $color_date > 0 ? date( "Y-m-d H:i:s", ( $color_date + ( $config['date_adjust'] * 60 ) ) ) : "";
						
						$super_vip = $super_vip_date ? "<span style=\"color: #F00; font-weight: bold;\" title=\"Super VIP до {$super_vip_date}\">Super VIP</span>" : "";
						$vip = $vip_date ? "<span style=\"color: #F00; font-weight: bold;\" title=\"VIP до {$vip_date}\">VIP</span>" : "";
						$color = $color_date ? "<span style=\"color: {$color};\">(цвет \"{$color}\" до {$color_date})</color>" : "";
						
						$board_type = $Board->DB['board_type'][ $board_type ] != "" ? $Board->DB['board_type'][ $board_type ]['name'] : "---";
						$currency = $Board->DB['currency'][ $currency ] != "" ? $Board->DB['currency'][ $currency ]['symbol'] : "---";
						$city = $Board->DB['city'][ $country ][ $city ] != "" ? $city = $Board->DB['city'][ $country ][ $city ]['name'] : "---";
						$country = $Board->DB['country'][ $country ] != "" ? $Board->DB['country'][ $country ]['name'] : "---";
						$EchoCat = ( $category && $Board->DB['category'][ $category ] ) ? htmlspecialchars( stripslashes( $Board->DB['category'][ $category ]['name'] ) ) : "---";
						$EchoApprove = $approve == 1 ? "<span style=\"color: green;\">Да</span>" : "<span style=\"color: #F00;\">Нет</span>";
						$LinkPost = $Board->ReturnLinkPost( $id, $alt_name, $category );
				
$list .= <<<HTML

<div id="del_BoardPost_{$id}">
	<table width="100%" border="0">
		<tr height="30px" onmouseover="this.bgColor='#F1F7FE'" onmouseout="this.bgColor='#FFFFFF'">
			<td width="70px" align="center" id="StatusIDBoardPost_{$id}">{$id}</td>
			<td>
				{$super_vip}
				{$vip}
				{$photos}
				<a href="?mod=board&action=post_edit&id={$id}" onmouseover="showhint( '<strong>Стоимость:</strong> {$price}{$currency}<br /><strong>Тип:</strong> {$board_type}<br /><strong>Страна:</strong> {$country}<br /><strong>Город:</strong> {$city}<br /><strong>Комментариев:</strong> {$comm_num}', this, event, '250px')" onclick="EditBoardPost( '{$id}' ); return false;">{$title}</a>
				<a href="{$LinkPost}" target="_blank" title="Посмотреть новость на сайте"><img src="engine/inc/board/style/images/next.png" alt="&rarr;" class="ImgNextLink" /></a>
				{$color}
			</td>
			<td width="100px" align="center"><a href="javascript:void(0);" onclick="ShowUser( '{$author_id}' ); return false;">{$author}</a></td>
			<td width="100px" align="center">{$EchoApprove}</td>
			<td width="150px" align="center">{$EchoCat}</td>
			<td width="100px" align="center">{$date}</td>
			<td width="100px" align="center">
				<a onclick="return dropdownmenu( this, event, MenuBoardPost( '{$id}' ), '120px')" href="javascript:void(0);">
					<img src="engine/skins/images/browser_action.gif" border="0">
				</a>
			</td>
			<td width="30px">
				<input type="checkbox" name="CheckBoardPost[]" value="{$id}" />
			</td>
		</tr>
		<tr>
			<td colspan="10" background="engine/skins/images/mline.gif" height="1"></td>
		</tr>
	</table>
</div>

HTML;

					}
			}
				else
			{
				$list = showStart()."<div style=\"padding: 15px; text-align: center;\">Объявлений не обнаружено</div>".showStart();
			}
	}
		else
	{
		$list = showStart()."<div style=\"padding: 15px; text-align: center;\">Объявлений не обнаружено</div>".showStart();
	}

$RequestData = "title={$STitle}&board_type={$SBoardType}&currency={$SCurrency}&price_ot={$SPriceOt}&price_do={$SPriceDo}&date_ot={$SDateOt}&date_do={$SDateDo}&end_date_ot={$SEndDateOt}&end_date_do={$SEndDateDo}&on_page={$SOnPage}&country={$SCountry}&city={$SCity}&category={$SCategory}&approve={$SApprove}&page=";
$n_url = "$PHP_SELF?mod=board&{$RequestData}";
$ajax_navigation = "UpdateList( 'main', '{$RequestData}{page}&sort={$order_board}' );";
$navigation = Board_Navigation( "{$n_url}&sort={$order_board}", "{$n_url}&sort={$order_board}", $total, $page, $ajax_navigation );

$Sort_ID = board_sort( $n_url, "id", $RequestData );
$Sort_TITLE = board_sort( $n_url, "title", $RequestData );
$Sort_DATE = board_sort( $n_url, "date", $RequestData );
$Sort_APPROVE = board_sort( $n_url, "approve", $RequestData );
$Sort_AUTHOR = board_sort( $n_url, "author", $RequestData );
$Sort_CATEGORY = board_sort( $n_url, "category", $RequestData );

$list = <<<HTML

<form name="main" method="post" action="" onsubmit="SetFormSubmit( 'main' ); return false;">
	<table width="100%" border="0" style="margin: 2px 0px 2px 0px;">
		<tr>
			<td width="70px" align="center">ID {$Sort_ID}</td>
			<td>Название {$Sort_TITLE}</td>
			<td width="100px" align="center">Автор {$Sort_AUTHOR}</td>
			<td width="100px" align="center">Выводится {$Sort_APPROVE}</td>
			<td width="150px" align="center">Категория {$Sort_CATEGORY}</td>
			<td width="100px" align="center">Добавлено {$Sort_DATE}</td>
			<td width="100px" align="center">Действие</td>
			<td width="30px">
				<input type="checkbox" name="ChecboxAll" onclick="CheckboxAllSelect( 'main' );" />
			</td>
		</tr>
	</table>
	<div class="hr_line"></div>
	{$list}
	<div class="hr_line"></div>
	<div style="padding: 5px;">
		<table width="100%" border="0">
			<tr>
				<td class="news_navigation">
					{$navigation}
				</td>
				<td align="right">
					<span id="status_main"></span>
					<select name="action" id="action_main">
						<option value=""> - Выберите действие - </option>
						<option value="public">Опубликовать</option>
						<option value="unpublic">На модерацию</option>
						<option value="del">Удалить</option>
					</select>
					<input type="submit" name="submit" value="Выполнить" class="edit" />
					<input type="hidden" name="TypeAction" value="board_post" />
				</td>
			</td>
		</table>
	</div>
	
	<script language="javascript" type="text/javascript">
		var UpdateUrlMainData = "{$RequestData}{$page}&sort={$order_board}";
	</script>
</form>

HTML;

opentable();
tableheader( "Список всех объявлений" );
echo $list;
echo CalendarSearch();
closetable();

?>