From 1c30a93f4cd76e350fd2dbbda64933e424935c02 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] 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 --- C4/Calendar.pm | 18 +++++++++++------- tools/exceptionHolidays.pl | 5 +++-- tools/holidays.pl | 20 ++++++++------------ tools/newHolidays.pl | 6 +++--- 4 files changed, 25 insertions(+), 24 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..325eee3 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(); @@ -27,8 +28,8 @@ my $description = $input->param('showDescription'); my $holidaytype = $input->param('showHolidayType'); my $datecancelrange = $input->param('datecancelrange'); my $calendardate = sprintf("%04d-%02d-%02d", $year, $month, $day); -my $isodate = C4::Dates->new($calendardate, 'iso'); -$calendardate = $isodate->output('syspref'); +my $isodate = eval{ dt_from_string( $calendardate ); }; +$calendardate = output_pref( { dt => $isodate, dateonly => 1 } ); my $calendar = C4::Calendar->new(branchcode => $branchcode); 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..883cb26 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(); @@ -35,8 +35,8 @@ 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 $isodate = eval{ dt_from_string( $calendardate ); }; +$calendardate = output_pref( { dt => $isodate, dateonly => 1 } ); my @dateend = split(/[\/-]/, $dateofrange); if (C4::Context->preference("dateformat") eq "metric") { -- 1.7.10.4