<?php

/*

 osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License 
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );

	require('includes/application_top.php');
	define('FPDF_FONTPATH','font/');
	require('includes/fpdf.php');
	$customer_id=isset($HTTP_GET_VARS["cID"])?$HTTP_GET_VARS["cID"]:0;
	$customer_ids='0';
	if ($customer_id!=""){
		$customer_id_splt=split(",",$customer_id);
		$customer_ids="'" . join("','",$customer_id_splt) . "'";
	}

	tep_db_query("update  " . TABLE_ORDERS_PRODUCTS . "  set idcards_printed='Y' where orders_id in(" .$customer_ids. ")");
	$found_results=false;
	$unit=72/25.4;
	
	// get the template details if not found use default template
	$template_query=tep_db_query("SELECT template_width,template_height,template_content from " . TABLE_GENERAL_TEMPLATES . " where template_type='ICD'");
	
	if (tep_db_num_rows($template_query)>0) {
		$template_result=tep_db_fetch_array($template_query);
	} else {
		$template_result=getDefaultTemplate();
	}

	// split into invidual elements
	$template_splt=split("{}",$template_result["template_content"]);
	for ($icnt=0;$icnt<count($template_splt);$icnt=$icnt+2){
		$key=$template_splt[$icnt];
		$template[$key]=$template_splt[$icnt+1];
	}

	$ticket_width=$template_result["template_width"]*10;
	$ticket_height=$template_result["template_height"]*10;
	
	$tInfo=new objectInfo($template);
	$tInfo->member_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->member_logo_image;
	$tInfo->oscomm_logo_image=DIR_FS_CATALOG_IMAGES . $tInfo->oscomm_logo_image;


	
	// set page setup default width and height
	$pdf=new FPDF("P","mm",array($ticket_width,$ticket_height));

	
	$pos_left=5;
	$pos_top=5;
	$pos_width=$ticket_width;
	$pos_height=$ticket_height;
	$content=array();

	// get the ticket details to be shown
	//$customer_query=tep_db_query("SELECT customers_id,customers_firstname,customers_lastname from " . TABLE_CUSTOMERS . " where customers_id in(" . $customer_ids . ")");
	//echo "SELECT c.customers_id,c.customers_firstname,c.customers_lastname from " . TABLE_CUSTOMERS . " c, ".TABLE_ORDERS ." o, ".TABLE_CUSTOMERS_TO_CUSTOMERS. " cc  where o.orders_id=cc.orders_id and c.customers_id=cc.customer_id and o.orders_id in(" . $customer_ids . ")";
	$customer_query=tep_db_query("SELECT c.customers_id,c.customers_firstname,c.customers_lastname from " . TABLE_CUSTOMERS . " c, ".TABLE_ORDERS ." o, ".TABLE_CUSTOMERS_TO_CUSTOMERS. " cc  where o.orders_id=cc.orders_id and c.customers_id=cc.customer_id and o.orders_id in(" . $customer_ids . ")");
	//$customer_query=tep_db_query("SELECT customers_id,customers_firstname,customers_lastname from " . TABLE_CUSTOMERS . " where customers_id in(" . $customer_ids . ")");
				
	if (tep_db_num_rows($customer_query)) $found_results=true;

	if (file_exists($tInfo->oscomm_logo_image)){
		$info_oscomm=getimagesize($tInfo->oscomm_logo_image);
		$info_oscomm[0]=($info_oscomm[0]/$unit)*74/100;
		$info_oscomm[1]=($info_oscomm[1]/$unit)*74/100;
		$bottom_height=$info_oscomm[1];
	}
	if (file_exists($tInfo->member_logo_image)){
		$info_member=getimagesize($tInfo->member_logo_image);
		$info_member[0]=($info_member[0]/$unit)*74/100;
		$info_member[1]=($info_member[1]/$unit)*74/100;
	}
	$bar_width=15;
	

	
	// print the ticket details
	if ($found_results){
		require("barcode/image.php");
		while($customer_result=tep_db_fetch_array($customer_query)) {
			$pdf->AddPage();

			$pdf->SetFont('Arial','B',10);
			$pos_left=5;
			$pos_top=5;

			// create bar image
			$bar_text=$customer_result["customers_id"];
			$bar_filename=DIR_FS_CATALOG_IMAGES . "id_bar_" . $customer_result["customers_id"] . ".png";
			if (!file_exists($bar_filename)){
				create_bar_image($bar_text,$bar_filename);
			}

			if (file_exists($bar_filename)){
				$info_bar=getimagesize($bar_filename);
				$info_bar[0]=$info_bar[0]/$unit*74/100;
				$info_bar[1]=$info_bar[1]/$unit*74/100;
				$bar_width=$info_bar[0];
			}
			
			// adjust for the bar image
			if ($tInfo->bar_image_position=="L"){
				$pos_left=$bar_width+2;
			} else {
				$pos_width=$ticket_width-$bar_width-2;
			}
			$content_width=$ticket_width-$bar_width-2;
			
	

			//bar image
			if (isset($info_bar)){
				if ($tInfo->bar_image_position=="L"){
					$pdf->Image($bar_filename,2,2);
				} else {
					$pdf->Image($bar_filename,$ticket_width-$bar_width-2,2);
				}
			}
			
			// print member logo
			if (isset($info_member)){
				if ($tInfo->member_logo_position=="L"){
					$pdf->Image($tInfo->member_logo_image,($tInfo->bar_image_position=="L"?$pos_left:0),0);
				} else  {
					$pdf->Image($tInfo->member_logo_image,($tInfo->bar_image_position=="L"?$pos_width-$info_member[0]:$pos_width-$info_member[0]-2),0);
				}
			}
			
			

		
			// conditions
			$pdf->SetFont('Arial','B',8);
			if (!isset($cond_splt)){
				$cond_height=0;
				$cond_splt=split("\n",$tInfo->event_condition_content);
				$cond_height=count($cond_splt)*3;
				$cond_width=0;
				$temp_width=0;
				for ($icnt=0;$icnt<count($cond_splt);$icnt++){
					$temp_width=$pdf->GetStringWidth($cond_splt[$icnt]);
					if ($temp_width>$cond_width) $cond_width=$temp_width;
				}
			}
			
			// print osCommRes
			$temp_left=0;
			$temp_right=$ticket_width-$info_oscomm[0];
			if (isset($info_oscomm)){
				if ($tInfo->oscomm_logo_position=="L"){
					$pdf->Image($tInfo->oscomm_logo_image,$temp_left,$ticket_height-$info_oscomm[1]);
					$temp_left+=$info_oscomm[0];
				} else  {
					$pdf->Image($tInfo->oscomm_logo_image,$temp_right,$ticket_height-$info_oscomm[1]);
				}
			}
			
			$temp_top=$ticket_height-2-$cond_height;
			if ($tInfo->event_condition_position=="L"){
				$cond_left=$temp_left+2;
			} else {
				$cond_left=$temp_right-2-$cond_width;
			}
			for ($icnt=0;$icnt<count($cond_splt);$icnt++){
				$pdf->Text($cond_left,$temp_top,$cond_splt[$icnt]);
				$temp_top+=3;
				$bottom_height1+=3;
			}

			$pdf->SetFont('Arial','',8);
			// full condition
			if ($bottom_height>$bottom_height1){
				$temp_height=$bottom_height;
			} else {
				$temp_height=$bottom_height1;
			}
			$temp_left=$pos_left;
			$temp_top=20+$name_height;
			$content=$tInfo->event_condition_large;
			$word="";
			$current_width=0;
			for ($icnt=0;$icnt<strlen($content);$icnt++){
				$char=substr($content,$icnt,1);
				if ($char==" " || $char=="\n"){
					if ($current_width+$pdf->GetStringWidth($word)>$content_width-8){
						$temp_left=$pos_left;
						$temp_top+=3;
						$current_width=0;
					} else if ($char!="\n") {
						$word.=$char;
					}
					$pdf->Text($temp_left,$temp_top,$word);
					if ($char!="\n") {
						$temp_left+=$pdf->GetStringWidth($word);
						$current_width+=$pdf->GetStringWidth($word);
					} else {
						$temp_left=$pos_left;
						$temp_top+=3;
						$current_width=0;
					}
					$word="";
				}  else {
					$word.=$char;
				}
			}
			if ($word!=""){
				if ($current_width+$pdf->GetStringWidth($word)>$content_width-8){
					$temp_left=$pos_left;
					$temp_top+=3;
					$current_width=0;
				}
				$pdf->Text($temp_left,$temp_top,$word);
			}
			$pdf->SetFont('Arial','B',12);
			// customer name
			$content=$tInfo->customer_details_content;
			$details=array(	TEXT_FN=>$customer_result["customers_firstname"],
							TEXT_LN=>$customer_result["customers_lastname"]
							);
			reset($details);
			while(list($key1,$value1)=each($details)){
				$content=eregi_replace("%%" . $key1 . "%%",$value1,$content);
			}
			$name_splt=split("\n",$content);
			$temp_width=0;
			$name_width=0;
			$name_height=0;
			for ($icnt=0;$icnt<count($name_splt);$icnt++){
				$temp_width=$pdf->GetStringWidth($name_splt[$icnt]);
				if ($temp_width>$name_width) $name_width=$temp_width;
			}
			$temp_top=13;
			$temp_left=$pos_left+($content_width-$temp_width)/2;
			for ($icnt=0;$icnt<count($name_splt);$icnt++){
				$pdf->Text($temp_left,$temp_top,$name_splt[$icnt]);
				$temp_top+=5;
				$name_height+=5;
			}
		}
	} else {
		$pdf->AddPage();
		$pdf->SetFont('Arial','B',10);
		$pdf->Text(5,5,"No Details found");
	}
	$idcart_filename=sprintf("id_card_%s_%s",$login_id,time());
	$pdf->output(DIR_FS_CATALOG . "images/".$idcart_filename.".pdf");



	tep_redirect(DIR_WS_CATALOG . "images/".$idcart_filename.".pdf");

	function &getDefaultTemplate(){
		$template_result=array(	"template_width"=>9.0,
								"template_height"=>5.5,
								"template_content"=>"member_logo_image{}oscommres_micro_logo.png{}member_logo_position{}L{}customer_details_content{}%%First_Name%% %%Last_Name%%{}oscomm_logo_image{}oscommres_micro_logo.png{}oscomm_logo_position{}R{}event_condition_content{}{}event_condition_position{}L{}event_condition_large{}Card is valid for one until last class date. No refunds for unused classes. Make-up any missed classes before last class date. Renew card ph 12345678, oscommres.com{}bar_image_position{}R"
								);
		return $template_result;
	}
?>