From 0ba93cbadb28fe9d42e75cab04da2bbbd285e31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20V=C3=A9ron?= Date: Sun, 4 Oct 2015 22:31:40 +0200 Subject: [PATCH] [SIGNED OFF] Bug 14954 - Remove C4::Dates from holiday related files in folder tools This patch removes C4::Dates from: Remove C4::Dates from: - tools/exceptionHolidays.pl - tools/holidays.pl - tools/newHolidays.pl - C4/Calendar.pm To test: - Go to Home > Tools > Calendar - Add, edit, delete the different types of holidays and exceptions - git grep 'C4::Calendar' and test such files - prove t/Calendar.t (Amended following comments #3 and #4 / mv) (Amended following comment #7 / 25.10.2015 / mv Signed-off-by: Mirko Tietgen http://bugs.koha-community.org/show_bug.cgi?id=14945 Signed-off-by: Nick Clemens --- C4/Calendar.pm | 18 +++++++++++------- tools/exceptionHolidays.pl | 29 ++++------------------------- tools/holidays.pl | 20 ++++++++------------ tools/newHolidays.pl | 46 ++++++++++++++-------------------------------- 4 files changed, 37 insertions(+), 76 deletions(-) diff --git a/C4/Calendar.pm b/C4/Calendar.pm index d8705af..bf3b96f 100644 --- a/C4/Calendar.pm +++ b/C4/Calendar.pm @@ -644,7 +644,6 @@ C<$year> Is the year to check whether if is a holiday or not. sub isHoliday { my ($self, $day, $month, $year) = @_; # FIXME - date strings are stored in non-padded metric format. should change to iso. - # FIXME - should change arguments to accept C4::Dates object $month=$month+0; $year=$year+0; $day=$day+0; @@ -700,7 +699,7 @@ sub copy_to_branch { my ($day, $month, $year) = $calendar->addDate($date, $offset) -C<$date> is a C4::Dates object representing the starting date of the interval. +C<$startdate> is the starting date of the interval. C<$offset> Is the number of days that this function has to count from $date. @@ -708,7 +707,8 @@ C<$offset> Is the number of days that this function has to count from $date. sub addDate { my ($self, $startdate, $offset) = @_; - my ($year,$month,$day) = split("-",$startdate->output('iso')); + $startdate = eval { output_pref( { dt => dt_from_string( $startdate ), dateonly => 1, dateformat => 'iso' } ); }; + my ( $year, $month, $day) = split( "-", $startdate ); my $daystep = 1; if ($offset < 0) { # In case $offset is negative # $offset = $offset*(-1); @@ -730,14 +730,16 @@ sub addDate { } else { ## ($daysMode eq 'Days') ($year, $month, $day) = &Date::Calc::Add_Delta_Days($year, $month, $day, $offset ); } - return(C4::Dates->new( sprintf(ISO_DATE_FORMAT,$year,$month,$day),'iso')); + my $date_ret = sprintf(ISO_DATE_FORMAT,$year,$month,$day); + $date_ret = eval { output_pref( { dt => dt_from_string( $date_ret), dateonly => 1, dateformat => 'iso' } ); }; + return($date_ret); } =head2 daysBetween my $daysBetween = $calendar->daysBetween($startdate, $enddate) -C<$startdate> and C<$enddate> are C4::Dates objects that define the interval. +C<$startdate> and C<$enddate> define the interval. Returns the number of non-holiday days in the interval. useDaysMode syspref has no effect here. @@ -747,8 +749,10 @@ 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')); + $startdate = eval { output_pref( { dt => dt_from_string( $startdate ), dateonly => 1, dateformat => 'iso' } ); }; + $enddate = eval { output_pref( { dt => dt_from_string( $enddate ), dateonly => 1, dateformat => 'iso' } ); }; + my ( $yearFrom, $monthFrom, $dayFrom) = split( "-", $startdate); + my ( $yearTo, $monthTo, $dayTo ) = split( "-", $enddate); 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 ) diff --git a/tools/exceptionHolidays.pl b/tools/exceptionHolidays.pl index 45e2208..0b1818d 100755 --- a/tools/exceptionHolidays.pl +++ b/tools/exceptionHolidays.pl @@ -10,6 +10,7 @@ use C4::Output; use DateTime; use C4::Calendar; +use Koha::DateUtils; my $input = new CGI; my $dbh = C4::Context->dbh(); @@ -19,16 +20,11 @@ my $weekday = $input->param('showWeekday'); my $day = $input->param('showDay'); my $month = $input->param('showMonth'); my $year = $input->param('showYear'); -my $day1; -my $month1; -my $year1; my $title = $input->param('showTitle'); my $description = $input->param('showDescription'); my $holidaytype = $input->param('showHolidayType'); -my $datecancelrange = $input->param('datecancelrange'); +my $datecancelrange_dt = eval { dt_from_string( $input->param('datecancelrange') ) }; my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day); -my $isodate = C4::Dates->new($calendardate, 'iso'); -$calendardate = $isodate->output('syspref'); my $calendar = C4::Calendar->new(branchcode => $branchcode); @@ -40,30 +36,13 @@ if ($description) { $description = ''; } -# We format the date -my @dateend = split(/[\/-]/, $datecancelrange); -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]; -} - # We make an array with holiday's days my @holiday_list; -if ($year1 && $month1 && $day1){ +if ($datecancelrange_dt){ my $first_dt = DateTime->new(year => $year, month => $month, day => $day); - my $end_dt = DateTime->new(year => $year1, month => $month1, day => $day1); for (my $dt = $first_dt->clone(); - $dt <= $end_dt; + $dt <= $datecancelrange_dt; $dt->add(days => 1) ) { push @holiday_list, $dt->clone(); diff --git a/tools/holidays.pl b/tools/holidays.pl index 4b0579a..bec897d 100755 --- a/tools/holidays.pl +++ b/tools/holidays.pl @@ -26,6 +26,7 @@ use C4::Output; use C4::Branch; # GetBranches use C4::Calendar; +use Koha::DateUtils; my $input = new CGI; @@ -44,15 +45,10 @@ my ($template, $loggedinuser, $cookie) my $keydate; # calendardate - date passed in url for human readability (syspref) my $calendardate; -my $today = C4::Dates->new(); -my $calendarinput = C4::Dates->new($input->param('calendardate')) || $today; +my $calendarinput_dt = eval { dt_from_string( $input->param('calendardate') ); } || dt_from_string; # if the url has an invalid date default to 'now.' -unless($calendardate = $calendarinput->output('syspref')) { - $calendardate = $today->output('syspref'); -} -unless($keydate = $calendarinput->output('iso')) { - $keydate = $today->output('iso'); -} +$calendardate = output_pref( { dt => $calendarinput_dt, dateonly => 1 } ); +$keydate = output_pref( { dt => $calendarinput_dt, dateonly => 1, dateformat => 'iso' } ); $keydate =~ s/-/\//g; my $branch= $input->param('branch') || C4::Context->userenv->{'branch'}; @@ -123,11 +119,11 @@ foreach my $monthDay (keys %$day_month_holidays) { my $exception_holidays = $calendar->get_exception_holidays(); my @exception_holidays; foreach my $yearMonthDay (keys %$exception_holidays) { - my $exceptiondate = C4::Dates->new($exception_holidays->{$yearMonthDay}{date}, "iso"); + my $exceptiondate = eval { dt_from_string( $exception_holidays->{$yearMonthDay}{date} ) }; my %exception_holiday; %exception_holiday = (KEY => $yearMonthDay, DATE_SORT => $exception_holidays->{$yearMonthDay}{date}, - DATE => $exceptiondate->output("syspref"), + DATE => output_pref( { dt => $exceptiondate, dateonly => 1, dateformat => 'iso' } ), TITLE => $exception_holidays->{$yearMonthDay}{title}, DESCRIPTION => $exception_holidays->{$yearMonthDay}{description}); push @exception_holidays, \%exception_holiday; @@ -136,11 +132,11 @@ foreach my $yearMonthDay (keys %$exception_holidays) { my $single_holidays = $calendar->get_single_holidays(); my @holidays; foreach my $yearMonthDay (keys %$single_holidays) { - my $holidaydate = C4::Dates->new($single_holidays->{$yearMonthDay}{date}, "iso"); + my $holidaydate = eval { dt_from_string( $single_holidays->{$yearMonthDay}{date} ) }; my %holiday; %holiday = (KEY => $yearMonthDay, DATE_SORT => $single_holidays->{$yearMonthDay}{date}, - DATE => $holidaydate->output("syspref"), + DATE => output_pref( { dt => $holidaydate, dateonly => 1, dateformat => 'iso' } ), TITLE => $single_holidays->{$yearMonthDay}{title}, DESCRIPTION => $single_holidays->{$yearMonthDay}{description}); push @holidays, \%holiday; diff --git a/tools/newHolidays.pl b/tools/newHolidays.pl index dec4b3b..eda4c1b 100755 --- a/tools/newHolidays.pl +++ b/tools/newHolidays.pl @@ -12,9 +12,9 @@ use C4::Output; use Koha::Cache; -use C4::Dates; use C4::Calendar; use DateTime; +use Koha::DateUtils; my $input = new CGI; my $dbh = C4::Context->dbh(); @@ -25,33 +25,18 @@ our $weekday = $input->param('newWeekday'); our $day = $input->param('newDay'); our $month = $input->param('newMonth'); our $year = $input->param('newYear'); -my $day1; -my $month1; -my $year1; my $dateofrange = $input->param('dateofrange'); 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; @@ -62,16 +47,13 @@ if ($description) { # We make an array with holiday's days our @holiday_list; -if ($year1 && $month1 && $day1){ - my $first_dt = DateTime->new(year => $year, month => $month, day => $day); - my $end_dt = DateTime->new(year => $year1, month => $month1, day => $day1); - - for (my $dt = $first_dt->clone(); - $dt <= $end_dt; - $dt->add(days => 1) ) - { - push @holiday_list, $dt->clone(); - } +if ($end_dt){ + for (my $dt = $first_dt->clone(); + $dt <= $end_dt; + $dt->add(days => 1) ) + { + push @holiday_list, $dt->clone(); + } } if($allbranches) { -- 2.1.4