From 506fd71af6af7af6e8a38a915fc180d2396a4505 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 28 Feb 2020 13:33:13 +0100 Subject: [PATCH] Bug 24159: Throw an exception if days_mode option is not given when needed Sponsored-by: Institute of Technology Carlow --- Koha/Calendar.pm | 33 +++++++++++++++++++++++++++------ Koha/CirculationRules.pm | 3 +++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Koha/Calendar.pm b/Koha/Calendar.pm index dfc358d240..6865ac75ac 100644 --- a/Koha/Calendar.pm +++ b/Koha/Calendar.pm @@ -1,14 +1,14 @@ package Koha::Calendar; -use strict; -use warnings; -use 5.010; +use Modern::Perl; + +use Carp; use DateTime; use DateTime::Set; use DateTime::Duration; use C4::Context; use Koha::Caches; -use Carp; +use Koha::Exceptions; sub new { my ( $classname, %options ) = @_; @@ -47,7 +47,6 @@ sub _init { 1; } - $self->{days_mode} ||= C4::Context->preference('useDaysMode'); $self->{test} = 0; return; } @@ -137,6 +136,10 @@ sub single_holidays { sub addDate { my ( $self, $startdate, $add_duration, $unit ) = @_; + + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->addDate: days_mode") + unless exists $self->{days_mode}; + # Default to days duration (legacy support I guess) if ( ref $add_duration ne 'DateTime::Duration' ) { $add_duration = DateTime::Duration->new( days => $add_duration ); @@ -165,6 +168,9 @@ sub addHours { # If we are using the calendar behave for now as if Datedue # was the chosen option (current intended behaviour) + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->addHours: days_mode") + unless exists $self->{days_mode}; + if ( $self->{days_mode} ne 'Days' && $self->is_holiday($base_date) ) { @@ -185,7 +191,8 @@ sub addDays { my ( $self, $startdate, $days_duration ) = @_; my $base_date = $startdate->clone(); - $self->{days_mode} ||= q{}; + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->addDays: days_mode") + unless exists $self->{days_mode}; if ( $self->{days_mode} eq 'Calendar' ) { # use the calendar to skip all days the library is closed @@ -235,6 +242,9 @@ sub addDays { sub get_push_amt { my ( $self, $base_date) = @_; + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->get_push_amt: days_mode") + unless exists $self->{days_mode}; + my $dow = $base_date->day_of_week; return ( # We're using Dayweek useDaysMode option @@ -288,6 +298,10 @@ sub is_holiday { sub next_open_days { my ( $self, $dt, $to_add ) = @_; + + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->next_open_days: days_mode") + unless exists $self->{days_mode}; + my $base_date = $dt->clone(); $base_date->add(days => $to_add); @@ -300,6 +314,10 @@ sub next_open_days { sub prev_open_days { my ( $self, $dt, $to_sub ) = @_; + + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->get_open_days: days_mode") + unless exists $self->{days_mode}; + my $base_date = $dt->clone(); # It feels logical to be passed a positive number, though we're @@ -323,6 +341,9 @@ sub days_forward { my $start_dt = shift; my $num_days = shift; + Koha::Exceptions::MissingParameter->throw("Missing mandatory option for Koha:Calendar->days_forward: days_mode") + unless exists $self->{days_mode}; + return $start_dt unless $num_days > 0; my $base_dt = $start_dt->clone(); diff --git a/Koha/CirculationRules.pm b/Koha/CirculationRules.pm index df68310aee..17e7674130 100644 --- a/Koha/CirculationRules.pm +++ b/Koha/CirculationRules.pm @@ -109,6 +109,9 @@ our $RULE_KINDS = { issuelength => { scope => [ 'branchcode', 'categorycode', 'itemtype' ], }, + useDaysMode => { + scope => [ 'branchcode', 'categorycode', 'itemtype' ], + }, lengthunit => { scope => [ 'branchcode', 'categorycode', 'itemtype' ], }, -- 2.20.1