<?php
$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");

// config
$nRefreshCall = "refreshImages();";

// Get passed-in vars
$nRecID = (isset($_GET['id']) ? $_GET['id'] : NULL);
if(!$nRecID){ $nRecID = (isset($_POST['id']) ? $_POST['id'] : NULL); }

// AJAX Specific vars
$nPassThru = "";
$nDivToUpdate = "editModal";
$nPageInDiv = "dialog/image_edit.php";

// Get info
if($nRecID){
	$nSQL = "SELECT * FROM nImage WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nPath = $Row["nPath"];
		$nTitle = $Row["nTitle"];
		$nCaption = $Row["nCaption"];
	} else { exit("<div align=\"center\" style=\"padding:20px;\"><em>No record found.</em></div>"); }
	
	if($nPath){
		$nFilename = nGetImageNameFromPath($nPath);
		$nImgTag = "<img src=\"../$nPath\" alt=\"\" height=\"150\" 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<150){ $nImgTag = "<img src=\"../$nPath\" alt=\"\" width=\"$nImgW\" height=\"$nImgH\" border=\"0\">"; }
			if($nResizeRatio && ($nImgH/$nResizeRatio)>100){ $nImgTag = "<div style=\"overflow:hidden; height:121px; width:150px; vertical-align:middle;\">$nImgTag</div>"; }
		}
	}
	
	$nImagePlacementPageARRAY = nGetImagePlacementPageARRAY($nRecID);
	$nImagePlacementPageCount = count($nImagePlacementPageARRAY);
	$nImgPlacementText = "";
	if($nImagePlacementPageCount){
		$nImageLinkARRAY = array();
		foreach($nImagePlacementPageARRAY AS $nPageID => $nPageName){
			$nImageLinkARRAY[]="<a href=\"page_edit.php?id=$nPageID\">$nPageName</a>";
		}
		$nImgPlacementText = implode(", ",$nImageLinkARRAY);
	} else {
		$nImgPlacementText = "<em>Not yet linked to any pages.</em>";
	}
}

// Defaults

?>

<div id="<?php echo $nDivToUpdate;?>">
<?php
include("../inc/form_class.php");

$nFormName = "FormImageEdit";
$nForm1 = new Form();
//$nForm1->DEBUG = true;
$nForm1->Name = $nFormName;
$nForm1->Action = $_SERVER['PHP_SELF'];
$nForm1->CancelText = "Cancel";
$nForm1->CancelRedirect = "$.colorbox.close()";
$nForm1->CancelRedirectIsJS = true;
if($nRecID){
	$nForm1->SubmitText = "Save Changes";
	$nForm1->ModifyDBTable = "nImage";
	$nForm1->ModifyDBField = "nID";
	$nForm1->ModifyDBID = "$nRecID";
}
$nForm1->DontProcess = "id";
$nForm1->DBLink = $Link;
$nForm1->nUseADODB = false;
// AJAX settings
$nForm1->SubmitIsJS = true;
$nForm1->SubmitToJS = "return submitForm('$nDivToUpdate','$nPageInDiv$nPassThru','$nFormName');return false;";
// Form styling
$nForm1->CleanMarkup = true;
$nForm1->ButtonTemplate = "<div class=\"clearfix grey-highlight\"><div class=\"input no-label\">[#Buttons#]</div></div>\n";
$nForm1->SubmitClass = "button blue";
$nForm1->CancelClass = "button";
$nForm1->FieldTemplate = "<div class=\"clearfix\" id=\"[#RowID#]\" style=\"[#RowStyle#]\">[#Label#]<div class=\"input\" style=\"[#ItemStyle#]\">[#BeforeField#][#Field#][#AfterField#]<span class=\"info\">[#Caption#]</span></div></div>\n";
$nForm1->GroupTemplate = "<div id=\"[#ID#]\" style=\"[#Style#]\" class=\"[#Class#]\">\n[#Name#][#FieldCode#]\n</div>\n";

// Create groups
$nFormGroup1 = new FormGroup();

// Create fields
$nFormField = new FormField("hidden","id");
$nFormField->Value = $nRecID;
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("static","","");
$nFormField->Value = "<div style=\"float:left; margin-right:20px;\">$nImgTag</div><div style=\"float:left; margin-right:20px;\"><BR><strong>Filename:</strong><BR>$nFilename<BR><BR><strong>Dimensions:</strong><BR>{$nImgW}px x {$nImgH}px</div>";
$nFormField->RowSize = "Half";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("text","nTitle",_("Title:"));
$nFormField->Caption = _("Used in image galleries. Max. 150 characters.");
$nFormField->Rel = "tooltips";
$nFormField->Value = $nTitle;
$nFormField = $nForm1->AddValidation("maxlength",$nFormField,150);
$nFormField->RowSize = "Full";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("textarea","nCaption",_("Full Caption:"));
$nFormField->Caption = _("Used in image galleries. HTML is accepted.");
$nFormField->Rel = "tooltips";
$nFormField->Value = $nCaption;
$nFormField->RowSize = "Full";
$nFormGroup1->AddField($nFormField);

$nFormField = new FormField("static","",_("This image is linked to the following pages:"));
$nFormField->Value = "$nImgPlacementText";
$nFormField->RowStyle = "clear:both;float:none;";
$nFormField->RowSize = "Full";
$nFormGroup1->AddField($nFormField);

// Finalize and display form
$nForm1->AddGroup($nFormGroup1);
$nFormToDisplay = $nForm1->GetForm();

// AJAX redirect if form submitted w/o error, otherwise display form (again, if necessary)
if($nForm1->HasProcessed){
	// Post-processing
	echo "<script>$nRefreshCall $.colorbox.close();</script>";
} else {
	echo $nFormToDisplay;
	echo "<script>$.colorbox.resize();</script>";
}
?>
</div>
<?php
function nGetImageNameFromPath($nPath){
	if(!$nPath){ return; }
	$nPathARRAY = explode("/",$nPath);
	$nImgName = $nPathARRAY[(count($nPathARRAY)-1)];
	return $nImgName;
}
function nGetImagePlacementPageARRAY($nImgID){
	global $DBName,$Link;
	$nResultsARRAY = array();
	$nSQL = "SELECT xComponentPage.nPageID FROM nTranslate JOIN xComponentPage ON nTranslate.nItemID = xComponentPage.nContentID WHERE nTranslate.nImgID='$nImgID'";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	while($Row = mysql_fetch_array($Result)){
		$nPageID = $Row["nPageID"];
		if($nPageID){ $nPageName = nGetPageName($nPageID); }
		$nResultsARRAY["$nPageID"] = "$nPageName";
	}
	return $nResultsARRAY;
}
function nGetPageName($nPageID){
	global $DBName,$Link;
	if(!$nPageID){ return; }
	$nSQL = "SELECT nNavTitle FROM nPage WHERE nID=$nPageID LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nNavTitle = $Row["nNavTitle"];
	}
	return $nNavTitle;
}
?>
<?php require("../../inc/db_close.php"); ?>
