X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FCalendar.pm;h=a9d39dcaaf67c55df48ac6fb21c896b6bf1b914e;hb=937480abe03863215c8134e67f56e1b968aaf229;hp=2f1f7205487f3418ee68eee92b9f371b31d6ec45;hpb=ec73dc92f95b3adaaa1395d7b156131541e1e4d4;p=koha_gimpoz diff --git a/C4/Calendar.pm b/C4/Calendar.pm index 2f1f720548..a9d39dcaaf 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -20,30 +20,11 @@ use warnings; use vars qw($VERSION @EXPORT); use Carp; -use Date::Calc qw( Date_to_Days ); +use Date::Calc qw( Date_to_Days Today); use C4::Context; -BEGIN { - # set the version for version checking - $VERSION = 3.01; - require Exporter; - @EXPORT = qw( - &get_week_days_holidays - &get_day_month_holidays - &get_exception_holidays - &get_single_holidays - &insert_week_day_holiday - &insert_day_month_holiday - &insert_single_holiday - &insert_exception_holiday - &delete_holiday - &isHoliday - &addDate - &daysBetween - ); -} - +use constant ISO_DATE_FORMAT => "%04d-%02d-%02d"; =head1 NAME C4::Calendar::Calendar - Koha module dealing with holidays. @@ -54,15 +35,14 @@ C4::Calendar::Calendar - Koha module dealing with holidays. =head1 DESCRIPTION -This package is used to deal with holidays. Through this package, you can set all kind of holidays for the library. +This package is used to deal with holidays. Through this package, you can set +all kind of holidays for the library. =head1 FUNCTIONS -=over 2 +=head2 new -=item new - - $calendar = C4::Calendar->new(branchcode => $branchcode); + $calendar = C4::Calendar->new(branchcode => $branchcode); Each library branch has its own Calendar. C<$branchcode> specifies which Calendar you want. @@ -120,7 +100,7 @@ sub _init { $exception_holidays{"$year/$month/$day"}{title} = $title; $exception_holidays{"$year/$month/$day"}{description} = $description; $exception_holidays{"$year/$month/$day"}{date} = - sprintf("%04d-%02d-%02d", $year, $month, $day); + sprintf(ISO_DATE_FORMAT, $year, $month, $day); } $self->{'exception_holidays'} = \%exception_holidays; @@ -130,15 +110,15 @@ sub _init { $single_holidays{"$year/$month/$day"}{title} = $title; $single_holidays{"$year/$month/$day"}{description} = $description; $single_holidays{"$year/$month/$day"}{date} = - sprintf("%04d-%02d-%02d", $year, $month, $day); + sprintf(ISO_DATE_FORMAT, $year, $month, $day); } $self->{'single_holidays'} = \%single_holidays; return $self; } -=item get_week_days_holidays +=head2 get_week_days_holidays - $week_days_holidays = $calendar->get_week_days_holidays(); + $week_days_holidays = $calendar->get_week_days_holidays(); Returns a hash reference to week days holidays. @@ -150,9 +130,9 @@ sub get_week_days_holidays { return $week_days_holidays; } -=item get_day_month_holidays - - $day_month_holidays = $calendar->get_day_month_holidays(); +=head2 get_day_month_holidays + + $day_month_holidays = $calendar->get_day_month_holidays(); Returns a hash reference to day month holidays. @@ -164,8 +144,8 @@ sub get_day_month_holidays { return $day_month_holidays; } -=item get_exception_holidays - +=head2 get_exception_holidays + $exception_holidays = $calendar->exception_holidays(); Returns a hash reference to exception holidays. This kind of days are those @@ -180,8 +160,8 @@ sub get_exception_holidays { return $exception_holidays; } -=item get_single_holidays - +=head2 get_single_holidays + $single_holidays = $calendar->get_single_holidays(); Returns a hash reference to single holidays. This kind of holidays are those which @@ -195,7 +175,7 @@ sub get_single_holidays { return $single_holidays; } -=item insert_week_day_holiday +=head2 insert_week_day_holiday insert_week_day_holiday(weekday => $weekday, title => $title, @@ -215,15 +195,18 @@ sub insert_week_day_holiday { my $self = shift @_; my %options = @_; + my $weekday = $options{weekday}; + croak "Invalid weekday $weekday" unless $weekday =~ m/^[0-6]$/; + my $dbh = C4::Context->dbh(); my $insertHoliday = $dbh->prepare("insert into repeatable_holidays (id,branchcode,weekday,day,month,title,description) values ( '',?,?,NULL,NULL,?,? )"); - $insertHoliday->execute( $self->{branchcode}, $options{weekday},$options{title}, $options{description}); - $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title}; - $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description}; + $insertHoliday->execute( $self->{branchcode}, $weekday, $options{title}, $options{description}); + $self->{'week_days_holidays'}->{$weekday}{title} = $options{title}; + $self->{'week_days_holidays'}->{$weekday}{description} = $options{description}; return $self; } -=item insert_day_month_holiday +=head2 insert_day_month_holiday insert_day_month_holiday(day => $day, month => $month, @@ -254,7 +237,7 @@ sub insert_day_month_holiday { return $self; } -=item insert_single_holiday +=head2 insert_single_holiday insert_single_holiday(day => $day, month => $month, @@ -280,6 +263,9 @@ sub insert_single_holiday { my $self = shift @_; my %options = @_; + @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) + if $options{date} && !$options{day}; + my $dbh = C4::Context->dbh(); my $isexception = 0; my $insertHoliday = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); @@ -289,7 +275,7 @@ sub insert_single_holiday { return $self; } -=item insert_exception_holiday +=head2 insert_exception_holiday insert_exception_holiday(day => $day, month => $month, @@ -315,6 +301,9 @@ sub insert_exception_holiday { my $self = shift @_; my %options = @_; + @options{qw(year month day)} = ( $options{date} =~ m/(\d+)-(\d+)-(\d+)/o ) + if $options{date} && !$options{day}; + my $dbh = C4::Context->dbh(); my $isexception = 1; my $insertException = $dbh->prepare("insert into special_holidays (id,branchcode,day,month,year,isexception,title,description) values ('', ?,?,?,?,?,?,?)"); @@ -324,7 +313,134 @@ sub insert_exception_holiday { return $self; } -=item delete_holiday +=head2 ModWeekdayholiday + + ModWeekdayholiday(weekday =>$weekday, + title => $title, + description => $description) + +Modifies the title and description of a weekday for $self->{branchcode}. + +C<$weekday> Is the title to update for the holiday. + +C<$description> Is the description to update for the holiday. + +=cut + +sub ModWeekdayholiday { + my $self = shift @_; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE branchcode = ? AND weekday = ?"); + $updateHoliday->execute( $options{title},$options{description},$self->{branchcode},$options{weekday}); + $self->{'week_days_holidays'}->{$options{weekday}}{title} = $options{title}; + $self->{'week_days_holidays'}->{$options{weekday}}{description} = $options{description}; + return $self; +} + +=head2 ModDaymonthholiday + + ModDaymonthholiday(day => $day, + month => $month, + title => $title, + description => $description); + +Modifies the title and description for a day/month holiday for $self->{branchcode}. + +C<$day> The day of the month for the update. + +C<$month> The month to be used for the update. + +C<$title> The title to be updated for the holiday. + +C<$description> The description to be update for the holiday. + +=cut + +sub ModDaymonthholiday { + my $self = shift @_; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $updateHoliday = $dbh->prepare("UPDATE repeatable_holidays SET title = ?, description = ? WHERE month = ? AND day = ? AND branchcode = ?"); + $updateHoliday->execute( $options{title},$options{description},$options{month},$options{day},$self->{branchcode}); + $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{title} = $options{title}; + $self->{'day_month_holidays'}->{"$options{month}/$options{day}"}{description} = $options{description}; + return $self; +} + +=head2 ModSingleholiday + + ModSingleholiday(day => $day, + month => $month, + year => $year, + title => $title, + description => $description); + +Modifies the title and description for a single holiday for $self->{branchcode}. + +C<$day> Is the day of the month to make the update. + +C<$month> Is the month to make the update. + +C<$year> Is the year to make the update. + +C<$title> Is the title to update for the holiday formed by $year/$month/$day. + +C<$description> Is the description to update for the holiday formed by $year/$month/$day. + +=cut + +sub ModSingleholiday { + my $self = shift @_; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $isexception = 0; + my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); + $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); + $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; + $self->{'single_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + return $self; +} + +=head2 ModExceptionholiday + + ModExceptionholiday(day => $day, + month => $month, + year => $year, + title => $title, + description => $description); + +Modifies the title and description for an exception holiday for $self->{branchcode}. + +C<$day> Is the day of the month for the holiday. + +C<$month> Is the month for the holiday. + +C<$year> Is the year for the holiday. + +C<$title> Is the title to be modified for the holiday formed by $year/$month/$day. + +C<$description> Is the description to be modified for the holiday formed by $year/$month/$day. + +=cut + +sub ModExceptionholiday { + my $self = shift @_; + my %options = @_; + + my $dbh = C4::Context->dbh(); + my $isexception = 1; + my $updateHoliday = $dbh->prepare("UPDATE special_holidays SET title = ?, description = ? WHERE day = ? AND month = ? AND year = ? AND branchcode = ? AND isexception = ?"); + $updateHoliday->execute($options{title},$options{description},$options{day},$options{month},$options{year},$self->{branchcode},$isexception); + $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{title} = $options{title}; + $self->{'exception_holidays'}->{"$options{year}/$options{month}/$options{day}"}{description} = $options{description}; + return $self; +} + +=head2 delete_holiday delete_holiday(weekday => $weekday day => $day, @@ -399,10 +515,9 @@ sub delete_holiday { return $self; } -=item isHoliday - - $isHoliday = isHoliday($day, $month $year); +=head2 isHoliday + $isHoliday = isHoliday($day, $month $year); C<$day> Is the day to check whether if is a holiday or not. @@ -438,7 +553,36 @@ sub isHoliday { } -=item addDate +=head2 copy_to_branch + + $calendar->copy_to_branch($target_branch) + +=cut + +sub copy_to_branch { + my ($self, $target_branch) = @_; + + croak "No target_branch" unless $target_branch; + + my $target_calendar = C4::Calendar->new(branchcode => $target_branch); + + my ($y, $m, $d) = Today(); + my $today = sprintf ISO_DATE_FORMAT, $y,$m,$d; + + my $wdh = $self->get_week_days_holidays; + $target_calendar->insert_week_day_holiday( weekday => $_, %{ $wdh->{$_} } ) + foreach keys %$wdh; + $target_calendar->insert_day_month_holiday(%$_) + foreach values %{ $self->get_day_month_holidays }; + $target_calendar->insert_exception_holiday(%$_) + foreach grep { $_->{date} gt $today } values %{ $self->get_exception_holidays }; + $target_calendar->insert_single_holiday(%$_) + foreach grep { $_->{date} gt $today } values %{ $self->get_single_holidays }; + + return 1; +} + +=head2 addDate my ($day, $month, $year) = $calendar->addDate($date, $offset) @@ -472,10 +616,10 @@ sub addDate { } else { ## ($daysMode eq 'Days') ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); } - return(C4::Dates->new( sprintf("%04d-%02d-%02d",$year,$month,$day),'iso')); + return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); } -=item daysBetween +=head2 daysBetween my $daysBetween = $calendar->daysBetween($startdate, $enddate) @@ -485,16 +629,16 @@ Returns the number of non-holiday days in the interval. useDaysMode syspref has no effect here. =cut -sub daysBetween ($$$) { - my $self = shift or return undef; - my $startdate = shift or return undef; - my $enddate = shift or return undef; - my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); - my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); - if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { - return 0; - # we don't go backwards ( FIXME - handle this error better ) - } +sub daysBetween { + my $self = shift or return; + my $startdate = shift or return; + my $enddate = shift or return; + my ($yearFrom,$monthFrom,$dayFrom) = split("-",$startdate->output('iso')); + my ($yearTo, $monthTo, $dayTo ) = split("-", $enddate->output('iso')); + if (Date_to_Days($yearFrom,$monthFrom,$dayFrom) > Date_to_Days($yearTo,$monthTo,$dayTo)) { + return 0; + # we don't go backwards ( FIXME - handle this error better ) + } my $count = 0; while (1) { ($yearFrom != $yearTo or $monthFrom != $monthTo or $dayFrom != $dayTo) or last; # if they all match, it's the last day @@ -510,8 +654,6 @@ sub daysBetween ($$$) { __END__ -=back - =head1 AUTHOR Koha Physics Library UNLP