<?php
set_time_limit(99999);
error_reporting(0);
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=80;
	$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="&nbsp;&nbsp;".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 printCatalogue($p,$id,$lang,$show_characteristic,$show_description,$show_specification){
	if($_REQUEST['action']=="createCatalogue")$_GET['print_version']="true";
	$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select product_picture,packing_front_picture,name,code,description_card,specification_card,characteristic_card,used_to_card,image_card from ecmproducts where id='".$id."'"));
	$product_picture='modules/EcmProductB2Bs/upload/images/big/'.$r['product_picture'];

	$bag1='modules/EcmProductB2Bs/bag1.png';
	$bag2='modules/EcmProductB2Bs/bag2.png';

	if($lang=="en"){
		$features_label="Features";
		$description_label="Product Description";
		$specification_label="Specification";
	}
	else {
		$features_label="Cechy";
		$description_label="Opis produktu";
		$specification_label="Specyfikacja";
	}

	$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']; 
	}

	$left=150;

	//header
	$start=$p->GetY();
	$y=$p->GetY();
	$p->SetFillColor(10,10,104); 
	$p->SetTextColor(255,255,255); 
	$p->Cell(190,22,"",0,1,'L',1);
	$p->SetY($y);
	$p->SetFont("arialpl","B",14);
	$p->Cell($left,10,iconv("UTF-8","ISO-8859-2",$r['name']),0,1,'L',1);
	$p->SetFont("arialpl","",8);
	$p->Cell($left,6,"INDEX: ".$r['code'],0,1,'L',1);
	$p->Cell($left,6,"EAN: ".$rr['ean'],0,1,'L',1);
	
	$p->SetFont("arialpl","",7);
	if($show_characteristic==true){
		$p->SetXY($left,$y);
		$text=blockToPrint($features_label,$ld);
		if(count($text)>0){
			foreach($text as $t){
				$p->SetX($left);
				$t=trim($t);
				$t=iconv("UTF-8","ISO-8859-2",$t);
				$t=str_replace("&bull;&nbsp;","- ",$t);
				$t=str_replace("&quot;",'"',$t);
				$t=str_replace("&nbsp;"," ",$t);
				$t=str_replace("\\n","",$t);
				$t=str_replace("\\l","",$t);
				$t=str_replace("\\r","",$t);
				$t=str_replace("
","",$t);
				$p->Cell(50,6,$t,0,0,'L',1);
				$p->Ln(5);
			}
		}
	}

	$y=$p->GetY();
	$p->Image($bag1,10,$start+16,190);
	$p->Ln();
	$y=$p->GetY();

	$p->SetFillColor(255,255,255); 
	$p->SetTextColor(0,0,0); 

	if($show_description==true){
		$text=blockToPrint($description_label,$ld);
		if(count($text)>0){
			foreach($text as $t){
				$t=trim($t);
				$t=iconv("UTF-8","ISO-8859-2",$t);
				$t=str_replace("&bull;&nbsp;","- ",$t);
				$t=str_replace("&quot;",'"',$t);
				$t=str_replace("&nbsp;"," ",$t);
				$t=str_replace("\\n","",$t);
				$t=str_replace("\\l","",$t);
				$t=str_replace("\\r","",$t);
				$t=str_replace("
","",$t);
				$p->Cell(100,6,$t,0,0,'L',1);
				$p->Ln(5);
			}
		}
	}

	if($show_specification==true){
		$text=blockToPrint($specification_label,$ld);
		if(count($text)>0){
			foreach($text as $t){
				if($_GET['print_version']=="true" && $p->GetY()>$start+130)break;
				$t=trim($t);
				$t=iconv("UTF-8","ISO-8859-2",$t);
				$t=str_replace("&bull;&nbsp;","- ",$t);
				$t=str_replace("&quot;",'"',$t);
				$t=str_replace("\\n","",$t);
				$t=str_replace("\\l","",$t);
				$t=str_replace("\\r","",$t);
				$t=str_replace("
","",$t);
				$p->Cell(100,6,$t,0,0,'L',1);
				$p->Ln(5);
			}
		}
	}
	$yy=$p->GetY();
	if($_GET['print_version']=="true"){
		$yy=$start+130;
	}
	//image
	$p->SetX($left);
	if(file_exists($product_picture))$p->Image($product_picture,103,$y,80);
	$p->Image($bag2,10,$yy,190);

	$p->Ln(4);
	return $p;
}










function printProductCard($id=""){
if($_GET['x'])$x=$_GET['x'];
else $x=1.5;
if($id=="")$id=$_REQUEST['record'];
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select product_picture,packing_front_picture,name,code,description_card,specification_card,characteristic_card,used_to_card,image_card from ecmproducts where id='".$id."'"));


if($_GET['html']==1 && !$_GET['no_print'])$leo="leo";
else $leo="bag";

$bag1=imagecreatefrompng('modules/EcmProductB2Bs/'.$leo.'1.png');
$bag2=imagecreatefrompng('modules/EcmProductB2Bs/'.$leo.'2.png');

$file='modules/EcmProductB2Bs/upload/images/big/'.$r['product_picture'];
chmod($file,0777);
if(strstr($r['product_picture'],".jpg") || strstr($p,".jpeg"))$handle=imagecreatefromjpeg($file);
if(strstr($r['product_picture'],".gif"))$handle=imagecreatefromgif($file);
if(strstr($r['product_picture'],".png"))$handle=imagecreatefrompng($file);

if($_GET['print_version']=="true")$img_height=691*$x;
else $img_height=1100*$x;
$img=imagecreatetruecolor(1007*$x,$img_height);

if($_GET['html']!=1 && !$_GET['no_print'])header('Content-Type: image/png');

$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,10,10,104);
$grey=imagecolorallocate($img,55,55,55);

imagefill($img,0,0,$white);
list($w,$h)=getimagesize('modules/EcmProductB2Bs/'.$leo.'1.png');  
imagecopyresampled($img,$bag1,0,134*$x,0,0,($w),($h),($w),($h)); 
$ww=$w;
$hh=$h;

if($_REQUEST['language']=="en"){
	$lang="en";
	$features_label="Features";
	$description_label="Product Description";
	$specification_label="Specification";
}
else {
	$lang="pl";
	$features_label="Cechy";
	$description_label="Opis produktu";
	$specification_label="Specyfikacja";
}
imagefilledrectangle($img,0,0,$w*$x,134*$x,$black);

$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'];
}

list($w,$h)=getimagesize($file);  
imagecopyresampled($img,$handle,(1007-$w/1.7)*$x,190*$x,0,0,($w/1.7)*$x,($h/1.7)*$x,($w),($h));
$img_h=190*$x;
$img_h+=($h/1.7)*$x;

$font = 'modules/EcmProductB2Bs/fonts/dejavusans.ttf';
if($_GET['show_characteristic']=="true")$name_size=22*$x;
else $name_size=24*$x;
if(eregi("\n",wordwrap($r['name'],44,"\n")))imagettftext($img,$name_size,0,33*$x,43*$x,$white,$font,wordwrap($r['name'],44,"\n"));
else imagettftext($img,$name_size,0,33*$x,49*$x,$white,$font,wordwrap($r['name'],48,"\n"));
$font = 'modules/EcmProductB2Bs/fonts/dejavusansb.ttf';
if($_GET['show_characteristic']=="true"){
	$text=blockToPrint($features_label,$ld);
	$m=40*$x;
	if(count($text)>0)foreach($text as $t){
		imagettftext($img,12*$x,0,750*$x,$start+$m,$white,$font,$t);
		$m+=26*$x;
	}
}
$m=0;
if(eregi("\n",wordwrap($r['name'],44,"\n"))){
	$index_h=106*$x;
	$ean_h=127*$x;
}
else {
	$index_h=106*$x;
	$ean_h=127*$x;
}
$font = 'modules/EcmProductB2Bs/fonts/arial.ttf';
imagettftext($img,10*$x,0,30*$x,$index_h,$white,$font,'INDEX '.strtoupper($r['code']));

imagettftext($img,10*$x,0,30*$x,$ean_h,$white,$font,'EAN '.strtoupper($rr['ean']));


$list=str_replace("&nbsp;"," ",$list);
$list=str_replace("<li>","",str_replace("</li>","",$ee[0]));
$l=explode("
",$list);

$mm=21*$x;
$start=210*$x;
$font = 'modules/EcmProductB2Bs/fonts/arialbd.ttf';
$fontn = 'modules/EcmProductB2Bs/fonts/arial.ttf';
if($_GET['show_description']=="true"){
	//imagettftext($img,12,0,33,$start,$black,$font,"Opis produktu");
	$text=blockToPrint($description_label,$ld);
	//$m+=40;
	if(count($text)>0)foreach($text as $t){
		imagettftext($img,11*$x,0,33*$x,$start+$m,$grey,$fontn,$t);
		$m+=$mm;
	}
	$m+=$mm;
}


if($_GET['show_specification']=="true"){
	//imagettftext($img,12,0,33,$start+$m,$black,$font,"Specyfikacja");
	$text=blockToPrint($specification_label,$ld);
	//$m+=40;
	if(count($text)>0)foreach($text as $t){
		imagettftext($img,11*$x,0,33*$x,$start+$m,$grey,$fontn,$t);
		$m+=$mm;
	}
}

$pos=$start+$m;
if($_GET['print_version']=="true")$pos=691*$x-$hh;
elseif($r['packing_front_picture']!="" && file_exists('modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture'])) {
	$img_pos=$m+$start;
	//if($img_pos>1000){
		$file='modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture'];
		chmod($file,0777);
		if(strstr($r['packing_front_picture'],".jpg") || strstr($r['packing_front_picture'],".jpeg"))$handle=imagecreatefromjpeg($file);
		if(strstr($r['packing_front_picture'],".gif"))$handle=imagecreatefromgif($file);
		if(strstr($r['packing_front_picture'],".png"))$handle=imagecreatefrompng($file);
		list($w,$h)=getimagesize('modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture']); 
		//$white_picture=imagecreatetruecolor($w*$x,$h*$x);
		imagefill($handle,0,0,imagecolorallocate($handle, 255, 255, 255));
		imagecopyresampled($img,$handle,(1007-$w/1.7)*$x,(190+$h/1.7)*$x,0,0,($w/1.7)*$x,($h/1.7)*$x,$w,$h);
		$img_h+=($h/1.7)*$x;
	//}
	if($img_pos>$img_h)$pos=$img_pos;
	else $pos=$img_h+20*$x;

}
//$pos+=110;
//if($pos<700*$x)
if($_GET['print_version']=="true")$pos=640*$x+70;
else{
//if($pos>1740*$x)
$pos=1040*$x+80;
}
if($_GET['html']==1)$ww=2500;
imagecopyresampled($img,$bag2,0,$pos,0,0,$ww,$hh,$ww,$hh);
if($_GET['html']==1)$file1="modules/EcmProductB2Bs/cards/".$_REQUEST['record']."_".$_GET['show_attributes']."_".$_GET['show_characteristic']."_".$_GET['show_specification'].".png";
else $file1=null;
imagepng($img,$file1,9);

