<?php
/////////////////////////////////////////////
// Add Menu Code /////
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 11/13/2009	ELL
// Added code to ensure menu code exists only once and in only one border area.
// Revised to run from multi-menu-type menu assignment console.
// Revised: 2/7/2012	ELL
// Added table prefix handling.  Added reference to script folder constant.
// 
//
	include ("check_session.php");
	
	$today = addslashes(date("m/d/Y  H:m:s"));
	
	if (BWB_WEBPAGE_DIR == "")
		$rel_path = "";
	else
		$rel_path = "../";
	
	// Add code to selected border area.
	$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_special WHERE page_name = '$border_area'";
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$page_content = $row["page_content"];
	
	$searchStr= "new menu (MENU" . $suffix  . "_ITEMS, MENU" . $suffix  . "_POS);";
	$pos = strrpos($page_content, $searchStr);
	if ($pos === false)
	{
		$page_content .= "\n\n<!--js menu script--><script>\n";
		$page_content .= "	if (is_mobile === true) new menu (MENU_MOBILE" . $suffix  . "_ITEMS, MENU_MOBILE" . $suffix  . "_POS);\n";
		$page_content .= "	else new menu (MENU" . $suffix  . "_ITEMS, MENU" . $suffix  . "_POS);\n";
		$page_content .= "</script><!--js menu script-->\n\n";
	
		$page_content = addslashes($page_content);
		$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_special SET page_content='$page_content', revised='$today' WHERE page_name='$border_area'";
		$cnt = $bwb_dbh->exec($query);
		
		$query = "DELETE FROM " . BWB_TABLE_PREFIX . "page_data_draft WHERE page_name='$border_area'";
		$cnt = $bwb_dbh->exec($query);
	}
//	else
//		echo "<br>Note: Menu code already exists in this border area.<br>";
	
	// Add code to all head content.
	$query = "SELECT * FROM `" . BWB_TABLE_PREFIX . "head`";
	$result = $bwb_dbh->query($query);
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$head_content  = $row["head_content"];
		$head_name = $row["head_name"]; 
		
		$searchStr = "<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu.js'></script>";
		$pos = strrpos($head_content , $searchStr);
		if ($pos === false)
		{
			$head_content .= "\n\n<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu.js'></script>\n";
    	}
		
		$searchStr = "<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_items.js'></script>";
		$pos = strrpos($head_content , $searchStr);
		if ($pos === false)
		{
			$head_content .= "\n<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_items.js'></script>\n";
    	}
		
		$searchStr = "<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_mobile_items.js'></script>";
		$pos = strrpos($head_content , $searchStr);
		if ($pos === false)
		{
			$head_content .= "\n<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_mobile_items.js'></script>\n";
    	}
		
		$searchStr = "<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_tpl.js'></script>";
		$pos = strrpos($head_content , $searchStr);
		if ($pos === false)
		{
			$head_content .= "\n<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_tpl.js'></script>\n";
    	}
		
		$searchStr = "<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_mobile_tpl.js'></script>";
		$pos = strrpos($head_content , $searchStr);
		if ($pos === false)
		{
			$head_content .= "\n<script language='JavaScript' src='" . $rel_path . BWB_SCRIPTS_DIR . "/menu" . $suffix . "_mobile_tpl.js'></script>\n";
    	}
		
		$head_content = addslashes($head_content);
		$query2 = "UPDATE `" . BWB_TABLE_PREFIX . "head` SET `head_content`='$head_content', `revised`='$today' WHERE `head_name`='$head_name'";
		$cnt = $bwb_dbh->exec($query2);
		
		$query = "DELETE FROM " . BWB_TABLE_PREFIX . "page_data_draft WHERE page_name='$head_name'";
		$cnt = $bwb_dbh->exec($query);
    }

?>