Class-DBI-Plugin-Calendar

 view release on metacpan or  search on metacpan

lib/Class/DBI/Plugin/Calendar.pm  view on Meta::CPAN

sub import {
	my ($self, $date_field) = @_;

		# we require that they pass a $date_field
	croak __PACKAGE__." requires a date field to be passed to import() (aka use())" unless $date_field;
	
	my $caller = caller();
	no strict 'refs';

		# add some SQL to the calling class, as requested
	$caller->set_sql(calendar => <<"");
		SELECT *
		FROM __TABLE__
		%s
		ORDER BY `$date_field`


		# add the calendar method to the calling class
	*{"$caller\::calendar"} = sub {
		my($class,$month,$year,$mondays) = @_;

			# generate default values if necessary
		unless($month and $year) {
			my @lt = localtime;
			$month ||= sprintf '%02d', $lt[4] + 1;
			$year  ||= sprintf '%02d', $lt[5] + 1900;
		}

			# mysql required (for now?)
		my $where = qq[ WHERE MONTH(`$date_field`) = '$month' AND YEAR(`$date_field`) = '$year' ];

			# get the objects which are within the chosen month
		my @objects = $class->sth_to_objects($class->sql_calendar($where));
			
			# get the calendar layout for this month
			# map the dates to day objects:
			#
		my @weeks = Calendar::Simple::calendar($month,$year,$mondays);
		for my $week (@weeks) {
			for my $day (@$week) {
				$day ||= 0;
				my @events = ();
				while(@objects and $objects[0]->$date_field->mday == $day) {
					push @events, shift @objects;
				}
				my $date = $day
						? Time::Piece->strptime(join('-',$year,sprintf('%02d', $month),sprintf('%02d', $day)),'%Y-%m-%d')
						: undef;

lib/Class/DBI/Plugin/Calendar.pm  view on Meta::CPAN

=head1 NAME

Class::DBI::Plugin::Calendar - Simple Calendar Support for Class::DBI

=head1 SYNOPSIS

  package DB;
  use base 'Class::DBI';
  use Class::DBI::Plugin::Calendar qw(date_fieldname);

  # the same as Calendar::Simple::calendar
  my @curr      = DB->calendar;             # get current month
  my @this_sept = DB->calendar(9);          # get 9th month of current year
  my @sept_2002 = DB->calendar(9, 2002);    # get 9th month of 2002
  my @monday    = DB->calendar(9, 2002, 1); # week starts with Monday

=head1 DESCRIPTION

Please note that this module only works with mysql at this point, as 
far as I know. Retrieve the objects in useful calendar-like data 
structures, similar to Calendar::Simple.

=head2 my @weeks = calendar([$month,$year,$monday])

@weeks holds arefs of 7 days each (there are dummy placeholders where needed), 
which are represented by Class::DBI::Plugin::Calendar::Day objects. Please
refer to the Class::DBI::Plugin::Calendar::Day perldoc for instructions.

=head1 SEE ALSO

Class::DBI, Calendar::Simple, Class::DBI::Plugin::Calendar::Day

=head1 AUTHOR

lib/Class/DBI/Plugin/Calendar/Day.pm  view on Meta::CPAN

=head1 NAME

Class::DBI::Plugin::Calendar::Day - Calendar Day Support for Class::DBI

=head1 SYNOPSIS

  package DB;
  use base 'Class::DBI';
  use Class::DBI::Plugin::Calendar qw(date_fieldname);

  my @weeks = DB->calendar; # current month, based on Calendar::Simple
  for my $week (@weeks) {
    for my $day (@$week) { # always 7 days, some may be placeholders
      if($day->ok) {
        printf '%03d', $day->date->mday;
      } else { # just a placeholder
        print "   ";
      }
      print "\n";
    }
  }

t/Class-DBI-Plugin-Calendar.t  view on Meta::CPAN

	My::Film->create({ title => $title, date => $year });
}

{
	my @films = My::Film->retrieve_all;
	is @films, 14, "Got 14 films";
}

# no args
{
	my @weeks = My::Film->calendar;
	my $ok = 1;

	my($one) = grep { $_->ok && $_->date->mday == 1 } grep $_->ok, @{$weeks[0]};
	my(@one) = $one->agenda;
	$ok = 0 unless @one == 2;
	$ok = 0 unless $one[0]->title eq 'Rocky';
	$ok = 0 unless $one[1]->title eq 'Nashville';

	my($two) = grep { $_->ok && $_->date->mday == 2 } (@{$weeks[0]},@{$weeks[1]});
	my(@two) = $two->agenda;
	$ok = 0 unless @two == 1 and $two[0]->title eq 'Ape';

	my($thr) = grep { $_->ok && $_->date->mday == 3 } (@{$weeks[0]},@{$weeks[1]});
	my(@thr) = $thr->agenda;
	$ok = 0 unless @thr == 1 and $thr[0]->title eq 'JFK';

	ok($ok,"calendar()");
}

# month
{
	my @weeks = My::Film->calendar($om);
	my $ok = 1;

	my($one) = grep { $_->date->mday == 1 } grep $_->ok, @{$weeks[0]};
	my(@one) = $one->agenda;
	$ok = 0 unless @one == 3;
	$ok = 0 unless $one[0]->title eq 'Jaws';
	$ok = 0 unless $one[1]->title eq 'Manhattan';
	$ok = 0 unless $one[2]->title eq 'Network';

	ok($ok,'calendar($m)');
}

# year
my $day1 = 0;
{
	my @weeks = My::Film->calendar($om,$oy);
	my $ok = 1;

	for(my $count = 0; $count < @{$weeks[0]}; $count++) {
		next unless $weeks[0]->[$count]->ok && $weeks[0]->[$count]->date->mday == 1;
		$day1 = $count;
		last;
	}

	my($one) = grep { $_->date->mday == 1 } grep $_->ok, @{$weeks[0]};
	my(@one) = $one->agenda;
	$ok = 0 unless @one == 6;
	$ok = 0 unless $one[0]->title eq 'Red';
	$ok = 0 unless $one[1]->title eq 'White';
	$ok = 0 unless $one[2]->title eq 'Blue';
	$ok = 0 unless $one[3]->title eq 'Dekalog';
	$ok = 0 unless $one[4]->title eq 'Hospital';
	$ok = 0 unless $one[5]->title eq 'Heaven';

	ok($ok,'calendar($m,$y)');
}

# mondays
$day1 ||= 7; # re-order if the 1st is a Sunday
{
	my @weeks = My::Film->calendar($om,$oy,1);
	
	my $m1 = 0;
	for(my $count = 0; $count < @{$weeks[0]}; $count++) {
		next unless $weeks[0]->[$count]->ok && $weeks[0]->[$count]->date->mday == 1;
		$m1 = $count + 1;
		last;
	}

	ok($m1 == $day1,'calendar($m,$y,1)');
}


__END__

{
	my $count1976 = My::Film->count_search('date' => '1976');
	is $count1976, 4, "Got 4 1976 films";
}



( run in 0.813 second using v1.01-cache-2.11-cpan-5dc5da66d9d )