<?php
//============================================================+
// File name   :  converter.php
//
// Description : TCPDF class
//               WriteHTML and RTL support

//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: WriteHTML and RTL support
 * @author Nicola Asuni
 * @since 2008-03-04
 */


	$lldocroot = defined('SITEROOT') ? SITEROOT : $_SERVER['DOCUMENT_ROOT'];
	if(file_exists($lldocroot . "/dap/dap-config.php")) include_once ($lldocroot . "/dap/dap-config.php");
	
	if(file_exists(dirname(__FILE__)."/../../../dap-config.php")){
  		include_once (dirname(__FILE__)."/../../../dap-config.php");
 	}
	/*
	if( !Dap_Session::isLoggedIn() ) { 
		//send viewer to login page
		exit;
	}
	else if( Dap_Session::isLoggedIn() ) { 
		//get userid
		$session = Dap_Session::getSession();
		$user = $session->getUser();
		$user = Dap_User::loadUserById($user->getId()); //reload User object
		if(!isset($user)) {
			//send viewer to login page
			exit;
		} 
	}
	*/
	$admin_email = $_SESSION['invadminemail'];
	if(!isset($admin_email) && $admin_email == "") {
		$admin_email = Dap_Config::get("ADMIN_EMAIL");
	}
	
	$companyname = $_SESSION['invcompanyname'];
	if(!isset($companyname) && $companyname == "") {
		$companyname = Dap_Config::get("SITE_URL_DAP");
	}
	$companyurl = $_SESSION['invcompanyurl'];
	if(!isset($companyurl) && $companyurl == "") {
		$companyurl = Dap_Config::get("SITE_URL_DAP");
	}
	 //$product = Dap_Product::loadProduct($_SESSION['invpid']);
	 $product = Dap_Product::loadProduct($_REQUEST["productId"]);
	 
	 if($product) {
		$itemName=$product->getName();
		$description=$product->getDescription();
	 }		
//	logToFile("loadUserByEmail: productId=".$_REQUEST["productId"],LOG_DEBUG_DAP);
//	logToFile("loadUserByEmail: description=".$description,LOG_DEBUG_DAP);
	
//	logToFile("loadUserByEmail: logo=".$_SESSION['invlogo'],LOG_DEBUG_DAP);

	// get the HTML
    ob_start();
	$template=$_SESSION['invinvoicetemplate'];
	if($template ==""){
		$template=$_REQUEST['invoicetemplate'];
	}
	$invblogpath=$_SESSION['invblogpath'];
	if($invblogpath ==""){
		$invblogpath=$_REQUEST['blogpath'];
	}
	 
	$lldocroot = defined('SITEROOT') ? SITEROOT : $_SERVER['DOCUMENT_ROOT'];
	$fulltemplate=$invblogpath."/wp-content/plugins/DAP-WP-LiveLinks/includes/transactions/invoices/".$template."/invoicetemplate.html";
	$cssurl = $invblogpath."/wp-content/plugins/DAP-WP-LiveLinks/includes/transactions/invoices/".$template."/style.css";
		
    //include(dirname(__FILE__).'/res/exemple01.php');
    //include($blogpath. "/invoices/".$_REQUEST['invtemplate']."/invoicetemplate.html");
	$fullcustominvoicehtml =  $invblogpath."/wp-content/plugins/DAP-WP-LiveLinks/includes/transactions/invoices/".$template."/custominvoicetemplate.html";   
	if(file_exists($fullcustominvoicehtml)) {
		 include($fullcustominvoicehtml);
	}
	else
	{
		 include($fulltemplate);
	}
	
	$temp_content = ob_get_clean();
	//String replace for merge tags//
	$current_msg=$temp_content;
	
	 
	if( $_REQUEST['city'] != ""){
		$_REQUEST['city'] = $_REQUEST['city'].",";
	}
	if( $_REQUEST['state'] != ""){
		$_REQUEST['state'] = $_REQUEST['state'].",";
	}
	 
 	$current_msg = str_replace( '%%TRANSACTIONID%%', $_REQUEST['transactionId'], $current_msg); 
	$current_msg = str_replace( '%%EMAIL_ID%%', $_REQUEST['email'], $current_msg); 
	$current_msg = str_replace( '%%PAYMENTTYPE%%', "Payment: " . $_REQUEST['ptype'], $current_msg); 
	$current_msg = str_replace( '%%ITEMNAME%%', $itemName, $current_msg); 
	$current_msg = str_replace( '%%AMOUNT%%', $_REQUEST['amount'], $current_msg);
	$current_msg = str_replace( '%%FIRST_NAME%%', $_REQUEST['fname'], $current_msg);
	$current_msg = str_replace( '%%LAST_NAME%%', $_REQUEST['lname'], $current_msg);
	$current_msg = str_replace( '%%ADDRESS1%%', str_replace('+', ' ', $_REQUEST['address1'].'<br/>'), $current_msg);
	$current_msg = str_replace( '%%CITY%%', str_replace('+', ' ', $_REQUEST['city']), $current_msg);
	$current_msg = str_replace( '%%STATE%%', str_replace('+', ' ', $_REQUEST['state']), $current_msg);
	$current_msg = str_replace( '%%ZIP%%', str_replace('+', ' ', $_REQUEST['zip']), $current_msg);
	$current_msg = str_replace( '%%COUNTRY%%', $_REQUEST['country'], $current_msg);
	$companyname = str_replace('http://', "", $companyname);
	$current_msg = str_replace( '%%COMPANYNAME%%', $companyname, $current_msg); 
	$current_msg = str_replace( '%%COMPANYURL%%', $companyurl, $current_msg);
	$current_msg = str_replace( '%%ADMINEMAIL%%', $admin_email, $current_msg);  
	$current_msg = str_replace( '%%logo%%', $_REQUEST['logo'], $current_msg);
	$current_msg = str_replace( '%%TRANSACTIONDATE%%', $_REQUEST['tdate'], $current_msg);
	$current_msg = str_replace( '%%CURRENCY%%', $_REQUEST['currency'], $current_msg);
	$current_msg = str_replace( '%%DESCRIPTION%%', strip_tags($description), $current_msg);
	
	$content = $current_msg; 
	//$content .= '<style>'.file_get_contents($cssurl).'</style>';
	// Include the main TCPDF library (search for installation path).
	require_once('tcpdf_include.php');

	// create new PDF document
	$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	
	//set title of the page
	$pdf->SetTitle("Invoice from ".$companyname);
 
	// set default header data
	$pdf->SetHeaderData('', '', 'INVOICE' , '');

	// set header and footer fonts
	$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
	$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

	// set default monospaced font
	$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

	// set margins
	$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
	$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
	$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

	// set auto page breaks
	$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

	// set image scale factor
	//$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

	// set some language-dependent strings (optional)
	if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
		require_once(dirname(__FILE__).'/lang/eng.php');
		$pdf->setLanguageArray($l);
	}

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

 try
    {
		// set font
		 $pdf->SetFont('helvetica', '', 12);
	  //$pdf->SetFont('dejavusans', '', 11);

		// add a page
		$pdf->AddPage();
		
	 	// set cell padding
		//$pdf->setCellPaddings(2, 2, 2, 2);

		// set cell margins
		//$pdf->setCellMargins(1, 1, 1, 1);		
		
		
		// output the HTML content
		$pdf->writeHTML($content, true, false, true, false, '');	 
		
		//Close and output PDF document
		$pdf->Output('example_01.pdf', 'I');		
		
	} catch(Exception $e) {
		echo $e;
        exit;
	}

 
