<?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";
	require_once GEKKO_ROOT."lib/format.lib.php";
	require_once GEKKO_ROOT."modules/blocks/main.lib.php";

	db_init();

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

	if (!gauth("blocks")) abort(accessDenied());

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

	switch ($action) {
		case "delete":
			execWithEach($id, "deleteBlock");
		break;
		case "enable":
			blockSetStatus($id, 1);
		break;
		case "move":

			gpGlobalize("id, where");

			requireVars($id, $where);

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

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

				$db->query("SELECT ID, OrderID FROM ".db_table("block")."
				WHERE Position = '{$row["Position"]}' 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()) {
					$row2 = $db->fetch_row();

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

					slog("blocks: block '$id' moved", "actions", 3);
				}
			}

		break;
		case "create":

			gpGlobalize("icon, position, status, title, content, modules,
			scriptpath, scriptvars, orderid, csstext, blockclass, auth_access");

			requireVars($title, $position, $modules);

			rtb_codefix($content);

			// joining modules
			$modules = isset($modules) ? implode(",", $modules) : "";

			// script existence
			if (!blockscript_exists($scriptpath)) $scriptpath = "";

			// working with variables
			$scriptvars = isset($scriptvars) ? bv_serialize($scriptvars) : "";

			// appending block
			$db->query ("SELECT OrderID FROM ".db_table("block")."
			WHERE Position = '{$position}' ORDER BY OrderID ".(($orderid) ? "DESC" : "ASC")." LIMIT 1",
			__FILE__, __LINE__);
			$row = $db->fetch_row();
			$orderid = $row["OrderID"]+ ($orderid ? 1 : -1);

			// inserting into database
			$db->query ("
			INSERT INTO ".db_table("block")."
				(
					ID,
					Icon,
					Position,
					Status,
					Title,
					Content,
					Modules,
					ScriptPath,
					ScriptVars,
					CreationTime,
					OrderID,
					cssText,
					blockClass,
					auth_Access
				)
					VALUES
				(
					'".$db->free_id("block")."',
					'$icon',
					'$position',
					'$status',
					'".htmlspecialchars($title)."',
					'$content',
					'$modules',
					'$scriptpath',
					'$scriptvars',
					'".time()."',
					'{$orderid}',
					'{$csstext}',
					'{$blockclass}',
					'{$auth_access}'
				)
			", __FILE__, __LINE__);

			slog("blocks: created '$title'", "actions", 1);

		break;
		case "edit":

			gpGlobalize("id, icon, position, status, title, content, modules,
			scriptpath, scriptvars, orderid, csstext, blockclass, auth_access");

			requireVars($id, $title, $position, $modules);

			rtb_codefix($content);

			// joining modules
			$modules = isset($modules) ? implode(",", $modules) : "";

			// script existence
			if (!blockscript_exists($scriptpath)) $scriptpath = "";

			// working with variables
			$scriptvars = isset($scriptvars) ? bv_serialize($scriptvars) : "";

			/*
				when whe change a block Position, OrderID must be
				changed too.
			*/
			$db->query("SELECT Position, OrderID FROM ".db_table("block")."
			WHERE ID = '{$id}'
			", __FILE__, __LINE__);

			$row = $db->fetch_row();

			if ($position != $row["Position"]) {
				$db->query("SELECT OrderID FROM ".db_table("block")." WHERE
				Position = '{$position}' ORDER BY OrderID DESC LIMIT 1",
				__FILE__, __LINE__);
				$row2 = $db->fetch_row();
				// overwriting $orderid
				$orderid = $row2["OrderID"]+1;
			} elseif ($orderid != $row["OrderID"]) {
				// order exchange
				$db->query("
				UPDATE ".db_table("block")." SET
					OrderID = '{$row["OrderID"]}'
				WHERE OrderID = '{$orderid}'", __FILE__, __LINE__);
			}

			$db->query("
			UPDATE ".db_table("block")." SET
				Icon = '{$icon}',
				Position = '{$position}',
				Status = '{$status}',
				Title = '".htmlspecialchars($title)."',
				Content = '{$content}',
				Modules = '{$modules}',
				ScriptPath = '{$scriptpath}',
				ScriptVars = '{$scriptvars}',
				OrderID = '{$orderid}',
				cssText = '{$csstext}',
				blockClass = '{$blockclass}',
				auth_Access = '{$auth_access}'
			WHERE ID = '{$id}'",
			__FILE__, __LINE__);

			slog("block: block '$id' was modified", "actions", 4);
		break;
		default:
			abort(_l("L_UNDEFINED_ACTION"));
		break;
	}

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

	db_exit();
?>