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

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




##if @BUILDER.bCreateLoginPage##
//	check if logged in
if(!@$_SESSION["UserID"] || !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{ 
	$_SESSION["MyURL"]=$_SERVER["SCRIPT_NAME"]."?".$_SERVER["QUERY_STRING"];
	header("Location: login.php?message=expired"); 
	return;
}
##endif##

$filename="";	
$message="";

$all=false;
##if @BUILDER.bViewPDF##
$all=postvalue("all");
##endif##
$mypage=1;

//connect database
$conn = db_connect();

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

$strWhereClause="";
if(!$all)
{
	$keys=array();
##foreach @TABLE.arrKeyFields as @k##
	$keys["##@k s##"]=postvalue("editid##@index##");
##endfor##

//	get current values and show edit controls

	$strWhereClause = KeyWhere($keys);

##if @TABLE.nSecOptions!=ADVSECURITY_ALL && @BUILDER.nLoginMethod==SECURITY_TABLE && @BUILDER.bCreateLoginPage##
//	select only owned records
	$strWhereClause=whereAdd($strWhereClause,SecuritySQL("Search"));
##endif##

	$strSQL=gSQLWhere($strWhereClause);
}
else
{
	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);
	}
	$strOrderBy=$_SESSION[$strTableName."_order"];
	if(!$strOrderBy)
		$strOrderBy=$gstrOrderBy;
	$strSQL.=" ".trim($strOrderBy);
//	order by
	$strOrderBy=$_SESSION[$strTableName."_order"];
	if(!$strOrderBy)
		$strOrderBy=$gstrOrderBy;
	$strSQL.=" ".trim($strOrderBy);
		$numrows=gSQLRowCount($strWhereClause,##@TABLE.bGroupBy##);

}


$strSQLbak = $strSQL;
if(function_exists("BeforeQueryView"))
	BeforeQueryView($strSQL,$strWhereClause);
if($strSQLbak == $strSQL)
	$strSQL=gSQLWhere($strWhereClause);

if(!$all)
{
	LogInfo($strSQL);
	$rs=db_query($strSQL,$conn);
}
else
{
//	 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##
}

$data=db_fetch_array($rs);

include('oldlibs/Smarty.class.php');
$smarty = new Smarty();

