Bug 14544: [QA Follow-up] DBIx::Class changes
[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::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             C4::AuthoritiesMarc::merge( $recordid2, GetAuthority($recordid2), $recordid1, $record );
72             $error = (DelAuthority($recordid2) == 0);
73         }
74         push @errors, $error if ($error);
75     }
76
77     # Parameters
78     $template->param(
79         result  => 1,
80         recordid1 => $recordid1
81     );
82
83     #-------------------------
84     # Show records to merge
85     #-------------------------
86 }
87 else {
88     my $mergereference = $input->param('mergereference');
89     $template->{'VARS'}->{'mergereference'} = $mergereference;
90
91     if ( scalar(@authid) != 2 ) {
92         push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
93     }
94     else {
95         my $recordObj1 = Koha::Authority->get_from_authid($authid[0]) || Koha::Authority->new();
96         my $recordObj2;
97
98         if (defined $mergereference && $mergereference eq 'breeding') {
99             $recordObj2 =  Koha::Authority->get_from_breeding($authid[1]) || Koha::Authority->new();
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, { code => 'FRAMEWORK_NOT_SELECTED' };
110             }
111             else {
112                 $framework = $recordObj1->authtype;
113             }
114             if ($mergereference eq 'breeding') {
115                 $mergereference = $authid[0];
116             }
117
118             # Getting MARC Structure
119             my $tagslib = GetTagsLabels( 1, $framework );
120             foreach my $field ( keys %$tagslib ) {
121                 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
122                     $tagslib->{$field}->{'tab'} = 0;
123                 }
124             }
125
126             #Setting $notreference
127             my $notreference = $authid[1];
128             if($mergereference == $notreference){
129                 $notreference = $authid[0];
130                 #Swap so $recordObj1 is always the correct merge reference
131                 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
132             }
133
134             # Creating a loop for display
135
136             my @record1 = $recordObj1->createMergeHash($tagslib);
137             my @record2 = $recordObj2->createMergeHash($tagslib);
138
139             # Parameters
140             $template->param(
141                 recordid1        => $mergereference,
142                 recordid2        => $notreference,
143                 record1        => @record1,
144                 record2        => @record2,
145                 framework      => $framework,
146             );
147         }
148         else {
149
150             # Ask the user to choose which record will be the kept
151             $template->param(
152                 choosereference => 1,
153                 recordid1         => $authid[0],
154                 recordid2         => $authid[1],
155                 title1          => $recordObj1->authorized_heading,
156                 title2          => $recordObj2->authorized_heading,
157             );
158             if ( $recordObj1->authtype ne $recordObj2->authtype ) {
159                 my $frameworks = getauthtypes;
160                 my @frameworkselect;
161                 foreach my $thisframeworkcode ( keys %$frameworks ) {
162                     my %row = (
163                         value => $thisframeworkcode,
164                         frameworktext =>
165                           $frameworks->{$thisframeworkcode}->{'authtypetext'},
166                     );
167                     if ( $recordObj1->authtype eq $thisframeworkcode ) {
168                         $row{'selected'} = 1;
169                     }
170                     push @frameworkselect, \%row;
171                 }
172                 $template->param(
173                     frameworkselect => \@frameworkselect,
174                     frameworkcode1  => $recordObj1->authtype,
175                     frameworkcode2  => $recordObj2->authtype,
176                     frameworklabel1 => $frameworks->{$recordObj1->authtype}->{'authtypetext'},
177                     frameworklabel2 => $frameworks->{$recordObj2->authtype}->{'authtypetext'},
178                 );
179             }
180         }
181     }
182 }
183
184 my $authtypes = getauthtypes;
185 my @authtypesloop;
186 foreach my $thisauthtype (
187     sort {
188         $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'}
189     }
190     keys %$authtypes
191   )
192 {
193     my %row = (
194         value        => $thisauthtype,
195         authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
196     );
197     push @authtypesloop, \%row;
198 }
199 $template->{VARS}->{authtypesloop} = \@authtypesloop;
200
201 if (@errors) {
202
203     # Errors
204     $template->param( errors => \@errors );
205 }
206
207 output_html_with_http_headers $input, $cookie, $template->output;
208 exit;
209
210 =head1 FUNCTIONS
211
212 =cut
213
214 # ------------------------
215 # Functions
216 # ------------------------