Re: [htmltmpl] event calendar code
Brought to you by:
samtregar
From: P K. <pk...@ei...> - 2004-02-17 14:53:36
|
Kapoor, Nishikant wrote: >Sorry if this post is off-topic but I have a feeling people in this list would have definitely come across it. > >I am looking to generate an event calendar using H::T. Can anyone help me with some pointers? code snippets? > >I did search the archives but could not find much info. > >Thanks, >Nishi > > > here is a fragment of some code I wrote a long time ago. It has some dependencies unique to my situation, and is crufty and inelegant, but it should give you a place to start. I was using SQLite to store the events, grabbing the data from the database, and passing the recordset to different routines to create the monthly, yearly, daily or weekly views. Here is the monthly view. The getappts() gets the data from the database, and getappt() converts it to viewable code (I know, I know, I already said it is crufty and inelegant, but it works) -- sub month { my @cal = (); my $start = my $firstOfMonth = timelocal(0, 0, 0, 1, $mon, $year); my $end = my $lastOfMonth = timelocal(59, 59, 23, $daysInMonth, $mon, $year); my $appts_ref = &getappts($start, $end); my @firstOfMonth = localtime($firstOfMonth); my $wdayOfFirstOfMonth = $firstOfMonth[6]; my $date = 1; # first week my %week; #my $thisweek = $week; my $thisweek = int(($firstOfMonth[7] + 1) / 7) + 1; $week{'week'} = $thisweek; $week{'weekcd'} = timelocal(0, 0, 0, $date, $mon, $year); my @week; for (1..7) { my %day; my @appointments; if ($_ <= $wdayOfFirstOfMonth) { $day{'day'} = ''; $day{'daycd'} = ''; $day{'appointments'} = \@appointments; } else { $day{'day'} = ($date == $today) ? "<div id='today'>" . $date . '</div>' : $date; $day{'daycd'} = timelocal(0, 0, 0, $date, $mon, $year); $day{'appointments'} = &getappt($mon, $date, $hour, 'month', $appts_ref); $date++; } push(@week, \%day); } $week{'days'} = \@week; push(@cal, \%week); # end first week # rest of the weeks while ($date <= $daysInMonth) { my %week; $thisweek++; $week{'week'} = $thisweek; $week{'weekcd'} = timelocal(0, 0, 0, $date, $mon, $year); my @week; for (1..7) { my %day; my @appointments; if ($date <= $daysInMonth) { $day{'day'} = ($date == $today) ? "<div id='today'>" . $date . '</div>' : $date; $day{'daycd'} = timelocal(0, 0, 0, $date, $mon, $year); $day{'appointments'} = &getappt($mon, $date, $hour, 'month', $appts_ref); } else { $day{'day'} = ''; $day{'daycd'} = ''; $day{'appointments'} = \@appointments; } $date++; push(@week, \%day); } $week{'days'} = \@week; push(@cal, \%week); } return \@cal; } sub getappts { my ($start, $end) = @_; my $dbh = &connecttoDB(); my $sql = "SELECT event_id, start, end, title FROM events WHERE start BETWEEN $start AND $end ORDER BY start"; my $sth = $dbh->prepare(qq{$sql}); $sth->execute; return my $tbl_ary_ref = $sth->fetchall_arrayref({}); } sub getappt { my ($m, $d, $h, $doh, $ary_ref) = @_; my @inres = @$ary_ref; my @outres; foreach (@inres) { my %row = %$_; my ($ssec, $smin, $shour, $smday, $smon, $syear, $swday, $syday, $sisdst) = localtime($row{'start'}); if ($doh eq 'day') { if ($shour == $h) { $row{'start'} = &timetobase12($shour, $smin); my ($esec, $emin, $ehour, $emday, $emon, $eyear, $ewday, $eyday, $eisdst) = localtime($row{'end'}); $row{'end'} = &timetobase12($ehour, $emin); push(@outres, \%row) ; } } elsif ($doh eq 'week') { if ($smday == $d and $shour == $h) { $row{'start'} = &timetobase12($shour, $smin); my ($esec, $emin, $ehour, $emday, $emon, $eyear, $ewday, $eyday, $eisdst) = localtime($row{'end'}); $row{'end'} = &timetobase12($ehour, $emin); push(@outres, \%row) ; } } elsif ($doh eq 'month') { if ($smday == $d) { $row{'start'} = &timetobase12($shour, $smin); my ($esec, $emin, $ehour, $emday, $emon, $eyear, $ewday, $eyday, $eisdst) = localtime($row{'end'}); $row{'end'} = &timetobase12($ehour, $emin); push(@outres, \%row) ; } } elsif ($doh eq 'year') { if ($smon == $m and $smday == $d) { $row{'start'} = &timetobase12($shour, $smin); my ($esec, $emin, $ehour, $emday, $emon, $eyear, $ewday, $eyday, $eisdst) = localtime($row{'end'}); $row{'end'} = &timetobase12($ehour, $emin); push(@outres, \%row) ; } } } return \@outres; } $template->param( prev => $prev, next => $next, currentlink => $currentlink, currentdesc => $currentdesc, view => $view, monthwidget => &monthwidget, daywidget => &daywidget, yearwidget => &yearwidget, cal => &month(), selecteddayvalue => $mday, selecteddaydisplay => $mday, selectedmonthvalue => $mon, selectedmonthdisplay => $months[$mon], selectedyearvalue => $year, selectedyeardisplay => $year ); ========= <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Month</title> <link rel="stylesheet" TYPE="text/css" href="../perlpim.css"> </head> <body> <tmpl_include navbar.html> <table border="1" width="600" cellpadding="2" cellspacing="1" bgcolor="black"> <tr bgcolor="silver"><td colspan="8"><tmpl_var currentdesc></td></tr> <tr bgcolor="#ccffff"> <td width="40" bgcolor="#cccc99">Week</td> <td width="80" bgcolor="silver">Sun</td> <td width="80">Mon</td> <td width="80">Tue</td> <td width="80">Wed</td> <td width="80">Thu</td> <td width="80">Fri</td> <td width="80" bgcolor="silver">Sat</td> </tr> <!-- start month loop --> <tmpl_loop cal> <tr bgcolor="white" valign="top"> <td bgcolor="#cccc99" height="70"> <a href="index.cgi?view=week&cd=<tmpl_var weekcd>"><tmpl_var week></a></td> <tmpl_loop days> <td <tmpl_if __first__>bgcolor="silver"</tmpl_if><tmpl_if __last__>bgcolor="silver"</tmpl_if><tmpl_unless day>bgcolor="silver"</tmpl_unless>> <tmpl_if day> <a href="index.cgi?view=day&cd=<tmpl_var daycd>" title="Day view"><tmpl_var day></a><br> </tmpl_if> <tmpl_loop appointments> <tmpl_if start> <tmpl_var start> - <tmpl_var end>: <a href="index.cgi?event_id=<tmpl_var event_id>" title="Details on this event"> <tmpl_var title></a><br> </tmpl_if> </tmpl_loop> </td> </tmpl_loop> </tr> </tmpl_loop> <!-- end month loop --> </table> </body> </html> |