HTML-Calendar-Simple
    
    
  
  
  
view release on metacpan or search on metacpan
Revision history for Perl extension HTML::Calendar::Simple.
0.05 Thur Jul 12 09:05:00 2012
	- Ten years later, I was given a small patch to apply. So I did.
	  Thanks to Aaron Yorkovitch
0.04 Sat Mar 16 14:05:00 2002
        - Changed calendar_year interface, so can include a pin-up 
          and link on any day in any month. Deprecated html call
          in favour of name change to calendar_month
0.03  Fri Mar 15 9:00:00 2002
        - Added pin_up, calendar_year and some doc changes.
0.02  Thu Mar 14 10:10:00 2002
        - Added CGI and Test::More to prerequisites.
0.01  Wed Mar 13 11:25:09 2002
	- original version; created by h2xs 1.21 with options
		-X -n HTML::Calendar::Simple
HTML/Calendar/Simple.pm view on Meta::CPAN
package HTML::Calendar::Simple; 
$HTML::Calendar::Simple::VERSION = "0.05";
=pod
=head1 NAME
HTML::Calendar::Simple - A simple html calendar
=head1 SYNOPSIS
  use HTML::Calendar::Simple;
  my $cal = HTML::Calendar::Simple->new; # This month, this year
     $cal = HTML::Calendar::Simple->new({ 'month' => $month }); # This year
     $cal = HTML::Calendar::Simple->new({ 'month' => $month, 
                                          'year'  => $year});
HTML/Calendar/Simple.pm view on Meta::CPAN
  $cal->pin_up(a_picture_location);  
  $cal->daily_info({ 'day'      => $day,
                     'day_link' => $location,
                     $type1     => $info1,
                     $type2     => $info2,
                     'link'     => [$link, $tag],
  });
  print $cal;
  
  print $cal->calendar_month;
                  or
  print $cal->calendar_month({border => 0}); #this allows you to change the border of the table ( default is set to 1 )
                                                                          which shows the border with padding.
  my $html = HTML::Calendar::Simple->calendar_year;
     $html = HTML::Calendar::Simple->calendar_year({ 'year' => $year });
     $html = HTML::Calendar::Simple->calendar_year(
               { 'pin_up' => $where_to_find_the_picture,
                 'year'   => $year, 
                 $month   => { $day1 => $link1,
                               $day2 => $link2, }
               });
=head1 DESCRIPTION
This is a simple module which will make an HTML representation of a 
given month. You can add links to individual days, or in fact, any 
HTML/Calendar/Simple.pm view on Meta::CPAN
=head2 month
  my $month = $cal->month;
This will return the numerical value of the month.
=head2 year
  my $year = $cal->year;
This will return the four-digit year of the calendar
=cut
sub month      { $_[0]->{month}          } # month in numerical format
sub year       { $_[0]->{year}           } # year in YYYY form
sub _spacer    { return ""               } # the filler for the first few entries
sub _the_month { @{ $_[0]->{the_month} } } # this is the list of hashrefs.
sub _cgi {
  my $self = shift;
HTML/Calendar/Simple.pm view on Meta::CPAN
  
  for my $day (0..$end_day_of_month){
        $cal->daily_info({ 'day'      => $day,
                     'day_link' => $location, # puts an href on the day
                     $type1     => $info1,
                     $type2     => $info2,
                     'link'     => [$link, $tag],
        });
  }
  
  print $cal->calendar_month();
 
=cut
sub _current_day {
    my $self = shift;
    my $class = shift;
    return $self->_cgi->a({ -href => 'http://#', -class => $class }, undef)
  }
HTML/Calendar/Simple.pm view on Meta::CPAN
    or return;
  my %info = %{ $ref };
  delete $info{'day'};
  foreach my $day_ref ($self->_the_month) {
    next unless $day_ref && $day_ref->{date} == $day;
    $day_ref->{$_} = $info{$_} foreach keys %info;
    last;
  }
}
# Glerg. Make each cell in the calendar table a table of its own. And each row
# of this table will contain a little snippet of information.
sub _row_elem {
  my $self = shift;
  my $ref  = shift or return $self->_spacer;
  return $ref if $ref eq $self->_spacer;
  my $q = $self->_cgi;
  my $day = exists $ref->{day_link} 
          ? $q->a({ -href => $ref->{day_link} }, $ref->{date}->day)
          : $ref->{date}->day;
HTML/Calendar/Simple.pm view on Meta::CPAN
  my $self = shift;
  my @week = @_; my @row;
  push @row, $self->_row_elem($_) foreach @week;
  return @row;
}
=head2 pin_up
  $cal->pin_up(a_picture_with_location);
