@@ -, +, @@ notices --- C4/Letters.pm | 21 ++++++++++++--------- Koha/Object.pm | 8 ++++++++ Koha/Objects.pm | 4 ++++ t/db_dependent/Letters/TemplateToolkit.t | 14 +++++++++++++- 4 files changed, 37 insertions(+), 10 deletions(-) --- a/C4/Letters.pm +++ a/C4/Letters.pm @@ -702,12 +702,13 @@ sub GetPreparedLetter { return; } + my $objects = $params{objects} || {}; my $tables = $params{tables} || {}; my $substitute = $params{substitute} || {}; my $loops = $params{loops} || {}; # loops is not supported for historical notices syntax my $repeat = $params{repeat}; - %$tables || %$substitute || $repeat || %$loops - or carp( "ERROR: nothing to substitute - both 'tables', 'loops' and 'substitute' are empty" ), + %$tables || %$substitute || $repeat || %$loops || %$objects + or carp( "ERROR: nothing to substitute - all of 'objects', 'tables', 'loops' and 'substitute' are empty" ), return; my $want_librarian = $params{want_librarian}; @@ -782,10 +783,11 @@ sub GetPreparedLetter { $letter->{content} = _process_tt( { - content => $letter->{content}, - tables => $tables, - loops => $loops, + content => $letter->{content}, + tables => $tables, + loops => $loops, substitute => $substitute, + objects => $objects, } ); @@ -1457,9 +1459,10 @@ sub _set_message_status { sub _process_tt { my ( $params ) = @_; - my $content = $params->{content}; - my $tables = $params->{tables}; - my $loops = $params->{loops}; + my $content = $params->{content}; + my $tables = $params->{tables}; + my $loops = $params->{loops}; + my $objects = $params->{objects}; my $substitute = $params->{substitute} || {}; my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE}; @@ -1475,7 +1478,7 @@ sub _process_tt { } ) or die Template->error(); - my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute }; + my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute, %$objects }; $content = qq|[% USE KohaDates %]$content|; --- a/Koha/Object.pm +++ a/Koha/Object.pm @@ -120,6 +120,8 @@ Returns: sub store { my ($self) = @_; + return $self if ( ( caller() )[0] eq 'Template::Document' ); + try { return $self->_result()->update_or_insert() ? $self : undef; } @@ -162,6 +164,8 @@ Returns: sub delete { my ($self) = @_; + return $self if ( ( caller() )[0] eq 'Template::Document' ); + # Deleting something not in storage throws an exception return -1 unless $self->_result()->in_storage(); @@ -351,6 +355,10 @@ sub AUTOLOAD { my @known_methods = qw( is_changed id in_storage get_column discard_changes update ); Koha::Exceptions::Object::MethodNotCoveredByTests->throw( "The method $method is not covered by tests!" ) unless grep {/^$method$/} @known_methods; + if ( $method eq 'update' ) { + return $self if ( ( caller() )[0] eq 'Template::Document' ); + } + my $r = eval { $self->_result->$method(@_) }; if ( $@ ) { Koha::Exceptions::Object->throw( ref($self) . "::$method generated this error: " . $@ ); --- a/Koha/Objects.pm +++ a/Koha/Objects.pm @@ -383,6 +383,10 @@ sub AUTOLOAD { my $method = our $AUTOLOAD; $method =~ s/.*:://; + if ( $method eq 'update' || $method eq 'delete' ) { + return $self if ( ( caller() )[0] eq 'Template::Document' ); + } + carp "The method $method is not covered by tests" and return unless grep {/^$method$/} @known_methods; my $r = eval { $self->_resultset->$method(@params) }; if ( $@ ) { --- a/t/db_dependent/Letters/TemplateToolkit.t +++ a/t/db_dependent/Letters/TemplateToolkit.t @@ -19,7 +19,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 17; +use Test::More tests => 18; use Test::Warn; use MARC::Record; @@ -42,6 +42,7 @@ use Koha::Serial; use Koha::Subscription; use Koha::Suggestion; use Koha::Checkout; +use Koha::Patrons; use Koha::Notice::Messages; use Koha::Notice::Templates; use Koha::Patron::Modification; @@ -134,6 +135,17 @@ $prepared_letter = GetPreparedLetter( ); is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly with arrayref' ); +$prepared_letter = GetPreparedLetter( + ( + module => 'test', + letter_code => 'TEST_PATRON', + objects => { + borrower => scalar Koha::Patrons->find( $patron->{borrowernumber} ), + }, + ) +); +is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly as object' ); + $sth->execute( "TEST_BIBLIO", "[% biblio.id %]" ); $prepared_letter = GetPreparedLetter( ( --