<?php

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

 m_authorsdel.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");

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


if ($GLOBALS["candelete"] == False)
{
   Header("Location: ".BuildLink('adminlogin.php'));
}
else
{
   DeleteAuthor();
   Header("Location: ".BuildLink('m_authors.php')."&page=".$HTTP_GET_VARS["page"]."&sort=".$HTTP_GET_VARS["sort"]."&filtergroupname=".$HTTP_GET_VARS["filtergroupname"]);
}


function DeleteAuthor()
{
   global $HTTP_GET_VARS;

   $authorId = GetTransferAuthor();

   if (ValidateDelete($HTTP_GET_VARS["AuthorID"]))
   {
      // If this author has created any data, we transfer ownership of those entries to admin
      $strQuery = "UPDATE ".$GLOBALS["eztbTopgroups"]." SET authorid='".$authorId."' WHERE authorid='".$HTTP_GET_VARS["AuthorID"]."'";
      $result = dbExecute($strQuery,true);

      $strQuery = "UPDATE ".$GLOBALS["eztbGroups"]." SET authorid='".$authorId."' WHERE authorid='".$HTTP_GET_VARS["AuthorID"]."'";
      $result = dbExecute($strQuery,true);

      $strQuery = "UPDATE ".$GLOBALS["eztbSubgroups"]." SET authorid='".$authorId."' WHERE authorid='".$HTTP_GET_VARS["AuthorID"]."'";
      $result = dbExecute($strQuery,true);

      $strQuery = "UPDATE ".$GLOBALS["eztbContents"]." SET authorid='".$authorId."' WHERE authorid='".$HTTP_GET_VARS["AuthorID"]."'";
      $result = dbExecute($strQuery,true);

      // Only then do we delete the author
      $strQuery = "DELETE FROM ".$GLOBALS["eztbAuthors"]." WHERE authorid='".$HTTP_GET_VARS["AuthorID"]."'";
      $result = dbExecute($strQuery,true);
      dbCommit();
   }
} // function DeleteAuthor()


function GetTransferAuthor()
{
   $authorId = 1;
   $strQuery = "SELECT authorid,usergroup FROM ".$GLOBALS["eztbAuthors"]." WHERE usergroup='".$GLOBALS["gsAdminPrivGroup"]."'";
   $result = dbRetrieve($strQuery,true,0,1);
   if ($rs = dbFetch($result))
   {
      if ($rs["usergroup"] != $GLOBALS["gsAdminPrivGroup"]) { $authorId = $rs["authorid"]; }
   }
   dbFreeResult($result);
   return $authorId;
} // function sGetGroupName()


function ValidateDelete($authorId)
{
   $valid = True;
   $strQuery = "SELECT usergroup FROM ".$GLOBALS["eztbAuthors"]." WHERE authorid='".$authorId."'";
   $result = dbRetrieve($strQuery,true,0,1);
   if ($rs = dbFetch($result))
   {
      if ($rs["usergroup"] == $GLOBALS["gsAdminPrivGroup"]) { $valid = False; }
   }
   dbFreeResult($result);
   return $valid;
} // function sGetGroupName()


?>
