#!/usr/bin/perl
#
#calendar database should be in form
#Day|Events|Expls|Times

use cgi;
use dnfunc;

$dbname = "../calendar.db";
$cgiurl = "http://www.historicaljeromecounty.com/calendar2/index.cgi" ;

#############################################################################
#Addevent.cgi
# by Jay Eckles
# last modified, 5-20-98
#
# This cgi script is used as a part of the CGI Calendar system, a collection
# of perl scripts and modules with the purpose of creating a web-based
# group event calendar.  
#
# This script's job is to 1. add an event to the calendar database
#                         2. delete an event from the calendar database
#                         3. generate an html form for adding an event
# Which event should be added or deleted is communicated to the script via
# an html form.  The following is a list of the name and value pairs that
# Addevent.cgi must receive and the order in which they must be received.
# If a name/value pair is optional, it is listed in square brackets ([]).
# if multiple values are possible for a name, all optional values are
# listed in parentheses (()), and spearated by pipes (|)
#
# mode=(add|delete|gen)
# month=an integer greater than or equal to 1 and less than or equal to 12
# date=an integer greater than or equal to 1 and less than or equal to 31
# year=a positive integer
# time=a string in the format hh:mm where hh is a one or two digit integer
#      greater than or equal to 1 and less than or equal to 12, and mm is
#      a two digit integer greater than or equal to 0 and less than or 
#          equal to 59
# ampm=(AM|PM)
# summary=a string containing no newline characters
# [extended=a string containing no newline characters]
#
# These name/value pairs may be passed to the cgi program using either the 
# GET or POST methods.  The information is retrieved by calling a function
# found in the cgi.pm module.  
#
# The addition or deletion of the information from the database is accomplished
# by retrieving the current record for the date indicated, altering that record,
# then updating that record in the database by using the dn_add function 
# provided by the dnfunc.pm module.
#
# One line must be changed in this script to set it up for a new server; that
# is line 10.  It must be changed from 
# $cgiurl = "http://www.students.rhodes.edu/~pikes/calendar/index.cgi" ;
# to
# $cgiurl = "http://newserverdomain/pathtocalendardir/index.cgi" ;
# where newserverdomain is the domain of the new server the calendar is being
# setup on and pathtocalendardir is the path to the calendar directory
# containing index.cgi.
################################################################################

