<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dashboard</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
    	<div class="contentblock">
			<?php
				echo('<h2>Welcome, '.$_p->tb_users->getFieldValue($_SESSION['auth_id'],'username').'</h2>');
				
				//Find unapproved comments
				$unapproved_comments=$_p->tb_comments->select('!%approved%');
				$unapproved_count=count($unapproved_comments);
				
				//Find drafted posts by this author
				$drafted_posts=$_p->tb_posts->select('!%is_published%&&%author%==\''.fetch::qEscape($_SESSION['AUTH_ID']).'\'',array('title','timestamp'));
				$drafted_posts=fetch::sortSelection($drafted_posts,'timestamp',SORT_DESC);
				
				//Find drafted pages
				$drafted_pages=$_p->tb_pages->select('!%is_published%',array('title','timestamp'));
				$drafted_pages=fetch::sortSelection($drafted_pages,'timestamp',SORT_DESC);
				
				//List tasks
				echo('<h3>Tasks</h3>');
				$taskfree=true;
				if($unapproved_count>0) {
					echo('<h4>Manage Comment Approval Queue</h4>
					There are '.count($unapproved_comments).' comments awaiting approval.<br />
					<a href="managecomments.php?show_unapproved">View unapproved comments?</a>');
					$taskfree=false;
				}
				if(count($drafted_posts)>0) {
					echo('<h4>Review Drafted Posts</h4>
					You have '.count($drafted_posts).' posts awaiting publishing.
					<ol>');
					foreach($drafted_posts as $postID=>$postData)
						echo('<li><strong>'.htmlentities($postData['title'],ENT_QUOTES,"UTF-8").'</strong> <a href="manageposts.php?p='.$postID.'&amp;edit">Edit &raquo;</a></li>');
					echo('</ol>');
					$taskfree=false;
				}
				if(count($drafted_pages)>0) {
					echo('<h4>Review Drafted Pages</h4>
					There are '.count($drafted_pages).' pages awaiting publishing.
					<ol>');
					foreach($drafted_pages as $pageID=>$pageData)
						echo('<li><strong>'.htmlentities($pageData['title'],ENT_QUOTES,"UTF-8").'</strong> <a href="managepages.php?p='.$pageID.'&amp;edit">Edit &raquo;</a></li>');
					echo('</ol>');
					$taskfree=false;
				}
				/*Determine which categories have not been updated in a week*/
				$categories=$_p->tb_categories->select('true');
				/*Determine if this user has not added more categories yet*/
				if(count($categories)==1) {
					echo('<h4>Add More Categories</h4>
					You have not added any new categories yet, consider <a href="categories.php">adding a category &raquo;</a>');
					$taskfree=false;
				}
				$stale_categories=array();
				$most_stale_category=array('id'=>0,'name'=>'Placeholder Name','timestamp'=>time());
				foreach($categories as $cat_id=>$cat_data) {
					/*Determine the date of the last post*/
					$posts_in_cat=$_p->get_cat($cat_id);
					if(count($posts_in_cat)>0) {
						$last_post=array_shift($posts_in_cat);
						/*Stale categories have not been updated in a week*/
						if((time()-$last_post['timestamp'])>60*60*24*7)
							$stale_categories[$cat_id]=$cat_data['name'];
						/*Update most stale category*/
						if($last_post['timestamp']<$most_stale_category['timestamp'])
							$most_stale_category=array('id'=>$cat_id,'name'=>$cat_data['name'],'timestamp'=>$last_post['timestamp']);
					}
				}
				if(count($stale_categories)>0) {
					echo('<h4>Revive Stale Categories</h4>
					There are '.count($stale_categories).' categories that have not recieved new posts in at least a week.
					<ol>');
					foreach($stale_categories as $cat_id=>$cat_name)
						echo('<li><a href="post.php?categories[]='.$cat_id.'">Create new post in <strong>'.htmlentities($cat_name,ENT_QUOTES,"UTF-8").'</strong> &raquo;</a></li>');
					echo('</ol>');
					$taskfree=false;
				}
				if($taskfree)
					echo('There are no matters that need your attention right now, consider <a href="post.php?categories[]='.$most_stale_category['id'].'">posting to the <strong>'.htmlentities($most_stale_category['name'],ENT_QUOTES,"UTF-8").'</strong> category &raquo;</a>');
				
				//Display recent comments
				echo('
				<h3>Recent Comments</h3>');
				$comments=fetch::sortSelection($_p->tb_comments->select('%approved%'),'timestamp',SORT_DESC);
				if(count($comments)<=0)
					echo('No comments to display.');
				else {
					echo('<ol>');
					$max=10;
					foreach($comments as $d) {
							if($max==0)
								break;
							echo('<li>
							<strong>'.
								htmlentities(
									($d['prominent']!==false?
									'['.$_p->tb_users->getFieldValue($d['prominent'],'username').']':
									$d['name'])
								,ENT_QUOTES,"UTF-8").
							'</strong>
							 on '.
							 date('j<\s\up>S</\s\up> M y',$d['timestamp']).' ('.$d['ip'].') said:
							 <br />'.htmlentities($d['content'],ENT_QUOTES,"UTF-8").'<br />');
							 
							$max--;
					}
					echo('</ol>');
				}
			?>
        </div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>