From 3d4d5edd8026524b0632586fa6323a983445e369 Mon Sep 17 00:00:00 2001 From: Kyle Hall Date: Thu, 21 Jul 2022 11:06:15 -0400 Subject: [PATCH] Bug 31211: Check slips and notices for valid Template Toolkit and report errors It would be useful for Koha to run a basic TT validation test and report back errors. That way if somebody misses an END tag or something of that nature, it will be easy to find and fix. Test Plan: 1) Apply this patch 2) Edit a notice, change the content to simply "[% END %]" or some other invalid Template Toolkit 3) Choose "Save and continue editing" 4) Note the new "Template Toolkit error" field that displays the error message Signed-off-by: Sally --- .../prog/en/modules/tools/letter.tt | 6 ++++ tools/letter.pl | 32 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt index bcb851c8ea..3e35a37a9c 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/letter.tt @@ -496,6 +496,12 @@
[% END %]
    + [% IF ( letter.tt_error ) %] +
  1. + Template Toolkit error: + [% letter.tt_error | html %] +
  2. + [% END %] [% IF ( letter.updated_on ) %]
  3. Last updated: diff --git a/tools/letter.pl b/tools/letter.pl index 4276e46280..35d9a12d14 100755 --- a/tools/letter.pl +++ b/tools/letter.pl @@ -42,6 +42,8 @@ use Modern::Perl; use CGI qw ( -utf8 ); +use Template; + use C4::Auth qw( get_template_and_user ); use C4::Context; use C4::Output qw( output_html_with_http_headers ); @@ -90,12 +92,12 @@ our $my_branch = C4::Context->preference("IndependentBranches") && !$staffflags- $template->param( independant_branch => $my_branch, - script_name => $script_name, - searchfield => $searchfield, - branchcode => $branchcode, - section => $section, - langtab => $langtab, - action => $script_name + script_name => $script_name, + searchfield => $searchfield, + branchcode => $branchcode, + section => $section, + langtab => $langtab, + action => $script_name ); if ( $op eq 'add_validate' or $op eq 'copy_validate' ) { @@ -196,6 +198,16 @@ sub add_form { my $lang; # 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', + FILTERS => {}, + ENCODING => 'UTF-8', + } + ); + for my $letter ( @$letters ) { # The letter_name if ( $first_flag_name and $letter->{name} ) { @@ -205,6 +217,12 @@ sub add_form { $first_flag_name = 0; } + my $output; + my $template = $letter->{content}; + unless ( $tt->process( \$template, {}, \$output ) ) { + $letter->{tt_error} = $tt->error(); + } + my $lang = $letter->{lang}; my $mtt = $letter->{message_transport_type}; $letters{ $lang }{templates}{$mtt} = { @@ -213,6 +231,7 @@ sub add_form { updated_on => $letter->{updated_on}, title => $letter->{title}, content => $letter->{content} // '', + tt_error => $letter->{tt_error}, }; } } @@ -445,6 +464,7 @@ sub default_display { my $loop_data = []; my $protected_letters = protected_letters(); + foreach my $row (@{$results}) { $row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} }; push @{$loop_data}, $row; -- 2.30.2