<?php 
session_start();

$pagetitle = "View & Edit Articles - Author Control Panel";
include('../config.php');
include('security.php');


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


include('header.php');
?>


<!-- LEFT SIDEBAR -->
<?php include('../sidebar.php'); ?>

<!-- CONTENT AREA -->
<div id="content-full">	

<h1>View & Edit Your Articles</h1>


<script type="text/javascript">
function confirmDelete(delUrl) {
  if (confirm("Are you sure you want to delete this article? It will be removed from your account immediately.")) {
    document.location = delUrl;
  }
}
</script>


<?php 


// PROBLEM ARTICLE TABLE

	$query = "select * from articles where authorid =".$id." and status = 2 order by ".$id." desc"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
	
	if ($num_results != 1) { // Check for plural
		$plural = "s";
	}
	
	if ($num_results == 0) { // Hide table if empty
	} else {
		
	echo "<div class=\"articletable\">
		 <table><caption style=\"color:#BA000A;\">You have <strong>".$num_results." Problem</strong> article".$plural.":</caption>
		 <tr><td width=\"100px\"><b>Date</b></td> <td width=\"280px\"><b>Title</b></td> <td colspan=\"2\"width=\"215px\"><b>REASON</b></td> 
		 <td colspan=\"2\" width=\"120px\"><b><center>Actions</center></b></td></tr>";
	
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result); 
		$date = strtotime($row['date']);
		$artdate = date('m/d/y', $date);
		echo "<tr><td>".$artdate."</td> 
			  <td>".htmlspecialchars(stripslashes($row['title']))."</td> 
			  <td colspan=\"2\">".$row['problem']."</td>
			  <td><a href=\"update.php?id=".$row['id']."\"><b>FIX</b></a></td> 
			  <td><a href=\"javascript:confirmDelete('articles.php?deleteid=".$row['id']."')\"><b>Delete</b></a></td></tr>"; 
	}
	echo "</table></div> <br /><br />";

	}


// "REVIEW" ARTICLES TABLE
	
	$query = "select * from articles where authorid =".$id." and status = 1 order by ".$id." desc"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);
	
	if ($num_results != 1) { // Check for plural
		$plural = "s";
	}
	
	if ($num_results == 0) { // Hide table if empty
	} else {

	
	echo "<div class=\"articletable\">
		 <table><caption style=\"color:#CFC553;\">You have <strong>".$num_results." Article".$plural."</strong> Under Review:</caption>
		 <tr><td width=\"100px\"><b>Submitted</b></td> <td width=\"350px\"><b>Title</b></td> <td width=\"100px\"><b>Total Views</b></td> 
		 <td colspan=\"2\" width=\"120px\"><b><center>Actions</center></b></td></tr>";
	
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result); 
		
		$query = "select * from articleviews where articleid = ".$row['id'];
		$results = mysql_query($query,$connection) or die(mysql_error());
		$viewinfo = mysql_fetch_array($results);
		$totalviews = $viewinfo['views'];

		$date = strtotime($row['date']);
		$artdate = date('m/d/y', $date);
		echo "<tr><td>".$artdate."</td> 
			  <td>".htmlspecialchars(stripslashes($row['title']))."</td> 
			  <td>".$totalviews."</td>
			  <td><a href=\"update.php?id=".$row['id']."\"><b>Edit</b></a></td> 
			  <td><a href=\"javascript:confirmDelete('articles.php?deleteid=".$row['id']."')\"><b>Delete</b></a></td></tr>"; 
	}
	echo "</table></div> <br /><br />";

	}
	

// ACTIVE ARTICLE TABLE

	$query = "select * from articles where authorid =".$id." and status = 0  order by ".$id." desc"; 
	$result = mysql_query($query,$connection) or die(mysql_error());
	$num_results = mysql_num_rows($result);

	if ($num_results != 1) { // Check for plural
		$plural = "s";
	}
	
	if ($num_results == 0) { // Hide table if empty
	} else {
		
	echo "<div class=\"articletable\">
		 <table><caption style=\"color:#14A300;\">You have <strong>".$num_results." Active</strong> article".$plural.":</caption>
		 <tr><td width=\"100px\"><b>Date</b></td> <td width=\"350px\"><b>Title</b></td> <td width=\"100px\"><b>Total Views</b></td> 
		 <td colspan=\"3\" width=\"150px\"><b><center>Actions</center></b></td></tr>";
	
	for ($i=0; $i <$num_results; $i++) { 
		$row = mysql_fetch_assoc($result); 
		
		$query = "select * from articleviews where articleid = ".$row['id'];
		$results = mysql_query($query,$connection) or die(mysql_error());
		$viewinfo = mysql_fetch_array($results);
		$totalviews = $viewinfo['views'];
	
		$date = strtotime($row['date']);
		$artdate = date('m/d/y', $date);
		echo "<tr><td>".$artdate."</td> 
			  <td>".htmlspecialchars(stripslashes($row['title']))."</td> 
			  <td>".$totalviews."</td>
			  <td><a href=\"../article.php?id=".$row['id']."\"><b>View</b></a></td> 
			  <td><a href=\"update.php?id=".$row['id']."\"><b>Edit</b></a></td> 
			  <td><a href=\"javascript:confirmDelete('articles.php?deleteid=".$row['id']."')\"><b>Delete</b></a></td></tr>"; 
	}
	echo "</table></div> <br /><br />";
	
	}


?>


</div> <!-- End Content -->


<?php include('../footer.php'); ?>