<?php 
/****************************************
/*  Author  : Kvvaradha
/*  Module  : Kvcodes CRM
/*  E-mail  : admin@kvcodes.com
/*  Version : 1.0
/*  Http    : www.kvcodes.com
/*  Function : Email templates
*****************************************/

$page_security = 'CRM_EMAILS';
$path_to_root="../../..";
include($path_to_root . "/includes/session.inc");
$version_id = get_company_prefs('version_id');
add_access_extensions();
$js = '';
if($version_id['version_id'] == '2.4.1'){
	if ($SysPrefs->use_popup_windows) 
		$js .= get_js_open_window(900, 500);	

	if (user_use_date_picker()) 
		$js .= get_js_date_picker();
	
}else{
	if ($use_popup_windows)
		$js .= get_js_open_window(900, 500);
	if ($use_date_picker)
		$js .= get_js_date_picker();
}
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/modules/KvcodesCRM/includes/crm.inc" );
?>
<style>
.nicEdit-selectTxt {line-height: 16px !important;}
</style>

<?php 
add_js_file('../modules/KvcodesCRM/js/nicEdit-latest.js');

page(_("Email Templates"), @$_REQUEST['popup'], false, "", $js);

simple_page_mode(true);

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

	$input_error = 0;

	if (strlen($_POST['description']) == 0) {
		$input_error = 1;
		display_error(_("The Email Template description cannot be empty."));
		set_focus('description');
	}

	if ($input_error != 1)	{
    	if ($selected_id != -1)     	{
    		Update('kv_crm_email_templates', array('id' => $selected_id), array('description' => $_POST['description'], 'name' => $_POST['name'], 'subject' => $_POST['subject'], 'type' => $_POST['type']));    		
			$note = _('Selected Email Template has been updated');
    	}   else  	{
    		$slug =  seoUrl($_POST['subject']);
    		Insert('kv_crm_email_templates', array('description' => $_POST['description'], 'slug' => $slug, 'name' => $_POST['name'], 'subject' => $_POST['subject'], 'type' => $_POST['type']));
			$note = _('New Email Template has been added');
    	}    
		display_notification($note);    	
		$Mode = 'RESET';
	}
} 

if ($Mode == 'Delete'){

	$cancel_delete = 0;

	// PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'

	/*if (key_in_foreign_table($selected_id, 'kv_crm_email_templates', 'type'))	{
		$cancel_delete = 1;
		display_error(_("Cannot delete this Type because Email template have been created using this pick type."));
	} */
	if ($cancel_delete == 0) {
		Delete('kv_crm_email_templates', array('id' => $selected_id));
		display_notification(_('Selected Email Template has been deleted'));
	} //end if Delete department
	$Mode = 'RESET';
} 

if ($Mode == 'RESET'){
	$selected_id = -1;
	$type = $_POST['type'];
	$sav = get_post('show_inactive');
	unset($_POST);
	$_POST['type'] = $type;
	if ($sav) $_POST['show_inactive'] = 1;
}
//-------------------------------------------------------------------------------------------------
start_form();

start_table(TABLESTYLE_NOBORDER);
	start_row();
	crm_total_types_list_cells(_("Select a Type"). " :", 'type', null,  _("Select Type"), true);
	end_row();
end_table();
start_table(TABLESTYLE, "width=70%");
$th = array(_("Name"), _("Subject"), "");
if($_SESSION["wa_current_user"]->username == 'kvcodes' || $_SESSION['wa_current_user']->username == 'root')
	$th[] = "";
inactive_control_column($th);

table_header($th);
$k = 0; 
if(get_post('type') || list_updated('type'))
	$result = GetAll('kv_crm_email_templates', array('type' => get_post('type')));
else
	$result = GetAll('kv_crm_email_templates');

if(list_updated('type'))
	$Ajax->activate('_page_body');
foreach($result as $myrow) {
	
	if(($_SESSION["wa_current_user"]->username != 'kvcodes' && $_SESSION['wa_current_user']->username != 'root' ) && $myrow["inactive"])
		continue;
	alt_table_row_color($k);
		
	//label_cell($myrow["id"]);
	label_cell($myrow["subject"]);
	label_cell($myrow["name"]);
	inactive_control_cell($myrow["id"], $myrow["inactive"], 'kv_crm_email_templates', 'id');
 	edit_button_cell("Edit".$myrow["id"], _("Edit"));
 	if($_SESSION["wa_current_user"]->username == 'kvcodes' || $_SESSION['wa_current_user']->username == 'root')
 		delete_button_cell("Delete".$myrow["id"], _("Delete"));
	end_row();
}

inactive_control_row($th);
end_table(1);

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

start_table(TABLESTYLE2);

if(list_updated('type')){
	unset($_POST['name'],$_POST['subject'], $_POST['description']);
}
if ($selected_id != -1) {
 	if ($Mode == 'Edit') {
		//editing an existing department
		$myrow = GetRow('kv_crm_email_templates', array('id' =>$selected_id));

		$_POST['subject']  = $myrow["subject"];
		$_POST['name']  = $myrow["name"];
		$_POST['description']  = htmlspecialchars_decode($myrow["description"]);
	}
	hidden("selected_id", $selected_id);
	
} else {
	unset($_POST['name'],$_POST['subject'], $_POST['description']);
}

text_row(_("Name"), 'name', null, 60, 80);
text_row(_("Subject"), 'subject', null, 60, 80);
if(isset($myrow['slug']))
	label_row(_("Slug"), $myrow["slug"]);
kv_crm_textarea_row(_("Template:"), 'description', null, 60, 15); 
end_table(1);
submit_add_or_update_center($selected_id == -1, '', 'both');
end_form();
end_page();
echo "<script type='text/javascript'> kvcodes_crm_nicEditor();  </script> \n";
?>