View | Details | Raw Unified | Return to bug 17971
Collapse All | Expand All

(-)a/C4/Letters.pm (-5 / +16 lines)
Lines 691-699 sub GetPreparedLetter { Link Here
691
691
692
    my $tables = $params{tables} || {};
692
    my $tables = $params{tables} || {};
693
    my $substitute = $params{substitute} || {};
693
    my $substitute = $params{substitute} || {};
694
    my $loops  = $params{loops} || {}; # loops is not supported for history syntax
694
    my $repeat = $params{repeat};
695
    my $repeat = $params{repeat};
695
    %$tables || %$substitute || $repeat
696
    %$tables || %$substitute || $repeat || %$loops
696
      or carp( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
697
      or carp( "ERROR: nothing to substitute - both 'tables', 'loops' and 'substitute' are empty" ),
697
         return;
698
         return;
698
    my $want_librarian = $params{want_librarian};
699
    my $want_librarian = $params{want_librarian};
699
700
Lines 770-775 sub GetPreparedLetter { Link Here
770
        {
771
        {
771
            content => $letter->{content},
772
            content => $letter->{content},
772
            tables  => $tables,
773
            tables  => $tables,
774
            loops  => $loops,
773
        }
775
        }
774
    );
776
    );
775
777
Lines 1440-1445 sub _process_tt { Link Here
1440
1442
1441
    my $content = $params->{content};
1443
    my $content = $params->{content};
1442
    my $tables = $params->{tables};
1444
    my $tables = $params->{tables};
1445
    my $loops = $params->{loops};
1443
1446
1444
    my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
1447
    my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
1445
    my $template           = Template->new(
1448
    my $template           = Template->new(
Lines 1454-1460 sub _process_tt { Link Here
1454
        }
1457
        }
1455
    ) or die Template->error();
1458
    ) or die Template->error();
1456
1459
1457
    my $tt_params = _get_tt_params( $tables );
1460
    my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) } };
1458
1461
1459
    my $output;
1462
    my $output;
1460
    $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error();
1463
    $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error();
Lines 1463-1471 sub _process_tt { Link Here
1463
}
1466
}
1464
1467
1465
sub _get_tt_params {
1468
sub _get_tt_params {
1466
    my ($tables) = @_;
1469
    my ($tables, $is_a_loop) = @_;
1467
1470
1468
    my $params;
1471
    my $params;
1472
    $is_a_loop ||= 0;
1469
1473
1470
    my $config = {
1474
    my $config = {
1471
        biblio => {
1475
        biblio => {
Lines 1552-1558 sub _get_tt_params { Link Here
1552
            my $pk = $config->{$table}->{pk};
1556
            my $pk = $config->{$table}->{pk};
1553
            my $fk = $config->{$table}->{fk};
1557
            my $fk = $config->{$table}->{fk};
1554
1558
1555
            if ( $ref eq q{} || $ref eq 'HASH' ) {
1559
            if ( $is_a_loop ) {
1560
                unless ( ref( $tables->{$table} ) eq 'ARRAY' ) {
1561
                    croak "ERROR processing table $table. Wrong API call.";
1562
                }
1563
                my $objects = $module->search( { $pk => { -in => $tables->{$table} } } );
1564
                $params->{ $config->{$table}->{plural} } = $objects;
1565
            }
1566
            elsif ( $ref eq q{} || $ref eq 'HASH' ) {
1556
                my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table};
1567
                my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table};
1557
                my $object;
1568
                my $object;
1558
                if ( $fk ) { # Using a foreign key for lookup
1569
                if ( $fk ) { # Using a foreign key for lookup
(-)a/t/db_dependent/Letters/TemplateToolkit.t (-2 / +20 lines)
Lines 3-8 Link Here
3
# This file is part of Koha.
3
# This file is part of Koha.
4
#
4
#
5
# Copyright (C) 2016 ByWater Solutions
5
# Copyright (C) 2016 ByWater Solutions
6
# Copyright (C) 2017 Koha Development Team
6
#
7
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# under the terms of the GNU General Public License as published by
Lines 18-24 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
20
use Modern::Perl;
21
use Modern::Perl;
21
use Test::More tests => 16;
22
use Test::More tests => 17;
22
use Test::Warn;
23
use Test::Warn;
23
24
24
use MARC::Record;
25
use MARC::Record;
Lines 325-330 subtest 'regression tests' => sub { Link Here
325
    };
326
    };
326
};
327
};
327
328
329
subtest 'loops' => sub {
330
    plan tests => 1;
331
    my $code = "TEST";
332
    my $module = "TEST";
333
334
    subtest 'primary key is AI' => sub {
335
        plan tests => 1;
336
        my $patron_1 = $builder->build({ source => 'Borrower' });
337
        my $patron_2 = $builder->build({ source => 'Borrower' });
338
339
        my $template = q|[% FOREACH patron IN borrowers %][% patron.surname %][% END %]|;
340
        reset_template( { template => $template, code => $code, module => $module } );
341
        my $letter = GetPreparedLetter( module => $module, letter_code => $code, loops => { borrowers => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } );
342
        my $expected_letter = join '', ( $patron_1->{surname}, $patron_2->{surname} );
343
        is( $letter->{content}, $expected_letter, );
344
    };
345
};
346
328
sub reset_template {
347
sub reset_template {
329
    my ( $params ) = @_;
348
    my ( $params ) = @_;
330
    my $template   = $params->{template};
349
    my $template   = $params->{template};
331
- 

Return to bug 17971