<?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
	*/

	if (!defined("IN-GEKKO")) die("Get a life!");

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

	$action = _ext("action", 2);
	$page = _ext("page", 2);
	$id = _ext("id", 3);

	$show = isset($_GET["show"]) ? $_GET["show"] : Conf::getKey("modules", "default");

	$status = Array (_l("L_DISABLED"), _l("L_ENABLED"));

	$classes = Array();
	$tmp = explode(",", conf::getKey("blocks", "block_classes"));
	foreach ($tmp as $class) {
		if (($class = trim($class)) !== false)
			$classes[$class] = $class;
	}

	switch ($action) {
		case "edit":
			if (!$id) break;

			$tpl = new ContentBlock();

			$tpl->insert("BLOCK_CONTENT", "blocks/index.admin.tpl");
			$tpl->insert("BLOCKS.FORM", "blocks/blocks.form.tpl");

			$db->query("
				SELECT
					ID, Modules, Icon, Title, Content, ScriptPath, ScriptVars,
					OrderID, Position, blockClass, CreationTime, auth_Access,
					cssText, Status
				FROM ".db_table("block")." WHERE ID = '{$id}'
			", __FILE__, __LINE__);

			if ($db->num_rows()) {

				$row = $db->fetch_row();
				saneHTML($row, "ScriptVars");

				// order
				$order = Array();
				$db->query("
					SELECT OrderID FROM ".db_table("block")."
					WHERE Position = '{$row["Position"]}'
					ORDER BY OrderID ASC
				", __FILE__, __LINE__);
				while ($row2 = $db->fetch_row()) {
					$order[$row2["OrderID"]] = $row2["OrderID"];
				}

				$row["ORDER.SEL"] = mkSelect($order, $row["OrderID"], "orderid");

				$rtb = new rtbEditor($row["Content"]);
				$rtb->setSize(350, 150);

				$row["BLOCK_TITLE"] = _l("L_BLOCKS");
				$row["BLOCK_SUBTITLE"] = _l("L_EDIT");
				$row["BLOCK_ICON"] = mkIcon("blocks.png", 16);

				$row["ICON.CHOOSER"] = mkIconChooser(16, $row["Icon"]);
				$row["STATUS.SEL"] = mkSelect($status, $row["Status"], "status");
				$row["BLOCKCLASS.SEL"] = mkSelect($classes, $row["blockClass"], "blockclass");
				$row["POSITION.SEL"] = mkSelect(getBlockPositions(), $row["Position"], "position");

				$row["SCRIPTPATH.SEL"] = mkSelect(getBlockScripts(), $row["ScriptPath"], "scriptpath");
				$row["MODULES"] = mkModuleChooser($row["Modules"], (strpos($row["Modules"], "*") !== false));

				$row["ACTION"] = "edit";
				$row["RETURN"] = "../../index.php/admin/blocks";
				$row["RTBEDITOR"] = $rtb->make();

				$vars = unserialize($row["ScriptVars"]);
				if ($row["ScriptPath"]) {
					$tmp = getBlockData($row["ScriptPath"]);
					$row["ScriptVars"] = "";
					foreach ($tmp["variables"] as $name => $value) {
						$row["ScriptVars"] .= "$name:<br /><input class=\"text\" type=\"text\" name=\"scriptvars[$name]\" size=\"40\" value=\"".(isset($vars[$name]) ? htmlspecialchars($vars[$name]) : "")."\" /><hr />";
					}
				}
				//$row["ScriptVars"] = bv_unserialize(serialize($vars));

				$tpl->set_arr($row);

				$mcBuff = $tpl->make();
			}

		break;
		default:

			$tpl = new ContentBlock();

			$tpl->insert("BLOCK_CONTENT", "blocks/index.admin.tpl");
			$tpl->insert("BLOCKS.FORM", "blocks/blocks.form.tpl");

			$db->query ("
				SELECT
					ID, Icon, Title, Position, OrderID, auth_Access, Status,
					Modules, ScriptPath, blockClass
				FROM ".db_table("block")."
				WHERE (Modules LIKE '%$show%' OR Modules LIKE '%*%')
				ORDER BY OrderID ASC
			", __FILE__, __LINE__);

			$blocks = $db->num_rows();

			// will handle block placements
			$buff = Array();

			// setting block placement as null
			$bp = explode(",", Conf::getKey("blocks", "block_positions"));
			foreach ($bp as $p) {
				$buff["BLOCK".trim($p)] = "";
			}

			// filling blocks
			while ($row = $db->fetch_row()) {

				if (blockCheckAuth($show, $row["Modules"], "*")) {

					saneHTML($row);

					$row["Icon"] = mkIcon($row["Icon"], 16);

					$row["Modules"] = str_replace(",", ", ", $row["Modules"]);

					/* default class name: block */
					if (!in_array($row["blockClass"], $classes)) $row["blockClass"] = "block";

					$row["Actions"] = Array();
					$row["Actions"][] = mkLink("modules/blocks/main.act.php?action=move&where=up&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_MOVE_UP"), "up.png", false, "icon");
					$row["Actions"][] = mkLink("modules/blocks/main.act.php?action=move&where=down&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_MOVE_DOWN"), "down.png", false, "icon");
					$row["Actions"][] = mkLink("index.php/admin/blocks/edit/".$row["ID"], _l("L_EDIT"), "edit.png", false, "icon");
					$row["Actions"][] = mkLink("modules/blocks/main.act.php?action=delete&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_DELETE"), "delete.png", true, "icon");
					if (!$row["Status"]) {
						$row["Actions"][] = mkLink("modules/blocks/main.act.php?action=enable&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_ENABLE"), "enable.png", false, "icon");
					}
					$row["Actions"] = mkButtonDisplay($row["Actions"], "iconbox");

					$row["Status"] = $status[$row["Status"]];

					//$row["auth_Access"] = getGroupNames(explode(",", $row["auth_Access"]));

					$tpl->set_arr($row,"BLOCK");
					$buff["BLOCK".$row["Position"]] .= $tpl->make("BLOCK");
				}
			}

			// Loading and setting up rtbEditor
			$rtb = new rtbEditor();
			$rtb->setSize(350, 150);

			$tpl->block("BLOCK", " ");

			$modsel = Array();
			$modules = modCore::getModules();
			$modlist["*"] = _l("L_ANY");
			foreach ($modules as $module) {
				if (file_exists(GEKKO_ROOT."modules/$module/main.inc.php")) {
					$modlist[$module] = $module;
				}
			}

			$tpl->set_arr (
				Array (
					"BLOCK_TITLE" => _l("L_BLOCKS"),
					"BLOCK_SUBTITLE" => _l("L_CREATE_BLOCK"),
					"BLOCK_ICON" => mkIcon("blocks.png", 16),
					"ICON.CHOOSER" => mkIconChooser(16, "blocks.png"),
					"STATUS.SEL" => mkSelect(array(_l("L_DISABLED"), _l("L_ENABLED")), 1, "status"),
					"BLOCKCLASS.SEL" => mkSelect($classes, "block", "blockclass"),
					"POSITION.SEL" => mkSelect(getBlockPositions(), Conf::getKey("blocks", "default_position", "L"), "position"),
					"ACTION" => "create",
					"SCRIPTPATH.SEL" => mkSelect(getBlockScripts(), ""),
					"MODULES" => mkModuleChooser(),
					"TITLE" => "",
					"CONTENT" => "",
					"ORDER.SEL" => mkSelect(array(_l("L_TOP"), _l("L_BOTTOM")), 1, "orderid"),
					"FOOTER" => "",
					"ID" => 0,
					"MODULES.OPTIONS" => mkSelect($modlist, $show),
					"RETURN" => "",
					"BLOCKS" => $blocks,
					"CSSTEXT" => "",
					"SCRIPTVARS" => "",
					"AUTH_ACCESS" => "*",
					"RTBEDITOR" => $rtb->make()
				)
			);
			$tpl->set_arr($buff);

			$mcBuff = $tpl->make();

		break;
	}

?>