From 3ee3cf10e61aaad8a47881fdc6cc565e74c847af Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Sat, 21 Jan 2017 14:26:27 +0100 Subject: [PATCH] Bug 17971: TT syntax for notices - Add support for plurals On of the awesome things we will be able to do with the TT syntax is the support of plurals. For instance we will be able to send a list of items, checkouts, etc. to the notice template. That way we will get rid of our custom syntax like <> or for instance. The existing code already has the playground for that but it is not used. Basically the idea is to add a "loops" key which can contain a list of object to retrieve from the DB and send to the template. For instance: loops => { overdues => [ $itemnumber_1, .., $itemnumber_N ] } will send a variable "overdues" to the template. It will contain the Koha::Checkout objects relative to the id passed. There is one quite big inconvenient to this approach so far: since we are still supporting the historical syntax, the objects can be fetch by a script, then the script will send the id to GetPreparedLetter which will refetch them. This must be improved, but I suggest to do that later. Test plan: prove t/db_dependent/Letters/TemplateToolkit.t should return green Signed-off-by: Josef Moravec Signed-off-by: Nick Clemens Signed-off-by: Kyle M Hall --- C4/Letters.pm | 21 ++++++++++++++++----- t/db_dependent/Letters/TemplateToolkit.t | 21 ++++++++++++++++++++- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 28eccacf51..8308bac8a9 100644 --- a/C4/Letters.pm +++ b/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 = { article_requests => { @@ -1558,7 +1562,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 diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 38b50480f2..cbcfe387c2 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/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; @@ -378,6 +379,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}; -- 2.39.5