Bug 5202: merge authorities from the authority file and reservoir
[koha_fer] / 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 2 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;
23 use C4::Output;
24 use C4::Auth;
25 use C4::AuthoritiesMarc;
26 use Koha::Authority;
27 use C4::Koha;
28 use C4::Biblio;
29
30 my $input  = new CGI;
31 my @authid = $input->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);
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     ModAuthority( $recordid1, $record, $typecode );
62
63     # Deleting the other record
64     if ( scalar(@errors) == 0 ) {
65
66         my $error;
67         if ($input->param('mergereference') eq 'breeding') {
68             require C4::ImportBatch;
69             C4::ImportBatch::SetImportRecordStatus( $recordid2, 'imported' );
70         } else {
71             $error = (DelAuthority($recordid2) == 0);
72         }
73         push @errors, $error if ($error);
74     }
75
76     # Parameters
77     $template->param(
78         result  => 1,
79         recordid1 => $recordid1
80     );
81
82     #-------------------------
83     # Show records to merge
84     #-------------------------
85 }
86 else {
87     my $mergereference = $input->param('mergereference');
88     $template->{'VARS'}->{'mergereference'} = $mergereference;
89
90     if ( scalar(@authid) != 2 ) {
91         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
92     }
93     else {
94         my $recordObj1 = Koha::Authority->get_from_authid($authid[0]) || Koha::Authority->new();
95         my $recordObj2;
96
97         if ($mergereference eq 'breeding') {
98             $recordObj2 =  Koha::Authority->get_from_breeding($authid[1]) || Koha::Authority->new();
99             $mergereference = $authid[0];
100         } else {
101             $recordObj2 =  Koha::Authority->get_from_authid($authid[1]) || Koha::Authority->new();
102         }
103
104         if ($mergereference) {
105
106             my $framework;
107             if ( $recordObj1->authtype ne $recordObj2->authtype && $mergereference ne 'breeding' ) {
108                 $framework = $input->param('frameworkcode')
109                   or push @errors, "Framework not selected.";
110             }
111             else {
112                 $framework = $recordObj1->authtype;
113             }
114
115             # Getting MARC Structure
116             my $tagslib = GetTagsLabels( 1, $framework );
117
118             my $notreference =
119               ( $authid[0] == $mergereference )
120               ? $authid[1]
121               : $authid[0];
122
123             # Creating a loop for display
124
125             my @record1 = $recordObj1->createMergeHash($tagslib);
126             my @record2 = $recordObj2->createMergeHash($tagslib);
127
128             # Parameters
129             $template->param(
130                 recordid1        => $mergereference,
131                 recordid2        => $notreference,
132                 record1        => @record1,
133                 record2        => @record2,
134                 framework      => $framework,
135             );
136         }
137         else {
138
139             # Ask the user to choose which record will be the kept
140             $template->param(
141                 choosereference => 1,
142                 recordid1         => $authid[0],
143                 recordid2         => $authid[1],
144                 title1          => $recordObj1->authorized_heading,
145                 title2          => $recordObj2->authorized_heading,
146             );
147             if ( $recordObj1->authtype ne $recordObj2->authtype ) {
148                 my $frameworks = getauthtypes;
149                 my @frameworkselect;
150                 foreach my $thisframeworkcode ( keys %$frameworks ) {
151                     my %row = (
152                         value => $thisframeworkcode,
153                         frameworktext =>
154                           $frameworks->{$thisframeworkcode}->{'authtypetext'},
155                     );
156                     if ( $recordObj1->authtype eq $thisframeworkcode ) {
157                         $row{'selected'} = 1;
158                     }
159                     push @frameworkselect, \%row;
160                 }
161                 $template->param(
162                     frameworkselect => \@frameworkselect,
163                     frameworkcode1  => $recordObj1->authtype,
164                     frameworkcode2  => $recordObj2->authtype,
165                 );
166             }
167         }
168     }
169 }
170
171 if (@errors) {
172
173     # Errors
174     $template->param( errors => \@errors );
175 }
176
177 output_html_with_http_headers $input, $cookie, $template->output;
178 exit;
179
180 =head1 FUNCTIONS
181
182 =cut
183
184 # ------------------------
185 # Functions
186 # ------------------------