<?php
error_reporting(0);
function productCardNew($p,$id,$lang,$show_description=true,$show_specification=true,$price="",$symbol="",$show_price=1, $ean=1, $simage){
	
	//set languages
	if($lang=="en"){
		$features_label="Features";
		$description_label="Product Description";
		$specification_label="Specification";
		$specification_lbl="Description";
	}
	else {
		$features_label="Cechy";
		$description_label="Opis produktu";
		$specification_label="Specyfikacja";
		$specification_lbl="Opis";
	}
	//create page, add background
	$p->AddPage();
	$p->SetMargins(6,0,0,0);
	$p->SetDrawColor(0,0,0);
	$p->Image("modules/EcmProductB2Bs/card_images/background.jpg",0,0,283);
	
	//get product informations
	$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select th,product_category_id,srp_price,name,code,product_picture,packing_front_picture, ean, ean2 from ecmproducts where id='".$id."' and deleted='0'"));
	if($price=="")$price=$r['srp_price'];
	if($symbol=="")$symbol="PLN";
	$product_picture='modules/EcmProductB2Bs/upload/images/big/'.$r['product_picture'];
	if(file_exists('modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture']))$packing_front_picture='modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture'];
	$rr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select ean,long_description,short_description from ecmproduct_language where ecmproduct_id='".$id."' and deleted='0' and lower(language)='".$lang."'"));
	$ld=str_replace("&lt;","<",str_replace("&gt;",">",$rr['long_description']));
	if($lang=="en")$r['name']=$rr['short_description'];
	if(!$rr['ean']){
		$rrr=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select ean from ecmproduct_language where ecmproduct_id='".$id."' and language='pl'"));
		$rr['ean']=$rrr['ean'];
	}
	
	if ($ean==2) {
		$r['ean'] = $r['ean2'];
	}
	


	
	//image border
	$p->SetLeftMargin(120);
	//$p->Image("modules/EcmProductB2Bs/card_images/border.jpg",100,40,160, 130);
	
	//if(file_exists($product_picture) && is_file($product_picture))$p->Image($product_picture,200,100,50);
	
	//product name
	$p->SetFont("arialpl","B",18);
	$p->SetTextColor(0,148,225); 
	$p->SetXY(15,45);
	$p->Multicell(110,8,prepareName($r['name']), 0, "L");
	$p->SetTextColor(0,0,0); 
	
	$cur_x = 70;
	
	//Index + EAN
	//insert red
	$p->Image("modules/EcmProductB2Bs/card_images/red.jpg", 5+10,$cur_x, 2,10);
	//index
	$p->SetFont("arialpl","B",9);
	$p->SetXY(8+10, $cur_x);
	$p->Cell(15,6,"INDEX:",0,0,'L',1);
	$p->SetFont("arialpl","",9);
	$p->Cell(40,6,$r['code'],0,1,'L',1);
	
	//ean
	$p->SetFont("arialpl","B",9);
	$p->SetXY(8+10, $cur_x+5);
	$p->Cell(15,6,"EAN:",0,0,'L',1);
	$p->SetFont("arialpl","",9);
	$p->Cell(40,6,$r['ean'],0,1,'L',1);
	
	$cur_x = 85;
	
	//opis
	$text=blockToPrint($description_label,$ld);
	$lines = 0;
	foreach($text as $tt){
		$tt=str_replace("&bull;&nbsp;","- ",$tt);
		$tt=str_replace("&quot;",'"',$tt);
		$tt=str_replace("&nbsp;"," ",$tt);
		//$ttext[]=wordwrap($tt,110,"\n  ");
		$t.= "\n".$tt;
		$lines++;
	}
	$t=trim($t);
	$t=iconv("UTF-8","ISO-8859-2",$t);
	
	//$t = "select ean,long_description,short_description from ecmproduct_language where ecmproduct_id='".$id."' and deleted='0' and lower(language)='".$lang."'";
		
	//blue point
	$p->SetXY(8+10,$cur_x+5);
	$p->Image("modules/EcmProductB2Bs/card_images/blue.jpg", 5+10,$cur_x, 2,2);

	$p->SetXY(8+10,$cur_x-2);
	$p->SetFont("arialpl","B",9);
	$p->Cell(200,6,$description_label);
	$p->SetXY(8+10,$cur_x+5);
	$p->SetFont("arialpl","",9);
	$p->MultiCell(110,4,$t, 0, "L");
	
	//specyfikacja
	$cur_x = $p->GetX();
	
	$text=blockToPrint($specification_label,$ld);
	$t='';
	foreach($text as $tt){
		$tt=str_replace("&bull;&nbsp;","- ",$tt);
		$tt=str_replace("&quot;",'"',$tt);
		$tt=str_replace("&nbsp;"," ",$tt);
		//$ttext[]=wordwrap($tt,110,"\n  ");
		$t.= "\n".$tt;
	}
	
	$price = $lines;
	$t=trim($t);
	$t=iconv("UTF-8","ISO-8859-2",$t);
		
	//blue point
	$p->SetXY(8+10,$cur_x+5);
	$p->Image("modules/EcmProductB2Bs/card_images/blue.jpg", 5+10,$cur_x, 2,2);

	$p->SetXY(8+10,$cur_x-2);
	$p->SetFont("arialpl","B",9);
	$p->Cell(200,6,$specification_label);
	$p->SetXY(8+10,$cur_x+5);
	$p->SetFont("arialpl","",9);
	$p->MultiCell(110,4,$t, 0, "L");
	
	//product main picture
	$p->SetLeftMargin(147);
		

	if ($show_price==1) {
	$p->SetXY(235,15);
	$p->SetFont("arialpl","B",18);
	$p->Cell(50,6,$price.' '.$symbol);
	}

	//get image size
	//$size = getimagesize($product_picture);
	if(file_exists($product_picture) && is_file($product_picture))
		$p->Image($product_picture,120,42,130);
	
	return $p;
}

