<?php 


ini_set('display_errors', 0); // disable error display
ini_set('log_errors', 0); // disable error logging
ini_set('max_execution_time', 6000);
ini_set('memory_limit', '1024M');
ini_set('upload_max_filesize', '256M');

$html = str_replace($varList,$valueList,$html);

require("Staff/dbconfig.php");
include("mpdf60/mpdf.php");
include("rtg_ls_fax_common.php");


$mpdf=new mPDF();
if(!isset($_POST['ajax']) && $_POST['ajax'] != 1){

    //getAutomationsForDropbox($dropdown_category_id);
    $mpdf->SetProtection(array("print"));
}


$mpdf->defaultfooterline = 0;
$mpdf->setFooter("Page {PAGENO} of {nb}");




$mpdf->WriteHTML($html);
$total_pages = $mpdf->page;



if(isset($deletionArray) && !empty($deletionArray)){
	foreach($deletionArray as $key=>$value){
		@unlink($value);
	}
}

$front_end_client_name = "";
if(isset($_POST['first_name']) && !empty($_POST['first_name'])){
    $front_end_client_name = $_POST['first_name'];
}

if(isset($_POST['last_name']) && !empty($_POST['last_name'])){
    if(!empty($front_end_client_name)){
        $front_end_client_name .="_".$_POST['last_name'];
    }else{
        $front_end_client_name = $_POST['last_name'];
    }
}

if(isset($_POST['client_name']) && !empty($_POST['client_name'])){
    $front_end_client_name = $_POST['client_name'];
    $front_end_client_name = str_replace(' ', '_', $front_end_client_name);
}

if(!empty($front_end_client_name)){
   $file_name = $front_end_client_name."_".$file_name;
   $file_name = str_replace(',', '', $file_name);
}

$mpdf->Output($file_name);

 

$fullPath =  $file_name;

if(isset($_POST['HAS_RTG_LS_FAX_ACCESS']) && $_POST['HAS_RTG_LS_FAX_ACCESS']){
    if(isset($_POST["image1"]) && !empty($_POST["image1"])){
        $_POST["image1"] = "contact_photo/".$_POST["client_id"]."/".basename($_POST["image1"]);
    }
    
    if(isset($_POST["image2"]) && !empty($_POST["image2"])){
        $_POST["image2"] = "contact_photo/".$_POST["client_id"]."/".basename($_POST["image2"]);
    }
    
    if(isset($_POST["image3"]) && !empty($_POST["image3"])){
        $_POST["image3"] = "contact_photo/".$_POST["client_id"]."/".basename($_POST["image3"]);
    }
    
    if(isset($_POST["image4"]) && !empty($_POST["image4"])){
        $_POST["image4"] = "contact_photo/".$_POST["client_id"]."/".basename($_POST["image4"]);
    }
    
    if(isset($_POST["image5"]) && !empty($_POST["image5"])){
        $_POST["image5"] = "contact_photo/".$_POST["client_id"]."/".basename($_POST["image5"]);
    }
    
    if(isset($_POST["image6"]) && !empty($_POST["image6"])){
        $_POST["image6"] = "contact_photo/".$_POST["client_id"]."/".basename($_POST["image6"]);
    }
    
    
    

    $request = $_POST;
    
    $request['file_path'] = $fullPath;

    $request['form_data'] = json_encode($request);

    $request['lettertypes'] = json_decode($request['lettertypes'],true);
}

function clientErrorLog($error){
    $error = json_encode($error);
    $current_date = date('Y-m-d H:i:s');
    $file = fopen("rtg_client_ids_error.txt", "a");
    fwrite($file, "\n\n" . $current_date . " : " . $error);
    fclose($file);
}

if(isset($_POST['ajax']) && $_POST['ajax'] == 1){

    $request['total_pdf_pages'] = $total_pages;
    $request['destAssocAddress'] = json_decode($request['destAssocAddress'],true);
    $request['destAssocReturnAddress'] = json_decode($request['destAssocReturnAddress'],true);
    $request['download_category'] = json_decode($request['download_category'],true);
    
    
    $result = isInconsistentClientId($conn, $request);
    if($result){
        $response = processAjaxRequest($conn,$request);
    }else{
        $response = "Please select  a valid client.";
        clientErrorLog($request);
    }
    
    echo json_encode($response);
    
}else{

    if(isset($_POST['HAS_RTG_LS_FAX_ACCESS']) && $_POST['HAS_RTG_LS_FAX_ACCESS']){
       
        $result = isInconsistentClientId($conn, $request);
        
        if(!$result){
            clientErrorLog($request);
            echo ("<script LANGUAGE='JavaScript'>
                    window.alert('Please select a valid client.');
                    window.location.href='".$_SERVER['HTTP_REFERER']."';
                    </script>");
            exit;
          
        }
        
        if(isset($request['onload_form_id']) && !empty($request['onload_form_id'])){
                $rtg_letters_id = updateRTGLetters($conn, $request['onload_form_id'], $request);
        }else{
            $rtg_letters_id = saveRTGLetters($conn, $request);
        }
        letterEmailSendToClient($conn, $request);
        insertSttPILetterSet($conn,$rtg_letters_id);
    }
    storePdfLetters($_POST['client_id'],$rtg_letters_id,3,$fullPath);
   //echo basename($fullPath);exit;
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header('Content-disposition: attachment;filename='.basename($fullPath));
    header("Content-Type: application/pdf");
    header("Content-Transfer-Encoding: Binary");
    header('Content-Length: '. filesize($fullPath));  
    //ob_implicit_flush(true);
    //ob_clean();
    flush();
    readfile($fullPath); 
}

@unlink($fullPath); 
 exit;
?>