#!/usr/bin/perl -wT
# Listing 3: view-thread.pl

# view-thread.pl, which allows people to view messages in a thread.

use strict;
use diagnostics;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use DBI;

my $database = "atf";
my $server = "localhost";
my $port = "3306";
my $username = "reuven";
my $password = "SETME";

# Remove buffering
$| = 1;

# ------------------------------------------------------------
# Create an instance of CGI
my $query = new CGI;

my $thread_id = $query->param("keywords");

# Send a MIME header
print $query->header("text/html");

print $query->start_html(-title => "View a thread",
			 -bgcolor => "#FFFFFF");

# ------------------------------------------------------------
# Connect to the database
my $dbh = 
    DBI->connect("DBI:mysql:$database:$server:$port",$username,$password);
die "DBI error from connect: \"$DBI::errstr\"" unless $dbh;

# ------------------------------------------------------------
# Get information about the thread
my $sql = "SELECT author,email,subject,text FROM ATFThreads WHERE id = $thread_id";

my $author = "";
my $email = "";
my $subject = "";
my $text = "";

# Send the query
my $sth = $dbh->prepare($sql);
die "DBI error with prepare: \"$sth->errstr\"" unless $sth;

# Execute the query
my $result = $sth->execute;
die "DBI error with execute: \"$sth->errstr\"" unless $result;

# We should only have received a single row.  Print it out.
if ($sth->rows)
{
    # Iterate through thread IDs and names
    while (my @row = $sth->fetchrow)
    {
	($author, $email, $subject, $text) = @row;
    }
}
else
{
    print "<P>Error retrieving thread information!  Aborting.</P>\n";
    exit;
}

print "<H1>Thread: $subject</H1>\n";
print "<blockquote><P>$text</P>\n";
print "<P>-- <a href=\"mailto:$email\">$author</a></blockquote>\n";
print "<HR>\n";

# ------------------------------------------------------------
$sql = "SELECT id, DATE_FORMAT(date, \"%W, %d %b %Y\"), ";
$sql .= "DATE_FORMAT(date, \"%h:%i %p\"), ";
$sql .= "author, email, subject, text FROM ATFMessages ";
$sql .= "WHERE thread = $thread_id ORDER BY date desc";

# Send the query
$sth = $dbh->prepare($sql);
die "DBI error with prepare: \"$sth->errstr\"" unless $sth;

# Execute the query
$sth->execute;
die "DBI error with execute: \"$sth->errstr\"" unless $sth;

# If we received threads from the SELECT, print them out
if ($sth->rows)
{
    # Iterate through thread IDs and names
    while (my @row = $sth->fetchrow)
    {
	my ($id, $date, $time, $author, $email, $subject, $text) = @row;

	print "<a name=\"$id\"><B>$subject</B>, ";
	print "by <a href=\"mailto:$email\">$author</a> on
$date at $time</P>\n";
	print "<blockquote>$text</blockquote>\n\n";
    }

    print "</ul>\n";

    # Finish that database call
    $sth->finish;
}
else
{
    print "<P>There are currently no messages here.</P>\n";
}

# ------------------------------------------------------------
# Menu bar
print "<P>\n";

# Send the user to the posting form
print "<a href=\"/cgi-bin/post-comment-form.pl?$thread_id\">[Post a message]";
print "</a>\n";

# Send the user to the thread list
print "<a href=\"/cgi-bin/list-threads.pl\">[View all threads]</a>\n";

# Send the user to the search
print "<a href=\"/atf/search-form.shtml\">[Search]</a>\n";

# Allow the user to create a new thread
print "<a href=\"/cgi-bin/add-thread-form.pl\">[Add a new
thread]</a>\n";

# Give a plug for the ATF home page
print "<a href=\"/atf/\">[ATF home]</a>\n";

print "</P>\n";

# Disconnect from the database
$dbh->disconnect;
