X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=tools%2FnewHolidays.pl;h=a161eaf1c87f151dade6d3ecefcd69f3988d0386;hb=refs%2Fheads%2Fkoha_ffzg;hp=c36d328f80adef5dc1b9839c2cddc87983e529f2;hpb=806fac60f466b79d9c60023df4425b134040bfdc;p=koha.git diff --git a/tools/newHolidays.pl b/tools/newHolidays.pl index c36d328f80..a161eaf1c8 100755 --- a/tools/newHolidays.pl +++ b/tools/newHolidays.pl @@ -1,53 +1,56 @@ #!/usr/bin/perl +#FIXME: perltidy this file -use strict; -use warnings; +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public Lic# along with Koha; if not, see . +# along with Koha; if not, see . -use CGI; + +use Modern::Perl; + +use CGI qw ( -utf8 ); use C4::Auth; use C4::Output; +use Koha::Caches; use C4::Calendar; use DateTime; +use Koha::DateUtils; my $input = new CGI; my $dbh = C4::Context->dbh(); -my $branchcode = $input->param('newBranchName'); +our $branchcode = $input->param('newBranchName'); my $originalbranchcode = $branchcode; -my $weekday = $input->param('newWeekday'); -my $day = $input->param('newDay'); -my $month = $input->param('newMonth'); -my $year = $input->param('newYear'); -my $day1; -my $month1; -my $year1; +our $weekday = $input->param('newWeekday'); +our $day = $input->param('newDay'); +our $month = $input->param('newMonth'); +our $year = $input->param('newYear'); my $dateofrange = $input->param('dateofrange'); -my $title = $input->param('newTitle'); -my $description = $input->param('newDescription'); -my $newoperation = $input->param('newOperation'); +our $title = $input->param('newTitle'); +our $description = $input->param('newDescription'); +our $newoperation = $input->param('newOperation'); my $allbranches = $input->param('allBranches'); -my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day); -my $isodate = C4::Dates->new($calendardate, 'iso'); -$calendardate = $isodate->output('syspref'); - -my @dateend = split(/[\/-]/, $dateofrange); -if (C4::Context->preference("dateformat") eq "metric") { - $day1 = $dateend[0]; - $month1 = $dateend[1]; - $year1 = $dateend[2]; -}elsif (C4::Context->preference("dateformat") eq "us") { - $month1 = $dateend[0]; - $day1 = $dateend[1]; - $year1 = $dateend[2]; -} else { - $year1 = $dateend[0]; - $month1 = $dateend[1]; - $day1 = $dateend[2]; -} + +my $first_dt = DateTime->new(year => $year, month => $month, day => $day); +my $end_dt = eval { dt_from_string( $dateofrange ); }; + +my $calendardate = output_pref( { dt => $first_dt, dateonly => 1, dateformat => 'iso' } ); + $title || ($title = ''); if ($description) { $description =~ s/\r/\\r/g; @@ -56,18 +59,29 @@ if ($description) { $description = ''; } +# We make an array with holiday's days +our @holiday_list; +if ($end_dt){ + for (my $dt = $first_dt->clone(); + $dt <= $end_dt; + $dt->add(days => 1) ) + { + push @holiday_list, $dt->clone(); + } +} + if($allbranches) { - my $branch; - my @branchcodes = split(/\|/, $input->param('branchCodes')); - foreach $branch (@branchcodes) { - add_holiday($newoperation, $branch, $weekday, $day, $month, $year, $title, $description); - } + my $libraries = Koha::Libraries->search; + while ( my $library = $libraries->next ) { + add_holiday($newoperation, $library->branchcode, $weekday, $day, $month, $year, $title, $description); + } } else { - add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description); + add_holiday($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description); } print $input->redirect("/cgi-bin/koha/tools/holidays.pl?branch=$originalbranchcode&calendardate=$calendardate"); +#FIXME: move add_holiday() to a better place sub add_holiday { ($newoperation, $branchcode, $weekday, $day, $month, $year, $title, $description) = @_; my $calendar = C4::Calendar->new(branchcode => $branchcode); @@ -100,51 +114,34 @@ sub add_holiday { } } elsif ( $newoperation eq 'holidayrange' ) { - #Make an array with holiday's days - my $first_dt = DateTime->new(year => $year, month => $month, day => $day); - my $end_dt = DateTime->new(year => $year1, month => $month1, day => $day1); - my @holiday_list = (); - - for (my $dt = $first_dt->clone(); - $dt <= $end_dt; - $dt->add(days => 1) ) - { - push @holiday_list, $dt->clone(); - } - - foreach my $date (@holiday_list){ - unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) { - $calendar->insert_single_holiday( - day => $date->{local_c}->{day}, - month => $date->{local_c}->{month}, - year => $date->{local_c}->{year}, - title => $title, - description => $description - ); + if (@holiday_list){ + foreach my $date (@holiday_list){ + unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) { + $calendar->insert_single_holiday( + day => $date->{local_c}->{day}, + month => $date->{local_c}->{month}, + year => $date->{local_c}->{year}, + title => $title, + description => $description + ); + } } } } elsif ( $newoperation eq 'holidayrangerepeat' ) { - #Make an array with holiday's days - my $first_dt = DateTime->new(year => $year, month => $month, day => $day); - my $end_dt = DateTime->new(year => $year1, month => $month1, day => $day1); - my @holiday_list = (); - - for (my $dt = $first_dt->clone(); - $dt <= $end_dt; - $dt->add(days => 1) ) - { - push @holiday_list, $dt->clone(); - } - - foreach my $date (@holiday_list){ - unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) { - $calendar->insert_day_month_holiday( - day => $date->{local_c}->{day}, - month => $date->{local_c}->{month}, - title => $title, - description => $description - ); + if (@holiday_list){ + foreach my $date (@holiday_list){ + unless ( $calendar->isHoliday( $date->{local_c}->{day}, $date->{local_c}->{month}, $date->{local_c}->{year} ) ) { + $calendar->insert_day_month_holiday( + day => $date->{local_c}->{day}, + month => $date->{local_c}->{month}, + title => $title, + description => $description + ); + } } } } + # we updated the single_holidays table, so wipe its cache + my $cache = Koha::Caches->get_instance(); + $cache->clear_from_cache( 'single_holidays') ; }