This will add a picture above the calendar month, just like the 
calendar I have hanging up in my kitchen, (It is a cat calendar, if
you are interested, as my second son loves cats. As do I!)
This could be used to have a mechanic's garage Pirelli-style pr0n
calendar, but that would be your call. Mine would be something including
a Triumph Daytona 955i. Mmmm, nice.
=cut
sub pin_up {
  my ($self, $pic) = @_;
  return unless $pic;
  $self->{picture} = $pic;
}
sub picture {
  my $self = shift;
  return exists $self->{picture} ? $self->{picture} : 0;
}
=head2 calendar_month
  my $html = $cal->calendar_month;
This will return an html string of the calendar month in question.
=head2 html
  my $html = $cal->html;
This will return an html string of the calendar month in question.
THIS CALL HAS BEEN DEPRECATED.
=cut
sub html { $_[0]->calendar_month }
sub calendar_month {
  my ($self, $alt_args) = @_;
  my $border = (defined $alt_args && ref $alt_args eq 'HASH' && exists $alt_args->{border}) ? $alt_args->{border} : 1;
  my @seq  = $self->_the_month;
  my $q    = $self->_cgi;
  my $mnth = $q->h3($months{$self->month} . " " . $self->year);
  my $cal  = $q->start_table({-border => $border}) 
           . $q->th([sort { $days{$a} <=> $days{$b} } keys %days]);
  while (@seq) {
    my @week_row = $self->_table_row(splice @seq, 0, 7);
    $cal .= $q->Tr($q->td([@week_row]));
  }
  $cal .= $q->end_table;
  $cal = $q->start_table . $q->Tr($q->td({ align => 'center' }, $mnth)) 
       . $q->Tr($q->td($cal)) . $q->end_table;
  $cal = $self->_add_pic($cal) if $self->picture;
  return $cal;
}
=head2 calendar_year
  
  my $html = HTML::Calendar::Simple->calendar_year;
     $html = HTML::Calendar::Simple->calendar_year({ 'year' => $year });
     $html = HTML::Calendar::Simple->calendar_year(
               { 'pin_up' => $where_to_find_the_picture,
                 'year'   => $year, 
                 $month   => { $day1 => $link1,
                               $day2 => $link2, }
               });
