<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 4/11/09	ELL
// Revised: 4/14/09 ELL
// Changed the no-save option to go back to the editor instead of the main menu.
// Revised: 7/30/09 ELL
// Added encapsulation of surrounding templates table cell and divs to give true WYSIWYG preview.
// Revised: 2/11/10	ELL
// Renamed "Go back without saving..." button to "Discard and go back to Editor".
// Revised:	2/14/10	ELL
// Modified style application to look for alternate border areas, i.e. right1, right2, etc.
// Revised:	3/11/10	ELL
// Completely redesigned preview to parse actual page file and re-construct it for true preview.
// Moved preview code to separate file.
// Revised: 9/27/2010 ELL
// Added mode follow-thru such that returning to main menu after editing pages, border areas, or creating pages brings up the same mode in the main menu.
// Revised: 10/12/2010	ELL
// Changed timestamping of `revised` field due to change from VARCHAR to DATETIME in table structure.
// Revised: 10/13/2010	ELL
// Added first_open flag on return to editor to fake out  edit_page.php so that editor does not save again and blow away page.
// Revised: 1/12/2/12	ELL
// Added saving to page_data_draft table to accomodate new upgrade for save to draft mode.
// Revised: 2/10/2012	ELL
// Added table prefix handling.
// Revised: 7/10/2012	ELL
// Added handling of blogging.
// 
// 
//

	include ("check_session.php");
		
	$update_and_return_to_main = $_POST['update_and_return_to_main'];
	$mode = $_REQUEST['mode'];
	
	$page_name = $_REQUEST['page_name'];
	$special = $_REQUEST['special'];	//Flag to tell if editing special page (header, footer, side panel...).
	$blog_id = $_REQUEST['blog_id'];

		
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_cache WHERE id = 1";	
	$result = mysql_query($query, $bwb_link);
	$row = mysql_fetch_array($result);
	$sValue = $row["tmp_page_data"];
		
	$page_name=addslashes($page_name);
//	$sValue=addslashes($sValue);
	$today = date("Y-m-d H:i:s");
		
	$result = mysql_query($query, $bwb_link);
	
	if ($mode == "blog")
	{
		$query = "SELECT blog_id FROM " . BWB_TABLE_PREFIX . "blogs_draft WHERE blog_id='$blog_id'";
		$result = mysql_query($query, $bwb_link);
		if (mysql_num_rows($result) > 0)	
			$query = "UPDATE " . BWB_TABLE_PREFIX . "blogs_draft SET post='$sValue', revised='$today' WHERE blog_id='$blog_id'";
		else
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "blogs_draft SET blog_id='$blog_id', post='$sValue', revised='$today'";
		$result = mysql_query($query, $bwb_link);
	}
	else
	{
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data_draft WHERE page_name='$page_name' AND `source`=$special";
		$result = mysql_query($query, $bwb_link);
		if (mysql_num_rows($result) > 0)	
			$query = "UPDATE " . BWB_TABLE_PREFIX . "page_data_draft SET page_content='$sValue', revised='$today', `source`=$special WHERE page_name='$page_name'";
		else
			$query = "INSERT INTO " . BWB_TABLE_PREFIX . "page_data_draft SET page_name='$page_name', page_content='$sValue', revised='$today', `source`=$special";
		$result = mysql_query($query, $bwb_link);
	}
		
	if ($update_and_return_to_main == "Publish and go back to Main Menu")
	{
		if ($mode == "blog")
		{
			$query = "SELECT id FROM " . BWB_TABLE_PREFIX . "blogs WHERE id='$blog_id'";
			$result = mysql_query($query, $bwb_link);
			if (mysql_num_rows($result) > 0)	
				$query = "UPDATE " . BWB_TABLE_PREFIX . "blogs SET post='$sValue', revised='$today' WHERE blog_id='$blog_id'";
			else
				$query = "INSERT INTO " . BWB_TABLE_PREFIX . "blogs SET id='$blog_id', post='$sValue', revised='$today'";
		}
		else
		{
			if ($special == 1)
				$table_name = "page_data_special";
			else
				$table_name = "page_data";
				
			$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . $table_name. " WHERE page_name='$page_name'";
			$result = mysql_query($query, $bwb_link);
			if (mysql_num_rows($result) > 0)	
				$query = "UPDATE " . BWB_TABLE_PREFIX . $table_name . " SET page_content='$sValue', revised='$today' WHERE page_name='$page_name'";
			else
				$query = "INSERT INTO " . BWB_TABLE_PREFIX . $table_name . " SET page_name='$page_name', page_content='$sValue', revised='$today'";
		}
		$result = mysql_query($query, $bwb_link);
		
		header("Location: main.php?mode=" . $mode);
	}
	else  //if ($update_from_preview == "Save and go back to Editor")
		header("Location: edit_page.php?page_name=$page_name&special=$special&mode=$mode&first_open=1&blog_id=$blog_id");
	
?>
