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