<?php

/*
 * What rolls down stairs alone or in pairs, Rolls over your neighbor's dog? What's great for a snack and fits on your back?
 * It's Log, Log, Log!!
 *  
 * Log class for handling discussion logs from the MUD.
 * Mike Williams (2006) for MUD-Con.
 */

define ("RED","<font color=\"#ce0000\">");
define ("WHITE","<font color=\"#FFFFFF\">");
define ("BLACK","<font color=\"black\">");
define ("GRAY","<font color=\"#888888\">");
define ("CLEAR","</font>");

class log {

	var $db;

	function __construct($db) {
		$this->db = $db;
	}
	
	/*
	 * List the channels.
	 */
	public function listChannels() {
		echo "<table id=\"tbl_channels\" cellspacing=0 cellpadding=2>";
		echo "<tr><th width=\"20%\">Channel Name</th><th width=\"10%\">Topics</th>";
		echo "<th width=\"10%\">Posts</th><th width=\"80%\">Recent Topic</th></tr>";
		$sql = "SELECT * FROM `channels`";
		if ($result = $this->db->query($sql)) {
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				$ch_id = $row['id'];
				$ch_name = $row['name'];
				
				$r_topic = $this->oneTopic($ch_id, $this->getRecentTopic($ch_id));
				
				echo "<tr valign=\"top\"><td><a href=\"" . $_SERVER['PHP_SELF'] . "?do=topics&arg1=" . $ch_id . "\">" . $ch_name . "</a></td>";
				echo "<td>" . $this->countTopics($ch_id) . "</td>";
				echo "<td>" . $this->countPostsInChannel($ch_id) . "</td>";
				if (!isset($r_topic['name']))
					echo "<td>&nbsp;</td></tr>";
				else
					echo "<td><em>" . $r_topic['subject'] . "</em> by <strong>" . $r_topic['name'] . "</strong>.</td></tr>";
			}
		} else {
			exit("Database error.");
		}
		echo "</table><br />";
	}
	
	/*
	 * List the topics in a channel.
	 */
	public function listTopics($chan_id) {
	echo "<ul>";
		$sql = "SELECT * FROM `topics` WHERE `chanId`='$chan_id' ORDER BY `name` ASC";
		if ($result = $this->db->query($sql)) {
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				foreach($row as $key => $value)
					$$key = $value;
				echo "<li><a href=\"" . $_SERVER['PHP_SELF'] . "?do=posts&arg1=" . $chan_id . "&arg2=" . $id . "\">" . $subject . "</a> (" . $this->countPostsInTopic($chanId, $id) . ") ";
				echo "by <strong>" . $name . "</strong></li>";	
			}
		} else {
			exit("Database error.");
		}
		echo "</ul>";
		if ($result->num_rows < 1)
			echo "<strong>No topics have been created in this channel.</strong>";
	}
	
	public function listPosts($chan_id, $topic_id) {
		$sql = "SELECT * FROM `Posts` WHERE `topicid`='$topic_id' AND `chanId`='$chan_id' ORDER BY `timestamp` ASC";
		if ($result = $this->db->query($sql)) {
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				foreach($row as $key => $value)
					$$key = $value;
				
				echo RED . "[" . CLEAR . $timestamp . RED . "] " . CLEAR;
				echo RED . "[" . CLEAR.$this->channelName($chan_id) . RED . "] [" . CLEAR . "T:" . $topic_id . RED . "][" . CLEAR . "P:" . $id . RED . "]" . CLEAR;
				
				if ($replyid == -1) {
					echo " " . $poster . " " . GRAY . "says" . CLEAR . RED . ":" . CLEAR . " " . $msg . "<br>\n";
				} else {
					$r_topic = $this->onePost($chan_id, $topic_id, $replyid);
					echo " In reply to " . GRAY . $r_topic['poster'] . CLEAR.RED . "[" . CLEAR . "P:" . $replyid . RED . "] " . CLEAR . $poster .
					" " . GRAY . "says" . CLEAR . RED . ":" . CLEAR . " " . $msg . "<br>\n";
				}				
			}
		} else {
			exit("Database error.");
		}
		
	}
	
	/*
	 * Return # of topics in a channel.
	 */
	protected function countTopics($chan_id) {
		$sql = "SELECT * FROM `topics` WHERE `chanId`='$chan_id'";
		if ($result = $this->db->query($sql)) {
			return $result->num_rows;
		}
		return -1;
	}
	
	/*
	 * Return the # of posts in a given channel.
	 */
	protected function countPostsInChannel($chan_id) {
		$sql = "SELECT * FROM `posts` WHERE `chanId`='$chan_id'";
		if ($result = $this->db->query($sql)) {
			return $result->num_rows;
		}
		return -1;
	}
	
	/*
	 * Return the # of posts in a topic.
	 */
	protected function countPostsInTopic($chan_id, $topic_id) {
		$sql = "SELECT * FROM `posts` WHERE `chanId`='$chan_id' AND `topicid`='$topic_id'";
		if ($result = $this->db->query($sql)) {
			return $result->num_rows;
		}
		return -1;
	}
	
	/*
	 * Return an array of one post.
	 */
	protected function onePost($chan_id, $topic_id, $post_id) {
		$data = array();
		$sql = "SELECT * FROM `posts` WHERE `chanId`='$chan_id' AND `topicid`='$topic_id' AND `id`='$post_id' LIMIT 1";
		if ($result = $this->db->query($sql)) {
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				foreach ($row as $key => $value) {
					$data[$key] = $value;
				}
			}
		}
		return $data;
	}
	
	/*
	 * Return an array of one topic. 
	 */
	protected function oneTopic($chan_id, $topic_id) {
		$data = array();
		$sql = "SELECT * FROM `topics` WHERE `chanId`='$chan_id' AND `id`='$topic_id' LIMIT 1";
		if ($result = $this->db->query($sql)) {
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				foreach ($row as $key => $value) {
					$data[$key] = $value;
				}
			}
		}
		return $data;
	}	
	
	/*
	 * Return the topic id for the most recent topic in a channel.
	 */
	protected function getRecentTopic($chan_id) {
		$sql = "SELECT * FROM `topics` WHERE `chanId`='$chan_id' ORDER BY `id` DESC LIMIT 1";
		if ($result = $this->db->query($sql)) {
			if ($row = $result->fetch_array(MYSQLI_ASSOC))
				return $row['id'];
		}
		return -1;
	}
	
	protected function channelName($chan_id) {
		$sql = "SELECT * FROM `channels` WHERE `id`='$chan_id'";
		if ($result = $this->db->query($sql)) {
			if ($row = $result->fetch_array(MYSQLI_ASSOC))
				return $row['name'];
		}
		return null;
	}
	
	/*
	 * Tally up some numbers.
	 */
	public function showStats() {
		$topics=0;
		$posts=0;
		$sql = "SELECT * FROM `topics`";
			if ($result = $this->db->query($sql)) {
				$topics = $result->num_rows;
			}
		$sql = "SELECT * FROM `posts`";
			if ($result = $this->db->query($sql)) {
				$posts = $result->num_rows;
			}
		echo "<br /><strong>Total Topics:</strong> " . $topics . ".<br />";
		echo "<strong>Total Posts:</strong> " . $posts . ".<br /><br />";
	}
	
	

}

?>