From 82d0a551117be224a72dec51e9eb5129dca1a060 Mon Sep 17 00:00:00 2001
From: Pasi Kallinen <pasi.kallinen@koha-suomi.fi>
Date: Wed, 5 Feb 2020 09:32:38 +0200
Subject: [PATCH] Bug 24589: Add plugin hook addbiblio_check_record

Adds a new plugin hook, addbiblio_check_record, to addbiblio.pl.
When saving a record, the hook is called with the MARC record as
a parameter. If the hook returns anything, the return values are
shown to the user, and saving the record is prevented.

This can be used to create plugins that eg. check or modify the record.

Advanced editor has not been changed.
---
 cataloguing/addbiblio.pl                           | 30 ++++++++++++++++++++++
 .../prog/en/modules/cataloguing/addbiblio.tt       | 10 ++++++++
 2 files changed, 40 insertions(+)

diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl
index a548bab8a6..16fe5b1bf5 100755
--- a/cataloguing/addbiblio.pl
+++ b/cataloguing/addbiblio.pl
@@ -41,6 +41,8 @@ use Koha::ItemTypes;
 use Koha::Libraries;
 
 use Koha::BiblioFrameworks;
+use Koha::Plugins;
+use Koha::Plugins::Handler;
 
 use Date::Calc qw(Today);
 use MARC::File::USMARC;
@@ -827,6 +829,34 @@ if ( $op eq "addbiblio" ) {
     # getting html input
     my @params = $input->multi_param();
     $record = TransformHtmlToMarc( $input, 1 );
+
+    if ( $record ne '-1' && C4::Context->preference('UseKohaPlugins') &&
+         C4::Context->config('enable_plugins') ) {
+        my @formaterrors;
+        my @plugins = Koha::Plugins->new()->GetPlugins({
+            method => 'addbiblio_check_record'
+        });
+        foreach my $plugin (@plugins) {
+            push(@formaterrors, @{$plugin->addbiblio_check_record($record)});
+        }
+        if (scalar(@formaterrors) > 0) {
+            $template->param(
+                marc_format_errors => \@formaterrors,
+                title => $record->title,
+                popup => $mode,
+                frameworkcode => $frameworkcode,
+                itemtype => $frameworkcode,
+                borrowernumber => $loggedinuser,
+                biblionumber => $biblionumber,
+                tab => scalar $input->param('tab')
+                );
+            $template->{'VARS'}->{'searchid'} = $searchid;
+            build_tabs ($template, $record, $dbh,$encoding,$input);
+            output_html_with_http_headers $input, $cookie, $template->output;
+            exit;
+        }
+    }
+
     # check for a duplicate
     my ( $duplicatebiblionumber, $duplicatetitle );
     if ( !$is_a_modif ) {
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
index 8904afaa5a..e96a4a0a8d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/addbiblio.tt
@@ -650,6 +650,16 @@
                     </div>
                 [% END %]
 
+                [% IF marc_format_errors %]
+                <div class="dialog alert">
+                   <ul>
+                   [% FOREACH MARC_ERR IN marc_format_errors %]
+                      <li>[% MARC_ERR %]</li>
+                   [% END %]
+                   </ul>
+                </div>
+                [% END %]
+
                 [% IF ( popup ) %]
                         <input type="hidden" name="mode" value="popup" />
                 [% END %]
-- 
2.11.0