Bug 33242: Allow passing add_{duration} options to KohaDates

For example, if I want to add a few days to the issuedate in a
TT template, I could do this:
    [% checkout.issuedate | $KohaDates add_days => 3 %]

This development allows you to pass add/subtract years, months, weeks,
days, hours, minutes and seconds.

Test plan:
Pick a notice like CHECKOUT.
Add a line like:
    [% checkout.date_due | $KohaDates with_hours => 1, add_minutes => 15 %]
Do a checkout. Verify that the notice generated contains a time that
shifted 15 minutes.
Undo your change.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Marcel de Rooy 2023-03-16 09:53:20 +01:00 committed by Tomas Cohen Arazi
parent 0386c14a29
commit c4f78fbdbb
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
1 changed files with 15 additions and 0 deletions

View File

@ -32,12 +32,27 @@ sub filter {
$config->{with_hours} //= 0;
my $dt = dt_from_string( $text, 'iso' );
$dt->add( %$_ ) for _parse_config_for_durations($config); # Allow date add/subtract; see _parse_config_for_durations
return $config->{as_due_date} ?
output_pref({ dt => $dt, as_due_date => 1, dateformat => $config->{dateformat} }) :
output_pref({ dt => $dt, dateonly => !$config->{with_hours}, dateformat => $config->{dateformat} });
}
sub _parse_config_for_durations {
# Supports passing things like add_years => 1 or subtract_years => 1
# Same for months, weeks, days, hours, minutes or seconds
# Returns a list of hashrefs like { years => 1 }, { days => -1 } that can be passed to dt->add
my $config = shift;
my @results;
foreach my $entry ( keys %$config ) {
if( $entry =~ /^(add|subtract)_(years|months|weeks|days|hours|minutes|seconds)$/ ) {
push @results, { $2 => ( $1 eq 'add' ? 1 : -1 ) * $config->{$entry} };
}
}
return @results;
}
sub output_preference {
my ( $self, @params ) = @_;
return output_pref( @params );