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