<?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!");

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

	/* menu types  */
	$menu_types = Array(
		0 => _l("L_NORMAL_MENU"),
		1 => _l("L_HORIZONTAL_MENU")
	);

	/* indentation level */
	$link_levels = Array(0,1,2,3);

	function mkMenuSelect($selected = null, $name = "menu") {
		$menu = Array();
		$q = $GLOBALS["db"]->query("SELECT ID, Title FROM ".db_table("menu_block")." ORDER BY Title DESC", __FILE__, __LINE__, true);
		while ($row = $GLOBALS["db"]->fetch_row($q)) {
			$menu[$row["ID"]] = $row["Title"];
		}
		return mkSelect($menu, $selected, "menu");
	}
	function mkMenuOrderSel($menu, $selected, $name = "orderid") {
		$or = Array(0);
		$q = $GLOBALS["db"]->query("SELECT OrderID FROM ".db_table("menu_link")."
		WHERE MenuLink = '{$menu}'
		ORDER BY OrderID ASC",
		__FILE__, __LINE__, true);
		while ($row2 = $GLOBALS["db"]->fetch_row($q)) {
			$or[$row2["OrderID"]] = $row2["OrderID"];
		}
		return mkSelect($or, $selected, $name);
	}

	switch ($action) {

		case "link":
			if ($id) {

				$tpl = new ContentBlock();

				$tpl->insert ("BLOCK_CONTENT", "menu-editor/link-editor.form.tpl");

				$db->query ("
					SELECT
						ID, MenuLink, OrderID, Type, Level, Title,
						Link, Tooltip, Icon, auth_Access
					FROM ".db_table("menu_link")."
					WHERE ID = '{$id}'
				", __FILE__, __LINE__);

				if ($db->num_rows()) {
					$row = $db->fetch_row();

					$row["ACTION"] = "edit";
					$row["LEVEL.SEL"] = mkSelect($link_levels, $row["Level"], "level");
					$row["MENU.SEL"] = mkMenuSelect($row["MenuLink"]);
					$row["ICON.CHOOSER"] = mkIconChooser(16, $row["Icon"]);
					$row["ORDER.SEL"] = mkMenuOrderSel($row["MenuLink"], $row["OrderID"]);
					$row["ACTIONS"] = Array (
						mkLink("modules/menu-editor/main.act.php?action=link-delete&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_DELETE"), false, 1)
					);
					$row["ACTIONS"] = mkButtonDisplay($row["ACTIONS"]);
					$row["RETURN"] = "";

					$tpl->set_arr ($row, "NLINK");
					$tpl->saveBuff("NLINK");

					$tpl->set_arr (
						Array (
							"BLOCK_TITLE" => _l("L_EDIT_LINK"),
							"BLOCK_ICON" => mkIcon("menu-editor.png", 16),
						)
					);

					$mcBuff = $tpl->make();
				}
			} else {
				$mcBuff = accessDenied();
			}
		break;
		case "menu":
			if ($id) {

				$tpl = new ContentBlock();

				$tpl->insert ("BLOCK_CONTENT", "menu-editor/index.admin.tpl");
				$tpl->insert ("MENU-EDITOR.FORM", "menu-editor/menu-editor.form.tpl");

				$tpl->insert ("LINK-EDITOR.FORM", "menu-editor/link-editor.form.tpl",
					Array (
						"V_ID" => 0,
						"V_TITLE" => "",
						"V_ACTION" => "create",
						"V_TYPE.SEL" => mkSelect(Array(_l("L_NONE")), 0, "type"),
						"V_MENU.SEL" => mkMenuSelect($id),
						"V_AUTH_ACCESS" => "*",
						"V_ICON.CHOOSER" => mkIconChooser(16, "default.png"),
						"V_TOOLTIP" => "",
						"V_LINK" => "http://",
						"V_ORDER.SEL" => mkSelect(Array(_l("L_TOP"), _l("L_BOTTOM")), 1, "orderid"),
						"V_LEVEL.SEL" => mkSelect($link_levels, 0, "level"),
						"V_RETURN" => "",
						"V_ACTIONS" => ""
					)
				);

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

				if (!$db->num_rows()) break;

				$row = $db->fetch_row();
				saneHTML($row);

				$row["ACTION"] = "edit";
				$row["RETURN"] = "../../index.php/admin/menu-editor";
				$row["TYPE.SEL"] = mkSelect($menu_types, $row["Type"], "type");
				$row["OPTIONS"] = mkCheckBox("no_icons", _l("L_NO_ICONS"), $row["opt_noIcons"]);

				$tpl->set_arr (
					$row, "NMENU"
				);
				$tpl->saveBuff ("NMENU");

				$db->query ("
				SELECT
					ID, Icon, Title, Link, Tooltip, Level, auth_Access
				FROM ".db_table("menu_link")."
				WHERE MenuLink = '{$id}' ORDER BY OrderID ASC
				", __FILE__, __LINE__);

				$links = $db->num_rows();
				while ($row = $db->fetch_row()) {
					saneHTML($row);

					$row["ACTIONS"] = Array (
						mkLink("modules/menu-editor/main.act.php?action=move&where=up&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_MOVE_UP"), "up.png", false, "icon"),
						mkLink("modules/menu-editor/main.act.php?action=move&where=down&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_MOVE_DOWN"), "down.png", false, "icon"),
						mkLink("index.php/admin/menu-editor/link/{$row["ID"]}", _l("L_EDIT"), "edit.png", false, "icon"),
						mkLink("modules/menu-editor/main.act.php?action=link-delete&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_DELETE"), "delete.png", true, "icon")
					);
					$row["ACTIONS"] = mkButtonDisplay($row["ACTIONS"], "iconbox");

					$row["MARGIN_LEVEL"] = $row["Level"]*8;
					$row["ICON"] = mkIcon($row["Icon"], 16);
					$tpl->set_arr ($row, "LINK");
					$tpl->saveBuff("LINK");
				}

				$tpl->set_arr(
					Array (
						"BLOCK_TITLE" => _l("L_MENU-EDITOR"),
						"BLOCK_SUBTITLE" => _l("L_EDIT"),
						"BLOCK_ICON" => mkIcon("menu-editor.png", 16),
						"LINK-EDITOR.TITLE" => _l("L_CREATE_LINK"),
						"LINKS" => $links
					)
				);

				$mcBuff = $tpl->make();
			} else {
				$mcBuff = accessDenied();
			}
		break;
		default:

			$tpl = new ContentBlock();

			$tpl->insert("BLOCK_CONTENT", "menu-editor/index.admin.tpl");

			$tpl->insert("MENU-EDITOR.FORM", "menu-editor/menu-editor.form.tpl",
				Array (
					"V_ID" => 0,
					"V_TITLE" => "",
					"V_OPTIONS" => mkCheckBox("no_icons", _l("L_NO_ICONS"), false),
					"V_TYPE.SEL" => mkSelect($menu_types, 0, "type"),
					"V_ACTION" => "create",
					"V_RETURN" => ""
				)
			);

			$db->query("SELECT ID, Title, Type FROM ".db_table("menu_block")."
			ORDER BY Title ASC", __FILE__, __LINE__);

			/* how many? */
			$menus = $db->num_rows();

			$menu = Array();
			while ($row = $db->fetch_row()) {

				saneHTML($row);

				$menu[$row["ID"]] = $row["Title"];

				$row["ACTIONS"] = Array (
					mkLink("modules/menu-editor/main.act.php?action=createblock&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_CREATE_BLOCK")),
					mkLink("index.php/admin/menu-editor/menu/".$row["ID"], _l("L_EDIT")),
					mkLink("modules/menu-editor/main.act.php?action=menu-delete&id=".$row["ID"]."&auth="._l("C_AUTH_STR")."", _l("L_DELETE"), false, 1)
				);
				$row["ACTIONS"] = mkButtonDisplay($row["ACTIONS"]);
				$row["Type"] = $menu_types[$row["Type"]];
				$tpl->set_arr (
					$row,
					"MENU"
				);
				$tpl->saveBuff("MENU");
			}

			$tpl->insert("LINK-EDITOR.FORM", "menu-editor/link-editor.form.tpl",
				Array (
					"V_ID" => 0,
					"V_TITLE" => "",
					"V_ACTION" => "create",
					"V_TYPE.SEL" => mkSelect($menu_types, 0, "type"),
					"V_MENU.SEL" => mkSelect($menu, Conf::getKey("menu-editor", "default_menu", 0), "menu"),
					"V_AUTH_ACCESS" => "*",
					"V_ICON.CHOOSER" => mkIconChooser(16, "button.png"),
					"V_TOOLTIP" => "",
					"V_LINK" => "http://",
					"V_ORDER.SEL" => mkSelect(Array(_l("L_TOP"), _l("L_BOTTOM")), 1, "orderid"),
					"V_LEVEL.SEL" => mkSelect(array(_l("L_NONE")), 0, "level"),
					"V_RETURN" => "",
					"V_ACTIONS" => ""
				)
			);

			$tpl->set_arr (
				Array (
					"BLOCK_TITLE" => _l("L_MENU-EDITOR"),
					"BLOCK_SUBTITLE" => _l("L_CREATE_MENU"),
					"BLOCK_ICON" => mkIcon("menu-editor.png", 16),
					"MENUS" => $menus,
					"LINK-EDITOR.TITLE" => _l("L_CREATE_LINK")
				)
			);

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

?>