<?php
//============================================================+
// File name   : certificate.php
// Begin       : 2009-04-16
// Last Update : 2013-05-14
//
// Description : Example 051 for TCPDF class
//               Full page background
//
// Author: Nicola Asuni
//
// (c) Copyright:
//               Nicola Asuni
//               Tecnick.com LTD
//               www.tecnick.com
//               info@tecnick.com
//============================================================+

/**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: Full page background
 * @author Nicola Asuni
 * @since 2009-04-16
 */
 

$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");
}
 
$user_id = $_REQUEST['user_id'];

if($user_id != ""){
	$user = Dap_User::loadUserById($user_id); //reload User object
	if(!isset($user)) {
		//send viewer to login page
		exit;
	} 
}else{
	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;
		} 
	}
}

// get the HTML
ob_start();
//get/set variables for certificate	 
$productId = $_REQUEST['productId'];
$certificate_id = $_REQUEST['certificate_id'];
$product_name = '';
$certificate_html = '';
$admin_name = '';
$logo = '';
$width_selected = '1200';
$logo_width = '240';
$logo_height = '180';
$logo_width1 = '240px';
$logo_height1 = '180px';
$date = date("m-d-Y");
$fname = $user->getFirst_Name();
$lname = $user->getLast_Name();
$username = $fname." ".$lname;
$product_obj = Dap_Product::loadProduct($productId); 
if($product_obj) {
	$product_name = $product_obj->getName();
}
 
//load Certificate by id
$certificate_obj = Dap_Certificate::loadById($certificate_id);
if($certificate_obj) {
	$certificate_html =$certificate_obj->getTemplateHtml();		 
	$admin_name = $certificate_obj->getAdminName();	
	$style_attrbutes = $certificate_obj->getCustomizerValues();   
	$style_attrbutes = explode("||",$style_attrbutes);
	$width_selected =  $style_attrbutes[0];
	$logo_width_old =  $style_attrbutes[1];
	$logo_height_old = $style_attrbutes[2];	
	if($logo_width_old ==""){
		$logo_width_old = '240';		 
	}	
	if($logo_height_old==""){		 
		$logo_height_old = '180';
	}	
	$logo_width_new = $logo_width_old-10;  
	$logo_height_new = $logo_height_old-10; 
	$logo_width1 = $logo_width_new."px";  
	$logo_height1 = $logo_height_new."px"; 
	$signature_img = $certificate_obj->getSignatureImg();
	$logo = $certificate_obj->getLogoImg();
	if($admin_name ==""){
		 $admin_name = '<img src="'.$signature_img.'" alt="Signature-img"   width="200px" height="40px" style="width:200px;height:40px">';		
	}
	
} 

 
$logo_width = ' width="'.$logo_width1 .'" ';
$logo_height = ' height="'.$logo_height1 .'" ';	 
$certificate_html = str_replace( '%%NAME%%', $username, $certificate_html); 
$certificate_html = str_replace( '%%COURSE%%', $product_name, $certificate_html); 
$certificate_html = str_replace( '%%DATE%%', $date, $certificate_html);
$certificate_html = str_replace( '%%ADMIN_NAME%%', $admin_name, $certificate_html);	
$certificate_html = str_replace( '%%imgwidth%%', $logo_width, $certificate_html);
$certificate_html = str_replace( '%%imgheight%%', $logo_height, $certificate_html);
$certificate_html = str_replace( '%%imgwidth%%=""', $logo_width, $certificate_html);	 
$certificate_html = str_replace( '%%imgheight%%=""', $logo_height, $certificate_html);	
$certificate_html = str_replace( '%%IMGWIDTH%%', $logo_width, $certificate_html);	
$certificate_html = str_replace( '%%IMGHEIGHT%%', $logo_height, $certificate_html);
 

 
 
$siteurl = SITE_URL_DAP;
//$cssurllink =  '<link rel="stylesheet" href="'.$siteurl.'/dap/admin/templates/certificates/template1/template1.css">';
//echo $cssurllink.$certificate_html;die;
$cssurl =  $siteurl.'/dap/admin/templates/certificates/template1/template1.css';

$temp_content = ob_get_clean();	 
$style_css = '<style>'.file_get_contents($cssurl).'</style>';
$temp_content = $style_css;
$temp_content = $temp_content.$certificate_html;
	
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
global $wid ;
global $height ;
global $img_path ;

$img_path =  $siteurl.'/dap/admin/templates/certificates/image/certificates1.jpg';
if($width_selected =="900"){
	$wid = 210;				
	$height = 170;			 
}elseif($width_selected =="1000"){
	$wid = 230;				
	$height = 180;	
}elseif($width_selected =="1100"){
	$wid = 250;				
	$height = 210;		
}else{	
	$wid = 297;				
	$height = 200;			 
}
		
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
	
	//Page header
	public function Header() {
		global $wid ;
		global $height ;
		global $img_path ;
		// get the current page break margin
		//$bMargin = $this->getBreakMargin();
		
		// get current auto-page-break mode
		$auto_page_break = $this->AutoPageBreak;
		// disable auto-page-break
		$this->SetAutoPageBreak(false, 0);
		
		// set bacground image			 
		$img_file = $img_path;
		 
		//for the width
		$this->Image($img_file, 0, 0, $wid, $height, '', '', '', false, $wid, '', false, false, 0);
 
		// restore auto-page-break status
		//$this->SetAutoPageBreak($auto_page_break, $bMargin);
		// set the starting point for the page content
		$this->setPageMark();
	}
}

// create new PDF document
 //$pdf = new MYPDF("P", PDF_UNIT,  PDF_PAGE_FORMAT, true, 'UTF-8', false); 
//for the width
if($width_selected =="900"){
	$pdf = new MYPDF("L", PDF_UNIT,  array(210, 170),  true, 'UTF-8', false);
	
}else if($width_selected =="1000"){
	$pdf = new MYPDF("L", PDF_UNIT,  array(230, 180),  true, 'UTF-8', false);
	
}else if($width_selected =="1100"){
	$pdf = new MYPDF("L", PDF_UNIT,  array(250, 180),  true, 'UTF-8', false);
	
}else{
	$pdf = new MYPDF("L", PDF_UNIT,  array(297, 200),  true, 'UTF-8', false);
	//$pdf = new MYPDF("L", PDF_UNIT,  "A4",  true, 'UTF-8', false);
}

// set document information
$pdf->SetCreator(PDF_CREATOR);
//$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('Certificate');
//$pdf->SetSubject('TCPDF Tutorial');
//$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

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

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

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, "10", PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0); 
 
// remove default footer
$pdf->setPrintFooter(false);

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

//remove the space
$tagvs = array(
 'p' => array(0 => array('n' => 0, 'h' => ''), 1 => array('n' => 0, 'h' => ''))
 );
$pdf->setHtmlVSpace($tagvs);

// set image scale factor
//for the width
if($width_selected =="900"){
	$pdf->setImageScale(1.35);
}elseif($width_selected =="1000"){
	$pdf->setImageScale(1.3);
}elseif($width_selected =="1100"){
	$pdf->setImageScale(1.2);
}else{
	$pdf->setImageScale(1.1);
}

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

// ---------------------------------------------------------
 
// set font
//$pdf->SetFont('times', '', 48);

// add a page
$pdf->AddPage();

// Print a text
$html = $temp_content;
$pdf->writeHTML($html, true, false, true, false, '');
  
// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('example_1.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
