<?php 
/*#################################################################*\
|# Licence Number 0DT7-0122-0NW1-0210
|# -------------------------------------------------------------   #|
|# Copyright (c)2010 PHP Link Directory.                           #|
|# http://www.phplinkdirectory.com                                 #|
\*#################################################################*/
	
/**
# ################################################################################
# Project:   PHP Link Directory
#
# **********************************************************************
# Copyright (C) 2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
#
# This software is for use only to those who have purchased a license.
# A license must be purchased for EACH installation of the software.
#
# By using the software you agree to the terms:
#
#    - You may not redistribute, sell or otherwise share this software
#      in whole or in part without the consent of the the ownership
#      of PHP Link Directory. Please contact david@david-duval.com
#      if you need more information.
#
#    - You agree to retain a link back to http://www.phplinkdirectory.com/
#      on all pages of your directory in you purchased the $25 version
#      of the software.
#
# License holders are entitled to upgrades to the 3.4 branch of the software
# as they are made available at http://www.phplinkdirectory.com/
#
# In some cases, license holders may be required to agree to changes
# in the software license before receiving updates to the software.
# **********************************************************************
#
# For questions, help, comments, discussion, etc., please join the
# PHP Link Directory Forum http://www.phplinkdirectory.com/forum/
#
# @link           http://www.phplinkdirectory.com/
# @copyright      2004-2009 NetCreated, Inc. (http://www.netcreated.com/)
# @projectManager David DuVal <david@david-duval.com>
# @package        PHPLinkDirectory
# @version        4.0.0
# ################################################################################
*/

require_once 'init.php';

if (empty ($_REQUEST['submit']) && !empty ($_SERVER['HTTP_REFERER']))
   $_SESSION['return'] = $_SERVER['HTTP_REFERER'];

if ($_REQUEST['action'])
{
   list ($action, $id) = split(':', $_REQUEST['action']);

   $action = strtoupper (trim ($action));
   $id     = ($id < 0  ? 0 : intval ($id));

   $tpl->assign('action', strtoupper ($action));
}

switch ($action)
{
	case 'D' :
		if ($db->Execute("DELETE FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($id)))
		{
			if (isset ($_SESSION['return']))
				http_custom_redirect($_SESSION['return']);
		}
      else
			$tpl->assign('sql_error', $db->ErrorMsg());
		break;
	case 'E' :
		if (empty ($_REQUEST['submit']))
			$data = $db->GetRow("SELECT * FROM `{$tables['email_tpl']['name']}` WHERE `ID` = ".$db->qstr($id));
	case 'N' :
	default :
		if ($id)
			$where = "WHERE `ID` != ".$db->qstr($id);

		if (empty ($_POST['submit']))
		{
			SmartyValidate :: disconnect();
			SmartyValidate :: connect($tpl, true);
			SmartyValidate :: register_criteria('isEmailAndAddLinkValid', 'validate_email_and_add_link');
			SmartyValidate :: register_validator('v_VALIDATE_EMAIL_TYPE', 'TPL_TYPE', 'isEmailAndAddLinkValid', false, false, 'trim');
			SmartyValidate :: register_validator('v_TITLE'              , 'TITLE'   , 'notEmpty', false, false, 'trim');
			SmartyValidate :: register_validator('v_SUBJECT'            , 'SUBJECT' , 'notEmpty', false, false, 'trim');
			SmartyValidate :: register_validator('v_BODY'               , 'BODY'    , 'notEmpty', false, false, 'trim');
		}
      else {
			SmartyValidate :: connect($tpl);
			$data = get_table_data('email_tpl');
			if (SmartyValidate :: is_valid($data))
			{
				if (empty ($id))
					$id = $db->GenID($tables['email_tpl']['name'].'_SEQ');

				$data['ID'] = $id;
				if ($db->Replace($tables['email_tpl']['name'], $data, 'ID', true) > 0)
				{
					$tpl->assign('posted', true);
					if (isset ($_SESSION['return']))
						http_custom_redirect($_SESSION['return']);
				}
            else
            {
					$tpl->assign('sql_error', $db->ErrorMsg());
					echo $db->ErrorMsg();
				}
			}
		}
		$tpl->assign('tpl_types', $email_tpl_types);
		$tpl->assign($data);
		$content = $tpl->fetch(ADMIN_TEMPLATE.'/email_message_edit.tpl');
		break;
}
$tpl->assign('content', $content);

//Clean whitespace
$tpl->load_filter('output', 'trimwhitespace');

//Make output
echo $tpl->fetch(ADMIN_TEMPLATE.'/main.tpl');
?>