@@ -, +, @@ loops => { overdues => [ $itemnumber_1, .., $itemnumber_N ] } prove t/db_dependent/Letters/TemplateToolkit.t --- C4/Letters.pm | 21 ++++++++++++++++----- t/db_dependent/Letters/TemplateToolkit.t | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) --- a/C4/Letters.pm +++ a/C4/Letters.pm @@ -691,9 +691,10 @@ sub GetPreparedLetter { my $tables = $params{tables} || {}; my $substitute = $params{substitute} || {}; + my $loops = $params{loops} || {}; # loops is not supported for history syntax my $repeat = $params{repeat}; - %$tables || %$substitute || $repeat - or carp( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ), + %$tables || %$substitute || $repeat || %$loops + or carp( "ERROR: nothing to substitute - both 'tables', 'loops' and 'substitute' are empty" ), return; my $want_librarian = $params{want_librarian}; @@ -770,6 +771,7 @@ sub GetPreparedLetter { { content => $letter->{content}, tables => $tables, + loops => $loops, } ); @@ -1440,6 +1442,7 @@ sub _process_tt { my $content = $params->{content}; my $tables = $params->{tables}; + my $loops = $params->{loops}; my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE}; my $template = Template->new( @@ -1454,7 +1457,7 @@ sub _process_tt { } ) or die Template->error(); - my $tt_params = _get_tt_params( $tables ); + my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) } }; my $output; $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error(); @@ -1463,9 +1466,10 @@ sub _process_tt { } sub _get_tt_params { - my ($tables) = @_; + my ($tables, $is_a_loop) = @_; my $params; + $is_a_loop ||= 0; my $config = { biblio => { @@ -1552,7 +1556,14 @@ sub _get_tt_params { my $pk = $config->{$table}->{pk}; my $fk = $config->{$table}->{fk}; - if ( $ref eq q{} || $ref eq 'HASH' ) { + if ( $is_a_loop ) { + unless ( ref( $tables->{$table} ) eq 'ARRAY' ) { + croak "ERROR processing table $table. Wrong API call."; + } + my $objects = $module->search( { $pk => { -in => $tables->{$table} } } ); + $params->{ $config->{$table}->{plural} } = $objects; + } + elsif ( $ref eq q{} || $ref eq 'HASH' ) { my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table}; my $object; if ( $fk ) { # Using a foreign key for lookup --- a/t/db_dependent/Letters/TemplateToolkit.t +++ a/t/db_dependent/Letters/TemplateToolkit.t @@ -3,6 +3,7 @@ # This file is part of Koha. # # Copyright (C) 2016 ByWater Solutions +# Copyright (C) 2017 Koha Development Team # # Koha is free software; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by @@ -18,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 16; +use Test::More tests => 17; use Test::Warn; use MARC::Record; @@ -325,6 +326,24 @@ subtest 'regression tests' => sub { }; }; +subtest 'loops' => sub { + plan tests => 1; + my $code = "TEST"; + my $module = "TEST"; + + subtest 'primary key is AI' => sub { + plan tests => 1; + my $patron_1 = $builder->build({ source => 'Borrower' }); + my $patron_2 = $builder->build({ source => 'Borrower' }); + + my $template = q|[% FOREACH patron IN borrowers %][% patron.surname %][% END %]|; + reset_template( { template => $template, code => $code, module => $module } ); + my $letter = GetPreparedLetter( module => $module, letter_code => $code, loops => { borrowers => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } ); + my $expected_letter = join '', ( $patron_1->{surname}, $patron_2->{surname} ); + is( $letter->{content}, $expected_letter, ); + }; +}; + sub reset_template { my ( $params ) = @_; my $template = $params->{template}; --