package FES::Util::Calendar; #PerlHandler #################################################################### #=================================================================== # $Id: Calendar.pm,v 1.4 2002/05/31 13:54:19 jrobison Exp $ #------------------------------------------------------------------- # Tag: $Name: $ # Revision: $Revision: 1.4 $ # Date: $Date: 2002/05/31 13:54:19 $ # Author: $Author: jrobison $ # Description: Calendar.pm is a wrapper around the calendar # which accepts input in order to determine what # data to present in the calendar. # Inputs: act=calendar # field=name of field in main form to populate # string=year,$year,month,$month,begin,$earliest,end,$latest # Call: # header javascript to point # to the correct controller for your application. i.e. everywhere # I have /fes? you will need to put in your url. For mod_perl, obviously # this will be your dispatching module. For CGI, just the url of this # script (After you've fixed the input parsing to use CGI, not # Apache::Request. # # CGI use: # This module is obviously made to use mod_perl, probably in an # MCV environment. Conversion to regular CGI should be relatively # simple and is left as an exercise for the reader. <-- heheheheh # Dates: # I work dates in a YYYYMMDD format, no - or / # This makes date comparison easier, etc. # If you need another format, I suggest you edit sub padZero. That # would be the easiest place to add -'s or /'s. # If you want to totally re-arrange the date, you'll have to do more. ##################################################################### use strict; use Apache::Request; use Apache::Constants qw/ :common /; use Date::Calc qw/ Days_in_Month /; use HTML::CalendarMonthSimple; sub handler { my $r = Apache::Request->new(shift); ## BEGIN parse input arguments my $input = {}; my @params = $r->param; foreach (@params) { $input->{$_} = $r->param($_); } ## Currently, $input->{'string'} holds an comma delimited string, if anything. ## it needs to be a hash built from that array. ## BEGIN AWFUL HACK ################################################## my @stringy = split(/\,/,$input->{'string'}); # This is an awful hack. delete $input->{'string'}; # Just something to work, # since my in-house mod_perl for (my $i = 0;$i < scalar(@stringy);$i++) { # parses input differently my $v = $i + 1; $input->{'string'}{$stringy[$i]} = $stringy[$v]; $i++; } ## Now, $input->{'string'} will hold {month}=6, {year}=2003, etc. ## END AWFUL HACK #################################################### unless($input) { _error($r,"Maiking input failed"); return DONE; } ## END parse input arguments ## Most of this is needed to keep the calendar small. ## CalendarMonthSimple does have other ways to pass in CSS class data, ## but for now this works. my $head = < Calendar Window EOH $head .= ""; my $footer .= "\n"; my $year = $input->{'string'}{'year'} || ((localtime(time))[5] + 1900); my $current_month = (localtime(time))[4]; my $display_month; my $month; if ($input->{'string'}{'month'}) { $month = $input->{'string'}{'month'} - 1; } else { $month = $current_month; } my $days_in_month = Days_in_Month($year,$month+1); # Create the calendar object my $cal = new HTML::CalendarMonthSimple ( month => $month+1, year => $year, ); # Set up some calendar view parameters $cal->border(0); $cal->cellalignment('center'); $cal->vcellalignment('bottom'); $cal->sharpborders('1'); $cal->bgcolor('#badee6'); $cal->todaycolor('yellow'); $cal->weekendcolor('#ffbbbb'); $cal->cellheight('10'); # ugly broken out like this, but easier to follow. my $last_month = $month; my $this_month = $month + 1; my $next_month = $month + 2; my $last_year = $year - 1; my $next_year = $year + 1; my $earliest = defined $input->{'string'}{'begin'} ? $input->{'string'}{'begin'} : '00000000'; my $latest = defined $input->{'string'}{'end'} ? $input->{'string'}{'end'} : '21000101'; ## BEGIN set the header my ($y,$m) = ( $cal->year, $cal->monthname() ); $cal->header("" . "\n" . "\n" . "\n" . "
\n" . "" . " $m " . " \n" . "" . "\n" . "" . " $y " . " \n" . "" . "
\n" ); ## END set the header unless ($cal) { _error($r,"Failed to make a calendar object!: $!"); return DONE; } my $field = $input->{'field'}; for (my $i=1;$i<=$days_in_month;$i++) { my $date = $year . padZero($month+1) . padZero($i); if (($date >= $earliest) && ($date <= $latest)) { $cal->setdatehref($i,"\"javascript: opener.document.forms[0].$field.value='$date'; self.close();\""); } } print $r->send_http_header('text/html'); print $head; print $cal->as_HTML; print $footer; return DONE; } sub padZero { my $in = shift; if ($in < 10) { $in = "0" . $in; } return $in; } sub _error { my $r = shift; my $error = shift; print $r->sent_http_header('text/html'); print "Error!" . "

$error

\n"; print "\n"; } 1;