#!/usr/bin/perl -w

# postcard-form.pl, a program that prints out an HTML form
# suitable for creating postcards.

use strict;
use diagnostics;
use CGI;  # available from http://www.perl.com/CPAN/
use Mysql;

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

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

# Begin the HTML output
print $query->start_html(-title => "Send a postcard!");

# Connect via Unix sockets to the database on this server
my $dbh = Mysql->connect("localhost", "test");

# Build up our SQL command
my $command = "";
$command = "select graphic_id,graphic_file from graphics";

# Uncomment for debugging
# print "<P>SQL command: <B>$command</B></P>\n";

# Send the query
my $sth = $dbh->query($command);

# Make sure that $sth returned reasonably
die "Error with command \"$command\"" unless (defined $sth);

# Iterate through the returned rows
my @arr = ();

# Print some HTML
print '<P>Now you can send a Web-based postcard to your friends!  ';
print 'Enter the appropriate information, and e-mail will be sent ';
print 'to your friends indicating where they can ';
print 'pick up their postcard.</P>', "\n";

print '<Form method="POST" action="/cgi-bin/send-postcard.pl">', "\n";

print '<P>Your name: <input type="text" name="sender_name"> </P>';
print '<P>Your e-mail address: <input type="text" name="sender_email"> </P>';

print '<P>Recipient's name: <input type="text" name="recipient_name"> </P>';

print '<P>Recipient's e-mail address: <input type="text" ';
print 'name="recipient_email"> </P>';

print '<P>Name of the graphic to insert: <input type="text" ';
print 'name="graphic_name"> </P>';

# Now get the graphics IDs from the database, and print them out
while (@arr = $sth->fetchrow)
{
    # Get the ID and filename
    my ($id, $name) = @arr;

    print "<P><input type=\"radio\" value=\"$id\" name=\"graphic_id\">$name</P>\n";
}

print '<textarea name="postcard_text" wrap="hard" rows="20" cols="60">', "\n";
print '</textarea>';

print '<P><input type="submit" value="Send postcard"></P>', "\n";
print '<P><input type="reset" value="Clear current contents"></P>', "\n";

print "</Form>\n";

# End the HTML
print $query->end_html;

exit;

