3 # Copyright 2013 C & P Bibliography Services
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::AuthoritiesMarc;
28 use Koha::Authority::MergeRequests;
29 use Koha::Authority::Types;
30 use Koha::MetadataRecord::Authority;
33 my @authid = $input->multi_param('authid');
34 my $merge = $input->param('merge');
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40 template_name => "authorities/merge.tt",
44 flagsrequired => { editauthorities => 1 },
48 #------------------------
50 #------------------------
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');
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' };
68 $template->param( errors => \@errors );
69 output_html_with_http_headers $input, $cookie, $template->output;
73 # Rewriting the leader
74 if( my $authrec = GetAuthority($recordid1) ) {
75 $record->leader( $authrec->leader() );
78 # Modifying the reference record
79 # This triggers a merge for the biblios attached to $recordid1
80 ModAuthority( $recordid1, $record, $typecode );
82 # Now merge for biblios attached to $recordid2
83 my $MARCfrom = GetAuthority( $recordid2 );
84 merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
86 # Delete the other record. Do not merge. It is unneeded and could under
87 # special circumstances have unwanted side-effects.
88 # Remove older pending merge requests for $recordid2 to itself. The above merge did the job already or will do. (See bug 22437)
89 my $condition = { authid => $recordid2, authid_new => [undef, 0, $recordid2], done => 0 };
90 Koha::Authority::MergeRequests->search($condition)->delete;
91 DelAuthority({ authid => $recordid2, skip_merge => 1 });
96 recordid1 => $recordid1
99 #-------------------------
100 # Show records to merge
101 #-------------------------
104 my $mergereference = $input->param('mergereference');
105 $template->{'VARS'}->{'mergereference'} = $mergereference;
107 if ( scalar(@authid) != 2 ) {
108 push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
109 } elsif( $authid[0] eq $authid[1] ) {
110 push @errors, { code => 'DESTRUCTIVE_MERGE' };
112 my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
114 push @errors, { code => "MISSING_RECORD", value => $authid[0] };
119 if (defined $mergereference && $mergereference eq 'breeding') {
120 $recordObj2 = Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
122 $recordObj2 = Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
125 push @errors, { code => "MISSING_RECORD", value => $authid[1] };
128 unless ( $recordObj1 && $recordObj2 ) {
130 $template->param( errors => \@errors );
132 output_html_with_http_headers $input, $cookie, $template->output;
136 if ($mergereference ) {
139 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
140 $framework = $input->param('frameworkcode');
143 $framework = $recordObj1->authtypecode;
145 if ($mergereference eq 'breeding') {
146 $mergereference = $authid[0];
149 # Getting MARC Structure
150 my $tagslib = GetTagsLabels( 1, $framework );
151 foreach my $field ( keys %$tagslib ) {
152 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
153 $tagslib->{$field}->{'tab'} = 0;
157 #Setting $notreference
158 my $notreference = $authid[1];
159 if($mergereference == $notreference){
160 $notreference = $authid[0];
161 #Swap so $recordObj1 is always the correct merge reference
162 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
165 # Creating a loop for display
169 recordid => $mergereference,
170 record => $recordObj1->record,
171 frameworkcode => $recordObj1->authtypecode,
172 display => $recordObj1->createMergeHash($tagslib),
176 recordid => $notreference,
177 record => $recordObj2->record,
178 frameworkcode => $recordObj2->authtypecode,
179 display => $recordObj2->createMergeHash($tagslib),
185 recordid1 => $mergereference,
186 recordid2 => $notreference,
187 records => \@records,
188 framework => $framework,
193 # Ask the user to choose which record will be the kept
195 choosereference => 1,
196 recordid1 => $authid[0],
197 recordid2 => $authid[1],
198 title1 => $recordObj1->authorized_heading,
199 title2 => $recordObj2->authorized_heading,
201 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
202 my $authority_types = Koha::Authority::Types->search( { authtypecode => { '!=' => '' } }, { order_by => ['authtypetext'] } );
204 frameworkselect => $authority_types->unblessed,
205 frameworkcode1 => $recordObj1->authtypecode,
206 frameworkcode2 => $recordObj2->authtypecode,
214 $template->param( errors => \@errors );
216 output_html_with_http_headers $input, $cookie, $template->output;