From 1964751bbde18dd087e57d4939483080b1e36acc Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 12 Jan 2018 10:45:43 -0500 Subject: [PATCH] Bug 19966 - Add ability to pass objects directly to slips and notices Koha spends an incredible amount of time on parsing and processing parameters passed in to slips and notices. It would be immensely more efficient to be able to pass objects directly to GetPreparedLetter so it doesn't need to do any fetching / processing on them. Test plan: 1) Apply this patch 2) prove t/db_dependent/Letters/TemplateToolkit.t --- 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(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 42893f1..e66fd47 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -699,12 +699,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}; @@ -779,10 +780,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, } ); @@ -1454,9 +1456,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}; @@ -1472,7 +1475,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|; diff --git a/Koha/Object.pm b/Koha/Object.pm index 7f36a7d..d53654b 100644 --- a/Koha/Object.pm +++ b/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: " . $@ ); diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 8c79afd..8c8aeb5 100644 --- a/Koha/Objects.pm +++ b/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 ( $@ ) { diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 611b936..f237976 100644 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/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( ( -- 2.10.2