<?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/page_delete.php - version 1.01
*/

$nSecureMe=1;

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

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

// config
$nPageName = "page_delete.php";
$nRefreshCall = "refreshPages();";

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

if($nRecID){
	$nSQL = "SELECT nNavTitle,nType FROM nPage WHERE nID='$nRecID' LIMIT 1";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	if($Row = mysql_fetch_array($Result)){
		$nName = $Row["nNavTitle"];
		$nType = $Row["nType"];
	} else { exit("<div align=\"center\" style=\"padding:20px;\">No record found.</div>"); }
} else { exit("<div align=\"center\" style=\"padding:20px;\">No record available. Invalid ID.</div>"); }

if($nUpdateDB){
	if($nRecID){
		// Delete all components/content of this page
		$nSQL = "SELECT nContentID FROM xComponentPage WHERE nPageID=$nRecID";
		mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
		while($Row = mysql_fetch_array($Result)){
			$nContentID = $Row["nContentID"];
			if($nContentID){ nDeleteContent($nContentID); }
		}
		$nSQL = "DELETE FROM xComponentPage WHERE nPageID='$nRecID'";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		// Delete page title records (all languages)
		$nSQL = "DELETE FROM nTranslate WHERE nType='$nType' AND nItemID='$nRecID'";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		// Delete record
		$nSQL = "DELETE FROM nPage WHERE nID='$nRecID' LIMIT 1";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		// Optmization
		$nSQL = "OPTIMIZE TABLE nPage";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		
		// Now that page no longer exists, replace all instances of this ID from system log with name
		$nSQL = "SELECT nID,nEventDesc FROM nSystemEventLog";
		mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
		while($Row = mysql_fetch_array($Result)){
			$nEventID = $Row["nID"];
			$nEventDesc = $Row["nEventDesc"];
			$nNewEventDesc = str_replace("[nPageID:$nRecID]","<strong>$nName</strong>",$nEventDesc);
			if($nNewEventDesc!=$nEventDesc){ nUpdateEventDesc($nEventID,$nNewEventDesc); }
		}
		
		// log system event
		$nSystemEventDesc = _("Page deleted:")." <strong>$nName</strong>";
		$Query = "INSERT INTO nSystemEventLog (nEventTime,nEventDesc,nEditorUser) VALUES ('".date("Y-m-d H:i:s")."','$nSystemEventDesc','$ValidUser')";
		mysql_select_db($DBName); mysql_query($Query, $Link);
	}
	echo "<script>$.colorbox.close();</script>";
	exit();
}
?>
<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="48" height="48" border="0"></td>
			<td class="blueBold" style="padding-right:10px;"><?php echo _("Are you sure you want to delete"); ?> <strong><?php echo $nName?></strong>?</td>
		  </tr>
		</table>
		
	</td>
  </tr>
  <tr>
	<td style="padding:10px; text-align:center;">
	<input type="submit" name="nYes" value="<?php echo _("Delete"); ?>" class="button black"> 
	<input type="button" name="nCancel" value="<?php echo _("Cancel"); ?>" class="button" onClick="$.colorbox.close();">
	</td>
  </tr>
</table>
</form>
<?php
function nDeleteContent($nContentID){
	global $DBName,$Link;
	if(!$nContentID){ return; }
	$nPageCount = nGetPageCount($nContentID);
	if($nPageCount<2){
		$nSQL = "DELETE FROM nContent WHERE nID='$nContentID' LIMIT 1";
		mysql_select_db($DBName);
		mysql_query($nSQL,$Link);
		// Delete any translations (all languages)
		$Query = "DELETE FROM nTranslate WHERE nItemID='$nContentID' AND nType='comp'";
		mysql_select_db($DBName);
		mysql_query($Query, $Link);
	}
}

function nUpdateEventDesc($nEventID,$nNewEventDesc){
	global $DBName,$Link;
	if(!$nEventID){ return; }
	$Query = "UPDATE nSystemEventLog SET nEventDesc='$nNewEventDesc' WHERE nID=$nEventID";
	mysql_select_db($DBName);
	mysql_query($Query, $Link);
}

function nGetPageCount($nContentID){
	global $DBName,$Link;
	$nSQL = "SELECT nID FROM xComponentPage WHERE nContentID='$nContentID'";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nTotalResults = mysql_num_rows($Result);
	return $nTotalResults;
}
?>
<?php require("../../inc/db_close.php"); ?>
