From c7a76bf0d42a553abefad0b3cb46d27bafd3bd03 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 --- 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 177a4f51f6..a2627a0d81 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -637,12 +637,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}; @@ -717,20 +718,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, } ); @@ -1552,9 +1556,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); @@ -1581,7 +1586,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 7e72517eb6..c663abb990 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.30.1 (Apple Git-130)