From aa511842fafb79f0dedc0572295d1089ab9c2ad7 Mon Sep 17 00:00:00 2001
From: David Cook <dcook@prosentient.com.au>
Date: Tue, 11 Mar 2025 04:24:34 +0000
Subject: [PATCH] Bug 39296: Provide a template plugin for getting a
 MARC::Record from MARCXML

This change takes MARCXML data (e.g. from the biblio_metadata table) and
returns a MARC::Record object which can be used in Reports templates to
allow for more sophisticated reporting using MARCXML data.

Test plan:
0. Apply the patch and koha-plack --restart kohadev
1. Go to http://localhost:8081/cgi-bin/koha/tools/letter.pl
2. Add a new notice for Reports with the following content:
[% USE KohaMarcRecord %]
[% SET all_subjects = {} %]
[% FOREACH row IN data %]
    [% SET record= KohaMarcRecord.from_xml(row.metadata) %]
    [% SET fields = record.field('6..') %]
    [% FOREACH field IN fields %]
        [% tmp = field.delete_subfield('code','9') %]
        [% SET subject = field.as_string() %]
        [% IF ( all_subjects.$subject ) %]
            [% all_subjects.$subject = all_subjects.$subject + 1 %]
        [% ELSE %]
            [% all_subjects.$subject = 1 %]
        [% END %]
    [% END %]
[% END %]
<table>
<tr><th>Subject</th><th>Count</th></tr>
[% FOREACH key IN all_subjects.nsort.reverse %]
<tr><td>[% key %]</td><td>[% all_subjects.$key %]</td></tr>
[% END %]
</table>
3. Go to http://localhost:8081/cgi-bin/koha/reports/guided_reports.pl?op=add_form_sql
4. Create a new report with the following SQL:
5. Click "Run with template" and choose the template/notice you've created above
6. Note that you get a list of MARC subjects sorted by the most used to the least used

Signed-off-by: Phil Ringnalda <phil@chetcolibrary.org>
---
 Koha/Template/Plugin/KohaMarcRecord.pm | 40 ++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 Koha/Template/Plugin/KohaMarcRecord.pm

diff --git a/Koha/Template/Plugin/KohaMarcRecord.pm b/Koha/Template/Plugin/KohaMarcRecord.pm
new file mode 100644
index 0000000000..0d87438808
--- /dev/null
+++ b/Koha/Template/Plugin/KohaMarcRecord.pm
@@ -0,0 +1,40 @@
+package Koha::Template::Plugin::KohaMarcRecord;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Template::Plugin;
+use base qw( Template::Plugin );
+
+use C4::Context;
+use MARC::File::XML;
+
+sub from_xml {
+    my ( $self, $xml ) = @_;
+    my $encoding = 'UTF-8';
+    my $format = C4::Context->preference('marcflavour');
+    my $record;
+    eval {
+        $record = MARC::Record->new_from_xml( $xml, $encoding, $format );
+    };
+    if ($@){
+        warn $@;
+    }
+    return $record;
+}
+
+1;
-- 
2.47.1