<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Revised: 6/15/2007
// Added head_name and WHERE clause for editing different head content.
// Revised: 3/26/2010	ELL
// Changed "Submit" to "Save"
// Fixed bug with ecape codes.
// Revised: 10/12/2010	ELL
// Added revision datetime tracking.
// Revised: 11/9/2010	ELL
// Added persitence of last head edited.
// Revised: 1/16/2012	ELL
// Added "Publish" button and changed "Save" button function to save to draft.  Also added "Revert" so that draft can be discarded.
// Revised: 2/8/2012	ELL
// Added table prefix handling.  Added reference to user files folder constant.
	
	include ("check_session.php");
		
	$head_name = $_REQUEST['head_name'];
	$_SESSION['last_head_edited'] = $head_name;
	$operation = $_REQUEST['operation'];
?>
<html>
<head>
<title>Breeze Website Builder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="manager.css" type="text/css">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">

<style type="text/css">
#help_button	{	
	position:relative;
	display:block;
	top:-15px;
	left:215px;
	height:31px;
	width:31px;
	z-index:100;
}
#edit_head_code_heading	{	
	display:block;
	position:relative;
	left:150px;
}
</style>

<script language="javascript" type="text/javascript" src="js/browser_detect.js"></script>
<script language="javascript" type="text/javascript" src="js/mouse_position.js"></script>
<script language="javascript" type="text/javascript" src="js/popup_msg.js"></script>

<script language="javascript" type="text/javascript">
var browser = BrowserDetect.browser;
var browser_version = BrowserDetect.version;

var param_array = {'popup_msg_type' : 1, 'status_msg' : '', 'popup_timeout' : -1};
var popup_msg = new PopupMsg(param_array);

function OpenHelpWindow()
{
	window.open("http://breezewebsitebuilder.com/docs/cms/help.html#edit_head", "Help", "scrollbars=1,resizable=1,status=0,toolbar=0,menubar=0,location=0,height=700,width=800");
}
</script>
</head>

<body>
<div class="button_heading_sub_level_1" id="edit_head_code_heading"><div class="button_heading_text_sub_level_1">Edit Head Code</div><div class="help_button" id="help_button" onClick="OpenHelpWindow();">&nbsp;</div></div>

<form name="frmEditHead" id="frmEditHead" action="save_head.php" method="post">
<div style='text-align:center;'>
<?php

	$query = "SELECT page_content FROM " . BWB_TABLE_PREFIX . "page_data_draft WHERE page_name='$head_name' AND `source`=2";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() > 0)
	{
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$head_content = $row["page_content"];
	}
	else
	{
		$query = "SELECT head_content FROM " . BWB_TABLE_PREFIX . "head WHERE head_name = '$head_name'";
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$head_content = $row["head_content"];
	}
	
	// Before we go any farther, back this puppy up!
	$revised = $row['revised'];
	$query = "INSERT INTO " . BWB_TABLE_PREFIX . "page_data_bkup SET page_name='$head_name', page_content=:head_content, revised='$revised', source=2";
	$stmt = $bwb_dbh->prepare($query);
	$stmt->execute(array(':head_content' => $head_content));
	
	$head_content = stripslashes($head_content);

	echo "<textarea name='textarea_head_content' rows='30' style='width:80%;'>$head_content</textarea>\n";
	
	if (BWB_BASE_DIR == '')
		$path = "../" . BWB_WEBPAGE_DIR . "/index.php";
	else
		$path = "../" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR . "/index.php";

?>
</div>
      <br>
	  <table width="80%" align="center"><tr>
      <td align="left" class="small_message" width="9%">
      <span id="page_name_label"><?php echo $head_name; ?></span>
	  </td>
      <td align="left">
      <div class='button_console_save' style="width:110px;" onClick="document.getElementById('operation').value='Save'; document.getElementById('frmEditHead').submit();"><span class='button_console_text_w_icon'>Save</span></div>
      <input type="hidden" name="head_name" value="<?php echo $head_name; ?>" />
      <input type="hidden" name="operation" id="operation" />
      </td>
      <td align="center">
      <div class='button_console_save' style="width:110px;" onClick="document.getElementById('operation').value='Publish'; document.getElementById('frmEditHead').submit();"><span class='button_console_text_w_icon'>Publish</span></div>
      </td>
       <td align="center">
      <div class='button_console_cancel' style="width:110px;" onClick="document.getElementById('operation').value='Revert'; document.getElementById('frmEditHead').submit();"><span class='button_console_text_w_icon'>Revert</span></div>
      </td>
      <td align="center">
      <div class='button_console_preview' style="width:110px;" onClick="window.open('<?php echo $path; ?>', '_blank');"><span class='button_console_text_w_icon'>See Site</span></div>
	  </td>
	  </tr></table>
</form>

<?php include("lib/popup_msg.html"); ?>

<script language="JavaScript"  type="text/javascript">
	HidePopupMsg();
<?php	if (isset($operation))	{	?>
	popup_msg.top_offset = 100;
	popup_msg.left_offset = 320;
	popup_msg.popup_msg_type = 1;
	popup_msg.popup_timeout = 3000;
<?php
	if ($operation == "Save")
		echo "	popup_msg.status_msg = 'Head content has bees saved.';";
	elseif ($operation == "Publish")
		echo "	popup_msg.status_msg = 'Head content has bees published.';";
	elseif ($operation == "Revert")
		echo "	popup_msg.status_msg = 'Head content has bees reverted.';";
?>
	popup_msg.ShowPopupMsg();
<?php	}	?>
</script>

</body>
</html>
