<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 4/22/09	ELL
// Added selection of proportional spacing.
// Revised: 5/1/09  ELL
// Added protection againts robots wiping out menu parameters by accident.
// Revined: 1/9/2011	ELL
// Modified to allow multiple JavaScript Menus.
// Revised: 2/10/2012	ELL
// Added table prefix handling.  Added references to script folder configuration.
// Revised: 5/15/2013	ELL
// Added submenu item height.

	include ("check_session.php");
	
	$menu = $_POST['menu'];
	$suffix = substr($menu, 4);
	
	$main_item_height = $_POST['main_item_height'];
	$main_item_width = $_POST['main_item_width'];
	$origin_top = $_POST['origin_top'];
	$origin_left = $_POST['origin_left'];
	$sub_menu_origin_top = $_POST['sub_menu_origin_top'];
	$sub_menu_origin_left = $_POST['sub_menu_origin_left'];
	$main_item_top_offset = $_POST['main_item_top_offset'];
	$main_item_left_offset = $_POST['main_item_left_offset'];
	$sub_item_top_offset = $_POST['sub_item_top_offset'];
	$sub_item_left_offset = $_POST['sub_item_left_offset'];
	$sub_item_width = $_POST['sub_item_width'];
	$sub_item_height = $_POST['sub_item_height'];
	$sub_left_offset_from_main = $_POST['sub_left_offset_from_main'];
	$hide_delay = $_POST['hide_delay'];
	$proportional_width = $_POST['proportional_width'];
	$proportional_sub_width = $_POST['proportional_sub_width'];
	if (!isset($proportional_width))
		$proportional_width = 0;
	if (!isset($proportional_sub_width))
		$proportional_sub_width = 0;
		
	if (!isset($main_item_height) && !isset($main_item_width) && !isset($origin_top) && !isset($origin_left))
	{
		echo "No parameters have been set.  Please hit the back button and fill in the parameters.";
		exit;
	}
	
	if (!isset($main_item_height)) $main_item_height = 0;
	if (!isset($main_item_width)) $main_item_width = 0;
	if (!isset($origin_top)) $origin_top = 0;
	if (!isset($origin_left)) $origin_left = 0;
	if (!isset($sub_menu_origin_top)) $sub_menu_origin_top = 0;
	if (!isset($sub_menu_origin_left)) $sub_menu_origin_left = 0;
	if (!isset($main_item_top_offset)) $main_item_top_offset = 0;
	if (!isset($main_item_left_offset)) $main_item_left_offset = 0;
	if (!isset($sub_item_top_offset)) $sub_item_top_offset = 0;
	if (!isset($sub_item_left_offset)) $sub_item_left_offset = 0;
	if (!isset($sub_item_width)) $sub_item_width = 0;
	if (!isset($sub_item_height)) $sub_item_height = 0;
	if (!isset($sub_left_offset_from_main)) $sub_left_offset_from_main = 0;
	if (!isset($hide_delay)) $hide_delay = 0;

	//Check for existing menu params.
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu_params WHERE menu_name = '$menu'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
	{
		//Update menu parameters.
		$query = "UPDATE " . BWB_TABLE_PREFIX . "menu_params SET main_item_height=" . $main_item_height . ", main_item_width=" . $main_item_width . ", origin_top=" . $origin_top .
				",origin_left=" . $origin_left . ", sub_menu_origin_top=" . $sub_menu_origin_top . ", sub_menu_origin_left=" . $sub_menu_origin_left . 
				", main_item_top_offset=" . $main_item_top_offset . ", main_item_left_offset=" . $main_item_left_offset . 
				", sub_item_top_offset=" . $sub_item_top_offset . ", sub_item_left_offset=" . $sub_item_left_offset . ", sub_item_width=" . $sub_item_width . ", sub_item_height=" . $sub_item_height . 
				", sub_left_offset_from_main=" . $sub_left_offset_from_main . ", hide_delay=" . $hide_delay . ", proportional_width=" . $proportional_width . 
				", proportional_sub_width=" . $proportional_sub_width . " WHERE menu_name = '" . $menu . "'";
	}
	else
	{
		//Insert new data into table.
		$query = "INSERT INTO " . BWB_TABLE_PREFIX . "menu_params (menu_name, main_item_height, main_item_width, origin_top, origin_left, sub_menu_origin_top, sub_menu_origin_left," . 
				" main_item_top_offset, main_item_left_offset, sub_item_top_offset, sub_item_left_offset, sub_item_width, sub_item_height, sub_left_offset_from_main, hide_delay," .
				" proportional_width, proportional_sub_width) VALUES('" . $menu . "'," . $main_item_height . "," . $main_item_width . "," . $origin_top . "," .
				$origin_left . "," . $sub_menu_origin_top . "," . $sub_menu_origin_left . "," . $main_item_top_offset . "," . $main_item_left_offset . "," . 
				$sub_item_top_offset . "," . $sub_item_left_offset . "," . $sub_item_width . "," . $sub_item_height . "," . $sub_left_offset_from_main . "," . $hide_delay . "," . 
				$proportional_width . "," . $proportional_sub_width . ")";
	}
	$cnt = $bwb_dbh->exec($query);
	
	/////////////////////////////////////////////
	// Build string for menu_tpl.js file /////
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu_params WHERE menu_name = '" . $menu . "'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);

	$main_item_height = $row["main_item_height"];
	$main_item_width = $row["main_item_width"];
	$origin_top = $row["origin_top"];
	$origin_left = $row["origin_left"];
	$sub_menu_origin_top = $row["sub_menu_origin_top"];
	$sub_menu_origin_left = $row["sub_menu_origin_left"];
	$main_item_top_offset = $row["main_item_top_offset"];
	$main_item_left_offset = $row["main_item_left_offset"];
	$sub_item_top_offset = $row["sub_item_top_offset"];
	$sub_item_left_offset = $row["sub_item_left_offset"];
	$sub_item_width = $row["sub_item_width"];
	$sub_item_height = $row["sub_item_height"];
	$sub_left_offset_from_main = $row["sub_left_offset_from_main"];
	$hide_delay = $row["hide_delay"];
	$proportional_width = $row["proportional_width"];
	$proportional_sub_width = $row["proportional_sub_width"];
	
	$menu_tpl_string = "var MENU" . $suffix . "_POS = [\n";
	$menu_tpl_string .= "{\n";
	$menu_tpl_string .= "  'height': $main_item_height,\n";
	$menu_tpl_string .= "  'width': $main_item_width,\n";
	$menu_tpl_string .= "  'menu_top': $origin_top,\n";
	$menu_tpl_string .= "  'menu_left': $origin_left,\n";
	$menu_tpl_string .= "  'block_top': $sub_menu_origin_top,\n";
	$menu_tpl_string .= "  'block_left': $sub_menu_origin_left,\n";
	$menu_tpl_string .= "  'top': $main_item_top_offset,\n";
	$menu_tpl_string .= "  'left': $main_item_left_offset,\n";
	$menu_tpl_string .= "  'top_sub': $sub_item_top_offset,\n";
	$menu_tpl_string .= "  'left_sub': $sub_item_left_offset,\n";
	$menu_tpl_string .= "  'hide_delay': $hide_delay,\n";
	$menu_tpl_string .= "  'css' : {\n";
	$menu_tpl_string .= "      'outer' : ['m0l0oout" . $suffix . "', 'm0l0oover" . $suffix . "'],\n";
	$menu_tpl_string .= "      'inner' : ['m0l0iout" . $suffix . "', 'm0l0iover" . $suffix . "']\n";
	$menu_tpl_string .= "   },\n";
	$menu_tpl_string .= "  'proportional_width': $proportional_width,\n";
	$menu_tpl_string .= "  'proportional_sub_width': $proportional_sub_width\n";
	$menu_tpl_string .= "},\n";
	$menu_tpl_string .= "{\n";
	$menu_tpl_string .= "  'width': $sub_item_width,\n";
	$menu_tpl_string .= "  'height': $sub_item_height,\n";
	$menu_tpl_string .= "  'block_left': $sub_left_offset_from_main,\n";
	$menu_tpl_string .= "  'css' : {\n";
	$menu_tpl_string .= "     'outer' : ['m0l1oout" . $suffix . "', 'm0l1oover" . $suffix . "'],\n";
	$menu_tpl_string .= "     'inner' : ['m0l1iout" . $suffix . "', 'm0l1iover" . $suffix . "']\n";
	$menu_tpl_string .= "   }\n";
	$menu_tpl_string .= "},\n";
	$menu_tpl_string .= "{\n";
	$menu_tpl_string .= "  'width': $sub_item_width,\n";
	$menu_tpl_string .= "  'height': $sub_item_height,\n";
	$menu_tpl_string .= "  'block_left': $sub_left_offset_from_main,\n";
	$menu_tpl_string .= "  'css' : {\n";
	$menu_tpl_string .= "     'outer' : ['m0l2oout" . $suffix . "', 'm0l2oover" . $suffix . "'],\n";
	$menu_tpl_string .= "     'inner' : ['m0l2iout" . $suffix . "', 'm0l2iover" . $suffix . "']\n";
	$menu_tpl_string .= "   }\n";
	$menu_tpl_string .= "}\n";
	$menu_tpl_string .= "];\n";

	////////////////////////////////////////////////////////
	/// Write menu items to menu_tpl.js file /////////////
	//Test to see if file already exists.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if (BWB_BASE_DIR == '')
		$userfiles = '';
	else
		$userfiles = "/" . BWB_BASE_DIR;		
	$file_name = $document_root . $userfiles . "/" . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_tpl.js";

	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . time();
		if (!rename($file_name, $bkup_file_name))
			echo "Could not rename $file_name to $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0755);
	}
	
	// Create file.
	$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
	$result = (fputs($fh, $menu_tpl_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0755);
	
	header("Location: edit_js_menu_params.php?menu=$menu");
	
?>
