<?php

/*
osCommerce, Open Source E-Commerce Solutions 
http://www.oscommerce.com 

Copyright (c) 2003 osCommerce 

osCommRes, Services Oline 
http://www.oscommres.com 
Copyright (c) 2005 osCommRes 

Freeway eCommerce 
http://www.zac-ware.com/freeway 
Copyright (c) 2007 ZacWare

Released under the GNU General Public License
*/
// Set flag that this is a parent file
	define( '_FEXEC', 1 );
	require("includes/application_top.php");
	$menu_arr=array();
	load_menu_new(0);
	$action=(isset($HTTP_GET_VARS["action"])?$HTTP_GET_VARS["action"]:'');
	
	switch($action){
		case "get_menu":
			$id=(isset($HTTP_GET_VARS["id"])?(int)$HTTP_GET_VARS["id"]:0);
			$menu_query=tep_db_query("select a.menu_id,ad.menu_text,a.filename,a.params,a.menu_pos,a.menu_item_type from admin_menus a,admin_menus_description ad where a.menu_id=ad.menu_id and a.menu_id='" . (int)$id . "'");
			$menu_result=tep_db_fetch_array($menu_query);
			echo "SUCCESS;" . $menu_result["menu_text"] . "|" . $menu_result["filename"] . "|" . $menu_result["params"] . "|" . $menu_result["menu_pos"] . '|' . $menu_result["menu_item_type"] . '|' . $menu_result["menu_id"];
			exit;
		case "store_menu":
			$id=(isset($HTTP_GET_VARS["id"])?(int)$HTTP_GET_VARS["id"]:0);
			$parent_id=(isset($HTTP_POST_VARS["parent_id"])?(int)$HTTP_POST_VARS["parent_id"]:0);
			$menu_text=(isset($HTTP_POST_VARS["txtMenuText"])?tep_db_prepare_input($HTTP_POST_VARS["txtMenuText"]):'');
			$filename=(isset($HTTP_POST_VARS["txtFileName"])?tep_db_prepare_input($HTTP_POST_VARS["txtFileName"]):'');
			$params=(isset($HTTP_POST_VARS["txtParams"])?tep_db_prepare_input($HTTP_POST_VARS["txtParams"]):'');
			$position=(isset($HTTP_POST_VARS["txtPosition"])?$HTTP_POST_VARS["txtPosition"]:'');
			$menu_type=(isset($HTTP_POST_VARS["cmbMenuType"])?$HTTP_POST_VARS["cmbMenuType"]:'');
			$params=str_replace(';and;','&',$params);
			if ($id>0){ // update
				if ($position=="") $position=$id;
				tep_db_query("UPDATE admin_menus_description set menu_text='" . tep_db_input($menu_text) . "' where menu_id='" . tep_db_input($id) . "' and language_id=1");
				tep_db_query("UPDATE admin_menus set filename='" . tep_db_input($filename) . "', params='" . tep_db_input($params) . "',menu_pos='" . $position . "',menu_item_type='" . $menu_type . "' where menu_id='" . tep_db_input($id) . "'");
			} else { //insert
				tep_db_query("INSERT into admin_menus(parent_id,filename,params,admin_groups_id,menu_item_type) values('" . tep_db_input($parent_id) . "','" . tep_db_input($filename) . "','" . tep_db_input($params) . "',1,'" . $menu_type . "')");
				$insert_id=tep_db_insert_id();
				tep_db_query("INSERT into admin_menus_description(menu_id,menu_text,language_id) values('" . tep_db_input($insert_id) . "','" . tep_db_input($menu_text) . "',1)");
				if ($position=="") $position=$insert_id;
				tep_db_query("UPDATE admin_menus set menu_pos='" . $position . "' where menu_id='" . (int)$insert_id . "'");
			}
			if ($id>0){
				echo "SUCCESS";
			} else {
				tep_redirect(tep_href_link("menus.php"));
			}
			exit;
		case "delete_menu":
			$id=(isset($HTTP_GET_VARS["id"])?(int)$HTTP_GET_VARS["id"]:0);
			if ($id>0){
				$child_query=tep_db_query("SELECT menu_id from admin_menus where parent_id='" . (int)$id . "'");
				if (tep_db_num_rows($child_query)>0) tep_redirect(tep_href_link("menus.php","error=1"));
				tep_db_query("DELETE from admin_menus where menu_id='" . (int)$id ."'");
				tep_db_query("DELETE from admin_menus_description where menu_id='" . (int)$id ."'");
			}
			tep_redirect(tep_href_link("menus.php"));
	}
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<LINK href="includes/TreeMenuStyle.css" type="text/css" rel="stylesheet">
		<script language="javascript" type="text/javascript" src="includes/TreeMenu.js"></script>
		<script language="javascript" type="text/javascript" src="includes/http.js"></script>
		<script language="javascript" type="text/javascript">
			//new tree item, and make it a parent called sub1
			<?php 
				reset($menu_arr);
				while(list($key,$value)=each($menu_arr)){
					if ($key==0) $key="";
					else $key="men" . $key;
					echo 'NewTree("' . $key .'");' . "\n";
					for ($icnt=0,$n=count($value);$icnt<$n;$icnt++){
						$temp_key="";
						if (isset($menu_arr[$value[$icnt]["id"]])) $temp_key="men" . $value[$icnt]["id"];
						echo 'AddStandardItem("' . $temp_key . '","' . $value[$icnt]["text"] . '","","","MyAlert(' . $value[$icnt]["id"] .');","men'. $value[$icnt]["id"] . '");' . "\n";
					}
					echo "EndTree();\n";
				}
			?>
			var selectedMenu;
			var prevMenu;
			var simple;
			function MyAlert(id)
			{
				var menu=document.getElementById("text_men"+id);
				if (prevMenu){
					prevMenu.style.backgroundColor="#FFFFFF";
					prevMenu.style.color="#000000";
				}
				prevMenu=menu;
				menu.style.backgroundColor="#0000FF";
				menu.style.color="#FFFFFF";
				selectedMenu=id;
				var element=document.getElementById("panelMenu");
				element.style.left=event.x;
				element.style.top=document.getElementById("test").scrollTop+event.y;
				element.style.display="";
			}
			function do_menu(action){
				document.getElementById("panelMenu").style.display="none";
				switch(action){
					case 1:
						start_comments('add');
						break;
					case 2:
						start_comments('edit');
						break;
					case 3:
						location.href="menus.php?action=delete_menu&id="+selectedMenu;
						break;
				}
			}
			function start_comments(mode){
				var panelEdit=document.getElementById("panelComments");
				var textarea=document.getElementById("comments");
				var textelement=document.getElementById(selectedMenu+"_comments");
			
				if (mode=='add'){
					panelEdit.menu_id=0;
					menu_update.parent_id.value=selectedMenu;
				} else {
					panelEdit.menu_id=selectedMenu;
					menu_update.parent_id.value=0;
					do_get_command("menus.php?action=get_menu&id="+selectedMenu);
				}
				menu_update.txtMenuText.value='';
				menu_update.txtFileName.value='';
				menu_update.txtParams.value='';
				menu_update.txtPosition.value='';
				menu_update.cmbMenuType.selectedIndex=0;
				document.getElementById("hidID").innerHTML="";
				panelEdit.style.top=document.getElementById("test").scrollTop+(parseInt(screen.height)-parseInt(panelEdit.style.height))/2;
				panelEdit.style.left=(parseInt(screen.width)-parseInt(panelEdit.style.width))/2;
				panelEdit.style.display="";
			}
			function end_comments(mode){
				var panelEdit=document.getElementById("panelComments");
				if (mode==1){
					if (menu_update.txtParams.value!=''){
						var temp=/&/g;
						menu_update.txtParams.value=menu_update.txtParams.value.replace(temp,';and;');
					}
					if (panelEdit.menu_id>0){
						do_post_command("menu_update","menus.php?action=store_menu&id="+panelEdit.menu_id);
					} else {
						menu_update.submit();
					}
				} 
				panelEdit.style.display="none";
			}
			function do_result(result){
				splt=result.split(";");
				if (splt[0]!='SUCCESS' || splt.length<=1) return;
				splt=splt[1].split('|');
				menu_update.txtMenuText.value=splt[0];
				menu_update.txtFileName.value=splt[1];
				menu_update.txtParams.value=splt[2];
				menu_update.txtPosition.value=splt[3];
				var combo=menu_update.cmbMenuType;
				for (icnt=0,n=combo.options.length;icnt<n;icnt++){
					if (combo.options[icnt].value==splt[4]){
						combo.selectedIndex=icnt;
						break;
					}
				}
				document.getElementById("hidID").innerHTML=splt[5];
			}
		</script>
	</head>
	<body id="test">
		<script>
			//Generate Menu
			GenerateTreeCode();
		</script>
		<div id="panelComments" style="position:absolute;height:200;width:450;display:none">
		<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#FCF7E4">
			<tr>
				<td>
				<table border="0" width="100%" cellpadding="" cellspacing="3"  bgcolor="#F5EED1">
					<tr>
						<td>
							Edit Menu
						</td>
					</tr>
				</table>
				</td>
			</tr>
			<form name="menu_update" action="menus.php?action=store_menu&id=0" method="post">
			<input type="hidden" name="parent_id">
			<tr>
				<td valign="top" align="center">
				<table border="0" width="100%" cellpadding="3" cellspacing="0">
					<tr>
						<Td>ID:</Td><td><span id="hidID"></span></td>
					</tr>
					<tr>
						<td width="100">Text:</td><td><input type="text" name="txtMenuText" value="" size="20"></td>
					</tr>
					<tr>
						<td>Filename:</td><td><input type="text" name="txtFileName" value="" size="100"></td>
					</tr>
					<tr>
						<td>Params:</td><td><input type="text" name="txtParams" value="" size="60"></td>
					</tr>
					<tr>
						<td>Menu Position:</td><td><input type="text" name="txtPosition" value="" size="5"></td>
					</tr>
					<tr>
						<td>Menu Type:</td>
						<td>
							<select name="cmbMenuType">
								<?php
									$types=array(''=>'General','P'=>'Products','E'=>'Events','S'=>'Subscriptions','V'=>'Services');
									while(list($key,$value)=each($types)){
										echo '<OPTION value="' . $key . '">' . $value . '</OPTION>';
									}
								?>
							</select>
						</td>
					</tr>
					<tr>
						<td align="center" colspan="2"><input type="button" name="btnUpdate" onClick="end_comments(1);" value="Update">&nbsp;<input type="button" name="btnCancel" onClick="end_comments(2);" value="Cancel"></td>
					</tr>
				</table>
				</td>
			</tr>
			</form>
		</table>
		</div>
		<div id="panelMenu" style="position:absolute;height:200;width:120;display:none">
		<table border="0" width="100%" cellpadding="3" cellspacing="0" bgcolor="#FCF7E4">
			<tr>
				<td valign="top" align="center">
				<table border="0" width="100%" cellpadding="3" cellspacing="0">
					<tr>
						<td><a href="javascript:do_menu(1);">New Sub Menu</a></td>
					</tr>
					<tr>
						<td><a href="javascript:do_menu(2);">Edit Menu</a></td>
					</tr>
					<tr>
						<td><a href="javascript:do_menu(3);">Delete Menu</a></td>
					</tr>
				</table>
				</td>
			</tr>
		</table>
		</div>
	</body>
</html>
<?php

	function load_menu_new($parent){
		global $menu_arr,$navig_list;
		$menu_query=tep_db_query("SELECT m.menu_id,md.menu_text,m.filename from " . TABLE_ADMIN_MENUS . " m," . TABLE_ADMIN_MENUS_DESCRIPTION . " md where m.parent_id='" . (int)$parent. "' and m.menu_id=md.menu_id order by m.menu_pos");
		if (tep_db_num_rows($menu_query)<=0) return;

		while($menu_result=tep_db_fetch_array($menu_query)){
			$menu_arr[$parent][]=array("text"=>$menu_result["menu_text"],"id"=>$menu_result["menu_id"]);
			load_menu_new($menu_result["menu_id"],$path . $menu_result["menu_id"]);
		}
		mysql_free_result($menu_query);
   }
?>