<?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_adm/all_fns_adm.php';

///////////////////////////////////
//Check if the super admin is connected
//////////////////////////////////
	
if(!empty($_SESSION['valid_admin']))
$valid_admin = $_SESSION['valid_admin'];

if(!check_super_admin())
redirect('index.php');

///////////////////////////////////
//Validation of the action
//////////////////////////////////

if(!empty($_GET['act']))
{
	if($_GET['act'] >= 1 && $_GET['act'] <= 13)
	$act = (int) $_GET['act'];
	
	else redirect('index.php');
}
else redirect('index.php');

///////////////////////////////////
//Validation of the categorie or field
//////////////////////////////////

$id_cat = (!empty($_POST['cat'])) ? (int) $_POST['cat'] : 0;
$id_field = (!empty($_GET['id_field'])) ? (int) $_GET['id_field'] : 0;

///////////////////////////////////
//Initializing variable $error
//////////////////////////////////

$error = '';

///////////////////////////////////
//Enable/disable fields
//////////////////////////////////

if($act == 1)
{
	$act_comp_num = (int) $cache_param_fields['act_comp_num'];
	$act_postcode = (int) $cache_param_fields['act_postcode'];
	$act_city = (int) $cache_param_fields['act_city'];
	$act_phone = (int) $cache_param_fields['act_phone'];
	$act_price = (int) $cache_param_fields['act_price'];
			
	if(!empty($_POST['valid']))
	{
		create_cache_param_fields($_POST);	
		redirect('fields.php?act=1&succes=1&menu=26');
	}

}

///////////////////////////////////
//Add field
//////////////////////////////////

if(count($_POST) > 0 && $act == 3 && !isset($_POST['lang']))
{
	foreach($cache_lang as $v)
	{
		$code_lang = stripslashes(htmlspecialchars($v['code_lang'], ENT_QUOTES));
		$active = (int) $v['active'];
		
		if($active == 1 && empty($_POST['name_'. $code_lang]))
		$error = 1;
	}
	 
	if(empty($error))
	{
		create_field($_POST, $id_cat);
		create_cache_fields();
			
		redirect("fields.php?act=2&succes=1&menu=26");
	}
}

///////////////////////////////////
//Update field
//////////////////////////////////

if(count($_POST) > 0 && $act == 4 && !isset($_POST['lang']))
{
	foreach($cache_lang as $v)
	{
		$code_lang = stripslashes(htmlspecialchars($v['code_lang'], ENT_QUOTES));
		$active = (int) $v['active'];
		
		if($active == 1 && empty($_POST['name_'. $code_lang]))
		$error = 1;
	}
	 
	if(empty($error))
	{
		update_field($_POST, $id_field, $id_cat);
		create_cache_fields();
			
		redirect("fields.php?act=2&succes=2&menu=26");
	}
}

///////////////////////////////////
//Delete field
//////////////////////////////////

if($act == 5)
{
	delete_field($id_field);
	create_cache_fields();
			
	redirect("fields.php?act=2&succes=3&menu=26");
}

///////////////////////////////////
//Delete all fields
//////////////////////////////////

if(!empty($_POST['check_all']) && $act == 6)
{
	$redirect = (count($_POST['check_all']) > 1) ? 'act=2&succes=4&menu=26' : 'act=2&succes=3&menu=26';
		
	$array = $_POST['check_all'];
	
	foreach($array as $id_field)
	delete_field($id_field);
	
	create_cache_fields();
			
	redirect('fields.php?'. $redirect);
}

///////////////////////////////////
//Close database connexion
//////////////////////////////////

$conn = null;

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

$view = array();

if($act == 1) 
$view[] = 'template_adm/tpl_fields_act_adm.php';

elseif($act == 3) 
$view[] = 'template_adm/tpl_fields_crea_adm.php';

elseif($act == 4)
$view[] = 'template_adm/tpl_fields_mod_adm.php';

else $view[] = 'template_adm/tpl_fields_adm.php';

require_once __DIR__.'/template_adm/tpl_layout_adm.php';