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};
{
content => $letter->{content},
tables => $tables,
+ loops => $loops,
}
);
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(
}
) 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();
}
sub _get_tt_params {
- my ($tables) = @_;
+ my ($tables, $is_a_loop) = @_;
my $params;
+ $is_a_loop ||= 0;
my $config = {
article_requests => {
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
# 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
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
-use Test::More tests => 16;
+use Test::More tests => 17;
use Test::Warn;
use MARC::Record;
};
};
+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};