#!/usr/local/bin/perl #################################################################### # Script: | EventCalender # # Version: | 1.0 # # By: | Dave Edis (i2 Services, Inc. / CGI World) # # Contact: | dave@cgi-world.com # # WWWeb: | http://www.cgi-world.com # # Copyright: | Dave Edis (CGI World) # # Released: | June 1st, 1998 # # Updated: | December 10th, 1998 # #################################################################### # By using this software, you have agreed to the license # # agreement packaged with this program. # # # #################################################################### # Done: # # (Do not edit below this point, Violation of License Agreement) ################################################################ # Find current directory path if ($0=~m#^(.*)\\#){ $cgidir = "$1"; } # cgidir on win/dos C:\dir\ elsif ($0=~m#^(.*)/# ){ $cgidir = "$1"; } # cgidir on unix /usr/bin/ else {`pwd` =~ /(.*)/; $cgidir = "$1"; } # else use unix `pwd` for cgidir $cgiurl = $ENV{'SCRIPT_NAME'}; # web path of script $|++; # Unbuffer output $datadir = "$cgidir"; $filelock = "$datadir/filelock"; @month = qw(January February March April May June July August September October November December); @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); @weekday = qw(Saturday Sunday Monday Tuesday Wednesday Thursday Friday); @wday = qw(Sat Sun Mon Tue Wed Thu Fri); $one_day = 60*60*24; # One Day (86400 sec) $one_month = $one_day * 28; # One Month (2419200 sec) $one_year = $one_day * 356; # One Year (30758400 sec) ($ctime{'d'},$ctime{'m'},$ctime{'y'}) = (localtime)[3..5]; # Current Day/month/year require "$cgidir/edis-lib.pl"; # Load EDIS CGI Library $SIG{__DIE__} = sub { # die signal handler print "Content-type: text/plain\n\n"; print "@_"; exit; }; # ------------------------------------------------------------------------ # Main : Test conditions and give commands # ------------------------------------------------------------------------ %in = &ReadForm; # Read CGI Form input if ($in{'m'} && $in{'y'} && !$in{'d'}) { $in{'d'} = 1; } &Template("$cgidir/_cal.html"); # Load Templates &Cal; # Create Calendar &GetDate; &List; # List Events print &Template("$cgidir/_cal.html",'html'); # Print Template exit; # ------------------------------------------------------------------------ # Get Date : Set right date for $d,$m,$y (day,month,year) # ------------------------------------------------------------------------ sub GetDate { ($d,$m,$y) = ($in{'d'},$in{'m'},$in{'y'}); # Set Day,Month,Year # use current date values if date isn't specified if (!$y) { $y = $ctime{'y'} + 1900; } # year if (!$m) { $m = $ctime{'m'} +1; } # month if (!$d) { $d = $ctime{'d'}; } # day # Check date input for invalid values if ($d < 1 || $d > 31 || $d != int $d) { die "Cal : Invalid day value '$d'\n"; } if ($m < 1 || $m > 12 || $m != int $m) { die "Cal : Invalid month value '$m'\n"; } if ($y < 1980 || $y > 2020 || $y != int $y) { die "Cal : Invalid year value '$y'\n"; } foreach ($m,$d) { $_ = sprintf("%02d",$_); } } # ------------------------------------------------------------------------ # List : List Events for the current day/month/year # ------------------------------------------------------------------------ sub List { %dat = &LoadHash("$datadir/cal-$y-$m.dat.cgi",$filelock); # Load Event data file foreach $key (sort MySort keys %dat) { ($day2,$num,$name) = split(/\./,$key); if ($day2 == $d && $name eq 'name') { $name = $dat{"$d.$num.name"}; $desc = $dat{"$d.$num.desc"}; $list .= &Cell('row'); } } unless ($list) { $list = &Cell('noevents'); } } sub MySort { ($day_a,$num_a,$name_a) = split(/\./,$a); ($day_b,$num_b,$name_b) = split(/\./,$b); $dat{"$day_a.$num_a.sort"} <=> $dat{"$day_b.$num_b.sort"}|| $dat{"$day_a.$num_a.name"} cmp $dat{"$day_b.$num_b.name"}; } # ------------------------------------------------------------------------ # Cal : Create Calendar for user selected month and year # ------------------------------------------------------------------------ sub Cal { ($d,$m,$y) = ($in{'d'},$in{'m'},$in{'y'}); # Set Day,Month,Year # use current date values if date isn't specified if (!$y) { $y = $ctime{'y'} } else { $y -= 1900 } # year if (!$m) { $m = $ctime{'m'} } else { $m -= 1 } # month if (!$d) { $d = $ctime{'d'} } # day # Check date input for invalid values if ($d < 1 || $d > 31 || $d != int $d) { die "Cal : Invalid day value '$d'\n"; } if ($m < 0 || $m > 11 || $m != int $m) { die "Cal : Invalid month value '$m'\n"; } if ($y < 80 || $y > 120 || $y != int $y) { die "Cal : Invalid year value '$y'\n"; } # we need to get time in seconds of the date requested by the user. # just like the 'time' function gives us, so we'll take a copy of # the current time in seconds in $dtime and keep adding or subtracting # one year/month untill we get the year/month we want # $dtime is the current time in seconds since 1970, we'll modify this # to be the 'time' of our requested date so we can feed it to localtime $dtime = time; # If requested year is less than $dtime year minus one year from $dtime while ((localtime($dtime))[5]>$y) { $dtime -= $one_year; } # If requested month is less than $dtime month minus one month from $dtime while ((localtime($dtime))[4]>$m) { $dtime -= $one_month; } # If requested year is more than $dtime year add one year from $dtime while ((localtime($dtime))[5]<$y) { $dtime += $one_year; } # If requested month is more than $dtime month add one month from $dtime while ((localtime($dtime))[4]<$m) { $dtime += $one_month; } # If requested month is less than $dtime month minus one month from $dtime while ((localtime($dtime))[4]>$m) { $dtime -= $one_month; } ($m,$y,$wd) = (localtime($dtime))[4..6]; $day = sprintf("%02d",$d); # two digit day of month $dy = int $d; # one/two digit day of month $mon = @mon[$m]; # Abbr month name $MON = uc @mon[$m]; # Abbr month name uppercase $month = @month[$m]; # Full month name $MONTH = uc @month[$m]; # Full month name uppercase $year = ($y+1900); # four digit year $yr = sprintf("%02d",$y>=100?$y-100:$y); # two digit year $y = $year; # Year var used in template $m = sprintf("%02d",$m+1); # Month var used in template ${"m$m"."_selected"} = "selected"; # Select Month in pulldown menu ${"y$y"."_selected"} = "selected"; # Select Year in pulldown menu # Check which days of the month have events so we can make those days links %dat = &LoadHash("$datadir/cal-$y-$m.dat.cgi",$filelock); # Load Event data file foreach $key (keys %dat) { # for record in month datafile my($day,$num,$name) = split(/\./,$key); # read record key if ($dat{"$day.$num.name"}) { # if day has any events $day = 0+$day; $events{$day}++; # give it a link in calendar } } $ttime = $dtime; # temp time var to count days in month while ((localtime($ttime))[3]>1) { # While day of month is higher than one $ttime -= $one_day; # minus one day from temp time var $ttime } $wpos = (localtime($ttime))[6]; # weekday position, 1=Sun, 7=Sat $d = 0; # day of the month counter ### Start of the month, print out blank table cells till first day $cal .= &Cell("row_start"); # start table row if ($wpos > 0) { # if first day isn't Sun for (1..$wpos) { $cal .= &Cell("day_blank"); } # add blank day cell } ### Month days, count out the days in the month with $ttime var while ((localtime($ttime))[4] == (localtime($dtime))[4] ) { # while we have days left in the month $ttime += $one_day; # Add one day to temp time var $wpos++; # Add one to weekday position $d++; # Add one to day counter if ($d == $in{'d'} || (!$in{'d'} && $d == $ctime{'d'})) { # If day is selected day $wd = (localtime($ttime))[6]; # Calculate current weekday for template $wday = $wday[$wd]; # Abbr weekday name $WDAY = uc $wday[$wd]; # Abbr weekday name uppercase $weekday = $weekday[$wd]; # Full weekday name $WEEKDAY = uc $weekday[$wd]; # Full weekday name uppercase $cal .= &Cell("day_today"); # add blue day cell for month day } elsif ($events{$d}) { $cal .= &Cell("day"); } # If Events on day, Add linked day else { $cal .= &Cell("day_text"); } # else add a plain text day w/ no link if ($wpos==7) { # after seven days, next row $cal .= &Cell("row_end"); # end table row undef $wpos; # reset weekday position if ((localtime($ttime))[4] == (localtime($dtime))[4]) { # if more days $cal .= &Cell("row_start"); # start another table row } } } ### End of month, print out blank table cells from last day to the end of row if ($wpos && $wpos < 7) { # if we don't have 7 days yet while ($wpos < 7) { # while we don't have 7 $cal .= &Cell("day_blank"); # add blank day cell $wpos++; # add one to weekday position } $cal .= &Cell("row_end"); # end table row } } # ------------------------------------------------------------------------ # Programming by Dave Edis