Bug 15572: Follow-up to fix error on authority creation
[koha.git] / authorities / merge.pl
index be24c8e..cfc1ca5 100755 (executable)
@@ -6,7 +6,7 @@
 #
 # 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 2 of the License, or (at your option) any later
+# 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
 
 use strict;
 use warnings;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Output;
 use C4::Auth;
 use C4::AuthoritiesMarc;
-use Koha::Authority;
+use Koha::MetadataRecord::Authority;
 use C4::Koha;
 use C4::Biblio;
 
@@ -49,7 +49,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 if ($merge) {
 
     # Creating a new record from the html code
-    my $record   = TransformHtmlToMarc($input);
+    my $record   = TransformHtmlToMarc($input, 0);
     my $recordid1   = $input->param('recordid1');
     my $recordid2   = $input->param('recordid2');
     my $typecode = $input->param('frameworkcode');
@@ -68,6 +68,7 @@ if ($merge) {
             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);
@@ -91,24 +92,24 @@ else {
         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
     }
     else {
-        my $recordObj1 = Koha::Authority->get_from_authid($authid[0]) || Koha::Authority->new();
+        my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]) || Koha::MetadataRecord::Authority->new();
         my $recordObj2;
 
         if (defined $mergereference && $mergereference eq 'breeding') {
-            $recordObj2 =  Koha::Authority->get_from_breeding($authid[1]) || Koha::Authority->new();
+            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]) || Koha::MetadataRecord::Authority->new();
         } else {
-            $recordObj2 =  Koha::Authority->get_from_authid($authid[1]) || Koha::Authority->new();
+            $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]) || Koha::MetadataRecord::Authority->new();
         }
 
         if ($mergereference) {
 
             my $framework;
-            if ( $recordObj1->authtype ne $recordObj2->authtype && $mergereference ne 'breeding' ) {
+            if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
                 $framework = $input->param('frameworkcode')
                   or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
             }
             else {
-                $framework = $recordObj1->authtype;
+                $framework = $recordObj1->authtypecode;
             }
             if ($mergereference eq 'breeding') {
                 $mergereference = $authid[0];
@@ -122,10 +123,13 @@ else {
                 }
             }
 
-            my $notreference =
-              ( $authid[0] == $mergereference )
-              ? $authid[1]
-              : $authid[0];
+            #Setting $notreference
+            my $notreference = $authid[1];
+            if($mergereference == $notreference){
+                $notreference = $authid[0];
+                #Swap so $recordObj1 is always the correct merge reference
+                ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
+            }
 
             # Creating a loop for display
 
@@ -151,46 +155,30 @@ else {
                 title1          => $recordObj1->authorized_heading,
                 title2          => $recordObj2->authorized_heading,
             );
-            if ( $recordObj1->authtype ne $recordObj2->authtype ) {
-                my $frameworks = getauthtypes;
+            if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
+                my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
                 my @frameworkselect;
-                foreach my $thisframeworkcode ( keys %$frameworks ) {
+                while ( my $authority_type = $authority_types->next ) {
                     my %row = (
-                        value => $thisframeworkcode,
-                        frameworktext =>
-                          $frameworks->{$thisframeworkcode}->{'authtypetext'},
+                        value => $authority_type->authtypecode,
+                        frameworktext => $authority_type->authtypetext,
                     );
-                    if ( $recordObj1->authtype eq $thisframeworkcode ) {
-                        $row{'selected'} = 1;
-                    }
                     push @frameworkselect, \%row;
                 }
                 $template->param(
                     frameworkselect => \@frameworkselect,
-                    frameworkcode1  => $recordObj1->authtype,
-                    frameworkcode2  => $recordObj2->authtype,
+                    frameworkcode1  => $recordObj1->authtypecode,
+                    frameworkcode2  => $recordObj2->authtypecode,
+                    frameworklabel1 => $recordObj1->authtypetext,
+                    frameworklabel2 => $recordObj2->authtypetext,
                 );
             }
         }
     }
 }
 
-my $authtypes = getauthtypes;
-my @authtypesloop;
-foreach my $thisauthtype (
-    sort {
-        $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'}
-    }
-    keys %$authtypes
-  )
-{
-    my %row = (
-        value        => $thisauthtype,
-        authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
-    );
-    push @authtypesloop, \%row;
-}
-$template->{VARS}->{authtypesloop} = \@authtypesloop;
+my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
+$template->param( authority_types => $authority_types );
 
 if (@errors) {