<?php

/***************************************************************************

 m_subcontentcatsdel.php
 ------------------------
 copyright : (C) 2002-2003 The ezContents Development Team

 ***************************************************************************/

/***************************************************************************
 The ezContents Development Team offers no warranties on this script.
 The owner/licensee of the script is solely responsible for any problems
 caused by installation of the script or use of the script.

 All copyright notices regarding ezContents must remain intact on the
 scripts and in the HTML for the scripts.

 For more info on ezContents,
 visit http://www.ezcontents.org/

/***************************************************************************

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the License which can be found within the
 *   zipped package.
 *
 ***************************************************************************/

include_once ("rootdatapath.php");
require_once ($GLOBALS["rootdp"]."include/config.php");
require_once ($GLOBALS["rootdp"]."include/db.php");
require_once ($GLOBALS["rootdp"]."include/session.php");
include ($GLOBALS["rootdp"]."include/access.php");
include ($GLOBALS["rootdp"]."include/functions.php");
include ("adminfunctions.php");

$GLOBALS["form"] = 'subcontent';
$validaccess = VerifyAdminLogin();


GetSpecialData($HTTP_GET_VARS["SCID"]);

if ($GLOBALS["candelete"] == False)
{
   Header("Location: ".BuildLink('adminlogin.php'));
}
else
{
   DeleteCat();
   Header("Location: ".BuildLink('m_subcontentcats.php')."&SCID=".$HTTP_GET_VARS["SCID"]."&page=".$HTTP_GET_VARS["page"]);
}


function DeleteCat()
{
   global $HTTP_GET_VARS;

   // Retrieve the cat reference before we delete it so we know its parent if it's a subcategory
   $strQuery="SELECT * FROM ".$GLOBALS["scCatTable"]." WHERE catid='".$HTTP_GET_VARS["CatID"]."'";
   $result = dbRetrieve($strQuery,true,0,0);
   $rs     = dbFetch($result);
   $CatRef = $rs["catref"];
   dbFreeResult($result);
   $catparents = explode('.',$CatRef);
   $discard = array_pop($catparents);
   $NewCatRef = implode('.',$catparents);


   // Delete the required record
   $strQuery = "DELETE FROM ".$GLOBALS["scCatTable"]." WHERE catid='".$HTTP_GET_VARS["CatID"]."'";
   $result = dbExecute($strQuery,true);


   // Move all child categories of the 'now deceased' parent up a level to the new parent tree
   $strQuery = "SELECT * FROM ".$GLOBALS["scCatTable"]." where catref LIKE '".$CatRef.".%'";
   $cresult = dbRetrieve($strQuery,true,0,0);
   while ($crs = dbFetch($cresult))
   {
      $newcatref = $NewCatRef.substr($crs["catref"],strlen($CatRef));
      $strQuery = "UPDATE ".$GLOBALS["scCatTable"]." SET catref='".$newcatref."' WHERE catid='".$crs["catid"]."'";
      $uresult = dbExecute($strQuery,true);
   }
   dbFreeResult($cresult);
   // Update any news articles that might be in this branch of the category hierarchy
   $strQuery = "SELECT ".$HTTP_GET_VARS["SCID"]."id AS id,catid FROM ".$GLOBALS["scTable"]." where catid='".$CatRef."' or catid LIKE '".$CatRef.".%'";
   $cresult = dbRetrieve($strQuery,true,0,0);
   while ($crs = dbFetch($cresult))
   {
      $newcatref = $NewCatRef.substr($crs["catref"],strlen($CatRef));
      $strQuery = "UPDATE ".$GLOBALS["scTable"]." SET catid='".$newcatref."' WHERE ".$HTTP_GET_VARS["SCID"]."id='".$crs["id"]."'";
      $uresult = dbExecute($strQuery,true);
   }
   dbFreeResult($cresult);

   dbCommit();
} // function DeleteTag()

?>
