<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/4/2009
// Added table prefix handling.  Added references to website files folder configurations.
// Revised: 7/1/2013	ELL
// Implemented popup message class.
	
	include ("check_session.php");
	
	$head_added = $_REQUEST['head_added'];
	$head_assigned = $_REQUEST['head_assigned'];
	
	
	// Get last used indexes to preload add-new forms.
	function GetNextIndex($table_prefix, $dbh)
	{
		$query = "SELECT head_name FROM " . $table_prefix . "head ORDER BY head_name DESC";
		$result = $dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$head_name = $row["head_name"];
		
		$index = array();
		$i = 1;
		$index[0] = substr($head_name, strlen('head'));
		while ($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$head_name = $row["head_name"];
			$index[$i] = substr($head_name, strlen('head'));
			$i++;
		}
		sort($index);
		$index[$i-1] = $index[$i-1] + 1;
		$next_index = $index[$i-1];
			
		return $next_index;
	}
	
	$next_head = GetNextIndex(BWB_TABLE_PREFIX, $bwb_dbh);
	
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Head Content | Breeze Website Builder</title>

<link rel="stylesheet" href="theme/<?php echo $theme; ?>/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;
}
#add_head_btn	{	width:230px;	}
#rename_head_btn	{	width:110px;	}
#delete_head_btn	{	width:110px;	}
</style>

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

<script>
<?php
	echo "	var MANAGE_HEAD_DELETE_HEAD_CONFIRM_1_MSG = \"" . MANAGE_HEAD_DELETE_HEAD_CONFIRM_1_MSG . "\";\n";
	echo "	var MANAGE_HEAD_DELETE_HEAD_CONFIRM_2_MSG = \"" . MANAGE_HEAD_DELETE_HEAD_CONFIRM_2_MSG . "\";\n";
	echo "	var YES_WORD = \"" . YES_WORD . "\";\n";
	echo "	var NO_WORD = \"" . NO_WORD . "\";\n";
?>
</script>

</head>

<body id="workspace_body">
<table width="90%" align="center"><tr><td>
<div class="button_heading_sub_level_1" id="manage_users_site_options_heading"><div class="button_heading_text_sub_level_1"><?php echo MANAGE_HEAD_HEADING; ?></div><div class="help_button" id="help_button" onclick="OpenHelpWindow();">&nbsp;</div></div>
</td></tr></table>

<table width="90%" align="center" class="group_box_table"><tr><td>
<span class="sub_title"><?php echo MANAGE_HEAD_ADD_HEAD_CONTENT; ?></span>
<table width="100%" class="layout_box_table" align="center">
<tr>
<td align="center">
  <form name="frmAddHead" id="frmAddHead" method="post" action="add_head.php">
  head<input type="text" name="suffix" style="width:100px;" value="<?php echo $next_head; ?>" />
<br />
  <div class='button_console_add' id="add_head_btn" onClick="document.getElementById('frmAddHead').submit();"><span class='button_console_text_w_icon'><?php echo MANAGE_HEAD_ADD_HEAD_BTN; ?></span></div>
  </form>
</td>
</tr>
</table>
</td></tr></table>
<br />

<table width="90%" align="center" class="group_box_table"><tr><td>
<table width="100%" class="layout_box_table" align="center">
<tr bgcolor="#CCCCCC">
  <td colspan="3"><span class="sub_title"><?php echo MANAGE_HEAD_ASSIGN_HEAD_CONTENT_TO_PAGE; ?></span></td></tr>
  <?php
  		$bgcolor = "#EEEEEE";
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data";
		$result = $bwb_dbh->query($query);
		$i = 1;
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$page_name = $row["page_name"];
			echo "<form name='frmAssign$i' id='frmAssign$i' method='post' action='assign_head.php'>\n";
			echo "<tr bgcolor='$bgcolor'>\n";
			echo "<td>$page_name</td>";
			
			/////////////////////////////////////////
			//Test to see if file already exists.
			$document_root = $_SERVER['DOCUMENT_ROOT'];
			if (BWB_BASE_DIR == '')
				$file_name = $document_root . "/" . BWB_WEBPAGE_DIR . "/" . $page_name . ".php";
			else
				$file_name = $document_root . "/" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR . "/" . $page_name . ".php";
				
			if (!file_exists($file_name))
			{
				echo "Error: $file_name does not exist.  Click <a href=\"manage_special.php\" title=\"here\">here</a> to return to the area assignment page.";
				exit;
			}
	
			// Read file.
			$fh = fopen($file_name, 'r') or die("Can't read $file_name: $php_errormsg");
			$contents = fread($fh, filesize($file_name));
			fclose($fh) or die($php_errormsg);
		
			$search_str_head = "head_name='head";
		
			$start_pos = strpos($contents, $search_str_head, 1);
			$end_pos = strpos($contents, "\"", $start_pos) - 1;
			$start_pos = $start_pos + strlen("head_name='");
			$head_str = substr($contents, $start_pos, $end_pos - $start_pos);
			////////////////////////////////////
			
			echo "<td>\n";
			echo "<select name=\"head_name\">\n";
			$query2 = "SELECT head_name FROM " . BWB_TABLE_PREFIX . "head";
			$result2 = $bwb_dbh->query($query2);
			while($row2 = $result2->fetch(PDO::FETCH_ASSOC))
			{
				$head_name = $row2["head_name"];
				if ($head_name == $head_str)
					echo "<option value='$head_name' selected='selected'>$head_name</option>\n";
				else
					echo "<option value='$head_name'>$head_name</option>\n";
			}
			echo "</select>\n";
			echo "</td>\n";
			
			echo "<td>\n";
			//echo "<input type='submit' name='submit' value='Assign' />\n";
			echo "<div class='button_console_edit' style='width:110px;' onClick=\"document.getElementById('frmAssign$i').submit();\"><span class='button_console_text_w_icon'>Assign</span></div>\n";
			echo "<input type='hidden' name='page_name' value='$page_name' />\n";
			echo "</td>\n";
			echo "</tr>\n";
			echo "</form>";
			$i++;
			if ($bgcolor == "#EEEEEE")
				$bgcolor = "#DDDDDD";
			else
				$bgcolor = "#EEEEEE";
		}
   ?>
