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

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

$mode=postvalue("mode");

##if @BUILDER.bCreateLoginPage##
if(!@$_SESSION["UserID"])
{ 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	return;
}
##endif##

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


$recordsCounter = 0;

##if @TABLE.arrMasterTables.len##
//	process masterkey value
$mastertable=postvalue("mastertable");
if($mastertable!="")
{
	$_SESSION[$strTableName."_mastertable"]=$mastertable;
//	copy keys to session
	$i=1;
	while(isset($_REQUEST["masterkey".$i]))
	{
		$_SESSION[$strTableName."_masterkey".$i]=$_REQUEST["masterkey".$i];
		$i++;
	}
	if(isset($_SESSION[$strTableName."_masterkey".$i]))
		unset($_SESSION[$strTableName."_masterkey".$i]);
}
else
	$mastertable=$_SESSION[$strTableName."_mastertable"];
##endif##

//$strSQL = $gstrSQL;

##foreach @TABLE.arrMasterTables as @m##
if($mastertable=="##@m.strMasterTable s##")
{
	$where ="";
	##foreach @m.arrDetailKeys as @mk##
	##if !@first##
	$where.=" and ";
	##endif##
	$where.= GetFullFieldName("##@mk s##")."=".make_db_value("##@mk s##",$_SESSION[$strTableName."_masterkey##@index##"]);
	##endfor##
}
##endfor##


$str = SecuritySQL("Search");
if(strlen($str))
	$where.=" and ".$str;
$strSQL = gSQLWhere($where);

$strSQL.=" ".$gstrOrderBy;

$rowcount=gSQLRowCount($where,##@TABLE.bGroupBy##);

$xt->assign("row_count",$rowcount);
if ( $rowcount ) {
	$xt->assign("details_data",true);
	$rs=db_query($strSQL,$conn);
##comment if Fields[bListPage && (strViewFormat==FORMAT_FILE_IMAGE || strViewFormat==FORMAT_DATABASE_IMAGE)].len##
##if Fields[strViewFormat==FORMAT_FILE_IMAGE || strViewFormat==FORMAT_DATABASE_IMAGE].len##
	$display_count=5;
##else##
	$display_count=10;
##endif##	
	if($mode=="inline")
		$display_count*=2;
	if($rowcount>$display_count+2)
	{
		$xt->assign("display_first",true);
		$xt->assign("display_count",$display_count);
	}
	else
		$display_count = $rowcount;

	$rowinfo=array();
		
	while (($data = db_fetch_array($rs)) && $recordsCounter<$display_count) {
		$recordsCounter++;
		$row=array();
		$keylink="";
	##foreach @TABLE.arrKeyFields as @k##
		$keylink.="&key##@index##=".htmlspecialchars(rawurlencode(@$data["##@k s##"]));
	##endfor##

	##comment foreach Fields as @f filter @f.bListPage##
	##foreach Fields as @f filter @f.bListPage || @f.bViewPage || @f.bPrinterPage##
	//	##@f.strName## - ##@f.strViewFormat##
		    $value="";
	##if @f.strViewFormat==FORMAT_DATABASE_IMAGE##
		##if @f.bShowThumbnail##
			$value.="<a target=_blank 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##
			$value.=" width=##@f.ListFormatObj.nImageWidth##";
		##endif##
		##if @f.ListFormatObj.nImageHeight##
			$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##
	 	// show thumbnail
			$thumbname="##@f.strThumbnail s##".$data["##@f.strName s##"];
			if(substr("##@f.strhlPrefix s##",0,7)!="http://" && !myfile_exists(GetUploadFolder("##@f.strName s##").$thumbname))
				$thumbname=$data["##@f.strName s##"];
			$value="<a target=_blank 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##
			$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##",$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_PRINT);
	##elseif NeedEncode(@f)##
			$value = ProcessLargeText(GetData($data,"##@f.strName s##", "##@f.strViewFormat##"),"field=##@f.strName uhs##".$keylink,"",MODE_PRINT);
	##else##
			$value = GetData($data,"##@f.strName s##", "##@f.strViewFormat##");
	##endif##
			$row["##@f.strName g##_value"]=$value;
	##endfor##
	$rowinfo[]=$row;
	}
	$xt->assign_loopsection("details_row",$rowinfo);
} else {
}
$xt->display("##@TABLE.strShortTableName##_detailspreview.htm");
if($mode!="inline")
	echo "counterSeparator".postvalue("counter");
?>