From df7b67d863bd5ae20cc10ea86caf4af229cb3d82 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 23 Jul 2021 08:07:59 -0400 Subject: [PATCH] Bug 19966: Add ability to pass objects directly to slips and notices Koha spends an incredible amount of time on parsing and processing parameters passed in to slips and notices. It would be immensely more efficient to be able to pass objects directly to GetPreparedLetter so it doesn't need to do any fetching / processing on them. Test plan: 1) Apply this patch 2) prove t/db_dependent/Letters/TemplateToolkit.t Signed-off-by: Josef Moravec Signed-off-by: Martin Renvoize --- C4/Letters.pm | 31 ++++++++++++++---------- t/db_dependent/Letters/TemplateToolkit.t | 14 ++++++++++- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index c36553ac9a..d0d1133762 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -638,12 +638,13 @@ sub GetPreparedLetter { $letter->{'content-type'} = 'text/html; charset="UTF-8"' if $letter->{is_html}; } + my $objects = $params{objects} || {}; my $tables = $params{tables} || {}; my $substitute = $params{substitute} || {}; my $loops = $params{loops} || {}; # loops is not supported for historical notices syntax my $repeat = $params{repeat}; - %$tables || %$substitute || $repeat || %$loops - or carp( "ERROR: nothing to substitute - both 'tables', 'loops' and 'substitute' are empty" ), + %$tables || %$substitute || $repeat || %$loops || %$objects + or carp( "ERROR: nothing to substitute - all of 'objects', 'tables', 'loops' and 'substitute' are empty" ), return; my $want_librarian = $params{want_librarian}; @@ -718,20 +719,23 @@ sub GetPreparedLetter { $letter->{content} = _process_tt( { - content => $letter->{content}, - tables => $tables, - loops => $loops, + content => $letter->{content}, + lang => $lang, + loops => $loops, + objects => $objects, substitute => $substitute, - lang => $lang + tables => $tables, } ); $letter->{title} = _process_tt( { - content => $letter->{title}, - tables => $tables, - loops => $loops, + content => $letter->{title}, + lang => $lang, + loops => $loops, + objects => $objects, substitute => $substitute, + tables => $tables, } ); @@ -1616,9 +1620,10 @@ sub _set_message_status { sub _process_tt { my ( $params ) = @_; - my $content = $params->{content}; - my $tables = $params->{tables}; - my $loops = $params->{loops}; + my $content = $params->{content}; + my $tables = $params->{tables}; + my $loops = $params->{loops}; + my $objects = $params->{objects}; my $substitute = $params->{substitute} || {}; my $lang = defined($params->{lang}) && $params->{lang} ne 'default' ? $params->{lang} : 'en'; my ($theme, $availablethemes); @@ -1645,7 +1650,7 @@ sub _process_tt { } ) or die Template->error(); - my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute }; + my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute, %$objects }; $content = add_tt_filters( $content ); $content = qq|[% USE KohaDates %][% USE Remove_MARC_punctuation %]$content|; diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 127a98bd11..37bbf742ac 100755 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -19,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 28; +use Test::More tests => 29; use Test::MockModule; use Test::Warn; @@ -44,6 +44,7 @@ use Koha::Serial; use Koha::Subscription; use Koha::Suggestion; use Koha::Checkout; +use Koha::Patrons; use Koha::Notice::Messages; use Koha::Notice::Templates; use Koha::Patron::Modification; @@ -139,6 +140,17 @@ $prepared_letter = GetPreparedLetter( is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly with arrayref for content' ); is( $prepared_letter->{title}, $patron->{firstname}, 'Patron object used correctly with arrayref for title' ); +$prepared_letter = GetPreparedLetter( + ( + module => 'test', + letter_code => 'TEST_PATRON', + objects => { + borrower => scalar Koha::Patrons->find( $patron->{borrowernumber} ), + }, + ) +); +is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly as object' ); + $sth->execute( "TEST_BIBLIO", "[% biblio.title %]", "[% biblio.id %]" ); $prepared_letter = GetPreparedLetter( ( -- 2.20.1