#!/usr/bin/perl

use cgi ;
use dnfunc ;
use calendar ;

#############################################################################
#Index.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 build a calendar in the form of an html table,
# including in the cell for each day any events found for that date in the
# calendar database.
#
# Which month and year should be loaded is communicated to the script via
# CGI communication.  The following is a list of the name and value pairs that
# Index.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 (|)
#
# month=an integer greater than or equal to 1 and less than or equal to 12
# year=a positive integer
#
# 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 generation of the calendar is accomplished by first finding the number
# of days in the month and the day of the week on which the month starts
# by calling functions provided by calendar.pm.  Then, an html table is 
# constructed with 7 columns, and as many rows as necessary to fill out 
# the calendar.  As each cell is created, the database is queried for events
# happening on the day that cell corresponds with - this is done by using the
# dn_select function of dnfunc.pm.  
#
# Originally authored by Jay Eckles <jay@ecklesweb.com>.  
# Contributors:
#     Many people from comp.infosystems.www.authoring.cgi - debugging tips, 
#        many feature ideas, help in pointing out errors and poor code, etc.
#     Ken MacCuish <ken@pwhinc.com> - modified code to highlight the cell of the
#        table containing the current day with a color defined by the variable
#        $cell_today_color and any day with an event with a color defined by
#        the variable $cell_active_color
################################################################################

#when assiging values to these next two variables, make sure you do it right:
#the colors must be six digit hex rgb color codes, like the color codes that 
#are used as the values of attributes in the HTML BODY tag.  According to 
#HTML specs, you must precede these codes with a hash, which means that 
#according to the specs you must put the whole thing inside quotation marks.
#that means that you have to be careful to escape the correct characters here.
#here's an example:
#   $cell_today_color = "\"\#cccccc\"" ;
#color to highlight today's cell on the calendar
$cell_today_color = "" ;
#color to highlight any cell with an event scheduled
$cell_active_color = "" ;

#filename of the calendar database
$filename = "calendar.db" ;

#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 by using a cgi.pm function
@input = retrieve_input() ;
%input = %cgi::in ;

$month = $input{"month"} ;
$year = $input{"year"} ;

#if no month or year is given, then use the current month or year according to the 
#system clock.
@timelist = localtime( time() ) ;
if( $month eq "" ) {
   $month = $timelist[4] + 1 ;
}
if( $year eq "" ) {
   $year = $timelist[5]+1900 ;
}
$dayofmonth = $timelist[3] ;
$curmonth = $timelist[4]+1 ;
$curyear = $timelist[5]+1900;

#determine the monthname by using $month as an index to the monthnames list.
$monthname = $monthnames[$month-1] ;
$monthyear = $monthname.$year ;

#determine how many days are in the given month and on what day of the week 
#that month starts.  These functions are provided by calendar.pm
$days_in_month = days_in_month( $month, $year ) ;
$monthstart = month_start( $month, $year ) ;

#if the calendar database exists
if( -e $filename ){
#and if it can be successfully opened
   if( dn_open( $filename ) eq "success" ){
#load the calendar database into memory
      %calendarbase = %dnfunc::database ;
   }
#if the calendar database was not successfully opened, create an empty one.
   else{
      %calendarbase ;
   }
}
#if the calandar database did not exists, create an empty database in memory
else{
   %calendarbase ;
}

################Start creating the table that will be the calendar.####################

#the html code for the table will be stored as a string in the scalar variable $table
$table = "<table border=1 cellpadding=2 cellspacing=0>\n" ;

# print top row of calendar which holds days of week
for( $i=0; $i < 7; $i++ ){
   $table = $table."<td width=75 valign=\"top\">$day[$i]</td>\n" ;
}

# next row
$table = $table."<tr valign=\"top\">\n" ;

# skip the appropriate number of days as indicated by the calendar::monthstart function
for( $i=0; $i < $monthstart; $i++ ){
   $table = $table."<td width=75 height=75 valign=\"top\"></td>\n" ;
}

# in day_of_week, Sunday is 0, Saturday is 6.
$day_of_week = $monthstart ;

#now, we have got the table to the point where we know what day of the week we're on and
#we know that the next cell is the 1st of the month.  We can now start creating the cells
#for the days which means we begin to look for events in the database.