</table>
</td></tr></table>
<br />

	<table width="90%" align="center" class="group_box_table"><tr><td>
    
	<table width="100%" align="center" class="layout_box_table">
      <tr>
      	<td class="sub_title"><?php echo MANAGE_HEAD_RENAME_HEAD_CONTENT; ?></td>
      	<td class="sub_title"><?php echo MANAGE_HEAD_DELETE_HEAD_CONTENT; ?></td>
      </tr>
      <tr>
        <td>
          <form name="frmRename" id="frmRename" method="post" action="rename_head.php">
      	  <table width="100%" align="center" class="layout_box_table">
      		<tr>
              <td align="right"><?php echo MANAGE_HEAD_SELECT_HEAD_CONTENT_TO_RENAME; ?>: </td>
              <td>
                <select name="head_old_name" id="head_old_name">
            <?php
                $query = "SELECT head_name FROM " . BWB_TABLE_PREFIX . "head ORDER BY head_name";
                $result = $bwb_dbh->query($query);
                while($row = $result->fetch(PDO::FETCH_ASSOC))
                {
                    $head_name = $row["head_name"];
                    if ($head_name != "head")
					{
                        echo "<option value='$head_name'>$head_name</option>\n";
					}
                }
            ?>
                </select>
              </td>
              <td align="right"><?php echo MANAGE_HEAD_NEW_NAME; ?>: </td>
              <td align="right" style="width:60px;">
              <div id="name_prefix_display">head_</div>
              <input type="hidden" name="name_prefix" id="name_prefix" value="head_" />
              </td>
              <td><input type="text" name="name_suffix" id="name_suffix" style="width:100px;" /></td>
              <td align="center">
              <div class='button_console_edit' id="rename_head_btn" onClick="document.getElementById('frmRename').submit();"><span class='button_console_text_w_icon'><?php echo RENAME_WORD; ?></span></div>
              </td>
			</tr>
         </table>
         </form>
      </td>
      
      <td>
      <table>
      <tr>
      
	  <td align="center"><?php echo MANAGE_HEAD_SELECT_HEAD_CONTENT_TO_DELETE; ?>: </td>
	  <td>
		<select name="head_name" id="head_name">
	<?php
		$query = "SELECT head_name FROM " . BWB_TABLE_PREFIX . "head";
		$result = $bwb_dbh->query($query);
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$head_name = $row["head_name"];
			if (($head_name != "head") && ($head_name != ""))
				echo "<option value='$head_name'>$head_name</option>";
		}
	?>
		</select>
	  </td>
	  <td align="center">
      <div class='button_console_delete' id="delete_head_btn" onClick="DeleteConfirm();"><span class='button_console_text_w_icon'><?php echo EDITOR_DELETE; ?></span></div>
	  </td>
      
      </tr>
      </table>
     </td>
     
	</tr></table>
    
    </td></tr></table>

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

<script language="javascript" type="text/javascript">
	HidePopupMsg();
	
<?php	if ($head_added == 1)	{	?>
	popup_msg.top_offset = 100;
	popup_msg.left_offset = 320;
	popup_msg.popup_msg_type = 1;
	popup_msg.popup_timeout = 2000;
	popup_msg.status_msg = "Head content has been added.";
	popup_msg.ShowPopupMsg();
<?php	}	?>

<?php	if ($head_assigned == 1)	{	?>
	popup_msg.top_offset = 100;
	popup_msg.left_offset = 320;
	popup_msg.popup_msg_type = 1;
	popup_msg.popup_timeout = 2000;
	popup_msg.status_msg = "Head content has been asigned.";
	popup_msg.ShowPopupMsg();
<?php	}	?>
</script>
</body>
</html>