<?php
///////////////////////////////////////////////////////////////////////////////////////////
///Script PHP/MYSQL of management of classifieds ads developed by Script PAG
///Script PAG all rights reserved. Use under license. http://www.script-pag.com
///////////////////////////////////////////////////////////////////////////////////////////

############################################################

require_once __DIR__.'/includes/all_fns.php';

///////////////////////////////////
//Validation the id of the ad
//////////////////////////////////

if(isset($_GET['id_ad']))
{
	$id_ad = (int) $_GET['id_ad'];
	
	if($id_ad == 0)
	redirect('index.php');
	
	if(!get_ad($id_ad, 0))
	redirect('index.php');
}
else redirect('index.php');
	
///////////////////////////////////
//Datas search engine
//////////////////////////////////
	
$page_number = isset($_GET['page']) && $_GET['page'] <= 0 ? 1 : (isset($_GET['page']) ? (int) $_GET['page'] : 1);
$sort = isset($_GET['sort']) && $_GET['sort'] < 0 && $_GET['sort'] > 4 ? 1 : (isset($_GET['sort']) ? (int) $_GET['sort'] : 1);

///////////////////////////////////
//Get total number of ads
//////////////////////////////////

$nb_ads = get_nb_ads_advertiser($id_ad);
$total = (int) $nb_ads['total'];

///////////////////////////////////
//Get the number of results by page
//////////////////////////////////

$limit = (int) $param_gen['nb_ads'];

$limit_page = ceil($total/$limit);

if ($limit_page > 0 && $page_number > $limit_page)
$page_number = $limit_page;

$offset = ($page_number - 1) * $limit;

$start_ads = ($page_number * $limit) - $limit + 1;
$end_ads = $limit * $page_number;

///////////////////////////////////
//Get ads
//////////////////////////////////

$search_array = get_all_ads_advertiser($id_ad, $offset, $limit, $sort);

///////////////////////////////////
//Get selected ad
//////////////////////////////////

$ad_selected = [];
	
foreach($_COOKIE as $k => $v)
{
	if(preg_match("#Script_PAG#", $v) == true)
	{
		$v = str_replace('Script_PAG', '', $v);
		$v = (int) $v;
	}
	else $v = 0;
	
	if($v != 0)
	$ad_selected[$v] = 1;
}

///////////////////////////////////
//Urls pagination infos
//////////////////////////////////

$url_pagination = $_SERVER['REQUEST_URI'];
	    
if(preg_match('#&page='. $page_number .'#', $url_pagination))
$url_pagination = str_replace('&page='. $page_number, '', $url_pagination);

$max_page_view = 9;
$max_page_limit_view = floor($max_page_view/2);

///////////////////////////////////
//Title and meta
//////////////////////////////////

$title = $language['title_page_advertiser'] .' '. htmlspecialchars($search_array[0]['name']);
$description = $language['description_page_advertiser'];
$words = $language['keywords_page_advertiser'];
$title_listing_infos = $language['info_page_advertiser'] .' '. htmlspecialchars($search_array[0]['name']);

///////////////////////////////////
//Variables of search engine
//////////////////////////////////

$type = 0;
$type_search = 5;
$listing_search = 5;
$count_adv = 0;

///////////////////////////////////
//Variables of elements
//////////////////////////////////

$index = 0;
$account = 0;

///////////////////////////////////
//Display view
//////////////////////////////////

pool::execute('beforestart');

$view = array();
$view[] = 'template/tpl_search_results.php';

require_once __DIR__.'/template/tpl_layout.php';