$out="";
$first=true;
while($data)
{



##foreach @TABLE.arrKeyFields as @k##
	##if Field(@k).strViewFormat==FORMAT_HTML || Field(@k).strViewFormat==FORMAT_FILE_IMAGE || Field(@k).strViewFormat==FORMAT_FILE || Field(@k).strViewFormat==FORMAT_HYPERLINK || Field(@k).strViewFormat==FORMAT_EMAILHYPERLINK || Field(@k).strViewFormat==FORMAT_CHECKBOX##
$smarty->assign("show_key##@index##", htmlspecialchars($data["##@k s##"]));
	##else##
$smarty->assign("show_key##@index##", htmlspecialchars(GetData($data,"##@k s##", "##Field(@k).strViewFormat##")));
	##endif##
##endfor##

$keylink="";
##foreach @TABLE.arrKeyFields as @k##
$keylink.="&key##@index##=".htmlspecialchars(rawurlencode(@$data["##@k s##"]));
##endfor##

##foreach Fields as @f filter @f.bViewPage order @f.nViewPageOrder##
////////////////////////////////////////////
//	##@f.strName## - ##@f.strViewFormat##
	$value="";
	##if @f.strViewFormat==FORMAT_DATABASE_IMAGE##
		##if @f.bShowThumbnail && !@f.bOriginalView##
		$value.="<a";
		##if @f.bUseiBox##
			$value .= " rel='ibox'";
		##else##
			$value .= " target=_blank";
		##endif##
		$value.=" href=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strName uhs##".$keylink."\">";
		$value.= "<img border=0";
		$value.=" src=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strThumbnail uhs##&alt=##@f.strName uhs##".$keylink."\">";
		$value.= "</a>";
		##else##
		$value = "<img";
		##if @f.ListFormatObj.nImageWidth && !@f.bOriginalView##
		$value.=" width=##@f.ListFormatObj.nImageWidth##";
		##endif##
		##if @f.ListFormatObj.nImageHeight && !@f.bOriginalView##
		$value.=" height=##@f.ListFormatObj.nImageHeight##";
		##endif##
		$value.=" border=0";
		$value.=" src=\"##@TABLE.strShortTableName s##_imager.php?field=##@f.strName uhs##".$keylink."\">";
	##endif##
	##elseif @f.strViewFormat==FORMAT_FILE_IMAGE##
	if(CheckImageExtension($data["##@f.strName s##"])) 
	{
	##if @f.bShowThumbnail && !@f.bOriginalView##
			 	// show thumbnail
		$thumbname="##@f.strThumbnail s##".$data["##@f.strName s##"];
		if(substr("##@f.strhlPrefix s##",0,7)!="http://" && !file_exists(GetUploadFolder("##@f.strName s##").$thumbname))
			$thumbname=$data["##@f.strName s##"];
		$value="<a";
		##if @f.bUseiBox##
			$value .= " rel='ibox'";
		##else##
			$value .= " target=_blank";
		##endif##
		$value.=" href=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$data["##@f.strName s##"]))."\">";
		$value.="<img";
		if($thumbname==$data["##@f.strName s##"])
		{
			##if @f.ListFormatObj.nImageWidth##
			$value.=" width=##@f.ListFormatObj.nImageWidth##";
			##endif##
			##if @f.ListFormatObj.nImageHeight##
			$value.=" height=##@f.ListFormatObj.nImageHeight##";
			##endif##
		}
		$value.=" border=0";
		$value.=" src=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$thumbname))."\"></a>";
	##else##
		$value="<img";
		##if @f.ListFormatObj.nImageWidth && !@f.bOriginalView##
		$value.=" width=##@f.ListFormatObj.nImageWidth##";
		##endif##
		##if @f.ListFormatObj.nImageHeight && !@f.bOriginalView##
		$value.=" height=##@f.ListFormatObj.nImageHeight##";
		##endif##
		$value.=" border=0";
		$value.=" src=\"".htmlspecialchars(AddLinkPrefix("##@f.strName s##",$data["##@f.strName s##"]))."\">";
	##endif##
	}
	##elseif @f.strViewFormat==FORMAT_DATABASE_FILE##
		##if @f.ListFormatObj.strFilename##
	$filename=$data["##@f.ListFormatObj.strFilename s##"];
	if(!$filename)
		$filename="file.bin";
		##else##
	$filename="file.bin";
		##endif##
	if(strlen($data["##@f.strName s##"]))
	{
		$value="<a href=\"##@TABLE.strShortTableName##_getfile.php?filename=".rawurlencode($filename)."&field=##@f.strName uhs##".$keylink."\">";
		$value.=htmlspecialchars($filename);
		$value.="</a>";
	}
	##elseif (@f.strEditFormat==EDIT_FORMAT_LOOKUP_WIZARD || @f.strEditFormat==EDIT_FORMAT_RADIO) && pLookupObj.nLookupType == LT_LOOKUPTABLE##
	$value=DisplayLookupWizard("##@f.strName s##",$data["##@f.strName s##"],$data,$keylink,MODE_VIEW);
			
	##elseif NeedEncode(@f)##
	$value = ProcessLargeText(GetData($data,"##@f.strName s##", "##@f.strViewFormat##"),"","",MODE_VIEW);
	##else##
	$value = GetData($data,"##@f.strName s##", "##@f.strViewFormat##");
	##endif##
	$smarty->assign("show_##@f.strName g##",$value);
##endfor##
$smarty->assign("pdf",postvalue("pdf"));
$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";
$smarty->assign("pageurl",$pagename);

$smarty->assign("all",$all);

$templatefile = "##@TABLE.strShortTableName##_view.htm";
if(function_exists("BeforeShowView"))
	BeforeShowView($smarty,$templatefile,$data);

if(!$all)
{
##if @BUILDER.bViewPDF##
	if(!postvalue("pdf"))
##endif##
		$smarty->display($templatefile);
##if @BUILDER.bViewPDF##
	else
	{
		$page = $smarty->fetch($templatefile);
		$pagewidth = postvalue("width")*1.05;
		$landscape=false;
		include("plugins/page2pdf.php");
	}
##endif##
	break;
}
else
{
##if @BUILDER.bViewPDF##
	$head="<span  name=page>";
	if(!postvalue("pdf"))
		$foot="</span><div style=\"page-break-after: always\">&nbsp;</div>";
	else
		$foot="</span><div style=\"page-break-after: always\"></div>";
	$page = $smarty->fetch($templatefile);
	$data=db_fetch_array($rs);
	$pos = strpos($page,"<!--startPDFcut-->");
	if(!$first)
	{
		if($pos!==false)
			$page=substr($page,$pos+strlen("<!--startPDFcut-->"));
		$page=$head.$page;
	}
	else
	{
		if($pos!==false)
			$page = substr($page,0,$pos).$head.substr($page,$pos);
		else
			$page=$head.$page;
	}
	$first=false;
	$pos = strpos($page,"<!--endPDFcut-->");
	if(!$data)
	{
		if($pos!==false)
			$page=substr($page,0,$pos).$foot.substr($page,$pos);
		else
			$page.=$foot;
	}
	else
	{
		if($pos!==false)
			$page=substr($page,0,$pos);
		$page.=$foot;
	}

	if(!postvalue("pdf"))
	{
		echo $page;
	}
	else
	{
		$out.=$head.$page.$foot;
	}
##endif##		
}

}
##if @BUILDER.bViewPDF##
if($all)
{
	if(!postvalue("pdf"))
		echo "<script>RunPDF();</script>";
	else
	{
		$page=$out;
		$pagewidth = postvalue("width")*1.05;
		$landscape=false;
		include("plugins/page2pdf.php");
	}
}
##endif##		

?>
