<?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##

$pageName = "report.php";

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


//	Before Process event
if(function_exists("BeforeProcessReport"))
	BeforeProcessReport($conn);

//	process reqest data, fill session variables
if(!count($_POST) && !count($_GET))
{
	$sess_unset = array();
	foreach($_SESSION as $key=>$value)
		if(substr($key,0,strlen($strTableName)+1)==$strTableName."_" &&
			strpos(substr($key,strlen($strTableName)+1),"_")===false)
			$sess_unset[] = $key;
	foreach($sess_unset as $key)
		unset($_SESSION[$key]);
}

if(isset($_REQUEST["pagesize"]))
{
	$_SESSION[$strTableName."_pagesize"]=intval($_REQUEST["pagesize"]);
	$_SESSION[$strTableName."_pagenumber"]=1;
}

if(isset($_REQUEST["goto"]))
	$_SESSION[$strTableName."_pagenumber"]=intval($_REQUEST["goto"]);

##if @BUILDER.arrLanguages.len > 1##
if(@$_REQUEST["language"])
	$_SESSION["language"]=@$_REQUEST["language"];

$var = GoodFieldName(mlang_getcurrentlang())."_langattrs";
$xt->assign($var,"selected");
$xt->assign("langselector_attrs","name=lang onchange=\"javascript: window.location='##@TABLE.strShortTableName hs##_report.##@ext##?language='+this.options[this.selectedIndex].value\"");
$xt->assign("languages_block",true);
##endif##

$xt->assign_section("body",
"<script type=\"text/javascript\" src=\"include/jquery.js\"></script>".
"<script language=\"JavaScript\" src=\"include/jsfunctions.js\"></script>\r\n".
"".loadindicator()."\r\n".
"<script language=\"JavaScript\" src=\"include/customlabels.js\"></script>\r\n".
"<script>".
"var bSelected=false;".
"var TEXT_FIRST = \"".##message FIRST##."\";".
"var TEXT_PREVIOUS = \"".##message PREVIOUS##."\";".
"var TEXT_NEXT = \"".##message NEXT##."\";".
"var TEXT_LAST = \"".##message LAST##."\";".
"</script>","");
$xt->assign("bodyattrs",'OnLoad="if(!window.stopload){document.getElementById(\'loading\').className=\'load_hide\';window.stopload=true;}"');

if(!$_SESSION[$strTableName."_pagenumber"])
	$_SESSION[$strTableName."_pagenumber"]=1;

if(!$_SESSION[$strTableName."_pagesize"])
{
	$_SESSION[$strTableName."_pagesize"] = ##@TABLE.nNumberOfRecords##;
}

$PageSize=$_SESSION[$strTableName."_pagesize"];
$pagestart=($_SESSION[$strTableName."_pagenumber"]-1)*$_SESSION[$strTableName."_pagesize"];
$pagelen=$_SESSION[$strTableName."_pagesize"];

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

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

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

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

$xt->assign('grid_row', array('data' => $arrReport['list']));
////////////////////////////////////////////////////////////////////////////////////////

