| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package HTML::Calendar::Monthly; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | # Monthly.pm -- A very simple HTML calendar | 
| 4 |  |  |  |  |  |  | # RCS Info        : $Id: Monthly.pm,v 1.4 2009/06/25 09:18:25 jv Exp $ | 
| 5 |  |  |  |  |  |  | # Author          : Johan Vromans | 
| 6 |  |  |  |  |  |  | # Created On      : Thu Apr 30 22:13:00 2009 | 
| 7 |  |  |  |  |  |  | # Last Modified By: Johan Vromans | 
| 8 |  |  |  |  |  |  | # Last Modified On: Thu Jun 25 11:18:16 2009 | 
| 9 |  |  |  |  |  |  | # Update Count    : 4 | 
| 10 |  |  |  |  |  |  | # Status          : Unknown, Use with caution! | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 3 |  |  | 3 |  | 77474 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 107 |  | 
| 13 | 3 |  |  | 3 |  | 16 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 138 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our $VERSION = "0.03"; | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =head1 NAME | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | HTML::Calendar::Monthly - A very simple HTML calendar | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | use HTML::Calendar::Monthly; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | my $cal = HTML::Calendar::Monthly->new; # This month, this year | 
| 26 |  |  |  |  |  |  | $cal = HTML::Calendar::Monthly->new({ 'month' => $month }); # This year | 
| 27 |  |  |  |  |  |  | $cal = HTML::Calendar::Monthly->new({ 'month' => $month, | 
| 28 |  |  |  |  |  |  | 'year'  => $year}); | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | my $month = $cal->month; | 
| 31 |  |  |  |  |  |  | my $year  = $cal->year; | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # Add a link for a day. | 
| 34 |  |  |  |  |  |  | $cal->add_link( $day, $link ); | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # Get HTML representation. | 
| 37 |  |  |  |  |  |  | my $html = $cal->calendar_month; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | This is a very simple module which will make an HTML representation of | 
| 42 |  |  |  |  |  |  | a given month. You can add links to individual days. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | Yes, the inspiration for this came out of me looking at | 
| 45 |  |  |  |  |  |  | HTML::Calendar::Simple, and thinking 'Hmmm. A bit too complicated for | 
| 46 |  |  |  |  |  |  | what I want. I know, I will write a simplified version.' So I did. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =cut | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 3 |  |  | 3 |  | 2758 | use Date::Simple; | 
|  | 3 |  |  |  |  | 33040 |  | 
|  | 3 |  |  |  |  | 244 |  | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | my @days   = qw( Ma Di Wo Do Vr Za Zo ); | 
| 53 |  |  |  |  |  |  | my @months = qw( Januari Februari Maart April Mei Juni Juli | 
| 54 |  |  |  |  |  |  | Augustus September Oktober November December ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 3 |  |  | 3 |  | 29 | use constant DAYS_IN_WEEK => 7; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 259 |  | 
| 57 | 3 |  |  | 3 |  | 18 | use constant DAYS_IN_MONTH => 31; # max | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 3058 |  | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =head2 new | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | my $cal = HTML::Calendar::Monthly->new; | 
| 62 |  |  |  |  |  |  | my $cal = HTML::Calendar::Monthly->new({ 'month' => $month }); | 
| 63 |  |  |  |  |  |  | my $cal = HTML::Calendar::Monthly->new({ 'month' => $month, | 
| 64 |  |  |  |  |  |  | 'year'  => $year }); | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | This will make a new HTML::Calendar::Monthly object. | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | =cut | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | sub new { | 
| 71 | 2 |  |  | 2 | 1 | 31 | my $self = {}; | 
| 72 | 2 |  |  |  |  | 5 | bless( $self, shift ); | 
| 73 | 2 |  |  |  |  | 9 | $self->_init(@_); | 
| 74 | 2 |  |  |  |  | 10 | return $self; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub _init { | 
| 78 | 2 |  |  | 2 |  | 4 | my $self = shift; | 
| 79 |  |  |  |  |  |  | # Validate the args passed to new, if there were any. | 
| 80 | 2 |  |  |  |  | 16 | my $valid_day = Date::Simple->new; | 
| 81 | 2 |  |  |  |  | 520 | my $ref = shift; | 
| 82 | 2 | 50 | 33 |  |  | 18 | if ( defined $ref && ref $ref eq 'HASH' ) { | 
| 83 | 2 | 50 |  |  |  | 8 | my $month = exists $ref->{month} ? $ref->{month} : $valid_day->month; | 
| 84 | 2 | 50 |  |  |  | 9 | my $year  = exists $ref->{year}  ? $ref->{year}  : $valid_day->year; | 
| 85 | 2 |  |  |  |  | 8 | $valid_day = $self->_date_obj($year, $month, 1); | 
| 86 | 2 | 50 |  |  |  | 40 | $valid_day = defined $valid_day ? $valid_day : Date::Simple->new; | 
| 87 |  |  |  |  |  |  | } | 
| 88 | 2 |  |  |  |  | 33 | $self->{month} = $valid_day->month; | 
| 89 | 2 |  |  |  |  | 10 | $self->{year}  = $valid_day->year; | 
| 90 | 2 |  |  |  |  | 12 | $self->{the_month} = $self->_days_list($self->{month}, $self->{year}); | 
| 91 | 2 |  |  |  |  | 19 | return $self; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =head2 month | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my $month = $cal->month; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | This will return the numerical value of the month. | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | my $month = $cal->month_name; | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | This will return the name of the month. | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =head2 year | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | my $year = $cal->year; | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | This will return the four-digit year of the calendar | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =cut | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  | 0 | 1 | 0 | sub month      { $_[0]->{month}            } # month in numerical format | 
| 113 | 0 |  |  | 0 | 0 | 0 | sub month_name { $months[$_[0]->{month}-1 ]} # month name | 
| 114 | 0 |  |  | 0 | 1 | 0 | sub year       { $_[0]->{year}             } # year in YYYY form | 
| 115 | 4 |  |  | 4 |  | 10 | sub _the_month { @{ $_[0]->{the_month} }   } # this is the list of hashrefs. | 
|  | 4 |  |  |  |  | 32 |  | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | =head2 add_link | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | $cal->add_link( $day, $link );    # puts an href on the day | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | =cut | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | sub add_link { | 
| 124 | 2 |  |  | 2 | 1 | 11 | my ($self, $day, $link) = @_; | 
| 125 | 2 |  |  |  |  | 6 | foreach my $day_ref ( $self->_the_month ) { | 
| 126 | 24 | 100 | 100 |  |  | 103 | next unless $day_ref && $day_ref->{date}->day == $day; | 
| 127 | 2 |  |  |  |  | 4 | $day_ref->{day_link} = $link; | 
| 128 | 2 |  |  |  |  | 5 | last; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | sub _cell { | 
| 133 | 70 |  |  | 70 |  | 87 | my ( $self, $ref ) = @_; | 
| 134 | 70 | 100 |  |  |  | 120 | return " | " unless $ref; | 
| 135 |  |  |  |  |  |  |  | 
| 136 | 60 | 100 |  |  |  | 90 | if ( exists $ref->{day_link} ) { | 
| 137 |  |  |  |  |  |  | return | 
| 138 | 2 |  |  |  |  | 19 | " | " | 
| 139 |  |  |  |  |  |  | . "" . $ref->{date}->day . "" | 
| 140 |  |  |  |  |  |  | . ""; | 
| 141 |  |  |  |  |  |  | } | 
| 142 |  |  |  |  |  |  | else { | 
| 143 |  |  |  |  |  |  | return | 
| 144 | 58 |  |  |  |  | 207 | " | " | 
| 145 |  |  |  |  |  |  | . $ref->{date}->day | 
| 146 |  |  |  |  |  |  | . ""; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | =head2 calendar_month | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | my $html = $cal->calendar_month; | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | This will return an html string of the calendar month. | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =cut | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | sub calendar_month { | 
| 159 | 2 |  |  | 2 | 1 | 70 | my $self = shift; | 
| 160 | 2 |  |  |  |  | 9 | my @seq  = $self->_the_month; | 
| 161 | 14 |  |  |  |  | 32 | my $cal  = " \n" 
\n";
| 162 |  |  |  |  |  |  | . " |  \n" 
 
| 163 | 2 |  |  |  |  | 10 | . join("\n", map { " | $_" } @days ) |  
\n";| 164 |  |  |  |  |  |  | . " |  
| 165 | 2 |  |  |  |  | 27 | while ( @seq ) { |  
| 166 | 10 |  |  |  |  | 23 | my @week_row = splice( @seq, 0, DAYS_IN_WEEK ); |  
| 167 | 10 |  |  |  |  | 22 | $#week_row = DAYS_IN_WEEK - 1; |  
| 168 | 70 |  |  |  |  | 105 | $cal .= " |  \n" 
 
| 169 | 10 |  |  |  |  | 16 | . join("\n", map { "    " . $self->_cell($_) } @week_row ) |  
\n";| 170 |  |  |  |  |  |  | . "\n |  
| 171 |  |  |  |  |  |  | } |  
| 172 | 2 |  |  |  |  | 4 | $cal .= " |  | 
| 173 | 2 |  |  |  |  | 14 | return $cal; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  |  | 
| 177 | 6 |  |  | 6 |  | 22 | sub _date_obj { Date::Simple->new($_[1], $_[2], $_[3]) } | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | sub _days_list { | 
| 180 | 2 |  |  | 2 |  | 4 | my $self = shift; | 
| 181 |  |  |  |  |  |  | # Fill in a Date::Simple object for every day, Why not Date::Range object? | 
| 182 |  |  |  |  |  |  | # Because I haven't installed it yet, and not sure it would be appropriate | 
| 183 |  |  |  |  |  |  | # for the way I have set this up. | 
| 184 | 2 |  |  |  |  | 3 | my ($month, $year) = @_; | 
| 185 | 2 |  |  |  |  | 5 | my $start = $self->_date_obj($year, $month, 1); | 
| 186 | 2 |  |  |  |  | 38 | my $end   = $start + DAYS_IN_MONTH; | 
| 187 | 2 |  |  |  |  | 42 | $end   = $self->_date_obj($end->year, $end->month, 1); | 
| 188 |  |  |  |  |  |  |  | 
| 189 | 2 |  |  |  |  | 29 | my $st = $start->day_of_week; | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | # We start weeks on monday. | 
| 192 | 2 |  |  |  |  | 13 | $st--; | 
| 193 | 2 | 50 |  |  |  | 11 | $st += DAYS_IN_WEEK if $st < 0; | 
| 194 |  |  |  |  |  |  |  | 
| 195 | 2 |  |  |  |  | 7 | my @seq   = ( undef ) x $st; | 
| 196 | 2 |  |  |  |  | 20 | push @seq, { 'date' => $start++ } while ($start < $end); | 
| 197 | 2 |  |  |  |  | 971 | return \@seq; | 
| 198 |  |  |  |  |  |  | } | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | =head1 AUTHOR | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | Johan Vromans Ejvromans@squirrel.nlE | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | Parts of this module are copied from HTML::Calendar::Simple, written by Stray Toaster Ecoder@stray-toaster.co.ukE. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =cut | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | 1; |