<?php

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

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


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


function ShuffleContents()
{
   global $HTTP_GET_VARS;

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

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

      // Swap the two, as long as there was a next or previous record to swap with
      if ($neworderid != '')
      {
         $strQuery = "UPDATE ".$GLOBALS["eztbContents"]." SET orderid='".$orderid."' WHERE contentname='".$newcontentname."'";
         $result = dbExecute($strQuery,true);
         $strQuery = "UPDATE ".$GLOBALS["eztbContents"]." SET orderid='".$neworderid."' WHERE contentname='".$HTTP_GET_VARS["ContentName"]."'";
         $result = dbExecute($strQuery,true);
      }
   }
   dbFreeResult($fromresult);
   dbCommit();

} // function ShuffleContents()

?>

