<?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 automatic mail
//////////////////////////////////
	
if(!empty($_GET['type']))
{
	if($_GET['type'] >= 1 && $_GET['type'] <= 22)
	$type = (int) $_GET['type'];
	
	else redirect('index.php');
}
else redirect('index.php');

///////////////////////////////////
//Select mail auto
//////////////////////////////////

$name = '';
$email = '';

foreach($cache_mails_auto as $v)
{
	if($v['type'] == $type)
	{
		$id_mail = (int) $v['id_mail'];
		$name = stripslashes(htmlspecialchars($v['name'], ENT_QUOTES));
		$email = stripslashes(htmlspecialchars($v['email'], ENT_QUOTES));
	}
}

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

$error = '';

///////////////////////////////////
//Check the datas
//////////////////////////////////

if(isset($_POST['name']))
{
	if(empty($_POST['name']))
	$error = 1;
	
	elseif(!valid_email($_POST['email']))
	$error = 2;
	
	foreach($cache_lang as $v)
	{
		$code_lang = htmlspecialchars($v['code_lang']);
		$active = (int) $v['active'];
		
		if($active == 1 && empty($_POST['title_'. $code_lang]))
		$error = 3;
		
		if($active == 1 && empty($_POST['message_'. $code_lang]))
		$error = 3;
	}
}

///////////////////////////////////
//Update mails auto and including html functions
//////////////////////////////////	

if(!empty($_POST['name']) && empty($error))
{
	update_mail_auto($_POST);
	create_cache_mails_auto();
	
	$error = 'succes';
}

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

$conn = null;

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

$view = array();
$view[] = 'template_adm/tpl_mails_auto_adm.php';

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