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