<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_EMAIL_TEMPLATES';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");

page(_($help_context = "Email Template"));

include_once($path_to_root . "/includes/ui.inc");

include_once($path_to_root . "/projects/includes/db/email_template_db.inc");

simple_page_mode(false);
//----------------------------------------------------------------------------------

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
{

	//initialise no input errors assumed initially before we test
	$input_error = 0;

	if (strlen($_POST['template_description']) == 0)
	{
		$input_error = 1;
		display_error(_("The template description cannot be empty."));
		set_focus('template_description');
	}
        if (is_numeric($_POST['template_description'])){
            
                $input_error = 1;
		display_error(_("Template description can not be numeric."));
		set_focus('template_description');
        }
	if (strlen($_POST['template_description'])>(20+2))
	{
		$input_error = 1;
		display_error(_("The template description is too long."));
		set_focus('template_description');
	}
        
	if (strlen($_POST['email_content']) == 0)
	{
		$input_error = 1;
		display_error(_("The email content can not be empty."));
		set_focus('email_content');
	}
        if (is_numeric($_POST['email_content'])){
            
                $input_error = 1;
		display_error(_("Email content can not be numeric."));
		set_focus('email_content');
        }
	if (strlen($_POST['email_content'])>(50))
	{
		$input_error = 1;
		display_error(_("The Email content is too long."));
		set_focus('email_content');
	}

	if ($input_error !=1) {
    	write_template($selected_id, $_POST['template_description'], $_POST['email_content']);
		if($selected_id != '')
			display_notification(_('Selected email template has been updated'));
		else
			display_notification(_('New email template has been added'));
		$Mode = 'RESET';
	}
}

//----------------------------------------------------------------------------------

if ($Mode == 'Delete')
{

        delete_email_template($selected_id);
        display_notification(_('Selected email template has been deleted'));
	$Mode = 'RESET';
}

if ($Mode == 'RESET')
{
	$selected_id = '';
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['show_inactive'] = $sav;
}

//----------------------------------------------------------------------------------

$result = get_all_email_templates(check_value('show_inactive'));

start_form();
start_table(TABLESTYLE, "width=41%");
$th = array(_('Template'),_('Content'), "", "");
inactive_control_column($th);

table_header($th);
$k = 0; //row colour counter

while ($myrow = db_fetch($result))
{

	alt_table_row_color($k);

	label_cell($myrow["template_description"]);
        label_cell($myrow["email_content"]);

 	edit_button_cell("Edit".$myrow["template_id"], _("Edit"));
 	delete_button_cell("Delete".$myrow["template_id"], _("Delete"));
	end_row();
}

//inactive_control_row($th);
end_table(1);

//----------------------------------------------------------------------------------

start_table(TABLESTYLE2);

if ($selected_id != '') 
{
 	if ($Mode == 'Edit') {
		//editing an existing item category

		$myrow = get_template_details($selected_id);

		$_POST['template_id'] = $myrow["template_id"];
                $_POST['email_content'] = $myrow["email_content"];
		$_POST['template_description']  = $myrow["template_description"];
	}
	hidden('selected_id', $myrow["template_id"]);
}

text_row(_("Template Name:"), 'template_description', null, 20, 20);
textarea_row(_("Email Content:"), 'email_content', null, 50, 10);
//number_list_row(_("Decimal Places:"), 'decimals', null, 0, 6, _("User Quantity Decimals"));

end_table(1);

submit_add_or_update_center($selected_id == '', '', 'both');

end_form();

end_page();

?>
