<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: folders.rearrange.php,v $ - $Revision: 1.6 $
// | $Date: 2004/07/21 02:49:32 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
$templatesused = 'redirect_folrearrange';
require_once('./global.php');

// ############################################################################
// Sort folders alphabetically
if ($cmd == 'autosort') {
	$folders = $DB_site->query("
		SELECT *
		FROM hive_folder
		WHERE userid = $hiveuser[userid]
		ORDER BY title
	");
	$display = 1;
	while ($folder = $DB_site->fetch_array($folders)) {
		$DB_site->query("
			UPDATE hive_folder
			SET display = $display
			WHERE folderid = $folder[folderid] AND userid = $hiveuser[userid]
		");
		$display++;
	}
}

// ############################################################################
// Move one folder up or down
if ($cmd != 'autosort') {
	$folderid = getinfo('folder', $folderid, true);
	$folders = $DB_site->query("
		SELECT *
		FROM hive_folder
		WHERE userid = $hiveuser[userid]
		ORDER BY display
	");
	$oldfolder = array();
	while ($thisfolder = $DB_site->fetch_array($folders)) {
		// Either this is the moving folder and we're moving it up, or the old folder is our man and we move it down
		if (!empty($oldfolder) and (($oldfolder['folderid'] == $folderid and $move == 'down') or ($thisfolder['folderid'] == $folderid and $move == 'up'))) {
			// Swap this folder's display order with the previous one
			$DB_site->query("
				UPDATE hive_folder
				SET display = $thisfolder[display]
				WHERE folderid = $oldfolder[folderid]
			");
			$DB_site->query("
				UPDATE hive_folder
				SET display = $oldfolder[display]
				WHERE folderid = $thisfolder[folderid]
			");
			break;
		}
		$oldfolder = $thisfolder;
	}
}

rebuild_folder_cache();
eval(makeredirect("redirect_folrearrange", "folders.list.php"));

?>