<?php

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

 m_groupsmove.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"] = 'groups';
$validaccess = VerifyAdminLogin();


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


function ShuffleGroups()
{
   global $HTTP_GET_VARS;

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

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

      // Swap the two, as long as there was a next or previous record to swap with
      if ($newgrouporderid != '')
      {
         $strQuery = "UPDATE ".$GLOBALS["eztbGroups"]." SET grouporderid='".$grouporderid."' WHERE groupname='".$newgroupname."'";
         $result = dbExecute($strQuery,true);
         $strQuery = "UPDATE ".$GLOBALS["eztbGroups"]." SET grouporderid='".$newgrouporderid."' WHERE groupname='".$HTTP_GET_VARS["GroupName"]."'";
         $result = dbExecute($strQuery,true);
      }
   }
   dbFreeResult($fromresult);
   dbCommit();

} // function ShuffleGroups()

?>

