Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / authorities / merge.pl
index cfc1ca5..0799c26 100755 (executable)
@@ -4,31 +4,32 @@
 #
 # 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 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.
+# 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, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 use CGI qw ( -utf8 );
-use C4::Output;
-use C4::Auth;
-use C4::AuthoritiesMarc;
+use C4::Output qw( output_html_with_http_headers );
+use C4::Auth qw( get_template_and_user );
+use C4::AuthoritiesMarc qw( GetAuthority ModAuthority DelAuthority GetTagsLabels merge );
+use C4::Biblio qw( TransformHtmlToMarc );
+
+use Koha::Authority::MergeRequests;
+use Koha::Authority::Types;
 use Koha::MetadataRecord::Authority;
-use C4::Koha;
-use C4::Biblio;
 
-my $input  = new CGI;
-my @authid = $input->param('authid');
+my $input  = CGI->new;
+my @authid = $input->multi_param('authid');
 my $merge  = $input->param('merge');
 
 my @errors;
@@ -38,7 +39,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         template_name   => "authorities/merge.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { editauthorities => 1 },
     }
 );
@@ -50,29 +50,39 @@ if ($merge) {
 
     # Creating a new record from the html code
     my $record   = TransformHtmlToMarc($input, 0);
-    my $recordid1   = $input->param('recordid1');
-    my $recordid2   = $input->param('recordid2');
+    my $recordid1   = $input->param('recordid1') // q{};
+    my $recordid2   = $input->param('recordid2') // q{};
     my $typecode = $input->param('frameworkcode');
 
+    # Some error checking
+    if( $recordid1 eq $recordid2 ) {
+        push @errors, { code => 'DESTRUCTIVE_MERGE' };
+    } elsif( !$typecode || !Koha::Authority::Types->find($typecode) ) {
+        push @errors, { code => 'WRONG_FRAMEWORK' };
+    } elsif( scalar $record->fields == 0 ) {
+        push @errors, { code => 'EMPTY_MARC' };
+    }
+    if( @errors ) {
+        $template->param( errors => \@errors );
+        output_html_with_http_headers $input, $cookie, $template->output;
+        exit;
+    }
+
     # Rewriting the leader
-    $record->leader( GetAuthority($recordid1)->leader() );
+    if( my $authrec = GetAuthority($recordid1) ) {
+        $record->leader( $authrec->leader() );
+    }
 
     # Modifying the reference record
+    # This triggers a merge for the biblios attached to $recordid1
     ModAuthority( $recordid1, $record, $typecode );
 
-    # Deleting the other record
-    if ( scalar(@errors) == 0 ) {
+    # Now merge for biblios attached to $recordid2
+    my $MARCfrom = GetAuthority( $recordid2 );
+    merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
 
-        my $error;
-        if ($input->param('mergereference') eq 'breeding') {
-            require C4::ImportBatch;
-            C4::ImportBatch::SetImportRecordStatus( $recordid2, 'imported' );
-        } else {
-            C4::AuthoritiesMarc::merge( $recordid2, GetAuthority($recordid2), $recordid1, $record );
-            $error = (DelAuthority($recordid2) == 0);
-        }
-        push @errors, $error if ($error);
-    }
+    # Delete the other record. No need to merge.
+    DelAuthority({ authid => $recordid2, skip_merge => 1 });
 
     # Parameters
     $template->param(
@@ -90,23 +100,38 @@ else {
 
     if ( scalar(@authid) != 2 ) {
         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
-    }
-    else {
-        my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]) || Koha::MetadataRecord::Authority->new();
-        my $recordObj2;
+    } elsif( $authid[0] eq $authid[1] ) {
+        push @errors, { code => 'DESTRUCTIVE_MERGE' };
+    } else {
+        my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
+        if (!$recordObj1) {
+            push @errors, { code => "MISSING_RECORD", value => $authid[0] };
+        }
+
 
+        my $recordObj2;
         if (defined $mergereference && $mergereference eq 'breeding') {
-            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]) || Koha::MetadataRecord::Authority->new();
+            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
         } else {
-            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]) || Koha::MetadataRecord::Authority->new();
+            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
+        }
+        if (!$recordObj2) {
+            push @errors, { code => "MISSING_RECORD", value => $authid[1] };
+        }
+
+        unless ( $recordObj1 && $recordObj2 ) {
+            if (@errors) {
+                $template->param( errors => \@errors );
+            }
+            output_html_with_http_headers $input, $cookie, $template->output;
+            exit;
         }
 
-        if ($mergereference) {
+        if ($mergereference ) {
 
             my $framework;
             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
-                $framework = $input->param('frameworkcode')
-                  or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
+                $framework = $input->param('frameworkcode');
             }
             else {
                 $framework = $recordObj1->authtypecode;
@@ -133,15 +158,27 @@ else {
 
             # Creating a loop for display
 
-            my @record1 = $recordObj1->createMergeHash($tagslib);
-            my @record2 = $recordObj2->createMergeHash($tagslib);
+            my @records = (
+                {
+                    recordid => $mergereference,
+                    record => $recordObj1->record,
+                    frameworkcode => $recordObj1->authtypecode,
+                    display => $recordObj1->createMergeHash($tagslib),
+                    reference => 1,
+                },
+                {
+                    recordid => $notreference,
+                    record => $recordObj2->record,
+                    frameworkcode => $recordObj2->authtypecode,
+                    display => $recordObj2->createMergeHash($tagslib),
+                },
+            );
 
             # Parameters
             $template->param(
                 recordid1        => $mergereference,
                 recordid2        => $notreference,
-                record1        => @record1,
-                record2        => @record2,
+                records        => \@records,
                 framework      => $framework,
             );
         }
@@ -156,43 +193,18 @@ else {
                 title2          => $recordObj2->authorized_heading,
             );
             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
-                my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
-                my @frameworkselect;
-                while ( my $authority_type = $authority_types->next ) {
-                    my %row = (
-                        value => $authority_type->authtypecode,
-                        frameworktext => $authority_type->authtypetext,
-                    );
-                    push @frameworkselect, \%row;
-                }
+                my $authority_types = Koha::Authority::Types->search( { authtypecode => { '!=' => '' } }, { order_by => ['authtypetext'] } );
                 $template->param(
-                    frameworkselect => \@frameworkselect,
+                    frameworkselect => $authority_types->unblessed,
                     frameworkcode1  => $recordObj1->authtypecode,
                     frameworkcode2  => $recordObj2->authtypecode,
-                    frameworklabel1 => $recordObj1->authtypetext,
-                    frameworklabel2 => $recordObj2->authtypetext,
                 );
             }
         }
     }
 }
 
-my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
-$template->param( authority_types => $authority_types );
-
 if (@errors) {
-
-    # Errors
     $template->param( errors => \@errors );
 }
-
 output_html_with_http_headers $input, $cookie, $template->output;
-exit;
-
-=head1 FUNCTIONS
-
-=cut
-
-# ------------------------
-# Functions
-# ------------------------