<?php
/*
 * This file is part of a larger software package. Please read the License file 
 * in the root of this installation for terms of usage and distribution.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * 
 * CORE: manage/list/images_show.php - version 1.0
*/

// This file requires the following functions be declared by the caller:
// deleteImage('fld')

$nSecureMe=1;

// Secure and connect
require("../../inc/dbconnect.php");
require("../inc/secure_page.php");

// bring in other required files
require("../../inc/config.php");
require("../inc/lang.php");
include("../inc/pageutil.php");
include("../inc/paginator.php");
include("../inc/calendar.php");
require("../inc/imgedit_gd.php");
include_once("../inc/imageedit_gd_class.php");

// Get passed-in vars
if(isset($_GET["fld"])){ $nFieldToUpdate = $_GET["fld"]; } else { $nFieldToUpdate = ""; }
$nImagesToShow = (isset($_GET["id"]) ? $_GET["id"] : NULL); // Comma-separated list of images IDs to show
if(!$nImagesToShow){ $nImagesToShow = 0; }

// Globals
$nSortableCreateARRAY = array();
$nSortableARRAY = array();

// Setup SQL query for displayed listing
$nListingSQL = "SELECT * FROM nImage WHERE nID=$nImagesToShow";

$nPassThru = "?id=$nImagesToShow"; // for refresh calls
$nPage = 1;
$nShowPerPg = 999;
$nDivToUpdate = "imageGallery";
$nPageInDiv = "list/images_show.php";

$nPagingText = nGetPaging($nListingSQL,$nPage,$nShowPerPg,$nPassThru,$nDivToUpdate,$nPageInDiv);

// for extrenal refresh requests
print "<script>refreshImages = function(){ loadContent('$nDivToUpdate','$nPageInDiv$nPassThru'); }</script>";
?>

<ul id="<?php echo $nDivToUpdate;?>" class="gallery" style="padding-bottom:10px;">
	<?php echo nGetImageList();?>
</ul>

<script type="text/javascript">
// Handle sorting and order saving
$("<?php if($nSortableARRAY){ echo implode(",",$nSortableARRAY); } ?>").sortable({
	distance: 20,
	update: function(event, ui) {
		var dataString = "group=" + $(this).attr("id") + "&order=" + $(this).sortable('toArray').toString();
		// save updated order
		var url = "edit/image_order.php";
		$.ajax({  
			type: "POST", url: url, data: dataString,  
			error: function(msg,msg2,errorMsg){ alert('error returned in call to '+url+': '+errorMsg); },
			success: function(msg){ }
		});
		$(this).effect("highlight", {}, 1500);
	}
});
</script>

<?php
function nGetImageList(){
	global $DBName,$Link,$nListingSQL;
	global $nShowPerPg,$nTotalRec,$nStartRec,$nEndRec,$nTotalPg,$nType,$nFieldToUpdate;
	global $nSortableCreateARRAY,$nSortableARRAY,$nScroll,$nRelID;
	if(!$nListingSQL){ return ""; }
	$nTable = "";
	$nTypeIDName = str_replace(" ","--",$nType);
	$nCurrRec = 0;
	mysql_select_db($DBName); $Result = mysql_query($nListingSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nCurrRec++;
		if($nCurrRec>=$nStartRec && $nCurrRec<=$nEndRec && $nCurrRec<=$nTotalRec){
			$nID = $Row["nID"];
			$nPath = $Row["nPath"];
			$nTitle = $Row["nTitle"];
			$nCaption = $Row["nCaption"];
			$nFilename = nGetImageNameFromPath($nPath);
			$nImgTag = "<img src=\"../$nPath\" alt=\"\" height=\"121\" border=\"0\">";
			if(file_exists("../../$nPath")){
				$nFilesize = filesize("../../$nPath");
				if($nFilesize){ $nFilesize = number_format(($nFilesize/1000),0,'.',''); }
				$nImgSizeARRAY = getimagesize("../../$nPath");
				$nImgW = $nImgSizeARRAY[0]; $nImgH = $nImgSizeARRAY[1]; $nResizeRatio = $nImgW/100;
				if($nImgH<121){ $nImgTag = "<img src=\"../$nPath\" alt=\"\" width=\"$nImgW\" height=\"$nImgH\" border=\"0\">"; }
				//if($nResizeRatio && ($nImgH/$nResizeRatio)>100){ $nImgTag = "<div style=\"overflow:hidden; height:121px; width:121px; vertical-align:middle;\">$nImgTag</div>"; }
			}
			if(!$nTitle){ $nTitle = $nFilename; }
			$nImageLink = "$nImgTag";
			$nHandle = "<td colspan=\"3\" id=\"galleryInfo\" class=\"handleComp\" style=\"width:100px\">&nbsp; &nbsp;$nTitle</td>";
			$nEdit = "<li class=\"edit\"><a href=\"javascript:;\" onClick=\"editImage('$nID');\" class=\"btn\">Edit</a></li>";
			$nDelete = "<li class=\"delete\"><a href=\"javascript:;\" onClick=\"deleteImage('$nFieldToUpdate');\" class=\"btn\">Delete</a></li>";
			$nTable.= <<<CODE
			<li id="libimg_$nID">
			    $nImageLink
			    <ul class="img_options">$nEdit $nDelete </ul>
			</li>
CODE;
		}
	}
	if(!$nTable){
		$nChooseButton = "<a href=\"list/images.php?t=$nType&id=$nRelID&mode=c&s=1\" title=\"Choose an image\" class=\"button modal\">Choose an image...</a>";
	} else {
		$nTable = "<div id=\"libImages_$nTypeIDName\">$nTable</div>";
	}
	// Create sortable
	$nSortableARRAY[] = "#libImages_$nTypeIDName";
	
	return $nTable;
}

function nGetImageNameFromPath($nPath){
	if(!$nPath){ return; }
	$nPathARRAY = explode("/",$nPath);
	$nImgName = $nPathARRAY[(count($nPathARRAY)-1)];
	return $nImgName;
}

function nGetSectionName($nRecID){
	global $DBName,$Link;
	$nSQL = "SELECT nName,nParentID FROM nSection WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nName = $Row["nName"];
		$nParentID = $Row["nParentID"];
	}
	if($nParentID){ $nParentName = nGetSectionName($nParentID)." &gt; "; } else { $nParentName = ""; }
	return "$nParentName$nName";
}

function nSectionExists($nSectionID){
	global $DBName,$Link;
	if(!$nSectionID){ return 0; }
	$nSQL = "SELECT nID FROM nSection WHERE nID=$nSectionID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nCount = mysql_num_rows($Result);
	return $nCount;
}
function nGetPageIDAccessARRAY($ValidUser){
	global $DBName,$Link;
	if(!$ValidUser){ return; }
	// Get user ID
	$nSQL = "SELECT nID FROM nEditor WHERE nUser='$ValidUser' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nEditorID = $Row["nID"];
	}
	$nDataARRAY = array();
	$nSQL = "SELECT nPageID FROM xEditorPage WHERE nEditorID=$nEditorID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nPageID"];
		$nDataARRAY[] = $nPageID;
	}
	return $nDataARRAY;
}
function nGetImgIDFromPath($nPath){
	global $DBName, $Link;
	if(!$nPath){ return; }
	$nID = "";
	$Query = "SELECT nID FROM nImage WHERE nPath='$nPath'";
	mysql_select_db($DBName); $Result = mysql_query($Query,$Link);
	while ($Row = mysql_fetch_array($Result)) {
		$nID = $Row["nID"];
	}
	return $nID;
}
?>
<?php require("../../inc/db_close.php"); ?>