$mypage=$_SESSION[$strTableName."_pagenumber"];
$maxpages=$arrReport['maxpages'];
//	write pagination
if($maxpages>1)
{
	$xt->assign("pagination_block",true);
	$xt->assign("pagination","<script language=\"JavaScript\">WritePagination(".$mypage.",".$maxpages.");
		function GotoPage(nPageNumber)
		{
			window.location='##@TABLE.strShortTableName##_report.php?goto='+nPageNumber;
		}
</script>");
}

$pagesize_option2 = array();
##if @TABLE.arrReportGroupFields.len##
$pagesize_option = array(array('n' => 1, 'txt' => '1'),
						 array('n' => 3, 'txt' => '3'),
						 array('n' => 5, 'txt' => '5'),
						 array('n' => 10, 'txt' => '10'),
						 array('n' => 50, 'txt' => '50'),
						 array('n' => 100, 'txt' => '100'));
##else##
$pagesize_option = array(array('n' => 10, 'txt' => '10'),
						 array('n' => 20, 'txt' => '20'),
						 array('n' => 50, 'txt' => '50'),
						 array('n' => 100, 'txt' => '100'),
						 array('n' => 500, 'txt' => '500'));
##endif##

$oldK = 0;
$userPP = ##@TABLE.nNumberOfRecords##;
for($nCnt = 0; $nCnt < count($pagesize_option); $nCnt ++)
{
	if($oldK < $userPP && $pagesize_option[$nCnt]['n'] > $userPP)
		$pagesize_option2 []= array('n' => $userPP, 'txt' => $userPP, 'selected' => $PageSize == $userPP ? 'selected' : '');
	$pagesize_option2 []= $pagesize_option[$nCnt];
	$oldK = $pagesize_option[$nCnt]['n'];
	if($PageSize == $oldK)
		$pagesize_option2[count($pagesize_option2) - 1]['selected'] = 'selected';
}
if($pagesize_option[count($pagesize_option) - 1]['n'] < $userPP)
	$pagesize_option2 []= array('n' => $userPP, 'txt' => $userPP, 'selected' => $PageSize == $userPP ? 'selected' : '');
	
$pagesize_option2 []= array('n' => -1, 'txt' => ##message SHOW_ALL##, 'selected' => $PageSize == -1 ? 'selected' : '');
$xt->assign_loopsection('rpp_options', $pagesize_option2);

##if @BUILDER.bCreateLoginPage##
$xt->assign("login_block",true);
$xt->assign("username",htmlspecialchars($_SESSION["UserID"]));
$xt->assign("logoutlink_attrs","onclick=\"window.location.href='login.##@ext##?a=logout';\"");
##endif##

//	table selector
##foreach @BUILDER.Tables as @t filter (@t.bMenuItem || @t.arrMasterTables[strMasterTable==@TABLE.strDataSourceTable].len)##
##if GroupSecurity##
$strPerm = GetUserPermissions("##@t.strDataSourceTable s##");
$allow_##@t.strShortTableName##= (strpos($strPerm, "A")!==false || strpos($strPerm, "S")!==false);
##else##
$allow_##@t.strShortTableName##=true;
##endif##
##endfor##
$createmenu=false;
##foreach @BUILDER.Tables as @t filter @t.bMenuItem order @t.nMenuOrder##
if($allow_##@t.strShortTableName##)
{
	$createmenu=true;
	$xt->assign("##@t.strShortTableName##_tablelink",true);
	$page="";
	##if @t.bList##
	$page="list";
	##if @t.bAdd##
	$strPerm = GetUserPermissions("##@t.strDataSourceTable s##");
	if(strpos($strPerm, "A")!==false && strpos($strPerm, "S")===false)
		$page="add";
	##endif##
	##elseif @t.bAdd##
	$page="add";
	##elseif @t.nType==titREPORT##
	$page="report";
	##elseif @t.nType==titCHART##
	$page="chart";
	##endif##
	$xt->assign("##@t.strShortTableName##_tablelink_attrs","href=\"##@t.strShortTableName##_".$page.".##@ext##\"");
	$xt->assign("##@t.strShortTableName##_optionattrs","value=\"##@t.strShortTableName##_".$page.".##@ext##\"");
}
##endfor##
if($createmenu)
{
	$xt->assign("menu_block",true);
	$xt->assign("menustyle_block",true);
}
##if CreateMenu##
$xt->assign("quickjump_attrs","onchange=\"window.location.href=this.options[this.selectedIndex].value;\"");
##endif##

##if @BUILDER.bWebReports##
$xt->assign("webreport_link",true);
##endif##

##if !GroupSecurity##
$allow_add=true;
$allow_delete=true;
$allow_edit=true;
$allow_search=true;
$allow_export=true;
$allow_import=true;
##else##
$strPerm = GetUserPermissions();
$allow_add=(strpos($strPerm,"A")!==false);
$allow_delete=(strpos($strPerm,"D")!==false);
$allow_edit=(strpos($strPerm,"E")!==false);
$allow_search=(strpos($strPerm,"S")!==false);
$allow_export=(strpos($strPerm,"P")!==false);
$allow_import=(strpos($strPerm,"I")!==false);
##endif##

$xt->assign("toplinks_block",$allow_search);
$xt->assign("asearch_link",$allow_search);
$xt->assign("asearchlink_attrs","onclick=\"window.location.href='##@TABLE.strShortTableName h##_search.##@ext##';return false;\"");
$xt->assign("print_link",$allow_export);
$xt->assign("printall_link",$allow_export);
$xt->assign("export_link",$allow_export);
$xt->assign("printlink_attrs","onclick=\"window.open('##@TABLE.strShortTableName h##_print.##@ext##','wPrint');return false;\"");
$xt->assign("printalllink_attrs","onclick=\"window.open('##@TABLE.strShortTableName h##_print.##@ext##?all=1','wPrint');return false;\"");
$xt->assign("recordspp_block",$allow_search);
$xt->assign("grid_block",$allow_search);

##if @BUILDER.bDynamicPermissions##
if(@$_SESSION["IsAdmin"])
	$xt->assign("adminarea_link",true);
$xt->assign("adminarealink_attrs","onclick=\"window.location.href='admin_rights_list.##@ext##';return false;\"");
##endif##
$xt->assign("changepwd_link",$_SESSION["AccessLevel"] != ACCESS_LEVEL_GUEST);
$xt->assign("changepwdlink_attrs","onclick=\"window.location.href='changepwd.##@ext##';return false;\"");

$templatefile = "##@TABLE.strShortTableName##_report.htm";
if(function_exists("BeforeShowReport"))
	BeforeShowReport($xt,$templatefile);
$xt->display($templatefile);

?>