This will return the an html string for every month in the year passed,
or the current year if nothing passed in.
This key of the hashref month is *another* hashref, where the key here 
HTML/Calendar/Simple.pm view on Meta::CPAN
        $cal->daily_info({ 'day'      => $day,
                           'day_link' => $links{$day},
        });
      }
    }
    push @year, $cal;
  }
  return @year;
}
sub calendar_year {
  my ($class, $ref) = @_;
  my $year = $ref->{year};
  my $when = defined $year 
           ? Date::Simple->new($year, 1, 1)
           : Date::Simple->new;
     $when = defined $when ? $when : Date::Simple->new;
  $year = $when->year;
  my @year = $class->_generate_months($year, $ref);
  my $year_string;
  my $q = CGI->new;
  while (@year) {
    my @qrtr = map { $_->calendar_month } splice @year, 0, 3;
    s/$year//g for @qrtr;
    $year_string .= $q->start_table . $q->Tr($q->td({valign => 'top'}, [@qrtr])) 
                 .  $q->end_table   . $q->br;
  }
  my $pic = defined $ref->{'pin_up'} ? $ref->{'pin_up'} : "";
  $pic = $q->Tr($q->td({ align => 'center' }, $q->img({ src  => $pic }))) if $pic; 
  $year_string = $q->start_table . $pic . $q->th($year)
               . $q->Tr($q->td($year_string)) 
               . $q->end_table;
  return $year_string;
HTML/Calendar/Simple.pm view on Meta::CPAN
=head1 BUGS
None known
=head2 TODO
Oh....lots of things.
  o Rip out the CGI stuff and put all the HTML in a template, so the user
    can decide on the format of the calendar themselves.
  o Allow for the setting of borders etc like HTML::CalendarMonthSimple.
  o Format the output better if there is info in a daily cell.
  o Perhaps overload '.' so you could add two calendars. Not sure.
  o Check the links passed in are of format http://www.stray-toaster.co.uk
    or something.
  o Get rid of the days and months hashes and replace with something better.
  o And if all that happens, it may as well be HTML::CalendarMonthSimple!!
  o Make HTML::Calendar::Day, HTML::Calendar::Month and HTML::Calendar::Year
=head1 SHOWING YOUR APPRECIATION
There was a thread on london.pm mailing list about working in a vacumn
- that it was a bit depressing to keep writing modules but never get
--- #YAML:1.0
name:               HTML-Calendar-Simple
version:            0.05
abstract:           A simple html calendar
author:
    - Stray Toaster <coder@straytoaster.co.uk>
license:            unknown
distribution_type:  module
configure_requires:
    ExtUtils::MakeMaker:  0
build_requires:
    ExtUtils::MakeMaker:  0
requires:
    CGI:           2.752
});
$string = $cal->html;
unlike $string, qr/HREFM/, "No separate link added";
$cal->daily_info({ 'day'  => 14,
                   'link' => ['http://www.stray-toaster.co.uk', 'My site'],
});
$string = $cal->html;
like $string, qr/<a href=\"http:\/\/www.stray-toaster.co.uk\">My site<\/a>/, 
  "HTML string now contains the link";
is $string, $cal->calendar_month, 
  "call calendar_month is actually the call html. What a country";
$string = $cal->calendar_month({nonsense => 'silliness'});
like $string, qr/border="1"/, 'Nonsense border args leave default size';
$string = $cal->calendar_month;
like $string, qr/border="1"/, 'No border args leave default size';
$string = $cal->calendar_month({border => 0});
like $string, qr/border="0"/, 'Border args give border size';
$string = "$cal";
unlike $string, qr/<a href=\"http:\/\/www.stray-toaster.co.uk\">My site<\/a>/, 
  "stringified doesn't contain the link";
unlike $string, qr/<a href=\"a link\">7<\/a>/, "stringified 7th is NOT an href";
unlike $string, qr/TESTDATA/, "stringified doesn't contain TESTDATA";
is $cal->picture, 0, "There is no pin-up";
$cal->pin_up("This would be a picture");
is $cal->picture, "This would be a picture", "There is now a pin-up";
my @months = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
my $year_cal = HTML::Calendar::Simple->calendar_year; # current year
my $year = $today->year;
like $year_cal, qr/$year/, "It is the correct year $year";
foreach my $month (@months) {
  like $year_cal, qr/$month/, " - year calendar contains $month";
}
$year--;
$year_cal = HTML::Calendar::Simple->calendar_year({ 'year' => $year });
like $year_cal, qr/$year/, "It is the correct year $year";
foreach my $month (@months) {
  like $year_cal, qr/$month/, " - year calendar contains $month";
}
( run in 0.510 second using v1.01-cache-2.11-cpan-5dc5da66d9d )