<?php
////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, 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: 12/7/2016	ELL
// Modified for consolidated menu items tables.
//
//
	include_once("check_session.php");
	
	if ($wiz_mode == 1)
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "menus WHERE menu_type = 1";
		$result2 = $bwb_dbh->query($query);
	}
	else
	{
		$query = "SELECT * FROM menus WHERE (menu_type = 1) AND (template_id = " . $template_id . ")";
		$result2 = $bwb_wiz_dbh->query($query);
	}

	while ($row2 = $result2->fetch(PDO::FETCH_ASSOC))
	{
		$menu_name = $row2['menu_name'];
		$suffix = substr($menu_name, 4);
		$suffix = str_replace("_mobile", "", $suffix);

		$menu_items_string = "var MENU" . $suffix . "_ITEMS = [\n";
		$previous_level = 0;
		$indent = '   ';

		$level = 0;
		$target = "_self";
		foreach ($page_arr as $key => $val)
		{	
			$item_text = $val[1];
			$URL = $val[0];
			
			$item_text = stripslashes($item_text);
			$URL = stripslashes($URL);
			$target = stripslashes($target);
				
			if ($previous_level == $level)
			{
				if ($URL || ($URL != ''))
					$menu_items_string .= $indent . "['$item_text', '$URL', {'tw' : '$target'}],\n";
				else
					$menu_items_string .= $indent . "['$item_text', null , null],\n";
			}
			elseif ($previous_level < $level)	//Nesting
			{
				$menu_items_string = substr_replace($menu_items_string, '', -3, 1);
				$indent .= "   ";
				if ($URL || ($URL != ''))
					$menu_items_string .= $indent . "['$item_text', '$URL', {'tw' : '$target'}],\n";
				else
					$menu_items_string .= $indent . "['$item_text', null , null],\n";
			}
			elseif ($previous_level > $level)	//Un-nesting
			{
				$menu_items_string = substr_replace($menu_items_string, '', -2, 1);
				$unnest = $previous_level - $level;
				for ($i = 0; $i < $unnest; $i++)
				{
					$indent = substr_replace($indent, '', -3, 3);
					$menu_items_string .= $indent . "],\n";
				}
				if ($URL || ($URL != ''))
					$menu_items_string .= $indent . "['$item_text', '$URL', {'tw' : '$target'}],\n";
				else
					$menu_items_string .= $indent . "['$item_text', null , null],\n";
			}
			
			$previous_level = $level;
		}
		
		if ($level > 0)	//Un-nest end if necessary
		{
			$menu_items_string = substr_replace($menu_items_string, '', -2, 1);
			for ($i = 0; $i < $level; $i++)
			{
				$indent = substr_replace($indent, '', -3, 3);
				$menu_items_string .= $indent . "],\n";
			}
		}
				
		$menu_items_string = substr_replace($menu_items_string, '', -2, 1);
		$menu_items_string .= "];\n";

		////////////////////////////////////////////////////////
		/// Write menu items to menu_items.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" . $suffix . "_items.js";
		}
		else
			$file_name = $document_root . "/tmp" . $cur_ts . "/userfiles/scripts/menu" . $suffix . "_items.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_items_string));
		fclose($fh) or die($php_errormsg);
		@chmod($file_name, 0755);
	}
?>