<?php 
	session_start();
	include ('../config.php');
	include ('secureadmin.php');
	
	
if(isset($_POST['add'])) { 
	
	if(get_magic_quotes_gpc()) {
		$title = $_POST['title'];
		$body = $_POST['body'];
	} else {
		$title = mysql_real_escape_string($_POST['title']);
		$body = mysql_real_escape_string($_POST['body']);
	}
	$onmenu = $_POST['onmenu'];
	
	if ($onmenu) {
		$onmenu = 0;
	} else {
		$onmenu = 1;
	}

		
	$sql = "INSERT INTO `pages` VALUES ( NULL,'".$title."', '".$body."', ".$onmenu.");";
			$query = mysql_query($sql);
			header('Location: pages.php?addpage=true');
			exit();
}



if(isset($_POST['update'])) { 

	$title = mysql_real_escape_string($_POST['title']);
	$body = $_POST['body'];
	$onmenu = $_POST['onmenu'];
	$pageid = $_POST['pageid'];
	
	if ($onmenu) {
		$onmenu = 0;
	} else {
		$onmenu = 1;
	}

		
	$sql = "UPDATE `pages` SET title='".$title."', body='".$body."', onmenu=".$onmenu." where id=".$pageid.";";
			$query = mysql_query($sql);
			header('Location: pages.php?updatepage=true');
			exit();
}


if(isset($_GET['deleteid'])) { 
	
	$deleteid = $_GET['deleteid'];
	
		$sql = "DELETE from `pages` WHERE `id`=".$deleteid.";";
			$query = mysql_query($sql);
			header('Location: pages.php?deletepage=true');
			exit();	
}

	$metatitle = "Page Management - Admin Control Panel";
	include ('includes/document_head.php');

?>
		<div id="wrapper">
			<?php include 'includes/topbar.php'?>		
			<?php include 'includes/sidebar.php'?>
			<div class="main_container container_16 clearfix">
				
<script type="text/javascript">
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete this page? It will be removed from your account immediately.")) {
    document.location = delUrl;
  }
}
</script>

		
			<div class="flat_area grid_16">
			
				<h2>Page Management</h2>
				<p>Pages are static pieces of content that display across the front-end of your site. All pages will display along your left sidebar, and you have the option of adding each page to the top menu bar.</p>
				<p>Common examples of pages are an "about us" page, contact information, article review guidelines, etc. Click the <strong>New Page</strong> button to create a new page, or edit/delete existing pages by clicking their titles in the table below.</p>
				
						<a href="pageedit.php?id=new"><button  class="skin_colour round_all"><img width="24" height="24" src="images/icons/small/white/Create Write.png"><span>Add New Page</span></button></a>

			</div>
		
<?php 
	if($_GET["addpage"] == "true") {
	echo '<center><p style="color: red;"><b>Page Added</b></p></center>';
	} 
	if($_GET["updatepage"] == "true") {
	echo '<center><p style="color: red;"><b>Page Updated</b></p></center>';
	} 
	if($_GET["deletepage"] == "true") {
	echo '<center><p style="color: red;"><b>Page Deleted</b></p></center>';
	} 
?>
	
			
			<div class="box grid_16 round_all">
				<table class="display table"> 
					<thead> 
						<tr> 
							<th width="60px">id</th> 
							<th width="170px">Title</th> 
							<th width="210px">Excerpt</th> 
							<th width="110px">Display on Menu?</th> 
							<th width="70px">Actions</th> 
						</tr> 
					</thead> 
					<tbody> 
					
	<?php // Populates the Dropdown list with all categories and subcats
	$query = "select * from pages;"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
	
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result);
		if ($row['onmenu'] == 0) {
			$onmenu = "YES";
		} else {
			$onmenu = "NO";
		}
		$removeitems = array("<p>", "</p>", "<div>", "</div>");// keep listings clean of unclosed tags
		$body = htmlspecialchars($row['body']);
		echo "<tr> <td>".$row['id']."</td> 
			 <td><a href=\"pageedit.php?id=".$row['id']."\">".$row['title']."</a></td> 
			 <td>".str_replace($removeitems, "", substr($body, 0, 40))."...</td>
			 <td>".$onmenu."</td>
			 <td><a href=\"pageedit.php?id=".$row['id']."\">Edit</a> | <a href=\"javascript:confirmDelete('pages.php?deleteid=".$row['id']."')\">Delete</a> </td></tr>";
			 
	}
	?>
					</tbody> 
				</table>
			</div>
			
			
			


			
		</div>	
<?php include 'includes/closing_items.php'?>