<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
include("include/dbcommon.php");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT");
header("Pragma: no-cache");
header("Cache-Control: no-cache");set_magic_quotes_runtime(0);

include("include/##@TABLE.strShortTableName s##_variables.php");

##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"],"Search"))
{
	echo "<p>".##message NO_PERMISSIONS##." <a href=\"login.php\">".##message BACK_TO_LOGIN##."</a></p>";
	return;
}
##endif##

include('include/xtempl.php');
$xt = new Xtempl();


//	Before Process event
if(function_exists("BeforeProcessPrint"))
	BeforeProcessPrint($conn);
	
$forExport = false;

if (@$_REQUEST["format"]=="excel")
{
	$forExport = true;
	header("Content-Type: application/vnd.ms-excel");
	header("Content-Disposition: attachment;Filename=##@TABLE.strShortTableName##.xls");
	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."\">";
}
else if (@$_REQUEST["format"]=="word")
{
	$forExport = true;
	header("Content-Type: application/vnd.ms-word");
	header("Content-Disposition: attachment;Filename=##@TABLE.strShortTableName##.doc");
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$cCharset."\">";
}

if(!$_SESSION[$strTableName."_pagenumber"])
	$_SESSION[$strTableName."_pagenumber"]=1;
	
if(!$_SESSION[$strTableName."_pagesize"])
	$_SESSION[$strTableName."_pagesize"]=##@TABLE.nReportLinesPerPage##;

$PageSize=$_SESSION[$strTableName."_pagesize"];
$bAll = isset($_REQUEST["all"]) && $_REQUEST["all"];
$pagestart = ($_SESSION[$strTableName."_pagenumber"]-1)*$_SESSION[$strTableName."_pagesize"];

if (@$_REQUEST["format"])
{
	//$forExport = true;
	// read stylesheet file
	$cssdata = myfile_get_contents(GetAbsoluteFileName("include/style.css"), "r");
	$xt->assign("cssdata",$cssdata);
	$xt->assign("stylesheetlink",false);
	//$bAll=false;
}
else
{
	$xt->assign("cssdata","");
	$xt->assign("stylesheetlink",true);	
}

$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);

$strSQLbak = $strSQL;
if(function_exists("BeforeQueryPrint"))
	BeforeQueryPrint($strSQL,$strWhereClause,$strOrderBy);

if($strSQLbak == $strSQL)
	$strSQL = gSQLWhere($strWhereClause);
LogInfo($strSQL);

//////////////////////////////////////////////////////////////////////////////////////
if(isset($_REQUEST['a']) && $_REQUEST['a'] == 'advsearch')
	$search = true;
else
	$search = false;

