https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=39716

--- Comment #5 from Lisette Scheer <[email protected]> ---
Created attachment 211126
  -->
https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=211126&action=edit
Bug 39716: Warn when saving a notice with template toolkit errors

Notices with broken Template Toolkit markup could be saved without any
warning, and then failed when they were sent. This enhancement checks the
templates
when saving a notice and returns:
- Errors, which stop the notice from being generated, prevent saving
- Warnings, for notices that will be generated but not show what is
  expected, can be saved anyway

The problems are listed in a dialog and in each template, with a link
to each line they are on. Clicking it selects the line in the message
body, and the line numbers are shown next to it, highlighting the lines
with problems. The line numbers only show when there's a message referencing
them.

A "Check template" button runs the same check on a single
template, and it runs when opening a saved notice.

The checks are done by Koha::Notice::Template->validate, through a new
REST API endpoint, POST /api/v1/notice_templates/validation, which needs
the edit_notices permission:
- errors: Template Toolkit syntax errors (e.g. an IF without END), and
  errors when processing the template without data (e.g. a filter that
  does not exist)
- warnings: unclosed [% tags, which Template Toolkit prints as plain
  text, and notices that receive a list of items but have nothing to
  list them with: overdue notices (ODUE and any code used in the overdue
  notice triggers) need a [% FOREACH ... IN overdues %] loop,
  <<items.content>> or <item>...</item>; the PREDUEDGST and DUEDGST
  digests need a loop on checkouts or <<items.content>>

For those notices, the field list also offers ready-made blocks to list
the items, in both syntaxes.

This also removes the "at .../tools/letter.pl line 211." suffix shown in
the errors of saved templates, as the code removing it expected line 213.

Test plan:
1. Apply the patch, run yarn build and restart services
2. Go to Tools > Notices and slips and edit the ODUE notice
3. In the Email template, remove the [% END %] closing the FOREACH loop
4. Click "Check template":
   => A red box says the template cannot be saved until the syntax
      error is fixed, with a link to its line
   => Line numbers show next to the message body, the line highlighted
5. Click the line number in the message:
   => The line is selected in the message body
6. Click Save:
   => A dialog says the notice cannot be saved, there is no
      "Save anyway" button
7. Close it, put the [% END %] back, and replace the whole FOREACH
   block with "You have overdue items."
8. Click "Check template":
   => A yellow box warns that the notice is sent with a list of items
      but the body has nothing to list them with, and how to add one
9. Click Save:
   => The dialog offers "Save anyway", which saves the notice
10. Edit ODUE again, pick "FOREACH overdue IN overdues" under
    ---REPEATABLE CONTENT--- in the field list, insert it and check:
   => No problems found
11. Add "[% x | $Price %]" without [% USE Price %] and check:
   => An error says the filter is not found
12. Type "Dear [% borrower.firstname" in the subject and check:
   => A warning says [% has no matching %], in the message subject
13. Set a custom notice code as the first overdue notice in Tools >
    Overdue notice/status triggers and repeat step 7 on that notice:
   => The same warning is shown
14. In the database, set the content of a notice to "[% x | $Price %]"
    and edit it:
   => The "Template Toolkit error" at the top of the template no longer
      ends with the path and line of tools/letter.pl
15. prove t/db_dependent/Koha/Notice/Template.t
t/db_dependent/api/v1/notice_templates.t xt/api.t

Co-Authored-By: Claude Opus 5.5 <[email protected]>

-- 
You are receiving this mail because:
You are watching all bug changes.
_______________________________________________
Koha-bugs mailing list -- [email protected]
To unsubscribe send an email to [email protected]
website : http://www.koha-community.org/
git : http://git.koha-community.org/
bugs : http://bugs.koha-community.org/

Reply via email to