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 1546-1552 sub _get_tt_params { Link Here
1546
            my $pk = $config->{$table}->{pk};
1550
            my $pk = $config->{$table}->{pk};
1547
            my $fk = $config->{$table}->{fk};
1551
            my $fk = $config->{$table}->{fk};
1548
1552
1549
            if ( $ref eq q{} || $ref eq 'HASH' ) {
1553
            if ( $is_a_loop ) {
1554
                unless ( ref( $tables->{$table} ) eq 'ARRAY' ) {
1555
                    croak "ERROR processing table $table. Wrong API call.";
1556
                }
1557
                my $objects = $module->search( { $pk => { -in => $tables->{$table} } } );
1558
                $params->{ $config->{$table}->{plural} } = $objects;
1559
            }
1560
            elsif ( $ref eq q{} || $ref eq 'HASH' ) {
1550
                my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table};
1561
                my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table};
1551
                my $object;
1562
                my $object;
1552
                if ( $fk ) { # Using a foreign key for lookup
1563
                if ( $fk ) { # Using a foreign key for lookup
(-)a/t/db_dependent/Letters/TemplateToolkit.t (-2 / +41 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 => 15;
22
use Test::More tests => 16;
22
use Test::Warn;
23
use Test::Warn;
23
24
24
use MARC::Record;
25
use MARC::Record;
Lines 39-44 use Koha::Serial; Link Here
39
use Koha::Subscription;
40
use Koha::Subscription;
40
use Koha::Suggestion;
41
use Koha::Suggestion;
41
use Koha::Checkout;
42
use Koha::Checkout;
43
use Koha::Notice::Templates;
42
use Koha::Patron::Modification;
44
use Koha::Patron::Modification;
43
45
44
my $schema = Koha::Database->schema;
46
my $schema = Koha::Database->schema;
Lines 279-281 $prepared_letter = GetPreparedLetter( Link Here
279
    )
281
    )
280
);
282
);
281
is( $prepared_letter->{content}, $modification->id(), 'Patron modification object used correctly' );
283
is( $prepared_letter->{content}, $modification->id(), 'Patron modification object used correctly' );
282
- 
284
285
subtest 'loops' => sub {
286
    plan tests => 1;
287
    my $code = "TEST";
288
    my $module = "TEST";
289
290
    subtest 'primary key is AI' => sub {
291
        plan tests => 1;
292
        my $patron_1 = $builder->build({ source => 'Borrower' });
293
        my $patron_2 = $builder->build({ source => 'Borrower' });
294
295
        my $template = q|[% FOREACH patron IN borrowers %][% patron.surname %][% END %]|;
296
        reset_template( { template => $template, code => $code, module => $module } );
297
        my $letter = GetPreparedLetter( module => $module, letter_code => $code, loops => { borrowers => [ $patron_1->{borrowernumber}, $patron_2->{borrowernumber} ] } );
298
        my $expected_letter = join '', ( $patron_1->{surname}, $patron_2->{surname} );
299
        is( $letter->{content}, $expected_letter, );
300
    };
301
};
302
303
sub reset_template {
304
    my ( $params ) = @_;
305
    my $template   = $params->{template};
306
    my $code       = $params->{code};
307
    my $module     = $params->{module} || 'test_module';
308
309
    Koha::Notice::Templates->search( { code => $code } )->delete;
310
    Koha::Notice::Template->new(
311
        {
312
            module                 => $module,
313
            code                   => $code,
314
            branchcode             => '',
315
            name                   => $code,
316
            title                  => $code,
317
            message_transport_type => 'email',
318
            content                => $template
319
        }
320
    )->store;
321
}

Return to bug 17971