<?php

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

 m_subgroupsmove.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");
require_once ($GLOBALS["rootdp"]."include/settings.php");
include ($GLOBALS["rootdp"]."include/access.php");
include ($GLOBALS["rootdp"]."include/functions.php");


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

if ($GLOBALS["canedit"] == True)
{
   ShuffleSubGroups();
   Header("Location: ".BuildLink('m_subgroups.php')."&page=".$HTTP_GET_VARS["page"]."&filtergroupname=".$HTTP_GET_VARS["filtergroupname"]);
}
else
{
   Header("Location: ".BuildLink('adminlogin.php'));
}


function ShuffleSubGroups()
{
   global $HTTP_GET_VARS;

   // get subgrouporderid for the group that we want to move....
   $strQuery = "SELECT groupname,subgrouporderid FROM ".$GLOBALS["eztbSubgroups"]." WHERE subgroupname='".$HTTP_GET_VARS["SubGroupName"]."' AND language='".$GLOBALS["gsDefault_language"]."'";
   $fromresult = dbRetrieve($strQuery,true,0,1);
   while ($rs = dbFetch($fromresult))
   {
      $groupname       = $rs["groupname"];
      $subgrouporderid = $rs["subgrouporderid"];

      // get subgrouporderid for the previous or next group in the list
      $newsubgrouporderid = '';
      if ($HTTP_GET_VARS["direction"] == 'up')
      {
         $strQuery = "SELECT subgroupname,subgrouporderid FROM ".$GLOBALS["eztbSubgroups"]." WHERE subgrouporderid<'".$subgrouporderid."' AND groupname='".$groupname."' AND language='".$GLOBALS["gsDefault_language"]."' ORDER BY subgrouporderid DESC";
      }
      else
      {
         $strQuery = "SELECT subgroupname,subgrouporderid FROM ".$GLOBALS["eztbSubgroups"]." WHERE subgrouporderid>'".$subgrouporderid."' AND groupname='".$groupname."' AND language='".$GLOBALS["gsDefault_language"]."' ORDER BY subgrouporderid";
      }
      $toresult = dbRetrieve($strQuery,true,0,1);
      while ($rs = dbFetch($toresult))
      {
         $newsubgroupname    = $rs["subgroupname"];
         $newsubgrouporderid = $rs["subgrouporderid"];
      }
      dbFreeResult($toresult);

      // Swap the two, as long as there was a next or previous record to swap with
      if ($newsubgrouporderid != '')
      {
         $strQuery = "UPDATE ".$GLOBALS["eztbSubgroups"]." SET subgrouporderid='".$subgrouporderid."' WHERE subgroupname='".$newsubgroupname."'";
         $result = dbExecute($strQuery,true);
         $strQuery = "UPDATE ".$GLOBALS["eztbSubgroups"]." SET subgrouporderid='".$newsubgrouporderid."' WHERE subgroupname='".$HTTP_GET_VARS["SubGroupName"]."'";
         $result = dbExecute($strQuery,true);
      }
   }
   dbFreeResult($fromresult);
   dbCommit();

} // function ShuffleSubGroups()

?>

