From b17ad817d23ebe3f0c19201780a84b2438bce2b8 Mon Sep 17 00:00:00 2001
From: Kyle Hall <kyle@bywatersolutions.com>
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
---
 .../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 bcb851c8eaa..3e35a37a9c5 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 @@
                                                 <fieldset class="rows mtt">
                                             [% END %]
                                                 <ol>
+                                                    [% IF ( letter.tt_error ) %]
+                                                        <li>
+                                                            <span class="label error">Template Toolkit error:</span>
+                                                            [% letter.tt_error | html %]
+                                                        </li>
+                                                    [% END %]
                                                     [% IF ( letter.updated_on ) %]
                                                         <li>
                                                             <span class="label">Last updated:</span>
diff --git a/tools/letter.pl b/tools/letter.pl
index 4276e462802..35d9a12d14c 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