#!/usr/bin/perl -wT

# Time-stamp: <1998-10-19 20:54:46 reuven>

# post-comment-form.pl, which creates a form for entering comments

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

use lib qw(/home/reuven/www/cgi-bin);
use ATFConstants;

# Remove buffering
$| = 1;

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

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

# Get the thread id, if it was set
my $thread_id = $query->param("keywords") || 0;

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

# ------------------------------------------------------------
# Create the form
print $query->start_html(-title => "Post a new message",
			 -bgcolor => "#FFFFFF");

print "<H1>Post a new message</H1>\n";

print "<P>Enter a new message in the ATF discussion board.</P>\n\n";

print "<Form method=\"POST\" action=\"/cgi-bin/post-comment.pl\">\n";
print "<Table>\n";

# Here is the form

# ------------------------------------------------------------
# Let the person enter their name
my $name = $query->cookie(-name => "name") || "";
print "<TR>\n";
print "<TD>Your name</TD>\n";
print "<TD><input type=\"text\" size=\"50\" ";
print "value=\"$name\" name=\"name\"></TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Enter the e-mail address
my $email = $query->cookie(-name => "email") || "";
print "<TR>\n";
print "<TD>Your e-mail address</TD>\n";
print "<TD><input type=\"text\" size=\"50\" ";
print "value=\"$email\" name=\"email\"></TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Enter the thread subject
print "<TR>\n";
print "<TD>Thread</TD>\n\n";
print "<TD>\n";

# Create the SQL query
my $sql = "SELECT id,subject FROM ATFThreads ORDER BY subject";

# 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;

# If we received threads from the SELECT, print them out
if ($sth->rows)
{
    print "<select name=\"thread\">\n";

    # Iterate through thread IDs and names
    while (my @row = $sth->fetchrow)
    {
	print "<option value=\"$row[0]\" ";
	print " selected " if ($thread_id == $row[0]);
	print ">$row[1]\n";
    }

    # Finish that database call
    $sth->finish;
    print "</select>\n";
}
# No threads defined?  Uh-oh.
else
{
    print "<P>No threads defined; posting will fail.</P>\n";
    print "<P>Please contact the system administrator.</P>\n";
}

print "</TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Enter the message subject
print "<TR>\n";
print "<TD>Message subject</TD>\n";
print "<TD><input type=\"text\" size=\"50\" name=\"subject\"></TD>\n";
print "</TR>\n\n";

# ------------------------------------------------------------
# Allow them to enter the message
print "<TR>\n";
print "<TD>Your posting</TD>\n";
print "<TD><textarea cols=\"60\" rows=\"20\" name=\"text\">";
print "HTML tags will be removed.\n\n";
print "Use blank lines to separate paragraphs.\n";
print "</textarea></TD>\n";
print "</TR>\n\n";

# Finish the table
print "</Table>\n";

# Produce a submit button
print '<input type="submit" value="Post message">';

# Finish the HTML
print "</Form>\n";

# ------------------------------------------------------------
# Menu bar
print "<P>\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";

print $query->end_html;

# Disconnect, even though it isn't really necessary
$dbh->disconnect;

