<?php

	/*
	*	Gekko - Open Source Web Development Framework
	*	------------------------------------------------------------------------
	*	Copyright (C) 2004-2005, J. Carlos Nieto <xiam@users.sourceforge.net>
	*	This program is Free Software.
	*
	*	@package	Gekko
	*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
	*	@author		J. Carlos Nieto <xiam@users.sf.net>
	*	@version	CVS: $Id:$
	*	@link		http://gekkoware.sourceforge.net
	*/

	define ("IN-GEKKO", true);

	require_once "../../conf.php";
	require_once GEKKO_ROOT."lib/core.lib.php";


	db_init();

	require_once GEKKO_ROOT."lib/auth.inc.php";

	if (!gauth("menu-editor")) abort(accessDenied());

	gpGet("action", 1);
	gpGet("id", 1);
	gpGlobalize("return", 1);

	switch ($action) {
		case "createblock":

			gpGlobalize("id");

			requireVars ($id);

			require_once GEKKO_ROOT."modules/blocks/main.lib.php";

			$block_id = createSimpleBlock(_l("L_MENU"), "", "menu-editor/menu-loader.php", "id={$id}", "menu.png");

			$return = "../../index.php/admin/blocks/edit/{$block_id}";
		break;
		case "move":
			gpGlobalize("id, where");

			requireVars($id, $where);

			$w = ($where == "up") ? "<" : ">";

			$db->query("
				SELECT
					MenuLink, OrderID FROM ".db_table("menu_link")."
				WHERE ID = '{$id}'
			");
			// wrong id!
			if (!$db->num_rows()) break;
			$row = $db->fetch_row();

			$db->query("SELECT ID, OrderID FROM ".db_table("menu_link")."
			WHERE MenuLink = '{$row["MenuLink"]}' AND OrderID $w '{$row["OrderID"]}'
			ORDER BY OrderID ".(($w == "<") ? "DESC": "ASC")." LIMIT 1"
			,__FILE__, __LINE__);

			// this is the latest or upper block!
			if (!$db->num_rows()) break;
			$row2 = $db->fetch_row();

			// exchanging OrderID
			$db->query("UPDATE ".db_table("menu_link")." SET
				OrderID = '{$row2["OrderID"]}'
			WHERE ID = '{$id}'");
			$db->query("UPDATE ".db_table("menu_link")." SET
				OrderID = '{$row["OrderID"]}'
			WHERE ID = '{$row2["ID"]}'");

		break;
		case "menu-delete":
			gpGlobalize("id");

			requireVars($id);

			$db->query("DELETE FROM ".db_table("menu_block")." WHERE ID = '{$id}'", __FILE__, __LINE__);

			$db->query("DELETE FROM ".db_table("menu_link")." WHERE MenuLink = '{$id}'", __FILE__, __LINE__);

			$return = "../../index.php/admin/menu-editor";

			slog("menu-editor: deleted menu id '$id'", "actions", 1);
		break;
		case "link-delete":
			gpGlobalize("id");

			requireVars($id);
			$db->query("SELECT MenuLink FROM ".db_table("menu_link")." WHERE ID = '{$id}'", __FILE__, __LINE__);
			$row = $db->fetch_row();

			$db->query("DELETE FROM ".db_table("menu_link")." WHERE ID = '{$id}'", __FILE__, __LINE__);

			$return = "../../index.php/admin/menu-editor/menu/{$row["MenuLink"]}";

			slog("menu-editor: deleted link '$id'", "actions", 3);
		break;
		case "menu-create":

			gpGlobalize("title, type, no_icons");

			requireVars($title);

			$db->query("
				INSERT INTO ".db_table("menu_block")." (
					ID,
					Title,
					Type,
					opt_noIcons
				) VALUES (
					".$db->free_id("menu_block").",
					'{$title}',
					'{$type}',
					'{$no_icons}'
				)
			", __FILE__, __LINE__);

			slog("menu-editor: created menu '$id'", "actions", 1);
		break;
		case "menu-edit":

			gpGlobalize("id, title, type, no_icons");

			requireVars($id, $title);

			$db->query("
				UPDATE ".db_table("menu_block")." SET
					Title = '{$title}',
					Type = '{$type}',
					opt_noIcons = '{$no_icons}'
				WHERE ID = '{$id}'
			", __FILE__, __LINE__);

			slog("menu-editor: modified menu id '$id'", "actions", 4);
		break;
		case "link-create":

			gpGlobalize("menu, auth_access, icon, title, type, link, tooltip, level, orderid");

			requireVars($menu, $title, $link);

			$db->query("SELECT OrderID FROM ".db_table("menu_link")." WHERE
			MenuLink = '{$menu}' ORDER BY OrderID ".(($orderid) ? "DESC": "ASC")." LIMIT 1");
			if ($db->num_rows()) {
				$row = $db->fetch_row();
				$orderid = $row["OrderID"] + ($orderid ? 1 : -1);
			} else {
				$orderid = 1;
			}

			$db->query("
				INSERT INTO ".db_table("menu_link")." (
					ID,
					MenuLink,
					OrderID,
					Type,
					Level,
					Title,
					Link,
					Tooltip,
					Icon,
					auth_Access
				) VALUES (
					'".$db->free_id("menu_link")."',
					'{$menu}',
					'{$orderid}',
					'{$type}',
					'{$level}',
					'{$title}',
					'{$link}',
					'{$tooltip}',
					'{$icon}',
					'{$auth_access}'
				)
			", __FILE__, __LINE__);

		break;
		case "link-edit":

			gpGlobalize("id, menu, auth_access, icon, title, type, link, tooltip, level,
			orderid");

			requireVars($id, $menu, $title, $link);

			$db->query("SELECT OrderID, MenuLink FROM ".db_table("menu_link")."
			WHERE ID = '{$id}'", __FILE__, __LINE__);

			$row = $db->fetch_row();

			if ($menu != $row["MenuLink"]) {
				$db->query("SELECT OrderID FROM ".db_table("menu_link")." WHERE
				MenuLink = '{$menu}' ORDER BY OrderID DESC LIMIT 1");
				if ($db->num_rows()) {
					$row2 = $db->fetch_row();
					$orderid = $row2["OrderID"]+1;
				}
			} elseif ($orderid != $row["OrderID"]) {
				$db->query("
				UPDATE ".db_table("menu_link")." SET
					OrderID = '{$row["OrderID"]}'
				WHERE OrderID = '$orderid'
				", __FILE__, __LINE__);
			}

			$db->query("
				UPDATE ".db_table("menu_link")." SET
					MenuLink = '{$menu}',
					OrderID = '{$orderid}',
					Type = '{$type}',
					Level = '{$level}',
					Title = '{$title}',
					Link = '{$link}',
					Tooltip = '{$tooltip}',
					Icon = '{$icon}',
					auth_Access = '{$auth_access}'
				WHERE ID = '{$id}'
			", __FILE__, __LINE__);

		break;
		default:
			abort(_l("L_UNDEFINED_ACTION"));
		break;
	}

	redirect($return ? $return : $_SERVER["HTTP_REFERER"]);

	db_exit();
?>