<?php 
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
session_cache_limiter("none");
set_magic_quotes_runtime(0);

include("oldinclude/dbcommon.php");
include("oldinclude/##@TABLE.strShortTableName s##_variables.php");
##if @BUILDER.arrLanguages.len>1##
include("oldinclude/languages.php");
##endif##

##if @BUILDER.bCreateLoginPage##
if(!@$_SESSION["UserID"])
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Export"))
{
	echo "<p>".##message NO_PERMISSIONS##."<a href=\"login.php\">".##message BACK_TO_LOGIN##."</a></p>";
	return;
}
##endif##

$conn=db_connect();
//	Before Process event
if(function_exists("BeforeProcessExport"))
	BeforeProcessExport($conn);

$strWhereClause="";

$options = "1";
if (@$_REQUEST["a"]!="") 
{
	$options = "";
	
	$sWhere = "1=0";	

//	process selection
	$selected_recs=array();
	if (@$_REQUEST["mdelete"])
	{
		foreach(@$_REQUEST["mdelete"] as $ind)
		{
			$keys=array();
	##foreach @TABLE.arrKeyFields as @k##
			$keys["##@k s##"]=refine($_REQUEST["mdelete##@index##"][$ind-1]);
	##endfor##
			$selected_recs[]=$keys;
		}
	}
	elseif(@$_REQUEST["selection"])
	{
		foreach(@$_REQUEST["selection"] as $keyblock)
		{
			$arr=split("&",refine($keyblock));
			if(count($arr)<##@TABLE.arrKeyFields.len##)
				continue;
			$keys=array();
	##foreach @TABLE.arrKeyFields as @k##
			$keys["##@k s##"]=urldecode($arr[##eval @index-1##]);
	##endfor##
			$selected_recs[]=$keys;
		}
	}

	foreach($selected_recs as $keys)
	{
		$sWhere = $sWhere . " or ";
		$sWhere.=KeyWhere($keys);
	}

##if @TABLE.nSecOptions==ADVSECURITY_VIEW_OWN  && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
	$sWhere = whereAdd($sWhere,SecuritySQL("Search"));
##endif##

	$strSQL = gSQLWhere($sWhere);
	$strWhereClause=$sWhere;
	
	$_SESSION[$strTableName."_SelectedSQL"] = $strSQL;
	$_SESSION[$strTableName."_SelectedWhere"] = $sWhere;
}

if ($_SESSION[$strTableName."_SelectedSQL"]!="" && @$_REQUEST["records"]=="") 
{
	$strSQL = $_SESSION[$strTableName."_SelectedSQL"];
	$strWhereClause=@$_SESSION[$strTableName."_SelectedWhere"];
}
else
{
	$strWhereClause=@$_SESSION[$strTableName."_where"];
##if @TABLE.nSecOptions==ADVSECURITY_VIEW_OWN  && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
	if($strWhereClause=="")
		$strWhereClause = whereAdd($strWhereClause,SecuritySQL("Search"));
##endif##
	$strSQL=gSQLWhere($strWhereClause);
}


$mypage=1;
if(@$_REQUEST["type"])
{
//	order by
	$strOrderBy=$_SESSION[$strTableName."_order"];
	if(!$strOrderBy)
		$strOrderBy=$gstrOrderBy;
	$strSQL.=" ".trim($strOrderBy);

	$strSQLbak = $strSQL;
	if(function_exists("BeforeQueryExport"))
		BeforeQueryExport($strSQL,$strWhereClause,$strOrderBy);
//	Rebuild SQL if needed
	if($strSQL!=$strSQLbak)
	{
//	changed $strSQL - old style	
		$numrows=GetRowCount($strSQL);
	}
	else
	{
		$strSQL = gSQLWhere($strWhereClause);
		$strSQL.=" ".trim($strOrderBy);
		$numrows=gSQLRowCount($strWhereClause,##@TABLE.bGroupBy##);
	}
	LogInfo($strSQL);

//	 Pagination:

	$nPageSize=0;
	if(@$_REQUEST["records"]=="page" && $numrows)
	{
		$mypage=(integer)@$_SESSION[$strTableName."_pagenumber"];
		$nPageSize=(integer)@$_SESSION[$strTableName."_pagesize"];
		if($numrows<=($mypage-1)*$nPageSize)
			$mypage=ceil($numrows/$nPageSize);
		if(!$nPageSize)
			$nPageSize=$gPageSize;
		if(!$mypage)
			$mypage=1;

##if @BUILDER.nDatabaseType==DATABASE_MySQL##
		$strSQL.=" limit ".(($mypage-1)*$nPageSize).",".$nPageSize;
	}
	$rs=db_query($strSQL,$conn);
##elseif @BUILDER.nDatabaseType==DATABASE_MSSQLServer##
		$strSQL = AddTop($strSQL, $mypage*$nPageSize);
	}
	$rs=db_query($strSQL,$conn);
	db_pageseek($rs,$nPageSize,$mypage);
##elseif @BUILDER.nDatabaseType==DATABASE_Access##
		$strSQL = AddTop($strSQL, $mypage*$nPageSize);
	}
	$rs=db_query_direct($strSQL,$conn,$numrows);
	db_pageseek($rs,$nPageSize,$mypage);
##elseif @BUILDER.nDatabaseType==DATABASE_Oracle##
		$strSQL = AddRowNumber($strSQL, $mypage*$nPageSize);
	}
	$rs=db_query_direct($strSQL,$conn,$numrows);
	db_pageseek($rs,$nPageSize,$mypage);
##elseif @BUILDER.nDatabaseType==DATABASE_PostgreSQL##
		$maxrecs=$nPageSize;
		$strSQL.=" limit ".$nPageSize." offset ".(($mypage-1)*$nPageSize);
	}
	$rs=db_query($strSQL,$conn);
##endif##

	if(!ini_get("safe_mode"))
		set_time_limit(300);
	
	if(@$_REQUEST["type"]=="excel")
		ExportToExcel();
	else if(@$_REQUEST["type"]=="word")
		ExportToWord();
	else if(@$_REQUEST["type"]=="xml")
		ExportToXML();
	else if(@$_REQUEST["type"]=="csv")
		ExportToCSV();
	else if(@$_REQUEST["type"]=="pdf")
##if !@BUILDER.bViewPDF##
		ExportToPDF();
##else##
		header("Location: ##@TABLE.strShortTableName##_view.php?all=1&page=".@$_REQUEST["records"]);
		exit();
##endif##

	db_close($conn);
	return;
}

