#!/usr/local/bin/perl -w

use strict;

use URI::Escape;
use XML::Simple;

# send back basic header
print "Content-Type: text/html\n\n";

# figure out CGI options
if( $ENV{'REQUEST_METHOD'} eq "POST" ) {
    local $/ = undef;
    $ENV{'QUERY_STRING'} = <>;
}

my %cgioptions = map {split /=/, $_, 2} split /&/, $ENV{QUERY_STRING};
my $ThisScript ="newsletter.pl";

#<!--#include file="header.html" -->
IncludeHTMLFile("../header.html");

print <<JSHEADER
<script type="text/javascript">
  page="pcc";
</script>
JSHEADER
;

#<!--#include file="menu.html" -->
IncludeHTMLFile("../menu.html");

print "<div id=\"pagecontent\">\n";
print "<h1 class=\"pagetitle\">Newsletter</h1>\n";

# display the form
print "<form action=\"$ThisScript\" method=\"post\">\n<input type=\"hidden\" name=\"NewItem\" value=\"True\" /><table class=\"formtable\"><tr>\n<td colspan=\"4\">Add a new Item to the Newsletter</td>\n</tr>\n";
print "<tr><td>Month</td>\n<td><select name=\"Month\">";
for (my $i = 1; $i <=12; $i++){
    print "<option ".($i == $cgioptions{'Month'}?"selected=\"\" ":"")."value=\"$i\">$i</option>\n";
}
print "</select>\n</td>\n";
print "<td>Year</td>\n<td><select name=\"Year\">";
for (my $i = 2005; $i <=2007; $i++){
    print "<option ".($i == $cgioptions{'Year'}?"selected=\"\" ":"")."value=\"$i\">$i</option>\n";
}
print "</select>\n</td>\n</tr>\n";


print "<tr>\n<td colspan=\"4\">Title</td>\n</tr>\n";
print "<tr><td colspan=\"4\"><input type=\"text\" name=\"Title\" value=\"\" size=\"60\" /></td>\n</tr>\n";
print "<tr>\n<td colspan=\"4\">Story</td>\n</tr>\n<tr><td colspan=\"4\"><textarea name=\"Story\" value=\"\" rows=\"20\" cols=\"60\"></textarea></td>\n</tr>\n";
print "<tr>\n<td><input type=\"submit\" value=\"submit\" /></td>\n</tr>\n";
print "</table></form>";

if (defined $cgioptions{'NewItem'}){
    if (defined $cgioptions{'Title'} && $cgioptions{'Title'} ne "" && defined $cgioptions{'Story'} && $cgioptions{'Story'} ne ""){
        $cgioptions{'Title'} = uri_unescape($cgioptions{'Title'});
        $cgioptions{'Story'} = uri_unescape($cgioptions{'Story'});
        $cgioptions{'Title'} =~ s/[+&]/ /gom;
        $cgioptions{'Story'} =~ s/[+&]/ /gom;
 #       $cgioptions{'Story'} =~ s/\302//gom;
#        $cgioptions{'Story'} =~ s/\243/&\#163;/gom;
        $cgioptions{'Story'} =~ s/\r\n/__p__/gom;
        my $xs = XML::Simple->new();

        my $xml;
        if ( -e "../newsletter-".$cgioptions{'Month'}."-".$cgioptions{'Year'}.".xml"){
            $xml = $xs->XMLin("../newsletter-".$cgioptions{'Month'}."-".$cgioptions{'Year'}.".xml", forcearray=>1);
        } else {
            $xml = {'item' => []};
        }
        my @items = ();
        my $count = 0;
        foreach my $item (@{$xml->{'item'}}){
            $items[$count++] = {'title' => $item->{'title'}[0], 'story' => $item->{'story'}[0]};
        }
        
        $items[$#items+1] = {'title' => $cgioptions{'Title'}, 'story' => $cgioptions{'Story'}};

        open( OUT, ">../newsletter-".$cgioptions{'Month'}."-".$cgioptions{'Year'}.".xml") || die "couldn't open : $!";  

        print OUT "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\n<month>\n";

        for (my $i = 0; $i <= $#items; $i++) {
            if (defined $items[$i]->{'title'}){
                print OUT "<item>\n<title>$items[$i]->{'title'}</title><story>$items[$i]->{'story'}</story>\n</item>\n";
            }
        }
        print OUT "</month>\n";
        close OUT;
        
        `cd .. && ./newsletter.pl > newsletter.shtml`;

        print "<table class=\"newitem\"><tr><td>Added new item: $cgioptions{'Title'}</td></tr>\n</table>";
    } else {
        print "<table class=\"newitem\"><tr><td>You must provide both a title and a story!</td></tr>\n</table>";
    }

}

# end of pagecontent
print "</div>";

# <!--#include file="breakingNews.html" -->
IncludeHTMLFile("../breakingNews.html");

# <!--#include file="calendar.html" -->
IncludeHTMLFile("../calendar.html");

# <!--#include file="footer.html" -->
IncludeHTMLFile("../footer.html");

;

#############
# Functions #
#############

sub IncludeHTMLFile( $ ){
    my $filename = shift;
    return undef if !(defined $filename && -e $filename);
    local $/ = undef;
    open (IN, $filename ) or return undef;
    print <IN>;
}
