Bug 26268: (QA follow-up) Split DB update checks
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Output;
23 use C4::Auth;
24 use C4::AuthoritiesMarc;
25 use C4::Koha;
26 use C4::Biblio;
27
28 use Koha::Authority::MergeRequests;
29 use Koha::Authority::Types;
30 use Koha::MetadataRecord::Authority;
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         flagsrequired   => { editauthorities => 1 },
44     }
45 );
46
47 #------------------------
48 # Merging
49 #------------------------
50 if ($merge) {
51
52     # Creating a new record from the html code
53     my $record   = TransformHtmlToMarc($input, 0);
54     my $recordid1   = $input->param('recordid1') // q{};
55     my $recordid2   = $input->param('recordid2') // q{};
56     my $typecode = $input->param('frameworkcode');
57
58     # Some error checking
59     if( $recordid1 eq $recordid2 ) {
60         push @errors, { code => 'DESTRUCTIVE_MERGE' };
61     } elsif( !$typecode || !Koha::Authority::Types->find($typecode) ) {
62         push @errors, { code => 'WRONG_FRAMEWORK' };
63     } elsif( scalar $record->fields == 0 ) {
64         push @errors, { code => 'EMPTY_MARC' };
65     }
66     if( @errors ) {
67         $template->param( errors => \@errors );
68         output_html_with_http_headers $input, $cookie, $template->output;
69         exit;
70     }
71
72     # Rewriting the leader
73     if( my $authrec = GetAuthority($recordid1) ) {
74         $record->leader( $authrec->leader() );
75     }
76
77     # Modifying the reference record
78     # This triggers a merge for the biblios attached to $recordid1
79     ModAuthority( $recordid1, $record, $typecode );
80
81     # Now merge for biblios attached to $recordid2
82     my $MARCfrom = GetAuthority( $recordid2 );
83     merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
84
85     # Delete the other record. No need to merge.
86     DelAuthority({ authid => $recordid2, skip_merge => 1 });
87
88     # Parameters
89     $template->param(
90         result  => 1,
91         recordid1 => $recordid1
92     );
93
94     #-------------------------
95     # Show records to merge
96     #-------------------------
97 }
98 else {
99     my $mergereference = $input->param('mergereference');
100     $template->{'VARS'}->{'mergereference'} = $mergereference;
101
102     if ( scalar(@authid) != 2 ) {
103         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
104     } elsif( $authid[0] eq $authid[1] ) {
105         push @errors, { code => 'DESTRUCTIVE_MERGE' };
106     } else {
107         my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
108         if (!$recordObj1) {
109             push @errors, { code => "MISSING_RECORD", value => $authid[0] };
110         }
111
112
113         my $recordObj2;
114         if (defined $mergereference && $mergereference eq 'breeding') {
115             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
116         } else {
117             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
118         }
119         if (!$recordObj2) {
120             push @errors, { code => "MISSING_RECORD", value => $authid[1] };
121         }
122
123         unless ( $recordObj1 && $recordObj2 ) {
124             if (@errors) {
125                 $template->param( errors => \@errors );
126             }
127             output_html_with_http_headers $input, $cookie, $template->output;
128             exit;
129         }
130
131         if ($mergereference ) {
132
133             my $framework;
134             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
135                 $framework = $input->param('frameworkcode');
136             }
137             else {
138                 $framework = $recordObj1->authtypecode;
139             }
140             if ($mergereference eq 'breeding') {
141                 $mergereference = $authid[0];
142             }
143
144             # Getting MARC Structure
145             my $tagslib = GetTagsLabels( 1, $framework );
146             foreach my $field ( keys %$tagslib ) {
147                 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
148                     $tagslib->{$field}->{'tab'} = 0;
149                 }
150             }
151
152             #Setting $notreference
153             my $notreference = $authid[1];
154             if($mergereference == $notreference){
155                 $notreference = $authid[0];
156                 #Swap so $recordObj1 is always the correct merge reference
157                 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
158             }
159
160             # Creating a loop for display
161
162             my @records = (
163                 {
164                     recordid => $mergereference,
165                     record => $recordObj1->record,
166                     frameworkcode => $recordObj1->authtypecode,
167                     display => $recordObj1->createMergeHash($tagslib),
168                     reference => 1,
169                 },
170                 {
171                     recordid => $notreference,
172                     record => $recordObj2->record,
173                     frameworkcode => $recordObj2->authtypecode,
174                     display => $recordObj2->createMergeHash($tagslib),
175                 },
176             );
177
178             # Parameters
179             $template->param(
180                 recordid1        => $mergereference,
181                 recordid2        => $notreference,
182                 records        => \@records,
183                 framework      => $framework,
184             );
185         }
186         else {
187
188             # Ask the user to choose which record will be the kept
189             $template->param(
190                 choosereference => 1,
191                 recordid1         => $authid[0],
192                 recordid2         => $authid[1],
193                 title1          => $recordObj1->authorized_heading,
194                 title2          => $recordObj2->authorized_heading,
195             );
196             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
197                 my $authority_types = Koha::Authority::Types->search( { authtypecode => { '!=' => '' } }, { order_by => ['authtypetext'] } );
198                 $template->param(
199                     frameworkselect => $authority_types->unblessed,
200                     frameworkcode1  => $recordObj1->authtypecode,
201                     frameworkcode2  => $recordObj2->authtypecode,
202                 );
203             }
204         }
205     }
206 }
207
208 if (@errors) {
209     $template->param( errors => \@errors );
210 }
211 output_html_with_http_headers $input, $cookie, $template->output;