<?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/dialog/file_delete.php - version 1.0
*/

require("../inc/stop_cache.php");
$nAdminOnlyPg = 1;

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

// bring in other required files
require("../inc/lang.php");

// config
$nPageName = "file_delete.php";
$nRefreshCall = "nLoadAjaxFiles();";

// Get passed-in vars
$nUpdateDB = (isset($_POST["nUpdateDB"]) ? $_POST["nUpdateDB"] : NULL);
$nRecID = (isset($_GET['id']) ? $_GET['id'] : NULL);

$nPageCount = 0;
//$nPageCount = nGetContentCountForImage($nRecID);

if(!$nUpdateDB){
	if($nRecID){
		// some stuff
		$nSQL = "SELECT nPath FROM nFile WHERE nID='$nRecID' LIMIT 1";
		mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
		if($Row = mysql_fetch_array($Result)){
			$nPath = $Row["nPath"];
		}
	} else { echo "<div align=\"center\" style=\"padding:20px;\">"._("No record available. Invalid ID.")."</div>"; }
} else {
	$nRecID = $_POST["nRecID"];
	if($nRecID){
		if($nPageCount<1){
			$nSQL = "SELECT nPath FROM nFile WHERE nID='$nRecID' LIMIT 1";
			mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
			if($Row = mysql_fetch_array($Result)){
				$nPath = $Row["nPath"];
			}
			if($nPath && file_exists("../../".$nPath)){ unlink("../../".$nPath); }
			// Delete record
			$nSQL = "DELETE FROM nFile WHERE nID='$nRecID' LIMIT 1";
			mysql_select_db($DBName);
			mysql_query($nSQL, $Link);
		}
		// Optmization
		$nSQL = "OPTIMIZE TABLE nFile";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
	}
	echo "<script>$.colorbox.close();</script>";
	exit();
}
?>

<?php if($nPageCount<1){ ?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="dialogForm" id="dialogForm" onSubmit="return submitFormModal('dialog/<?php echo $nPageName;?>?id=<?php echo $nRecID?>','dialogForm','<?php echo $nRefreshCall;?>');return false;">
	<input type="hidden" name="nUpdateDB" value="1">
	<input type="hidden" name="nRecID" value="<?php echo $nRecID?>">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td height="80" align="center" style="padding:10px;">
		
		<table border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td valign="top" style="padding-right:10px;"><img src="assets/images/icons/question.png" alt="" width="32" height="32" border="0"></td>
			<td class="blueBold" style="font-size:12px;padding-right:10px;"><?php echo _("Are you sure you want to delete this document?"); ?></td>
		  </tr>
		</table>
		
	</td>
  </tr>
  <tr>
	<td align="right" style="padding:10px;">
	<input type="submit" name="nYes" value="<?php echo _("Yes"); ?>" style="width:88px;"> &nbsp; 
	<input type="button" name="nCancel" value="<?php echo _("No"); ?>" style="width:88px;" onClick="$.colorbox.close();">
	</td>
  </tr>
</table>
</form>
<?php } else { ?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
	<td height="80" align="center" style="padding:10px;">
		
		<?php
		$nPageCountText = ngettext("It is currently in use %d time throughout the site.", "It is currently in use %d times throughout the site.", $nPageCount);
		?>
		
		<table border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td align="center" class="blueBold" style="font-size:12px;padding-right:10px;"><strong><?php echo _("This document cannot be deleted."); ?></strong><BR><BR><?php echo $nPageCountText;?></td>
		  </tr>
		</table>
		
	</td>
  </tr>
</table>
<?php } ?>

<?php
function nGetContentCountForImage($nImgID){
	global $DBName,$Link;
	if(!$nImgID){ return 0; }
	$nSQL = "SELECT nID FROM nContent WHERE nImgID=$nImgID";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nTotalContentResults = mysql_num_rows($Result);
	
	$nTotalResults = $nTotalContentResults + $nTotalHomeResults;
	return $nTotalResults;
}
?>
<?php require("../../inc/db_close.php"); ?>
