<?php 
require_once ('includes/config.inc.php'); 
$page_title = 'Inbox';
include ('includes/headr.php');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> <?php echo $page_title; ?> </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <link href="includes/grid.css" rel="stylesheet" type="text/css">
  <link href="includes/panels.css" rel="stylesheet" type="text/css">
  <link href="includes/main.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="js/jquery.js"></script>
  <script>
  $(document).ready(function(){
  $("#fshow").click(function(){
    $("#togglef").toggle();
	if(document.getElementById("fshow").innerHTML=="Show All")
	document.getElementById("fshow").innerHTML="Hide All";
	else
	document.getElementById("fshow").innerHTML="Show All";
  });
  $("#cshow").click(function(){
    $("#togglec").toggle();
	if(document.getElementById("cshow").innerHTML=="Show All")
	document.getElementById("cshow").innerHTML="Hide All";
	else
	document.getElementById("cshow").innerHTML="Show All";
  });
  $("#fashow").click(function(){
    $("#togglefa").toggle();
	if(document.getElementById("fashow").innerHTML=="Show All")
	document.getElementById("fashow").innerHTML="Hide All";
	else
	document.getElementById("fashow").innerHTML="Show All";
  });
  $("#nashow").click(function(){
    $("#togglena").toggle();
	if(document.getElementById("nashow").innerHTML=="Show All")
	document.getElementById("nashow").innerHTML="Hide All";
	else
	document.getElementById("nashow").innerHTML="Show All";
  });
});
  </script>
 </head>
 <?php 


if (isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])) { //Make sure user is logged in and an administrator as well!

//it's all good!
 
 //text search? redirect!
if (isset ($_GET['sitesearch'])){
	header('Location: home.php?sitesearch='.$_GET['sitesearch'].'');

}
 

	
	}//end check
