<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 1/12/2011	ELL
// 
//
// Revised: 9/30/2011	ELL
// Added building of download files for open-source customers who purchase templates.
// Mode will switch based on whether or not wizard is enabled in options_selected table.
// If wizard is enabled, setup_site.php copies files to perspective folders and runs
// SQL directly on tables just as originally designed.  If wizard is not enabled,
// setup_site.php creates SQL file from all of the SQL commands, copies all of the
// other necessary files to a temp location, zips these files into a single file, and
// initiates download of zip file to customer.  Open-source customers will then upload
// this zip file into their Breeze Website Builder which will then unpack the files and
// apply template to their site automatically by running the SQL commands in the SQL file
// and copying the perspective files to the correct locations.
// Revised: 2/10/2012	ELL
// Added table prefix handling.  Added reference to scripts folder configuration.
// Revised: 5/15/2013	ELL
// Added submenu item height.

//
//
	include_once ("check_session.php");
	
	/////////////////////////////////////////////
	// Build string for menu_tpl.js file /////
	if ($wiz_mode == 1)
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menu_params";
		$result = $bwb_dbh->query($query);
	}
	else
	{
		$query = "SELECT * FROM menu_params WHERE template_id = " . $template_id;
		$result = $bwb_wiz_dbh->query($query);
	}
	while($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$menu_name = $row["menu_name"];
		$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 ($wiz_mode == 1)
		{
			if (BWB_BASE_DIR == '')
				$userfiles = '';
			else
				$userfiles = "/" . BWB_BASE_DIR;
			$file_name = $document_root . $userfiles . "/" . BWB_SCRIPTS_DIR . "/" . $menu_name . "_tpl.js";
		}
		else
			$file_name = $document_root . "/tmp" . $cur_ts . "/userfiles/scripts/" . $menu_name . "_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");
		$res = (fputs($fh, $menu_tpl_string));
		fclose($fh) or die($php_errormsg);
		@chmod($file_name, 0755);
	}
	
?>