<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 1/10/2011	ELL
// Complete overhaul.
// Revised: 2/10/2012	ELL
// Added table prefix handling.  Added references to CSS folder configuration.
//
	include ("check_session.php");
	include ("lib/copy_table.php");
	
	$submit = $_POST['submit'];
	$menu = $_POST['menu'];
	$menu_type = $_POST['menu_type'];
	$suffix = substr($menu, 4);
	
	$site_css_table = $menu . "_css";
	copy_table(BWB_TABLE_PREFIX . "menu_styles_preview", BWB_TABLE_PREFIX . $site_css_table, $bwb_link, $bwb_database);

	$css_file = $menu . ".css";

	/////////////////////////////////////////////
	// Build string for menu.css file /////
	include("build_css_string.php");


	////////////////////////////////////////////////////////
	/// Write menu items to menu.css 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_CSS_DIR . "/" . $css_file;
		
	if (file_exists($file_name))
	{
		$bkup_file_name = $file_name . mktime();
		if (!rename($file_name, $bkup_file_name))
			echo "Could not rename $file_name to $bkup_file_name.<br>";
		@chmod($bkup_file_name, 0644);
	}
	
	// Create file.
	$fh = fopen($file_name, 'x') or die("Can't create $file_name: $php_errormsg");
	$result = (fputs($fh, $css_string));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	
	
	if ($submit == "Save and go back to Editor")
	{
		if ($menu_type == 1)
			header("Location: edit_js_menu_styles.php?menu=$menu");
		else
			header("Location: edit_css_menu_styles.php?menu=$menu");
	}
	else
		header("Location: main.php?mode=menu");
		
?>
