<?php
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='load_template'){
	$job_id = $_REQUEST['job_id'];
	$email_id = $_REQUEST['email_id'];
	$data = utility::getTableRowByField("job","id",$job_id);
	$lead_consultant = $data["lead_consultant"];
	$signature_text = utility::getTableFieldById("admin","signature_text","id",$lead_consultant);
	
	$row = utility::getTableRowByField("mail_schedule","id",$email_id);
	$email_subject = utility::getTableFieldById("mail_schedule","email_subject","id",$email_id);
	$mail_body = utility::getTableFieldById("mail_schedule","mail_body","id",$email_id);
	/////////////// Replacing String ///////////////////////////////////////////
	$client_field_array = array();
	$client_field_value_array = array();
	$mail_subject_array = array();
	$mail_subject_value_array = array();
	foreach(afcl::$jobDetailArray as $client_field){
		if( strpos( $mail_body, $client_field ) !== false) {
			array_push($client_field_array,"[".$client_field."]");
			if($client_field == "first_name"){
				$field_value = $data["first_name1"];
			}elseif($client_field == "last_name"){
				$field_value = $data["last_name1"];
			}
			array_push($client_field_value_array,$field_value);
		}
		if( strpos( $email_subject, $client_field ) !== false) {
			array_push($mail_subject_array,"[".$client_field."]");
			if($client_field == "first_name"){
				$field_value = $data["first_name1"];
			}elseif($client_field == "last_name"){
				$field_value = $data["last_name1"];
			}
			array_push($mail_subject_value_array,$field_value);
		}
	}	
	$mail_body = str_replace($client_field_array,$client_field_value_array,$mail_body);
	/////////////// End Replacing String ///////////////////////////////////////////

	/////////////// Replacing Link ///////////////////////////////////////////
	$client_link_array = array();
	$client_link_value_array = array();
	for($i = 1;$i<=10;$i++){
		if(isset($row["link$i"]) && $row["link$i"] != ""){
			array_push($client_link_array,"Link".$i);
			array_push($client_link_value_array,$row['link'.$i]);
		}
	}	
	$mail_body = str_replace($client_link_array,$client_link_value_array,$mail_body);
	if(isset($signature_text) && $signature_text !=""){
		$mail_body = $mail_body.$signature_text;
	}
	$email_subject = str_replace($mail_subject_array,$mail_subject_value_array,$email_subject);
	$returnArray["email_subject"] = $email_subject;
	/////////////// End Replacing Link ///////////////////////////////////////////
	
	/////////////// Replacing Image Path ///////////////////////////////////////////
	if( strpos( $mail_body, "/admin/UserFiles/" ) !== false) {
		if($_SERVER['HTTP_HOST'] == "localhost"){
			$mail_body = str_replace("/afcl/admin/UserFiles/",$config->frontEndURL."/admin/UserFiles/",$mail_body);
		}else{
			$mail_body = str_replace('/admin/UserFiles/',$config->frontEndURL.'admin/UserFiles/',$mail_body);
		}
	}
	/////////////// End Replacing Image Path ///////////////////////////////////////////
	$returnArray["mail_body"] = $mail_body;
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='load_message_template'){
	$message_id = $_REQUEST['message_id'];
	$message_body = utility::getTableFieldById("sms_schedule","sms_discription","id",$message_id);
	$returnArray["message_body"] = $message_body;
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='load_template_for_client'){
	$client_id = $_REQUEST['client_id'];
	$email_id = $_REQUEST['email_id'];
	$data = utility::getTableRowByField("client","id",$client_id);
	$lead_consultant = $data["lead_consultant"];
	$signature_text = utility::getTableFieldById("admin","signature_text","id",$lead_consultant);
	
	$row = utility::getTableRowByField("mail_schedule","id",$email_id);
	$email_subject = utility::getTableFieldById("mail_schedule","email_subject","id",$email_id);
	$mail_body = utility::getTableFieldById("mail_schedule","mail_body","id",$email_id);
	/////////////// Replacing String ///////////////////////////////////////////
	$client_field_array = array();
	$client_field_value_array = array();
	$mail_subject_array = array();
	$mail_subject_value_array = array();
	foreach(afcl::$clientDetailArray as $client_field){
		if( strpos( $mail_body, $client_field ) !== false) {
			array_push($client_field_array,"[".$client_field."]");
			$field_value = $data[$client_field];
			array_push($client_field_value_array,$field_value);
		}
		if( strpos( $email_subject, $client_field ) !== false) {
			array_push($mail_subject_array,"[".$client_field."]");
			$field_value = $data[$client_field];
			array_push($mail_subject_value_array,$field_value);
		}
	}
	$mail_body = str_replace($client_field_array,$client_field_value_array,$mail_body);
	$email_subject = str_replace($mail_subject_array,$mail_subject_value_array,$email_subject);
	$returnArray["email_subject"] = $email_subject;
	/////////////// End Replacing String ///////////////////////////////////////////

	/////////////// Replacing Link ///////////////////////////////////////////
	$client_link_array = array();
	$client_link_value_array = array();
	for($i = 1;$i<=10;$i++){
		if(isset($row["link$i"]) && $row["link$i"] != ""){
			array_push($client_link_array,"Link".$i);
			array_push($client_link_value_array,$row['link'.$i]);
		}
	}
								
	$mail_body = str_replace($client_link_array,$client_link_value_array,$mail_body);
	if(isset($signature_text) && $signature_text !=""){
		$mail_body = $mail_body.$signature_text;
	}
	/////////////// End Replacing Link ///////////////////////////////////////////
	
	/////////////// Replacing Image Path ///////////////////////////////////////////
	if( strpos( $mail_body, "/admin/UserFiles/" ) !== false) {
		if($_SERVER['HTTP_HOST'] == "localhost"){
			$mail_body = str_replace("/afcl/admin/UserFiles/",$config->frontEndURL."/admin/UserFiles/",$mail_body);
		}else{
			$mail_body = str_replace('/admin/UserFiles/',$config->frontEndURL.'admin/UserFiles/',$mail_body);
		}
	}
	/////////////// End Replacing Image Path ///////////////////////////////////////////
	$returnArray["mail_body"] = $mail_body;
	echo json_encode($returnArray);
}
?>