<?php

require_once('include/admin.inc.php');

auth_redirect(); // Require logged in user to access this page.

function human_time_diff( $from, $to = '' ) {
	if ( empty($to) )
		$to = time();
	$diff = (int) abs($to - $from);
	if ($diff <= 3600) {
		$mins = round($diff / 60);
		if ($mins <= 1) {
			$mins = 1;
		}
		$since = sprintf( $mins == 1 ? '%s minute' : '%s minutes', $mins);
	} elseif ($diff <= 86400) {
		$hours = round($diff / 3600);
		if ($hours <= 1) {
			$hour = 1;
		}
		$since = sprintf( $hours == 1 ? '%s hour' : '%s hours', $hours);
	} elseif($diff <= 604800 ) {
		$days = round($diff / 86400);
		if ($days <= 1) {
			$days = 1;
		}
		$since = sprintf( $days == 1 ? '%s day' : '%s days', $days);
	} else { 
		$weeks = round($diff / 604800);
		if( $weeks <= 1) {
			$weeks = 1;
		}
		$since = sprintf( $weeks == 1 ? '%s week' : '%s weeks', $weeks);
	}

	return $since;
}


adminhead();
adminmenu();
?>
<h2>Scratchpad</h2>
<form method="post" action="post-scratchpad.php">
<?php nonce_field('new-scratchpad'); ?>
	<ul class="historic">
	<?php
		$strips = array_reverse( $mtdb->getAll('SELECT UNIX_TIMESTAMP(s.published) AS pubdate, c.name, s.message FROM scratchpad s JOIN contributor c ON s.contributor = c.id ORDER BY published DESC LIMIT 5') );

		foreach($strips as $k=>$v)
		{
			$message = preg_replace('/(https?\:\/\/[^"\s\<\>]*[^.,;\'">\:\s\<\>\)\]\!])/i',
				'<a href="$1">$1</a>', htmlentities($v->message));
			printf( '<li>%s wrote %s ago: %s</li>', htmlspecialchars($v->name), human_time_diff($v->pubdate), $message);
		}
	?>
	</ul>
	<p style="padding-bottom:1em;"><input type="text" name="message" /><input type="submit" value="Send" /></p>
	
</form><br>

<h2>Recent Strips</h2>
<ul class="historic">
<?php
$strips = $mtdb->getAll("SELECT distinct id, title, UNIX_TIMESTAMP(published) as date FROM strip WHERE published <= NOW() order by id DESC LIMIT 5");

foreach($strips as $k=>$v) {
	printf( '<li>%d: <a href="%s/index.php?strip_id=%d">%s</a>, %s ago</li>', $v->id, SITE_HOST . SITE_PATH, $v->id, htmlspecialchars($v->title), human_time_diff($v->date) );
}
?>
</ul><br>

<h2>Upcoming Strips</h2>
<ul class="historic">
<?php
$strips = $mtdb->getAll("SELECT distinct id, title, UNIX_TIMESTAMP(published) as date FROM strip WHERE published > NOW() order by id ASC LIMIT 5");

foreach($strips as $k=>$v) {
	printf( '<li>%d: <a href="%s/edit-comic.php?strip_id=%d">%s</a>, in %s</li>', $v->id, SITE_HOST . SITE_PATH . '/' . SITE_ADMIN, $v->id, htmlspecialchars($v->title), human_time_diff($v->date) );
}
?>
</ul><br>

<h2>Recent Published Rants</h2>
<ul class="historic">
<?php
$rants = $mtdb->getAll('SELECT distinct rant.id,UNIX_TIMESTAMP(rant.published) as date,rant.title,contributor.name from rant,contributor where rant.author=contributor.id AND rant.status=\'published\' ORDER BY rant.published DESC limit 5');

foreach($rants as $k=>$v) {
	printf( '<li>%d: <a href="%s/index.php?rant_id=%d">%s</a> by %s, %s ago</li>', $v->id, SITE_HOST . SITE_PATH, $v->id, htmlspecialchars($v->title), htmlspecialchars($v->name), human_time_diff($v->date) );
}
?>
</ul><br>

<h2>Recent Draft Rants</h2>
<ul class="historic">
<?php
$rants = $mtdb->getAll('SELECT distinct rant.id,UNIX_TIMESTAMP(rant.published) as date,rant.title,contributor.name from rant,contributor where rant.author=contributor.id AND rant.status=\'draft\' ORDER BY rant.published DESC limit 5');

foreach($rants as $k=>$v) {
	printf( '<li>%d: <a href="%s/edit-rant.php?rant_id=%d">%s</a> by %s, %s ago</li>', $v->id, SITE_HOST . ADMIN_PATH, $v->id, htmlspecialchars($v->title), htmlspecialchars($v->name), human_time_diff($v->date) );
}
?>
</ul>

<?php
adminfooter();
?>