header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 

include('oldlibs/Smarty.class.php');
$smarty = new Smarty();
$smarty->assign("options",$options);
$smarty->display("##@TABLE.strShortTableName##_export.htm");


function ExportToExcel()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment;Filename=##@TABLE.strShortTableName##.xls");

	echo "<html>";
	echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
	
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToWord()
{
	global $cCharset;
	header("Content-type: application/vnd.ms-word");
	header("Content-Disposition: attachment;Filename=##@TABLE.strShortTableName##.doc");

	echo "<html>";
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
	echo "<body>";
	echo "<table border=1>";

	WriteTableData();

	echo "</table>";
	echo "</body>";
	echo "</html>";
}

function ExportToXML()
{
	global $nPageSize,$rs,$strTableName,$conn;
	header("Content-type: text/xml");
	header("Content-Disposition: attachment;Filename=##@TABLE.strShortTableName##.xml");
	if(!($row=db_fetch_array($rs)))
		return;
	global $cCharset;
	echo "<?xml version=\"1.0\" encoding=\"".$cCharset."\" standalone=\"yes\"?>\r\n";
	echo "<table>\r\n";
	$i=0;
	while((!$nPageSize || $i<$nPageSize) && $row)
	{
		echo "<row>\r\n";
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		$field=htmlspecialchars(XMLNameEncode("##@f.strName s##"));
		echo "<".$field.">";
##if (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
/*		
		if(strlen($row["##@f.strName s##"]))
		{
			$strdata = make_db_value("##@f.strName s##",$row["##@f.strName s##"]);
			$LookupSQL="SELECT ";
		##if @f.EditFormatObj.pLookupObj.bCustomDisplay##
			$LookupSQL.="##@f.strDisplayField s##";
		##else## 
			$LookupSQL.="##@f.strDisplayField ws##";
		##endif##
			$LookupSQL.=" FROM ##@f.EditFormatObj.pLookupObj.strTable ts## WHERE ##@f.strLinkField ws## = " . $strdata;
		##if EditFormatObj.pLookupObj.strWhere##
			$LookupSQL.=" and (".##EditFormatObj.pLookupObj.strWhere##.")"; 
		##endif##
			LogInfo($LookupSQL);
			$rsLookup = db_query($LookupSQL,$conn);
			$lookupvalue=$row["##@f.strName s##"];
			if($lookuprow=db_fetch_numarray($rsLookup))
				$lookupvalue=$lookuprow[0];
			echo htmlspecialchars(GetDataInt($lookupvalue,$row,"##@f.strName s##", "##@f.strViewFormat##"));
		}
*/		
		echo htmlspecialchars(DisplayLookupWizard("##@f.strName s##",$row["##@f.strName s##"],$row,"",MODE_EXPORT));
		
##elseif IsBinaryField(@f)##
		echo ##message LONG_BINARY##;
##else##
		echo htmlspecialchars(GetData($row,"##@f.strName s##",""));
##endif##
		echo "</".$field.">\r\n";
##endfor##
		echo "</row>\r\n";
		$i++;
		$row=db_fetch_array($rs);
	}
	echo "</table>\r\n";
}

