<?php
/*
 * This file is part of Web Pro Manager.
 * 
 * Web Pro Manager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Web Pro Manager is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Web Pro Manager.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author The Web Pro Miami, Inc. <info@thewebpro.com>
 * @copyright The Web Pro Miami, Inc. <info@thewebpro.com>, http://www.thewebpro.com/
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL
 * @package WebProManager
*/

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/config.php");
require("../inc/lang.php");

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

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

if($nRecID){
	
	$nSQL = "SELECT nName 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"];
	} else { echo "<div align=\"center\" style=\"padding:20px;\">No record found.</div>"; }
	
	// Get children count
	$nSQL = "SELECT nID FROM nSection WHERE nParentID='$nRecID'";
	mysql_select_db($DBName); $Result = mysql_query($nSQL,$Link);
	$nChildCount = mysql_num_rows($Result);
	
} else { echo "<div align=\"center\" style=\"padding:20px;\">"._("No record available. Invalid ID.")."</div>"; }

if($nUpdateDB){
	$nRecID = $_POST["nRecID"];
	if($nRecID){
		// Move all pages of this section to the Unclassified section (0)
		$Query = "UPDATE nPage SET nSectionID='0' WHERE nSectionID='$nRecID'";
		mysql_select_db($DBName);
		mysql_query($Query, $Link);
		// Delete record
		$nSQL = "DELETE FROM nSection WHERE nID='$nRecID' LIMIT 1";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		// Delete all children
		$nSQL = "DELETE FROM nSection WHERE nParentID='$nRecID' LIMIT 1";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		// Optmization
		$nSQL = "OPTIMIZE TABLE nSection";
		mysql_select_db($DBName);
		mysql_query($nSQL, $Link);
		
		// Now that section 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("[nSectionID:$nRecID]","<strong>$nName</strong>",$nEventDesc);
			if($nNewEventDesc!=$nEventDesc){ nUpdateEventDesc($nEventID,$nNewEventDesc); }
		}
		
		// log system event
		$nSystemEventDesc = _("Section 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="img/question.gif" alt="" width="32" height="32" border="0"></td>
			<td class="blueBold" style="padding-right:10px;">
				<?php echo _("Are you sure you want to delete"); ?> <strong><?php echo $nName?></strong>?<BR>
				<?php if($nChildCount){ ?><span class="caption" style="font-size:10px;"><?php echo _("(CAUTION: This will remove all sub-sections<BR>of this section as well.)"); ?></span><?php } ?>
			</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
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);
}
?>
<?php require("../../inc/db_close.php"); ?>
