#!/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" ;

#############################################################################
#Viewday.cgi
# by Jay Eckles
# last modified, 5-29-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. display information for all events on given day
#	       -OR-  		  2. display information for one given event on day
# Which event(s) should be shown is communicated to the script via
# CGI.  The following is a list of the name and value pairs that
# Viewday.cgi must receive; the order in which they must be received is of
# no importance.
# 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=(all|one)
# 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
# [summary=a string containing no newline characters]
#
# Note that summary is optional only if the value of mode is "all".  Providing
# a value for summary when the mode is "all" will have no effect.  Not providing 
# a value for summary when the mode is "one" will cause the script to go into
# "all" mode.
#
# 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 retrieval of the information from the database is accomplished
# by using the dn_select 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" ) ;

if( -e "addevent/\.htaccess" ){
   $add_delete = "Add an Event (username and password req.)" ;
}
else{
   $add_delete = "Add an Event" ;
}
 
#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] ;
 
@summary = split( /=/, $input[4] ) ;
$summary = $summary[1] ;

#if no summary is given, then the mode must be set to "all"
if( $summary eq "" ){
   $mode = "all" ;
}

#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] ;
$times = $day_record[3] ;
@events = split( /&/, $day_record[1] ) ;
@expls = decode( $day_record[2] ) ; #explanation is url encoded
@times = split( /&/, $day_record[3] ) ;

$html_date = "<font size=3 color=\"#8E5C1B\">$monthnames[$month-1] $date, $year</font>\n" ;
$html_addlink = "<a
href=\"addevent/addevent.cgi?mode=gen&month=$month&date=$date&year=$year\">$add_delete</a>\n";
$html_returnlink = "<a href=\"$cgiurl?month=$month&year=$year\">Return to the Calendar</a>\n" ;
$html_table = "" ;

if( $events[0] ne "" ){
   $html_table .= "
      <center>
      <table border=0 cellspacing=6>
      <tr>
      <td bgcolor=\"whitesmoke\" width=50><b>Time</b></td>
      <td bgcolor=\"whitesmoke\" width=150><b>Event</b></td>
      <td bgcolor=\"whitesmoke\" width=250><b>Comments</b></td>
      <td width=75></td>

   " ;
########################################All Mode#############################################
   if( $mode eq "all" ){
#for each event on the date, 
      for( $i=0; $i < @events; $i++ ){
         $html_table .= create_tablerow( $month, $date, $year, $times[$i],
$events[$i], $expls[$i] ) ;
      }#end for loop
   } #end if($mode eq "add")
 
   elsif( $mode eq "one" ){
#######################################One Mode##########################################

      $match = 1 ;

#if the summary entered matches a summary found in the events list, we'll 
#display 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 ;
         }#end if($summary eq $events[$i]
      }#end for loop

#if the summary match is found, create the table with just one row for that event
      if( $match == 0 ){
         $i -= 1 ;
         $html_table .= create_tablerow( $month, $date, $year, $times[$i],
$events[$i], $expls[$i] ) ;
      }#end if($match)
   }#end elsif(mode=one)

#close the table
   $html_table .="
      </center>
      </table><br><br><br><br>
   " ;
}#end if( @events > 0 )
if( $html_table eq "" ){
   $html_table = "No events are scheduled for today." ;
}
######################################Default Mode########################################
#If a mode other than all or one is indicated, try to deal with it by
#sending the user an error message
if( $mode ne "all" && $mode ne "one" ){
   return_error( "The mode sent to viewday.cgi, \"$mode\", is invalid." );
}

######################################Print HTML##########################################

#open top.html and read it into memory
open( HTML, "top.html" ) ;
$i = 0 ;
while( <HTML> ){
   $top[$i] = $_ ;
   $i++ ;
}
#close top.html once it is read into memory
close( HTML ) ;
#open bot.html and read it into memory
open( HTML, "bot.html" ) ;
$i = 0 ;
while( <HTML> ){
   $bot[$i] = $_ ;
   $i++ ;
}
#close bot.html once it is read into memory
close( HTML ) ;

#now print to stdout the information from top.html, all the code we've developed
#in this script, and the information from bot.html.
   print_header( "Content-type", "text/html" ) ;

   print "
      @top
      $html_date   <br><br>   
      <i>&nbsp; &nbsp;  $html_returnlink </i>
      <p>
      $html_table
      <p>
      @bot
   " ;

sub create_tablerow(){

$month = $_[0] ;
$date = $_[1] ;
$year = $_[2] ;
$time = $_[3] ;
$event = $_[4] ;
$expl = $_[5] ;

$string = "" ;
#start a new row
         $string .= "<tr>\n" ;
#create a cell with the delete event button
         $temp = encode( $event ) ;

#create a cell with the time
         $string .="
            <td valign=top>
            $time
            </td>
         " ;
#create a cell with the event name
         $string .="
            <td valign=top>
            $event
            </td>
         " ;
#create a cell with the explanation
         $string .="
            <td valign=top>
            $expl
            </td>
         " ;

         $string .= "
            <td valign=top>
            <font size=1>
            <a href=\"addevent/addevent.cgi?mode=delete&month=$month&date=$date&year=$year&summary=$temp\">Delete Event</a>
            </font>
            </td>
         " ;

return $string ;

}#end create_tablerow

