Bug 30714: Unit test
[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 qw( output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
24 use C4::AuthoritiesMarc qw( GetAuthority ModAuthority DelAuthority GetTagsLabels merge );
25 use C4::Biblio qw( TransformHtmlToMarc );
26
27 use Koha::Authority::MergeRequests;
28 use Koha::Authority::Types;
29 use Koha::MetadataRecord::Authority;
30
31 my $input  = CGI->new;
32 my @authid = $input->multi_param('authid');
33 my $merge  = $input->param('merge');
34
35 my @errors;
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {
39         template_name   => "authorities/merge.tt",
40         query           => $input,
41         type            => "intranet",
42         flagsrequired   => { editauthorities => 1 },
43     }
44 );
45
46 #------------------------
47 # Merging
48 #------------------------
49 if ($merge) {
50
51     # Creating a new record from the html code
52     my $record   = TransformHtmlToMarc($input, 0);
53     my $recordid1   = $input->param('recordid1') // q{};
54     my $recordid2   = $input->param('recordid2') // q{};
55     my $typecode = $input->param('frameworkcode');
56
57     # Some error checking
58     if( $recordid1 eq $recordid2 ) {
59         push @errors, { code => 'DESTRUCTIVE_MERGE' };
60     } elsif( !$typecode || !Koha::Authority::Types->find($typecode) ) {
61         push @errors, { code => 'WRONG_FRAMEWORK' };
62     } elsif( scalar $record->fields == 0 ) {
63         push @errors, { code => 'EMPTY_MARC' };
64     }
65     if( @errors ) {
66         $template->param( errors => \@errors );
67         output_html_with_http_headers $input, $cookie, $template->output;
68         exit;
69     }
70
71     # Rewriting the leader
72     if( my $authrec = GetAuthority($recordid1) ) {
73         $record->leader( $authrec->leader() );
74     }
75
76     # Modifying the reference record
77     # This triggers a merge for the biblios attached to $recordid1
78     ModAuthority( $recordid1, $record, $typecode );
79
80     # Now merge for biblios attached to $recordid2
81     my $MARCfrom = GetAuthority( $recordid2 );
82     merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
83
84     # Delete the other record. No need to merge.
85     DelAuthority({ authid => $recordid2, skip_merge => 1 });
86
87     # Parameters
88     $template->param(
89         result  => 1,
90         recordid1 => $recordid1
91     );
92
93     #-------------------------
94     # Show records to merge
95     #-------------------------
96 }
97 else {
98     my $mergereference = $input->param('mergereference');
99     $template->{'VARS'}->{'mergereference'} = $mergereference;
100
101     if ( scalar(@authid) != 2 ) {
102         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
103     } elsif( $authid[0] eq $authid[1] ) {
104         push @errors, { code => 'DESTRUCTIVE_MERGE' };
105     } else {
106         my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
107         if (!$recordObj1) {
108             push @errors, { code => "MISSING_RECORD", value => $authid[0] };
109         }
110
111
112         my $recordObj2;
113         if (defined $mergereference && $mergereference eq 'breeding') {
114             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
115         } else {
116             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
117         }
118         if (!$recordObj2) {
119             push @errors, { code => "MISSING_RECORD", value => $authid[1] };
120         }
121
122         unless ( $recordObj1 && $recordObj2 ) {
123             if (@errors) {
124                 $template->param( errors => \@errors );
125             }
126             output_html_with_http_headers $input, $cookie, $template->output;
127             exit;
128         }
129
130         if ($mergereference ) {
131
132             my $framework;
133             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
134                 $framework = $input->param('frameworkcode');
135             }
136             else {
137                 $framework = $recordObj1->authtypecode;
138             }
139             if ($mergereference eq 'breeding') {
140                 $mergereference = $authid[0];
141             }
142
143             # Getting MARC Structure
144             my $tagslib = GetTagsLabels( 1, $framework );
145             foreach my $field ( keys %$tagslib ) {
146                 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
147                     $tagslib->{$field}->{'tab'} = 0;
148                 }
149             }
150
151             #Setting $notreference
152             my $notreference = $authid[1];
153             if($mergereference == $notreference){
154                 $notreference = $authid[0];
155                 #Swap so $recordObj1 is always the correct merge reference
156                 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
157             }
158
159             # Creating a loop for display
160
161             my @records = (
162                 {
163                     recordid => $mergereference,
164                     record => $recordObj1->record,
165                     frameworkcode => $recordObj1->authtypecode,
166                     display => $recordObj1->createMergeHash($tagslib),
167                     reference => 1,
168                 },
169                 {
170                     recordid => $notreference,
171                     record => $recordObj2->record,
172                     frameworkcode => $recordObj2->authtypecode,
173                     display => $recordObj2->createMergeHash($tagslib),
174                 },
175             );
176
177             # Parameters
178             $template->param(
179                 recordid1        => $mergereference,
180                 recordid2        => $notreference,
181                 records        => \@records,
182                 framework      => $framework,
183             );
184         }
185         else {
186
187             # Ask the user to choose which record will be the kept
188             $template->param(
189                 choosereference => 1,
190                 recordid1         => $authid[0],
191                 recordid2         => $authid[1],
192                 title1          => $recordObj1->authorized_heading,
193                 title2          => $recordObj2->authorized_heading,
194             );
195             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
196                 my $authority_types = Koha::Authority::Types->search( { authtypecode => { '!=' => '' } }, { order_by => ['authtypetext'] } );
197                 $template->param(
198                     frameworkselect => $authority_types->unblessed,
199                     frameworkcode1  => $recordObj1->authtypecode,
200                     frameworkcode2  => $recordObj2->authtypecode,
201                 );
202             }
203         }
204     }
205 }
206
207 if (@errors) {
208     $template->param( errors => \@errors );
209 }
210 output_html_with_http_headers $input, $cookie, $template->output;