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