Bug 9988: Remove further references to dontmerge
[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 my $input  = new CGI;
31 my @authid = $input->multi_param('authid');
32 my $merge  = $input->param('merge');
33
34 my @errors;
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "authorities/merge.tt",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
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');
54     my $recordid2   = $input->param('recordid2');
55     my $typecode = $input->param('frameworkcode');
56
57     # Rewriting the leader
58     $record->leader( GetAuthority($recordid1)->leader() );
59
60     # Modifying the reference record
61     # This triggers a merge for the biblios attached to $recordid1
62     ModAuthority( $recordid1, $record, $typecode );
63
64     # Now merge for biblios attached to $recordid2
65     my $MARCfrom = GetAuthority( $recordid2 );
66     merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
67
68     # Deleting the other record
69     DelAuthority({ authid => $recordid2 });
70
71     # Parameters
72     $template->param(
73         result  => 1,
74         recordid1 => $recordid1
75     );
76
77     #-------------------------
78     # Show records to merge
79     #-------------------------
80 }
81 else {
82     my $mergereference = $input->param('mergereference');
83     $template->{'VARS'}->{'mergereference'} = $mergereference;
84
85     if ( scalar(@authid) != 2 ) {
86         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
87     }
88     else {
89         my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]) || Koha::MetadataRecord::Authority->new();
90         my $recordObj2;
91
92         if (defined $mergereference && $mergereference eq 'breeding') {
93             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_breeding($authid[1]) || Koha::MetadataRecord::Authority->new();
94         } else {
95             $recordObj2 =  Koha::MetadataRecord::Authority->get_from_authid($authid[1]) || Koha::MetadataRecord::Authority->new();
96         }
97
98         if ($mergereference) {
99
100             my $framework;
101             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
102                 $framework = $input->param('frameworkcode')
103                   or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
104             }
105             else {
106                 $framework = $recordObj1->authtypecode;
107             }
108             if ($mergereference eq 'breeding') {
109                 $mergereference = $authid[0];
110             }
111
112             # Getting MARC Structure
113             my $tagslib = GetTagsLabels( 1, $framework );
114             foreach my $field ( keys %$tagslib ) {
115                 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
116                     $tagslib->{$field}->{'tab'} = 0;
117                 }
118             }
119
120             #Setting $notreference
121             my $notreference = $authid[1];
122             if($mergereference == $notreference){
123                 $notreference = $authid[0];
124                 #Swap so $recordObj1 is always the correct merge reference
125                 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
126             }
127
128             # Creating a loop for display
129
130             my @records = (
131                 {
132                     recordid => $mergereference,
133                     record => $recordObj1->record,
134                     frameworkcode => $recordObj1->authtypecode,
135                     display => $recordObj1->createMergeHash($tagslib),
136                     reference => 1,
137                 },
138                 {
139                     recordid => $notreference,
140                     record => $recordObj2->record,
141                     frameworkcode => $recordObj2->authtypecode,
142                     display => $recordObj2->createMergeHash($tagslib),
143                 },
144             );
145
146             # Parameters
147             $template->param(
148                 recordid1        => $mergereference,
149                 recordid2        => $notreference,
150                 records        => \@records,
151                 framework      => $framework,
152             );
153         }
154         else {
155
156             # Ask the user to choose which record will be the kept
157             $template->param(
158                 choosereference => 1,
159                 recordid1         => $authid[0],
160                 recordid2         => $authid[1],
161                 title1          => $recordObj1->authorized_heading,
162                 title2          => $recordObj2->authorized_heading,
163             );
164             if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
165                 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
166                 my @frameworkselect;
167                 while ( my $authority_type = $authority_types->next ) {
168                     my %row = (
169                         value => $authority_type->authtypecode,
170                         frameworktext => $authority_type->authtypetext,
171                     );
172                     push @frameworkselect, \%row;
173                 }
174                 $template->param(
175                     frameworkselect => \@frameworkselect,
176                     frameworkcode1  => $recordObj1->authtypecode,
177                     frameworkcode2  => $recordObj2->authtypecode,
178                 );
179             }
180         }
181     }
182 }
183
184 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
185 $template->param( authority_types => $authority_types );
186
187 if (@errors) {
188
189     # Errors
190     $template->param( errors => \@errors );
191 }
192
193 output_html_with_http_headers $input, $cookie, $template->output;
194 exit;
195
196 =head1 FUNCTIONS
197
198 =cut
199
200 # ------------------------
201 # Functions
202 # ------------------------