From df2d70b5e03ad1f2bcde058a6612153242c4a1b8 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 18 Dec 2024 15:56:50 +0000 Subject: [PATCH] Bug 38758: Add strftime as TT virtual method Content-Type: text/plain; charset=utf-8 Test plan: Try things like [% borrower.dateexpiry.strftime('%d-%m-%y') %] in a notice. Also test datetime fields like borrower.lastseen. Add locale support with strftime('%d %B', 'nl_NL') etc. Signed-off-by: Marcel de Rooy --- C4/Letters.pm | 5 +++++ Koha/Template/Plugin/KohaDates.pm | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/C4/Letters.pm b/C4/Letters.pm index ce8ffda7e1..bfcaacbe90 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -40,6 +40,7 @@ use Koha::Patrons; use Koha::SMS::Providers; use Koha::SMTP::Servers; use Koha::Subscriptions; +use Koha::Template::Plugin::KohaDates; use constant SERIALIZED_EMAIL_CONTENT_TYPE => 'message/rfc822'; @@ -638,6 +639,10 @@ sub GetPreparedLetter { $lang = shift @languages; } + $Template::Stash::SCALAR_OPS->{strftime} = sub { + return Koha::Template::Plugin::KohaDates->strftime(@_); + }; + $letter->{content} = _process_tt( { content => $letter->{content}, diff --git a/Koha/Template/Plugin/KohaDates.pm b/Koha/Template/Plugin/KohaDates.pm index d8c31beec5..e3f39c2eb2 100644 --- a/Koha/Template/Plugin/KohaDates.pm +++ b/Koha/Template/Plugin/KohaDates.pm @@ -67,4 +67,14 @@ sub tz { return C4::Context->tz->name; } +sub strftime { # used as TT virtual method for scalars, wrapper around DateTime counterpart + my ( $self, $value, $format, $locale ) = @_; + my $dt = eval { dt_from_string($value) }; + if ($dt) { + $dt->set_locale($locale) if $locale; + return $dt->strftime($format); + } + return $value; +} + 1; -- 2.39.5