else //if user is not a logged in
	{
	// Show an error message.
		header('Location: Access Error.php');
	}

 if (isset($_POST['deleted'])){ //if notification is deleted
		$id= $_POST['id'];
		$q = "UPDATE notification SET seen='1' WHERE id ='$id'";
		$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));			

		
    }//end if deleted notification

 ?>

 <body>
  <h2 style="text-align:center;">Hello, <?php echo $_SESSION['username']; ?>!</h2>
  <p style="text-align:center;">Newest activity:</p>
  <div class="panel" id="followersi">
  <p>New followers:</p>
  <?php
  
		$q1="SELECT * FROM notification WHERE type='1' AND to_user='".$_SESSION['user_id']."' AND seen='0' GROUP BY from_user"; 
		$r1 = mysqli_query($dbc, $q1) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		if(mysqli_num_rows($r1)>0){
		print '<button id="fshow">Show All</button>
		<div id="togglef" style="display:none;">
		<table width="300" border="0" cellpadding="0" cellspacing="5">';
		while($row1 = mysqli_fetch_array ($r1, MYSQLI_ASSOC)) {	
		$q2="SELECT username FROM user WHERE user_id='".$row1['from_user']."'"; 
		$r2 = mysqli_query($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		$row2 = mysqli_fetch_array ($r2, MYSQLI_ASSOC);
		print '<tr>
		<td height="22" align="right" valign="middle" width="80%"><p><a href="profile.php?name='.$row2['username'].'&content=1">'.$row2['username'].'</a></p></td>
		<td align="left" width="20%"><div id="report">
		<form method="post" name="gotit" id="gotit">
		<input type="submit" title="Got it!" name="seen" id="seen" value="Got it!">
		<input type="hidden" name="id" value="'.$row1['id'].'" />
		<input type="hidden" name="deleted" value="TRUE" />
		</form>		
		</div></td></tr>';
		}
		print '<div class="dummyClear"></div></table></div>';
		}//end mysqli-num-rows
		else echo 'No new followers!</div>';
  ?>
  </div>
  <div class="panel" id="commentsi">
  <p>New Comments:</p>
  <?php
  
  //$row1 = mysqli_fetch_array ($r1, MYSQLI_ASSOC);
  
		$q1="SELECT * FROM notification WHERE type='2' AND to_user='".$_SESSION['user_id']."' AND seen='0'"; 
		$r1 = mysqli_query($dbc, $q1) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		if(mysqli_num_rows($r1)>0){
		print '<button id="cshow">Show All</button>
		<div id="togglec" style="display:none;">
		<table width="500" border="0" cellpadding="0" cellspacing="5">';
		while($row1 = mysqli_fetch_array ($r1, MYSQLI_ASSOC)) {	
		$q2="SELECT username, artwork_id, title FROM user, artwork WHERE user.user_id='".$row1['from_user']."' AND artwork_id='".$row1['itemref']."'"; 
		$r2 = mysqli_query($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		$row2 = mysqli_fetch_array ($r2, MYSQLI_ASSOC);
		print '<tr>
		<td height="22" align="right" valign="middle" width="80%">
		<p>User <a href="profile.php?name='.$row2['username'].'&content=1">'.$row2['username'].'</a> commented on your artwork <a href="view_artwork.php?artwork_id='.$row2['artwork_id'].'&content=2">'.$row2['title'].'</a></p></td>
		<td align="left" width="20%"><div id="report"><form method="post" name="gotit" id="gotit">
		<input type="submit" title="Got it!" name="seen" id="seen" value="Got it!">
		<input type="hidden" name="id" value="'.$row1['id'].'" />
		<input type="hidden" name="deleted" value="TRUE" />
		</form>		
		</div></td></tr>';
		
		}
		print '<div class="dummyClear"></div></table></div>';
		}//end mysqli-num-rows
		else echo 'No new comments!</div>';
		
  ?>
  </div>
  <div class="panel" id="favoritesi">
  <p>New Favorites:</p>
  <?php
		
		$q1="SELECT * FROM notification WHERE type='3' AND to_user='".$_SESSION['user_id']."' AND seen='0'"; 
		$r1 = mysqli_query($dbc, $q1) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		if(mysqli_num_rows($r1)>0){
		print '<button id="fashow">Show All</button>
		<div id="togglefa" style="display:none;">
		<table width="500" border="0" cellpadding="0" cellspacing="5">';
		while($row1 = mysqli_fetch_array ($r1, MYSQLI_ASSOC)) {	
		$q2="SELECT username, artwork_id, title FROM user, artwork WHERE user.user_id='".$row1['from_user']."' AND artwork_id='".$row1['itemref']."'"; 
		$r2 = mysqli_query($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		$row2 = mysqli_fetch_array ($r2, MYSQLI_ASSOC);
		print '
		<tr>
		<td height="22" align="right" valign="middle" width="80%">
		<p>User <a href="profile.php?name='.$row2['username'].'&content=1">'.$row2['username'].'</a> has added <a href="view_artwork.php?artwork_id='.$row2['artwork_id'].'&content=1">'.$row2['title'].'</a> to his favorites.</p></td>
		<td align="left" width="20%"><div id="report">
		<form method="post" name="gotit" id="gotit">
		<input type="submit" title="Got it!" name="seen" id="seen" value="Got it!">
		<input type="hidden" name="id" value="'.$row1['id'].'" />
		<input type="hidden" name="deleted" value="TRUE" />
		</form>		
		</div></td></tr>';
		
		
		}
		print '<div class="dummyClear"></div></table></div>';
		}//end mysqli-num-rows
		else echo 'No new favorites!</div>';
  ?>
  
  
  </div>
  <div class="panel" id="newactivity">
  <p>New Activity from people you follow:</p>
  <?php 
		
		$q1="SELECT * FROM notification WHERE type='4' AND to_user='".$_SESSION['user_id']."' AND seen='0' GROUP BY from_user"; 
		$r1 = mysqli_query($dbc, $q1) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
		if(mysqli_num_rows($r1)>0){
		print '<button id="nashow">Show All</button>
		<div id="togglena" style="display:none;">
		<table width="520" border="0" cellpadding="0" cellspacing="5">';
		while($row1 = mysqli_fetch_array ($r1, MYSQLI_ASSOC)) {	
		$q2="SELECT username FROM user WHERE user_id='".$row1['from_user']."'"; 
		$r2 = mysqli_query($dbc, $q2) or trigger_error("Query: $q2\n<br />MySQL Error: " . mysqli_error($dbc));
		$row2 = mysqli_fetch_array ($r2, MYSQLI_ASSOC);
		print '
		<tr>
		<td height="22" align="right" valign="middle" width="80%">
		<p><a href="profile.php?name='.$row2['username'].'&content=2">'.$row2['username'].'</a> has uploaded new work! Click the name to sheck it out!</p></td>
		<td align="left" width="20%"><div id="report">
		<form method="post" name="gotit" id="gotit">
		<input type="submit" title="Got it!" name="seen" id="seen" value="Got it!">
		<input type="hidden" name="id" value="'.$row1['id'].'" />
		<input type="hidden" name="deleted" value="TRUE" />
		</form>		
		</div></td></tr>';
		
		
		}
		print '<div class="dummyClear"></div></table></div>';
		}//end mysqli-num-rows
		else echo 'No new activity!</div>';
  ?>
  
  
  </div>
  <div id="push">
  <div class="dummyClear"></div></div>
 </body>
 <div><?php include ('includes/footer.php'); ?></div>
</html>