include('include/##@TABLE.strShortTableName s##_reportlib.php');
$rb = new Report(array($gsqlHead, $gsqlFrom, $gsqlWhereExpr, $gsqlTail), $g_orderindexes, $search, $conn, $PageSize, ##@TABLE.nReportLinesPerPage##);
$rb->forExport = $forExport;
if(!$search && isset($_SESSION[$strTableName.'_advsearch']))
	$rb->setWhere(unserialize($_SESSION[$strTableName.'_advsearch']));
if($search)
	$_SESSION[$strTableName.'_advsearch'] = serialize($rb->getWhere());
$arrReport = $rb->getReport($bAll ? -1 : $pagestart);

//////////////////////////////////////////////////////////////////////////////////////
$pages=array();
if($pagestart == -1 || $bAll)
{
	$pages = $rb->getPages();
	for($i = 0; $i < count($pages); $i++)
	{
		$pages[$i]['grid_row'] = array("data" => $arrReport['list'][$i]);
		$pages[$i]['begin'] = ($i < count($pages) - 1) ? "<div name=page class=printpage>" : "<div name=page>";
		$pages[$i]['end'] = "</div>";
	}
}
else
{
	$pages[0]['grid_row'] = array("data" => $arrReport['list']);
	$pages[0]['begin'] = "<div name=page class=printpage>";
	$pages[0]['end'] = "</div>";
}

foreach($arrReport['page'] as $key => $value)
	$xt->assign($key, $value);

##if @TABLE.bReportGlobalSummary##
	foreach($arrReport['global'] as $key => $value)
		$xt->assign($key, $value);
  
	if(0 == count($pages))
		$pages []= array();
		
	$pages[count($pages) - 1]['global_summary'] = true;
	$pages[count($pages) - 1]['begin'] = "<div name=page>";
	$pages[count($pages) - 1]['end'] = "</div>";
##endif##

$xt->assign_loopsection("page", $pages);
$mypage = $_SESSION[$strTableName."_pagenumber"];
$maxpages = count($pages);

$body=array();
##if @BUILDER.bViewPDF##
$pagename = $_SERVER["REQUEST_URI"];
if(!$pagename)
{
	$pagename=basename(__file__);
	$params="";
	foreach($_GET as $k=>$v)
	{
		if(strlen($params))
			$params.="&";
		$params.=rawurlencode($k)."=".rawurlencode($v);
	}
	if(strlen($params))
		$pagename.="?".$params;
}
if(strpos($pagename,"?")===false)
	$pagename.="?pdf=1";
else
	$pagename.="&pdf=1";

$body["begin"]="<script type=\"text/javascript\" src=\"include/jquery.js\"></script>".
	"<script type=\"text/javascript\" src=\"include/pdf.js\"></script>";
$body["begin"].="<script>".
	"\nvar TEXT_PDF_BUILD1='".jsreplace(##message PDF_BUILD1##)."';".
	"\nvar TEXT_PDF_BUILD2='".jsreplace(##message PDF_BUILD2##)."';".
	"</script>";

if(!postvalue("pdf"))
{
	$xt->assign("pdflink_block",true);
	$xt->assign("pdflink_attrs","onclick=\"RunPDF(); this.style.display='none'; return false;\"");
	$body["end"]="<script>
		var page = '".jsreplace($pagename)."';
		</script>
		<iframe name=\"pdf\" class=\"pdf\" style=\"position:absolute;left:-1000px;\">
		</iframe>";
}
$pdf_block = array("begin"=>"<div id=progress>", "end"=>"</div>");
if(count($pages))
	$pages[0]["pdf_block"]=&$pdf_block;
##endif##
$xt->assignbyref("body",$body);
$xt->assign("grid_block",true);
$xt->assign("grid_header",true);
##foreach Fields as @f filter @f.bReportPage##
$xt->assign("##@f.strName g##_fieldheader",true);
##endfor##

if (@$_REQUEST["format"] && $_REQUEST["format"]!="pdf")
{
	$pages[0]["page_summary"]=false;
	$xt->assign_loopsection("page",$pages);
	$xt->assign("pdflink_block",false);
	$body["begin"]="";
	$body["end"]="";
	$xt->assignbyref("body",$body);
}

$templatefile = "##@TABLE.strShortTableName##_print.htm";
if(function_exists("BeforeShowPrint"))
	BeforeShowPrint($xt,$templatefile);

if(!postvalue("pdf"))
{
	$xt->display($templatefile);

	if (@$_REQUEST["format"]=="pdf")
	{
		echo("<script>RunPDF();</script>");
	}	
}
else
{
##if @BUILDER.bViewPDF##
	$xt->load_template($templatefile);
	$page = $xt->fetch_loaded();
	$pagewidth=postvalue("width")*1.05;
	$pageheight=postvalue("height")*1.05;
	$landscape=false;
	if(postvalue("all"))
	{
		if($pagewidth>$pageheight)
		{
			$landscape=true;
			if($pagewidth/$pageheight<297/210)
				$pagewidth = 297/210*$pageheight;
		}
		else
		{
			if($pagewidth/$pageheight<210/297)
				$pagewidth = 210/297*$pageheight;
		}
	}
	include("plugins/page2pdf.php");
##endif##	
}
?>