<?php

include('frontend.inc.php');

$AUTHORS = mysqli_query($link,"SELECT DISTINCT id, name FROM contributor");

#$MTIME = mysqli_query($link,'SELECT UNIX_TIMESTAMP(MAX(published)) FROM strip') or die(mysqli_error($link));
#conditional_exit(current(mysqli_fetch_row($MTIME)));

pagehead('archive', 'Rant Archives');

?>

	<div class="content">
		<h2>View By:</h2>
		<div><ul class="nl">
			<li><a href="rant-archive.php?list_by=date">View by Date</a></li>
			<li><a href="rant-archive.php">View by Author</a></li>
		</ul></div>
	</div>

<?php

if(isset($_REQUEST['list_by']) && $_REQUEST['list_by'] == "date")
{
	$rants = mysqli_query($link,"SELECT rant.id AS id, rant.title AS title, contributor.name AS author,
											DATE_FORMAT(rant.published, '%M %D, %Y') AS pubdate
											FROM rant JOIN contributor ON rant.author = contributor.id
											WHERE (rant.published < NOW())
											AND rant.status = 'published' ORDER BY rant.published") or die(mysqli_error($link));

	echo '<div class="content"><h2>Rants by Date</h2><div><ol>';
	
	while($rant = mysqli_fetch_assoc($rants))
	{
		printf('<li><a title="%s" name="%d" href="./rant/%d">%s - %s</a></li>', $rant['pubdate'], $rant['id'], $rant['id'],
			$rant['author'], utfentities($rant['title']));
	}
	
	echo '</ol></div></div>';
}
else
{
?>
	<div class="content" id="typelinks">
		<h2>Rants by Author</h2>
		<div><ul><?php
			while ($author = mysqli_fetch_assoc($AUTHORS)) {
				$rants = mysqli_query($link,"SELECT distinct id
							FROM rant WHERE author = $author[id] AND published < NOW() AND rant.status = 'published'");
				if (mysqli_num_rows($rants) <= 0)
					continue;
				echo "\t\t<li><a href=\"rant-archive.php#$author[name]\">", utfentities($author[name]), "</a></li>\n";
			}
		?></ul></div>
	</div>
	<?php
	
	mysqli_data_seek($AUTHORS, 0);
	while ($author = mysqli_fetch_assoc($AUTHORS)) {
		$rants = mysqli_query($link,"SELECT distinct id, title,
							DATE_FORMAT(published, '%M %D, %Y') AS pubdate
							FROM rant WHERE author = $author[id] AND published < NOW() AND rant.status = 'published'");
		if (mysqli_num_rows($rants) <= 0)
					continue;
		echo '<div class="content"><h2><a id="', $author['name'], '">', utfentities($author['name']), '</a></h2><div><ol>';
		
		while($rant = mysqli_fetch_assoc($rants))
		{
			printf('<li><a title="%s" name="%d" href="./rant/%d">%s</a></li>', $rant['pubdate'], $rant['id'], $rant['id'],
				utfentities($rant['title']));
		}
		echo '</ol></div></div>';
	}
}

pagefoot();
?>
</body>
</html>