function blockToPrint($name,$str){
	$str=str_replace("&nbsp;"," ",$str);
	$str=str_replace("<ul> ","<ul>",$str);
	$str=str_replace("<ul></ul>","",$str);
	$str=str_replace("<ul> </ul>","",$str);
	$numc=99999;
	$e=explode("<strong>".trim($name),$str);
	$ee=explode("<strong>",$e[1]);
	if(!$ee[0])$ee=explode("</div>",$e[1]);
	$e=$ee;
	$e=explode("</strong>",$e[0]);
	$t=$e[1];
	$p=explode("<p>",$t);
	$p=explode("</p>",$p[1]);
	$p=$p[0];
	$u=explode("<ul>",$t);
	$u=explode("</ul>",$u[1]);
	$u=$u[0];
	$u=str_replace("&nbsp;"," ",$u);
	$u=str_replace("<li>","",str_replace("</li>","",$u));
	$l=explode("
",$u);
	$p=str_replace("
","",$p);
	if($p){
		$tt=wordwrap(strip_tags($p),$numc,"\n");
		$ee=explode("\n",$tt);
		if(count($ee)>0){
			foreach($ee as $e){
				$text[]=$e;
				$cc++;
			}
		}
	}
	if(count($l)>0){
		foreach($l as $list){
			if(!$list || $list==" " || strlen(strip_tags($list))<2)continue;
			if(eregi("<span>",$list))$tt="".wordwrap(str_replace("<span>","",str_replace("</span>","",$list)),34,"\n&nbsp;&nbsp;&nbsp;");
			else $tt="&bull;&nbsp;".wordwrap($list,$numc,"\n&nbsp;&nbsp;&nbsp;");
			$ee=explode("\n",$tt);
			foreach($ee as $e){
				$text[]=$e;
			}
		}
	}
	return $text;
}

function prepareName($t){
	$a1=array("&quot;","a","ą","b","c","ć","d","e","ę","f","g","h","i","j","k","l","ł","m","n","ń","o","ó","p","q","r","s","ś","t","u","v","w","x","y","z","ż","ź");
	$a2=array('"',"A","Ą","B","C","Ć","D","E","Ę","F","G","H","I","J","K","L","Ł","M","N","Ń","O","Ó","P","Q","R","S","Ś","T","U","V","W","X","Y","Z","Ż","Ź");
	$t=str_replace($a1,$a2,$t);
	return iconv("UTF-8","ISO-8859-2",$t);
}
?>