Bug 17380: Do not use GuessAuthTypeCode in MetadataRecord::Authority
[koha.git] / authorities / merge.pl
1 #!/usr/bin/perl
2
3 # Copyright 2013 C & P Bibliography Services
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 );
23 use C4::Output;
24 use C4::Auth;
25 use C4::AuthoritiesMarc;
26 use Koha::MetadataRecord::Authority;
27 use C4::Koha;
28 use C4::Biblio;
29
30 use Koha::Exceptions;
31
32 my $input  = new CGI;
33 my @authid = $input->multi_param('authid');
34 my $merge  = $input->param('merge');
35
36 my @errors;
37
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39     {
40         template_name   => "authorities/merge.tt",
41         query           => $input,
42         type            => "intranet",
43         authnotrequired => 0,
44         flagsrequired   => { editauthorities => 1 },
45     }
46 );
47
48 #------------------------
49 # Merging
50 #------------------------
51 if ($merge) {
52
53     # Creating a new record from the html code
54     my $record   = TransformHtmlToMarc($input, 0);
55     my $recordid1   = $input->param('recordid1');
56     my $recordid2   = $input->param('recordid2');
57     my $typecode = $input->param('frameworkcode');
58
59     # Rewriting the leader
60     $record->leader( GetAuthority($recordid1)->leader() );
61
62     # Modifying the reference record
63     # This triggers a merge for the biblios attached to $recordid1
64     ModAuthority( $recordid1, $record, $typecode );
65
66     # Now merge for biblios attached to $recordid2
67     my $MARCfrom = GetAuthority( $recordid2 );
68     merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
69
70     # Delete the other record. Do not merge. It is unneeded and could under
71     # special circumstances have unwanted side-effects.
72     DelAuthority({ authid => $recordid2, skip_merge => 1 });
73
74     # Parameters
75     $template->param(
76         result  => 1,
77         recordid1 => $recordid1
78     );
79
80     #-------------------------
81     # Show records to merge
82     #-------------------------
83 }
84 else {
85     my $mergereference = $input->param('mergereference');
86     $template->{'VARS'}->{'mergereference'} = $mergereference;
87
88     if ( scalar(@authid) != 2 ) {
89         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
90     }
91     else {
92         my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
93         Koha::Exceptions::ObjectNotFound->throw( "No authority record found for authid $authid[0]\n" ) if !$recordObj1;
94
95         my $recordObj2;
96         if (defined $mergereference && $mergereference eq 'breeding') {
97             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
98         } else {
99             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
100         }
101         Koha::Exceptions::ObjectNotFound->throw( "No authority record found for authid $authid[1]\n" ) if !$recordObj2;
102
103         if ($mergereference) {
104
105             my $framework;
106             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
107                 $framework = $input->param('frameworkcode')
108                   or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
109             }
110             else {
111                 $framework = $recordObj1->authtypecode;
112             }
113             if ($mergereference eq 'breeding') {
114                 $mergereference = $authid[0];
115             }
116
117             # Getting MARC Structure
118             my $tagslib = GetTagsLabels( 1, $framework );
119             foreach my $field ( keys %$tagslib ) {
120                 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
121                     $tagslib->{$field}->{'tab'} = 0;
122                 }
123             }
124
125             #Setting $notreference
126             my $notreference = $authid[1];
127             if($mergereference == $notreference){
128                 $notreference = $authid[0];
129                 #Swap so $recordObj1 is always the correct merge reference
130                 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
131             }
132
133             # Creating a loop for display
134
135             my @records = (
136                 {
137                     recordid => $mergereference,
138                     record => $recordObj1->record,
139                     frameworkcode => $recordObj1->authtypecode,
140                     display => $recordObj1->createMergeHash($tagslib),
141                     reference => 1,
142                 },
143                 {
144                     recordid => $notreference,
145                     record => $recordObj2->record,
146                     frameworkcode => $recordObj2->authtypecode,
147                     display => $recordObj2->createMergeHash($tagslib),
148                 },
149             );
150
151             # Parameters
152             $template->param(
153                 recordid1        => $mergereference,
154                 recordid2        => $notreference,
155                 records        => \@records,
156                 framework      => $framework,
157             );
158         }
159         else {
160
161             # Ask the user to choose which record will be the kept
162             $template->param(
163                 choosereference => 1,
164                 recordid1         => $authid[0],
165                 recordid2         => $authid[1],
166                 title1          => $recordObj1->authorized_heading,
167                 title2          => $recordObj2->authorized_heading,
168             );
169             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
170                 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
171                 my @frameworkselect;
172                 while ( my $authority_type = $authority_types->next ) {
173                     my %row = (
174                         value => $authority_type->authtypecode,
175                         frameworktext => $authority_type->authtypetext,
176                     );
177                     push @frameworkselect, \%row;
178                 }
179                 $template->param(
180                     frameworkselect => \@frameworkselect,
181                     frameworkcode1  => $recordObj1->authtypecode,
182                     frameworkcode2  => $recordObj2->authtypecode,
183                 );
184             }
185         }
186     }
187 }
188
189 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
190 $template->param( authority_types => $authority_types );
191
192 if (@errors) {
193
194     # Errors
195     $template->param( errors => \@errors );
196 }
197
198 output_html_with_http_headers $input, $cookie, $template->output;
199 exit;
200
201 =head1 FUNCTIONS
202
203 =cut
204
205 # ------------------------
206 # Functions
207 # ------------------------