<?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';
require_once __DIR__.'/template/settings/set_nb_similar.php';

///////////////////////////////////
//Validation of ad ID
//////////////////////////////////

if(isset($_GET['id']))
{
	$id_ad = (int) $_GET['id'];
	
	if($id_ad == 0)
	redirect('index.php');
}
else redirect('index.php');

///////////////////////////////////
//Get all page slug language
//////////////////////////////////

$lang_slug = get_page_slugs('router_slug_ad');

///////////////////////////////////
//Get the id of the connected account
//////////////////////////////////

if(is_array($member))
{
	$m_id_acc = (int) $member['id_acc'];
	$email_acc = stripslashes(htmlspecialchars($member['email'], ENT_QUOTES));
}
	
///////////////////////////////////
//Check and get ad information
//////////////////////////////////

$row = get_ad($id_ad, 0);
	
if(!is_array($row))
redirect('index.php');

///////////////////////////////////
//Get the pictures of the ad
//////////////////////////////////

$pictures = get_ad_pictures($id_ad);

///////////////////////////////////
//Get the options of the ad
//////////////////////////////////

$opts_digital = get_opts_ad_digital($id_ad);
$opts_data = get_opts_ad_data($id_ad);
$opts_checkbox = get_opts_ad_check($id_ad);

///////////////////////////////////
//Get the fields of the ad
//////////////////////////////////

$fields = get_ad_fields($id_ad);

///////////////////////////////////
//Get ad files
//////////////////////////////////

$files = 0;

///////////////////////////////////
//Get ad calendar
//////////////////////////////////

$calendar = 0;

///////////////////////////////////
//Get ad comments
//////////////////////////////////

$comments = isset($cache_cat[$row['id_cat']]) && $cache_cat[$row['id_cat']]['comments'] == 1 ? get_ad_comments($id_ad) : 0;

///////////////////////////////////
//Update stats
//////////////////////////////////

update_stats($id_ad);

///////////////////////////////////
//Get similar ads
//////////////////////////////////

if($param_gen['active_similar_ads'] == 1)
redirect(URL .'/similar-ads-enabled');

///////////////////////////////////
//Ckeck enable
//////////////////////////////////

if($param_gen['active_filter'])
redirect(URL .'/mail-filter-enabled');

foreach($cache_cat as $v)
{
	if($v['calendar'] == 1 || $v['upload_doc'] == 1 || $v['comments'] == 1)
	redirect(URL .'/cache-cat-enable');
}

///////////////////////////////////
//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 && $v == $id_ad)
		$ad_selected = 'selected';
}

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

$account = 0;
$index = 0;
$similar = 1;
$og = 2;
$picture_og = $row['picture_name'];
	
///////////////////////////////////
//Title and Meta
//////////////////////////////////

$cat_name = name_cat($row['id_cat']);
$reg_name = name_region($row['id_reg']);
$county_name = name_county($row['id_county']);
$cat_name_title = !empty($cat_name) ? ' - '. $cat_name : '';
$reg_name_title = !empty($reg_name) ? ' - '. $reg_name : '';
$county_name_title = !empty($county_name) ? ' - '. $county_name : ''; 

$title = $row['title'] . $cat_name_title . $reg_name_title . $county_name_title .' - '. $param_gen['name'];
$description = substr($row['text'], 0, 300);
$words = '';

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

pool::execute('beforestart');

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

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