function ExportToCSV()
{
	global $rs,$nPageSize,$strTableName,$conn;
	header("Content-type: application/csv");
	header("Content-Disposition: attachment;Filename=##@TABLE.strShortTableName##.csv");

	if(!($row=db_fetch_array($rs)))
		return;

	$totals=array();
##foreach Fields as @f filter @f.strTotalsType && @f.bExportPage## 
	$totals["##@f.strName s##"]=0;
##endfor##

	
// write header
	$outstr="";
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
	if($outstr!="")
		$outstr.=",";
	$outstr.= "\"##@f.strName s##\"";
##endfor##
	echo $outstr;
	echo "\r\n";

// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row)
	{
##foreach Fields as @f filter @f.strTotalsType && @f.bExportPage## 
			##if @f.strTotalsType=="COUNT"##
				$totals["##@f.strName s##"]+= ($row["##@f.strName s##"]!="");
			##else##
				$totals["##@f.strName s##"]+=($row["##@f.strName s##"]+0);
			##endif##
##endfor## 
		$outstr="";
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		if($outstr!="")
			$outstr.=",";
##if (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
/*
		if(strlen($row["##@f.strName s##"]))
		{
			$strdata = make_db_value("##@f.strName s##",$row["##@f.strName s##"]);
			$LookupSQL="SELECT ";
		##if @f.EditFormatObj.pLookupObj.bCustomDisplay##
			$LookupSQL.="##@f.strDisplayField s##";
		##else## 
			$LookupSQL.="##@f.strDisplayField ws##";
		##endif##
			$LookupSQL.=" FROM ##@f.EditFormatObj.pLookupObj.strTable ts## WHERE ##@f.strLinkField ws## = " . $strdata;
		##if EditFormatObj.pLookupObj.strWhere##
			$LookupSQL.=" and (".##EditFormatObj.pLookupObj.strWhere##.")"; 
		##endif##
			LogInfo($LookupSQL);
			$rsLookup = db_query($LookupSQL,$conn);

			$lookupvalue=$row["##@f.strName s##"];
			if($lookuprow=db_fetch_numarray($rsLookup))
				$lookupvalue=$lookuprow[0];
			$outstr.='"'.htmlspecialchars(GetDataInt($lookupvalue,$row,"##@f.strName s##", "##@f.strViewFormat##")).'"';
		}
*/		
		$value = DisplayLookupWizard("##@f.strName s##",$row["##@f.strName s##"],$row,"",MODE_EXPORT);
		if(strlen($value))
			$outstr.='"'.htmlspecialchars($value).'"';

##elseif IsBinaryField(@f)##
		$outstr.='"'.htmlspecialchars(##message LONG_BINARY##).'"';
##else##
	##if strViewFormat!=FORMAT_FILE_IMAGE && @f.strViewFormat!=FORMAT_FILE && @f.strViewFormat!=FORMAT_HYPERLINK && @f.strViewFormat!=FORMAT_EMAILHYPERLINK && @f.strViewFormat!=FORMAT_CHECKBOX##
		$format="##@f.strViewFormat##";
	##else##
		$format=FORMAT_NONE;
	##endif##			
		$outstr.='"'.htmlspecialchars(GetData($row,"##@f.strName s##",$format)).'"';
##endif##
##endfor##
		echo $outstr;
		echo "\r\n";
		$iNumberOfRows++;
		$row=db_fetch_array($rs);
	}

//	display totals
	$first=true;
##if Fields[strTotalsType].len##
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
	if(!$first)
		echo ",";
	else
		$first=false;
	echo "\"";
	##if @f.strTotalsType##
		##if strTotalsType=="COUNT"##
			echo ##message COUNT##.": ";
		##elseif strTotalsType=="TOTAL"##
			echo ##message TOTAL##.": ";
		##elseif strTotalsType=="AVERAGE"##
			echo ##message AVERAGE##.": ";
		##endif##
	echo htmlspecialchars(GetTotals("##@f.strName s##",$totals["##@f.strName s##"],"##@f.strTotalsType##",$iNumberOfRows,"##@f.strViewFormat##"));
	##endif##
	echo "\"";
	##endfor##
	echo "\r\n";
##endif##		

}


function WriteTableData()
{
	global $rs,$nPageSize,$strTableName,$conn;
	if(!($row=db_fetch_array($rs)))
		return;
// write header
	echo "<tr>";
	if($_REQUEST["type"]=="excel")
	{
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		echo '<td style="width: 100" x:str>'.PrepareForExcel("##@f.strName s##").'</td>';
##endfor##
	}
	else
	{
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		echo "<td>##@f.strName s##</td>";
##endfor##
	}
	echo "</tr>";

	$totals=array();
##foreach Fields as @f filter @f.strTotalsType && @f.bExportPage## 
	$totals["##@f.strName s##"]=0;
##endfor##
// write data rows
	$iNumberOfRows = 0;
	while((!$nPageSize || $iNumberOfRows<$nPageSize) && $row)
	{
##foreach Fields as @f filter @f.strTotalsType && @f.bExportPage## 
			##if @f.strTotalsType=="COUNT"##
				$totals["##@f.strName s##"]+= ($row["##@f.strName s##"]!="");
			##else##
				$totals["##@f.strName s##"]+=($row["##@f.strName s##"]+0);
			##endif##
##endfor##
		echo "<tr>";
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
##if IsCharField(@f)##
	if($_REQUEST["type"]=="excel")
		echo '<td x:str>';
	else
		echo '<td>';
##else##
	echo '<td>';
##endif##
##if (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
		if(strlen($row["##@f.strName s##"]))
		{
/*
			$strdata = make_db_value("##@f.strName s##",$row["##@f.strName s##"]);
			$LookupSQL="SELECT ";
		##if @f.EditFormatObj.pLookupObj.bCustomDisplay##
			$LookupSQL.="##@f.strDisplayField s##";
		##else## 
			$LookupSQL.="##@f.strDisplayField ws##";
		##endif##
			$LookupSQL.=" FROM ##@f.EditFormatObj.pLookupObj.strTable ts## WHERE ##@f.strLinkField ws## = " . $strdata;
		##if EditFormatObj.pLookupObj.strWhere##
			$LookupSQL.=" and (".##EditFormatObj.pLookupObj.strWhere##.")"; 
		##endif##
			LogInfo($LookupSQL);
			$rsLookup = db_query($LookupSQL,$conn);
			$lookupvalue=$row["##@f.strName s##"];
			if($lookuprow=db_fetch_numarray($rsLookup))
				$lookupvalue=$lookuprow[0];
				
			$strValue=GetDataInt($lookupvalue,$row,"##@f.strName s##", "##@f.strViewFormat##");
*/			
			$strValue = DisplayLookupWizard("##@f.strName s##",$row["##@f.strName s##"],$row,"",MODE_EXPORT);
			##if NeedEncode(@f)##	
			if($_REQUEST["type"]=="excel")
				echo PrepareForExcel($strValue);
			else
				echo htmlspecialchars($strValue);
			##else##
				echo $strValue;
			##endif##

		}
##elseif IsBinaryField(@f)##
		echo ##message LONG_BINARY##;
##else##

##if strViewFormat!=FORMAT_FILE_IMAGE && @f.strViewFormat!=FORMAT_FILE && @f.strViewFormat!=FORMAT_HYPERLINK && @f.strViewFormat!=FORMAT_EMAILHYPERLINK && @f.strViewFormat!=FORMAT_CHECKBOX##
		$format="##@f.strViewFormat##";
##else##
		$format=FORMAT_NONE;
##endif##			
	##if @f.strViewFormat==FORMAT_CUSTOM || (@f.strEditFormat==EDIT_FORMAT_TEXT_AREA && @f.m_EditFormatObj.m_bUseRTE) ##
		echo GetData($row,"##@f.strName s##",$format);
	##elseif NeedQuotes(@f)##
		if($_REQUEST["type"]=="excel")
			echo PrepareForExcel(GetData($row,"##@f.strName s##",$format));
		else
			echo htmlspecialchars(GetData($row,"##@f.strName s##",$format));
	##else##			
		echo htmlspecialchars(GetData($row,"##@f.strName s##",$format));
	##endif##
##endif##
	echo '</td>';
##endfor##		
		echo "</tr>";
		$iNumberOfRows++;
		$row=db_fetch_array($rs);
	}
##if Fields[strTotalsType].len##
	echo "<tr>";
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
	echo "<td>";
	##if @f.strTotalsType##
		##if strTotalsType=="COUNT"##
			echo ##message COUNT##.": ";
		##elseif strTotalsType=="TOTAL"##
			echo ##message TOTAL##.": ";
		##elseif strTotalsType=="AVERAGE"##
			echo ##message AVERAGE##.": ";
		##endif##
	echo htmlspecialchars(GetTotals("##@f.strName s##",$totals["##@f.strName s##"],"##@f.strTotalsType##",$iNumberOfRows,"##@f.strViewFormat##"));
	##endif##
	echo "</td>";
##endfor##
	echo "</tr>";
##endif##		

}

function XMLNameEncode($strValue)
{	
	$search=array(" ","#","'","/","\\","(",")",",","[","]","+","\"","-","_","|","}","{","=");
	return str_replace($search,"",$strValue);
}

function PrepareForExcel($str)
{
	$ret = htmlspecialchars($str);
	if (substr($ret,0,1)== "=") 
		$ret = "&#61;".substr($ret,1);
	return $ret;

}




function ExportToPDF()
{
	global $nPageSize,$rs,$strTableName,$conn;
		global $colwidth,$leftmargin;
	if(!($row=db_fetch_array($rs)))
		return;


	include("oldlibs/fpdf.php");

	class PDF extends FPDF
	{
	//Current column
		var $col=0;
	//Ordinate of column start
		var $y0;
		var $maxheight;

	function AcceptPageBreak()
	{
		global $colwidth,$leftmargin;
		if($this->y0+$this->rowheight>$this->PageBreakTrigger)
			return true;
		$x=$leftmargin;
		if($this->maxheight<$this->PageBreakTrigger-$this->y0)
			$this->maxheight=$this->PageBreakTrigger-$this->y0;
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		$this->Rect($x,$this->y0,$colwidth["##@f.strName s##"],$this->maxheight);
		$x+=$colwidth["##@f.strName s##"];
##endfor##
		$this->maxheight = $this->rowheight;
//	draw frame	
		return true;
	}

	function Header()
	{
		global $colwidth,$leftmargin;
	    //Page header
		$this->SetFillColor(192);
		$this->SetX($leftmargin);
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		$this->Cell($colwidth["##@f.strName s##"],$this->rowheight,"##@f.strLabel s##",1,0,'C',1);
##endfor##
		$this->Ln($this->rowheight);
		$this->y0=$this->GetY();
	}

	}

	$pdf=new PDF();

	$leftmargin=5;
	$pagewidth=200;
	$pageheight=290;
	$rowheight=5;


	$defwidth=$pagewidth/##Fields[bExportPage].len##;
	$colwidth=array();
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
    $colwidth["##@f.strName s##"]=$defwidth;
##endfor##
	
##if @BUILDER.strCharset==CP1250##
	$pdf->AddFont('CourierNewPSMT','','courcp1250.php');
##elseif @BUILDER.strCharset==CP1251##
	$pdf->AddFont('CourierNewPSMT','','courcp1251.php');
##elseif @BUILDER.strCharset==CP1253##
	$pdf->AddFont('CourierNewPSMT','','courcp1253.php');
##elseif @BUILDER.strCharset==CP1254##
	$pdf->AddFont('CourierNewPSMT','','courcp1254.php');
##elseif @BUILDER.strCharset==CP1257##
	$pdf->AddFont('CourierNewPSMT','','courcp1257.php');
##elseif @BUILDER.strCharset==CP1258##
	$pdf->AddFont('CourierNewPSMT','','courcp1258.php');
##else##
	$pdf->AddFont('CourierNewPSMT','','courcp1252.php');
##endif##	
	$pdf->rowheight=$rowheight;
	
	$pdf->SetFont('CourierNewPSMT','',8);
	$pdf->AddPage();
	

	$i=0;
	while((!$nPageSize || $i<$nPageSize) && $row)
	{
		$pdf->maxheight=$rowheight;
		$x=$leftmargin;
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		$pdf->SetY($pdf->y0);
		$pdf->SetX($x);
##if (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
		if(strlen($row["##@f.strName s##"]))
		{
/*			$strdata = make_db_value("##@f.strName s##",$row["##@f.strName s##"]);
			$LookupSQL="SELECT ";
		##if @f.EditFormatObj.pLookupObj.bCustomDisplay##
			$LookupSQL.="##@f.strDisplayField s##";
		##else## 
			$LookupSQL.="##@f.strDisplayField ws##";
		##endif##
			$LookupSQL.=" FROM ##@f.EditFormatObj.pLookupObj.strTable ts## WHERE ##@f.strLinkField ws## = " . $strdata;
		##if EditFormatObj.pLookupObj.strWhere##
			$LookupSQL.=" and (".##EditFormatObj.pLookupObj.strWhere##.")"; 
		##endif##
			LogInfo($LookupSQL);
			$rsLookup = db_query($LookupSQL,$conn);
			$lookupvalue=$row["##@f.strName s##"];
			if($lookuprow=db_fetch_numarray($rsLookup))
				$lookupvalue=$lookuprow[0];
			$pdf->Cell($colwidth["##@f.strName s##"],$rowheight,GetDataInt($lookupvalue,$row,"##@f.strName s##", "##@f.strViewFormat##"));
*/				
				
			$value = DisplayLookupWizard("##@f.strName s##",$row["##@f.strName s##"],$row,"",MODE_EXPORT);
			$pdf->Cell($colwidth["##@f.strName s##"],$rowheight,$value);
		}
##elseif @f.strViewFormat==FORMAT_DATABASE_IMAGE##
		$height=0;
		$pdf->Image_mem($row["##@f.strName s##"],$pdf->GetX()+1,$pdf->GetY()+1,$colwidth["##@f.strName s##"]-2,$height);
		$pdf->SetX($pdf->GetX()+$colwidth["##@f.strName s##"]);
		$pdf->SetY($pdf->y0+$height+2);
##elseif @f.strViewFormat==FORMAT_FILE_IMAGE##
		$height=0;
		$pdf->Image(AddLinkPrefix("##@f.strName s##",$row["##@f.strName s##"]),$pdf->GetX()+1,$pdf->GetY()+1,$colwidth["##@f.strName s##"]-2,$height);
		$pdf->SetX($pdf->GetX()+$colwidth["##@f.strName s##"]);
		$pdf->SetY($pdf->y0+$height+2);
##elseif @f.strViewFormat==FORMAT_DATABASE_FILE##
	##if @f.strFilename##
		$pdf->Cell($colwidth["##@f.strName s##"],$rowheight,$row["##@f.strFilename s##"]);
	##else##
		$pdf->Cell($colwidth["##@f.strName s##"],$rowheight,"File");
	##endif##
##else##
		$pdf->MultiCell($colwidth["##@f.strName s##"],$rowheight,GetData($row,"##@f.strName s##","##@f.strViewFormat##"));
##endif##
		$x+=$colwidth["##@f.strName s##"];
		if($pdf->GetY()-$pdf->y0>$pdf->maxheight)
			$pdf->maxheight=$pdf->GetY()-$pdf->y0;
##endfor##
//	draw fames
		$x=$leftmargin;
##foreach Fields as @f filter @f.bExportPage order @f.nExportPageOrder##
		$pdf->Rect($x,$pdf->y0,$colwidth["##@f.strName s##"],$pdf->maxheight);
		$x+=$colwidth["##@f.strName s##"];
##endfor##
		$pdf->y0+=$pdf->maxheight;
		$i++;
		$row=db_fetch_array($rs);
	}
	$pdf->Output();
}

?>