# initialize the monthnames and day lists
@monthnames =
("January","February","March","April","May","June","July","August","September","October","November","December") ;
@day =
("Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ) ;
 
#retrieve the name/value pairs via a function made available by cgi.pm
@input = retrieve_input() ;

#isolate the values of the name/value pairs by splitting the pairs by their '=' and
#then adding the second member of the resulting list to a scalar variable.

@mode = split( /=/, $input[0] ) ;
$mode = $mode[1] ;
 
@month = split( /=/, $input[1] ) ;
$month = $month[1] ;
 
@date = split( /=/, $input[2] ) ;
$date = $date[1] ;
 
@year = split( /=/, $input[3] ) ;
$year = $year[1] ;
 
#create a key based on the date given...key is in format ddmonthyyyy
#where dd is the date, month is the name of the month, and yyyy is the 
#year 
$key = $date.$monthnames[$month-1].$year ;

#open the database using a function provided by dnfunc.pm, make sure
#the open was successful and retrieve the record for the date given
#by using another function from dnfunc.pm
if( dn_open( $dbname ) eq "success" ){
   @day_record = dn_select( RECORD, $key ) ;
}
#if the database cannot be opened, return a message saying so and die.
else{
   return_error( "The database for the date you requested exists, but I can't open it.<p> <font size=-1>dn_open returned $status</font>" ) ;
}
 
#split the record up
$events = $day_record[1] ;
$expls = $day_record[2] ;
$times = $day_record[3] ;
#split up the events, expls, and times, into separate pieces
@events = split( /&/, $day_record[1] ) ;
@expls = split( /&/, $day_record[2] ) ;
@times = split( /&/, $day_record[3] ) ;
   
########################################Add Mode#############################################
if( $mode eq "add" ){
   @time = split( /=/, $input[4] ) ;
   $time = $time[1] ;
 
   @ampm = split( /=/, $input[5] ) ;
   $ampm = $ampm[1] ;
 
   @summary = split( /=/, $input[6] ) ;
   $summary = $summary[1] ;
 
   @extended = split( /=/, $input[7] ) ;
   $extended = encode( $extended[1] ) ;

   if( $summary eq "" || $month eq "" || $date eq "" || $year eq "" ){
      return_error( "You must enter the month, date, year, and a summary for the event!" ) ;
   }
 
#construct a new string that will be the record for the date
   $events .= $summary."&" ;
   $expls .= $extended."&" ;
   $times .= $time." ".$ampm."&" ;
   $recordstring = $key."\|".$events."\|".$expls."\|".$times ;
   dn_add( $key, $recordstring ) ;
 
#finally, send the user back to the calendar, loading the month and year that he added
#an event to.
   print_header( "Location", "$cgiurl?month=$month&year=$year" ) ; 

} #end if($mode eq "add")
 
elsif( $mode eq "delete" ){

#######################################Delete Mode##########################################
   @summary = split( /=/, $input[4] ) ;
   $summary = $summary[1] ;

   $match = 1 ;

#if the summary entered into the form matches a summary found in the events list, we'll 
#delete the parts of events, expls, and times with the same index.
   for( $i=0; $match == 1 && $i<@events; $i++ ){
      if( $summary eq $events[$i] ){
         $match = 0 ;
      }
   }
 
   $events = $expls = $times = "" ;

#if the summary match is found, reconstruct the events, times, and expls scalars
#but leave out the index being deleted
   if( $match == 0 ){
      $index = $i-1 ;
      for( $i=0; $i<@events; $i++ ){
         $events[$i] .= "&" ;
         $expls[$i] .= "&" ;
         $times[$i] .= "&" ;
      }
      for( $i=0; $i<@events; $i++ ){
         if( $i != $index ){
            $events .= $events[$i] ;
            $expls .= $expls[$i] ;
            $times .= $times[$i] ;
         }
      }
#construct the new string that will be the record
      $recordstring = $key."\|".$events."\|".$expls."\|".$times ;
#if there are still some events left on the day, add the newly constructed record string
#to the database
      if( $events ne "" ){
#update the database with the new record
         dn_add( $key, $recordstring ) ;
      }
#if there are no events left on the day, delete the entire record from the database (to
#save space and keep the database file managable.
      else{
         dn_delete( $key ) ;
      }
#finally, send the user back the the calendar, loading the month and year that he
#added an event to.
      print_header( "Location", "$cgiurl?month=$month&year=$year" ) ;
   }#end if($match)

#if the match was not found, return a message saying the deletion failed and die.
   else{
      return_error( "The summary you entered in the form did not match any event summary for this date in the database." ) ;
   }
}

elsif( $mode eq "gen" ){
##################################Gen Mode###################################

   open( HTML, "addevent.html" ) ;

   $i = 0 ;
   $monthname = $monthnames[$month-1] ;
   while( $input = <HTML> ){
      if( $input =~ /^<option value=$month>$monthname/ ){
         $html[$i] = "<option value=$month selected>$monthname\n" ;
      }
      elsif( $input =~ /^<input type=text size=2 name=date>/ ){
         $html[$i] = "<input type=text size=2 name=date value=$date>\n" ; 
      }
      elsif( $input =~ /^<input type=text size=4 name=year>/ ){
         $html[$i] = "<input type=text size=4 name=year value=$year>\n" ;
      }
      else{
         $html[$i] = $input ;
      }
      $i++ ;
   }
   close( HTML ) ;

   open( TOP, "../top.html" ) ;
   $i = 0 ;
   while( $input = <TOP> ){
      $top[$i] = $input ;
      $i++ ;
   }
   close( TOP ) ;
   open( BOT, "../bot.html" ) ;
   $i = 0 ;
   while( $input = <BOT> ){
      $bot[$i] = $input ;
      $i++ ;
   }
   close( BOT ) ;

   print_header( "Content-type", "text/html" ) ;
   print "@top @html @bot" ;

}

################################Default Mode########################################
#If a mode other than add or delete is indicated, try to deal with it by sending the user
#back to the calendar.
else{
   @name = split( /=/, $input[1] ) ;
   $name = $name[1] ;
 
   print_header( "Location", "$cgiurl?month=$prevmonth&year=$prevyear" ) ;
}
 
