<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

require_once('lib/tcpdf/tcpdf.php');
include_once("config/Tagger.php");
include_once("config/Logger.php");

class MYPDF extends TCPDF
{
	var $filename;
	var $imageFileName;
	var $signingFileName;
	var $nbPageBreak;
	var $dateFormat;
	var $duplicateLease;
	var $dl = false;
	var $output;
	var $leaseContentTitle;
	var $leaseContentSubTitle;
	var $leaseContentInitials;
	var $leaseContentPage;
	var $leaseContentSigningHeader;
	var $leaseContentSigningTenant;
	var $leaseContentSigningLandlord;
	var $leaseContentSigningTenant2;

	function __construct() {
		parent::__construct();
		// set document information
		$this->SetCreator(PDF_CREATOR);
		$this->SetAuthor('Cogites - eReserv');
		// $this->SetTitle("");
		// $this->SetSubject('');
		// $this->SetKeywords('TCPDF, PDF, example, test, guide');

		// set image scale factor
		$this->setImageScale(PDF_IMAGE_SCALE_RATIO);
		$this->dateFormat = "Y-m-d";
		$this->output = "D";
	}

	function SetLeaseContentTitle($val){
		$this->leaseContentTitle = $val;
	}
	function SetLeaseContentSubTitle($val){
		$this->leaseContentSubTitle = $val;
	}
	function SetLeaseContentInitials($val){
		$this->leaseContentInitials = $val;
	}
	function SetLeaseContentPage($val){
		$this->leaseContentPage = $val;
	}
	function SetLeaseContentSigningHeader($val){
		$this->leaseContentSigningHeader = $val;
	}
	function SetLeaseContentSigningTenant($val){
		$this->leaseContentSigningTenant = $val;
	}
	function SetLeaseContentSigningLandlord($val){
		$this->leaseContentSigningLandlord = $val;
	}
	function SetLeaseContentSigningTenant2($val){
		$this->leaseContentSigningTenant2 = $val;
	}
	function SetPageBreak($pageBreak){
		$this->nbPageBreak = $pageBreak;
	}
	function SetFilename($fname){
		$this->filename = $fname;
	}
	function SetImagefilename($fname){
		$this->imageFileName = $fname;
	}
	function SetSigningfilename($fname){
		$this->signingFileName = (file_exists($fname)) ? $fname : NULL;
	}
	function SetDateFormat($date){
		$this->dateFormat = $date;
	}
	function SetDuplicateLease($val){
		$this->duplicateLease = $val;
	}
	function SetOutput($output){
		$this->output = $output;
	}

	function BuildContent($arrLease, $action){
		$tagger = new Tagger();
		

		$this->AddPage();
		$this->Ln(30);
		foreach ($arrLease as $lease) {
			if ($lease->header == "<SAUT_PAGE>") {
				$this->AddPage();
				$this->Ln(30);
			}
			elseif ($lease->header == "<SIGNATURE>") {
				$this->TableSignature();
			} else {
				$this->TableHeader($tagger->tag($lease->header, $action));
				$this->TableLineFirstLine("");
				$this->TableLine($tagger->tag($lease->content, $action));
				$this->TableLine("");
				$this->TableEnd();
			}
		}
		$this->TableSignature();
		// lease duplication
		if ($this->duplicateLease) {
			$this->dl = true;
			$x = $this->getNumPages();
			for ($i=1;$i<=$x;$i++){
				$this->copyPage($i);
			}
		}
		if (file_exists ($this->filename)){
			unlink($this->filename);
		}

		$this->Output($this->filename, $this->output);
	}

	function Header()
	{
		$tagger = new Tagger();

		//Arial bold 15
		$this->SetFont('','B',15);
		$this->SetDrawColor(128,0,0);

		//Logo
		if (file_exists($this->imageFileName)){
			$this->Image($this->imageFileName,10,5);
		}
		$this->Ln(10);
		if (file_exists($this->imageFileName)){
			$this->Cell(90,10,'',0,0,'C');
			$this->Cell(90,10,$tagger->tag($this->leaseContentTitle, ""),1,0,'C');
			if (!empty($this->leaseContentSubTitle)) {
				$this->Ln(10);
				$this->Cell(90,10,'',0,0,'C');
				$this->SetFont('','',12);
				$this->Cell(90,10,$this->leaseContentSubTitle,0,0,'C');
			}
		} else {
			$this->Cell(180,10,$tagger->tag($this->leaseContentTitle, ""),1,0,'C');
			if (!empty($this->leaseContentSubTitle)) {
				$this->Ln(10);
				$this->SetFont('','',12);
				$this->Cell(180,10,$this->leaseContentSubTitle,0,0,'C');
			}
		}
	}
	function Footer()
	{
		//Position at 1.5 cm from bottom
		$this->SetY(-15);
		//Arial italic 8
		$this->SetFont('','I',8);

		// Do not print the left block on last page
		if ($this->getPage() != ($this->nbPageBreak+1)) {
			$this->Cell(40,10,$this->leaseContentInitials,1,0,'L');
		}
		$this->Cell(0,10,$this->leaseContentPage . "  " . $this->getPage() . '/' . ($this->nbPageBreak+1),0,0,'C');
		$this->Cell(0,10,date($this->dateFormat),0,0,'R');
	}
	function TableHeader($header)
	{
		//Colors, line width and bold font
		$this->SetFillColor(150,255,155);
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0,0,0);
		$this->SetDrawColor(128,0,0);

		$this->SetLineWidth(.3);
		$this->SetFont('','B',10);
		//Header
		$this->Cell(180,7,$header,1,0,'L',true);
		$this->Ln();
	}
	function TableLine($data)
	{
		//Color and font restoration
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('','',8);
		//Data
		$this->MultiCell(180,3,$data,'LR','L');
	//	$this->Ln();
	}
	function TableLineFirstLine($data)
	{
		//Color and font restoration
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('','',10);
		//Data
		$this->MultiCell(180,3,$data,'LR','L');
	//	$this->Ln();
	}
	function TableSignature()
	{
		//Colors, line width and bold font
		$this->SetFillColor(224,235,255);
		$this->SetTextColor(0);
		$this->SetFont('','',10);

		$this->SetLineWidth(.3);
		$this->SetFont('','I');
		//Header
		$this->Cell(180,10,$this->leaseContentSigningHeader,1,0,'L',true);
		$this->Ln();

		//Data
		$this->Cell(90,5,$this->leaseContentSigningTenant,'LTR',0);
		$this->Cell(90,5,$this->leaseContentSigningLandlord,'LTR',1);
		$this->SetFont('','',6);
		$this->Cell(90,3,$this->leaseContentSigningTenant2,'LR',0);
		$this->Cell(90,3,'','LR',1);
		$this->Cell(90,15,'','LR',0);
		// $this->SetX(110);
		if (!empty($this->signingFileName)){
			$this->Image($this->signingFileName, '103.5', '', '', '', '', '', 'T', false, 300, '', false, false, 0, false, false, false);
			$this->Cell(30,15,'','R',1);
		}else{
			$this->Cell(90,15,'','LR',1);
		}
		
		
		$this->Cell(180,0,'','T');
		$this->Ln(10);
		// $this->SetXY(110, 213);
		// $this->Image($this->signingFileName, '', '', '', '', '', '', 'T', false, 300, '', false, false, 0, false, false, false);
	}
	function TableEnd(){
		$this->Cell(180,0,'','T');
		$this->Ln(10);
	}
}

?>