From d5dc514f9326c42b3fce89d229442295a039b2b1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 18 Aug 2022 17:21:31 +0100 Subject: [PATCH] Bug 31373: DRY - Use try/catch around _process_tt This patch reduces the repetition in the tools/letter.pl controller replaceing it with a try/catch block wrapping a call to C4::Letters::_process_tt instead. Signed-off-by: Kyle M Hall Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Letters.pm | 2 +- tools/letter.pl | 38 ++++++++++---------------------------- 2 files changed, 11 insertions(+), 29 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 7c136f32f9..bde0d2b0dc 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1578,7 +1578,7 @@ sub _process_tt { my $content = $params->{content}; my $tables = $params->{tables}; my $loops = $params->{loops}; - my $objects = $params->{objects}; + my $objects = $params->{objects} || {}; my $substitute = $params->{substitute} || {}; my $lang = defined($params->{lang}) && $params->{lang} ne 'default' ? $params->{lang} : 'en'; my ($theme, $availablethemes); diff --git a/tools/letter.pl b/tools/letter.pl index 420556ff75..d709b2d6e9 100755 --- a/tools/letter.pl +++ b/tools/letter.pl @@ -42,7 +42,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); -use Template; +use Try::Tiny; use C4::Auth qw( get_template_and_user ); use C4::Context; @@ -197,29 +197,8 @@ sub add_form { my $first_flag_name = 1; my $lang; - # Get available includes - my $htdocs = C4::Context->config('intrahtdocs'); - my ($theme, $availablethemes); - ($theme, $lang, $availablethemes)= C4::Templates::availablethemes( $htdocs, 'about.tt', 'intranet', $lang); - my @includes; - foreach (@$availablethemes) { - push @includes, "$htdocs/$_/$lang/includes"; - push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en'; - } - # The letter name is contained into each mtt row. # So we can only sent the first one to the template. - my $tt = Template->new( - { - EVAL_PERL => 1, - ABSOLUTE => 1, - PLUGIN_BASE => 'Koha::Template::Plugin', - INCLUDE_PATH => \@includes, - FILTERS => {}, - ENCODING => 'UTF-8', - } - ); - for my $letter ( @$letters ) { # The letter_name if ( $first_flag_name and $letter->{name} ) { @@ -229,12 +208,15 @@ sub add_form { $first_flag_name = 0; } - my $output; - my $template = $letter->{content}; - $template = qq|[% USE KohaDates %][% USE Remove_MARC_punctuation %][% PROCESS 'html_helpers.inc' %]$template|; - unless ( $tt->process( \$template, {}, \$output ) ) { - $letter->{tt_error} = $tt->error(); - } + # Catch template issues + try { + C4::Letters::_process_tt($letter); + } catch { + my $error = $_; + $error =~ s/^ERROR PROCESSING TEMPLATE: //; + $error =~ s/at .*\/tools\/letter\.pl line 213\.$//; + $letter->{tt_error} = $error; + }; my $lang = $letter->{lang}; my $mtt = $letter->{message_transport_type}; -- 2.39.5