<?php

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

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

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


function ShuffleTopGroups()
{
   global $HTTP_GET_VARS;

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

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

      // Swap the two, as long as there was a next or previous record to swap with
      if ($newtopgrouporderid != '') {
         $strQuery = "UPDATE ".$GLOBALS["eztbTopgroups"]." SET topgrouporderid='".$topgrouporderid."' WHERE topgroupname='".$newtopgroupname."'";
         $result = dbExecute($strQuery,true);
         $strQuery = "UPDATE ".$GLOBALS["eztbTopgroups"]." SET topgrouporderid='".$newtopgrouporderid."' WHERE topgroupname='".$HTTP_GET_VARS["TopGroupName"]."'";
         $result = dbExecute($strQuery,true);
      }
   }
   dbFreeResult($fromresult);
   dbCommit();

} // function ShuffleTopGroups()

?>