#$current_day is the day of the month we are currently working on
$current_day = 0 ;
# this loop creates the individual day cells on the calendar.
for( $i=1; $i <= $days_in_month ; $i++ ){
   $current_day++ ;
# find out if it's a new week and we need to start a new row
   if( !( $day_of_week % 7 ) ){
      $table = $table."<tr valign=\"top\">\n" ;
   }

############################Begin Code Modified by Ken MacCuish############################
# build the key to the calendar database for that day (in form of
# dateMonthyear ex: 30April1998
   $key = $i.$monthnames[$month-1].$year ;
#retrieve the information for that day from the calendar database
   @daysevents = split( /&/, $calendarbase{ $key."Events" } ) ;
   @times = split( /&/, $calendarbase{ $key."Times" } ) ;
# hilite the cell if it is today or there is an activity today
   if ($i eq $dayofmonth && $curmonth eq $month && $year eq $curyear && $cell_today_color ne ""){
      $cellcolor="bgcolor=$cell_today_color"
   }
   elsif( @daysevents > 0 && $cell_active_color ne ""){
      $cellcolor="bgcolor=$cell_active_color" ;
   }
   else{
      $cellcolor="";
   }
# create the cell, include the day of the month - link day to viewday.cgi   
   $table = $table."<td width=75 height=75 $cellcolor valign=\"top\">
   <a href=\"viewday.cgi?mode=all&month=$month&date=$current_day&year=$year&calendar=
   $calendar\">$current_day</a>
   <br>" ;
# if there's anything on this day, put it in the day's cell.
   if( @daysevents > 0 ){
      for( $j=0; $j < @daysevents; $j++ ){     
##############################End Code by Ken MacCuish#####################################

####experimental-urlencode summary for transmission via get - encode from
####cgi.pm
$enc_summary = encode( $daysevents[$j] ) ;
#link event to viewday.cgi
         $table .= "<a href=\"viewday.cgi?mode=one&month=$month&date=$current_day&year=$year&summary=$enc_summary\">"; 
# if there is a time, print the time after the summary of the event.  If 
# there is no time given, don't print anything except the summary.
         if( $times[$j] =~ /^\D/ ){
            $table = $table."<font size=-2>$daysevents[$j]</font></a><br>\n" ; 
         }
         else{
            $table = $table."<font size=-2>$daysevents[$j], $times[$j]</font></a><br>\n" ; 
         }
      }
   }
# close the cell
   $table = $table."</td>\n" ;
   $day_of_week = $day_of_week + 1 ;
} #end of for loop: for( $i=1; $i <= $days_in_month ; $i++ )
#here, at the termination of the loop, we have created a cell for each day of the month.
#so we finish the table:
$table = $table."</table>\n" ;

#create the form that will be at the top of the calendar to provide the "jump to month"
#functionality

$form = "
<form action=\"index.cgi\" method=\"get\">
<b>Jump to month:</b>
Month: <select name=month>
<option value=$month>
<option value=1>January
<option value=2>February
<option value=3>March
<option value=4>April
<option value=5>May
<option value=6>June
<option value=7>July
<option value=8>August
<option value=9>September
<option value=10>October
<option value=11>November
<option value=12>December
</select>

Year: <input type=text value=$year name=year size=4>

<input type=submit value=\"Do it\.\">
</form> " ;

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

#finally print the html page back to the client
#print_header function provided by cgi.pm
print_header( "Content-type", "text/html" ) ;

# if the calendar shown is January, then the previous month is December of
# the previous year (year-1).  Otherwise it is just the previous month of
# the same year (month-1)
if( $month == 1 ){
   $prevmonth = 12 ;
   $prevyear = $year-1 ;
}
else{
   $prevmonth = $month-1 ;
   $prevyear = $year ;
}
# similarly, if the calendar shown is December, the next month is January
# of the next year.  Otherwise it is month+1
if( $month == 12 ){
   $nextmonth = 1 ;
   $nextyear = $year + 1 ;
} 
else{
   $nextmonth = $month+1 ;
   $nextyear = $year ;
}

#$calendarinfo holds the html code for the calendar, including the table and
#the form we created earlier
$calendarinfo = "
<center>
<table border=0>
<tr>
<td width=180 align=\"left\"><font size=-1><a 
href=\"index\.cgi\?month=$prevmonth&year=$prevyear\">$monthnames[$prevmonth-1]</a>
</font>
</td>
<td align=\"center\" width=180>
<font size=3 color=\"#8E5C1B\"><b> $monthname, $year </b></font>
</td>
<td width=180 align=\"right\">
<font size=-1><a 
href=\"index\.cgi\?month=$nextmonth&year=$nextyear\">$monthnames[$nextmonth-1]</a>
</font>
</td>
<tr>
<td colspan=3 align=\"center\" valign=\"middle\">
$form
</td>
</table>
</center>
<center>
$table
</center>


" ; 
#end $calendarinfo =...

#that html code we just developed goes inbetween the code found in two files,
#top.html and bot.html.  User's can customize the look of the calendar by editing
#top.html and bot.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 <<END
@top
$calendarinfo
@bot
END

