<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
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"])
{ 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	return;
}
##endif##

$conn=db_connect(); 
$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##);


if ( $rowcount ) {
	$rs=db_query($strSQL,$conn);
	echo ##message DETAILS_FOUND##.": <strong>".$rowcount."</strong>";
	##if Fields[bListPage && (strViewFormat==FORMAT_FILE_IMAGE || strViewFormat==FORMAT_DATABASE_IMAGE)].len##
		echo ( $rowcount > 5 ) ? ". Displaying first: <strong>5</strong>.<br /><br />" : "<br /><br />";
	##else##
		echo ( $rowcount > 10 ) ? ". Displaying first: <strong>10</strong>.<br /><br />" : "<br /><br />";
	##endif##	
	echo "<table cellpadding=1 cellspacing=1 border=0 align=left class=\"detailtable\"><tr>";
	##foreach Fields as @f filter @f.bListPage order @f.nListPageOrder##	
	echo "<td><strong>##@f.strLabel s##</strong></td>";
	##endfor##
	echo "</tr>";
	while ($data = db_fetch_array($rs)) {
		$recordsCounter++;
		##if Fields[bListPage && (strViewFormat==FORMAT_FILE_IMAGE || strViewFormat==FORMAT_DATABASE_IMAGE)].len##
			if ( $recordsCounter > 5 ) { break; }
		##else##
			if ( $recordsCounter > 10 ) { break; }
		##endif##
		echo "<tr>";
		$keylink="";
	##foreach @TABLE.arrKeyFields as @k##
		$keylink.="&key##@index##=".htmlspecialchars(rawurlencode(@$data["##@k s##"]));
	##endfor##

	##foreach Fields as @f filter @f.bListPage order @f.nListPageOrder##
	//	##@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://" && !file_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##
/*	
			if(strlen($data["##@f.strName s##"]))
			{
				$strdata = make_db_value("##@f.strName s##",$data["##@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=$data["##@f.strName s##"];
				if($lookuprow=db_fetch_numarray($rsLookup))
					$lookupvalue=$lookuprow[0];
				$value=ProcessLargeText(GetDataInt($lookupvalue,$data,"##@f.strName s##", "##@f.strViewFormat s##"),"field=##@f.strName uhs##".$keylink,"",MODE_PRINT);
			}
			else
				$value="";
*/				
			$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##
			echo "<td>".$value."</td>";
	##endfor##
		echo "</tr>";
	}
	echo "</table>";
} else {
	echo ##message DETAILS_FOUND##.": <strong>".$rowcount."</strong>";
}

echo "counterSeparator".postvalue("counter");
?>