if($_GET['html']==1 && !$_GET['no_print']){
	chmod($file1,0777);
	$wn=$GLOBALS['db']->query("select * from notes where parent_id='".$_REQUEST['record']."' and parent_type='EcmProductB2Bs' and deleted='0'");
	if(file_exists('modules/EcmProductB2Bs/upload/images/big/'.$r['product_picture']))$imgs[]='modules/EcmProductB2Bs/upload/images/big/'.$r['product_picture'];
	while($rn=$GLOBALS['db']->fetchByAssoc($wn)){
			if(eregi(" kar ",$rn['name']))continue;
			if($rn['file_mime_type']=="image/jpeg" || $rn['file_mime_type']=="image/jpg" || $rn['file_mime_type']=="image/png" || $rn['file_mime_type']=="image/gif"){
				if(file_exists("cache/upload/".$rn['id'])){
					chmod("cache/upload/".$rn['id'],0777);
					$exp=explode("/",$rn['file_mime_type']);
					if($exp[1]=="jpeg")$exp[1]="jpg";
					$ext=$exp[1];
					copy("cache/upload/".$rn['id'],"modules/EcmProductB2Bs/cards/".$rn['id'].".".$ext);
					chmod("modules/EcmProductB2Bs/cards/".$rn['id'].".".$ext,0777);
					$imgs[]="modules/EcmProductB2Bs/cards/".$rn['id'].".".$ext;
				}
			}
	}
	if(file_exists('modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture']) && is_file('modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture']))$imgs[]='modules/EcmProductB2Bs/upload/images/big/'.$r['packing_front_picture'];
	foreach($imgs as $im){
		$ii++;
		$imgst.='<a href="http://e5crm.more7.com/'.$im.'" target="_blank"><img border="0" src="http://e5crm.more7.com/pic.php?p='.$im.'&w=125&h=94"></a>';
		if($ii%8==0)$imgst.='<br>';
	}
	$table='<table cellspacing="0" cellpadding="0" border="0" style="width:1007px;" align="center"><tr><td style="padding:4px;color:black;font-weight:bold;background:#FBF07A;font-size:14px;width:50%">Kontakt</td><td style="padding:4px;color:black;font-weight:bold;background:#FBF07A;font-size:14px;width:50%">Opcje dostawy</td></tr><tr><td style="padding:4px;font-size:12px;"><span style="color:#666666;font-size:14px;font-weight:bold;">Leobite</span><br>tel.: (22) 382 50 97<br>Adres e-mail: <a href="mailto:sklep@leobite.pl">sklep@leobite.pl</a><br>Skype: <a href="callto:leobitepl">leobitepl</a><br>gg: <a href="gg:35768313">35768313</a><br><br></td><td style="padding:4px;font-size:12px;">Przesyłka pocztowa <b>8,99 zł</b><br>Przesyłka pocztowa za pobraniem <b>14,99 zł</b><br>Przesyłka kurierska <b>14,99 zł</b><br>Przesyłka kurierska za pobraniem <b>24,99 zł</b><br><br></td></tr><tr><td style="padding:4px;color:black;font-weight:bold;background:#FBF07A;font-size:14px;width:50%">Zdjęcia produktu</td><td style="padding:4px;color:black;font-weight:bold;background:#FBF07A;font-size:14px;width:50%">&nbsp;</td></tr><tr><td colspan="2"><br>'.$imgst.'</td></tr></table>';
	list($iw,$ih)=getimagesize($file1);
	$nh=1007*$ih/$iw;
	if($_GET['no_print']!=1)echo '<div style="text-align:center;"><textarea style="width:600px;height:460px;" align="center">'.str_replace("<","&lt;",str_replace(">","&gt;",'<div style="text-align:center;">'.$table.'<br><img src="http://e5crm.more7.com/'.$file1.'" width="1007" height="'.$nh.'" border="0" /></div>')).'</textarea></div>';
}

}

printProductCard();
?>