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>.
22 use C4::Output qw( output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
24 use C4::AuthoritiesMarc qw( GetAuthority ModAuthority DelAuthority GetTagsLabels merge );
25 use C4::Biblio qw( TransformHtmlToMarc );
27 use Koha::Authority::MergeRequests;
28 use Koha::Authority::Types;
29 use Koha::MetadataRecord::Authority;
32 my @authid = $input->multi_param('authid');
33 my $merge = $input->param('merge');
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 template_name => "authorities/merge.tt",
42 flagsrequired => { editauthorities => 1 },
46 #------------------------
48 #------------------------
51 # Creating a new record from the html code
52 my $record = TransformHtmlToMarc($input, 0);
53 my $recordid1 = $input->param('recordid1') // q{};
54 my $recordid2 = $input->param('recordid2') // q{};
55 my $typecode = $input->param('frameworkcode');
58 if( $recordid1 eq $recordid2 ) {
59 push @errors, { code => 'DESTRUCTIVE_MERGE' };
60 } elsif( !$typecode || !Koha::Authority::Types->find($typecode) ) {
61 push @errors, { code => 'WRONG_FRAMEWORK' };
62 } elsif( scalar $record->fields == 0 ) {
63 push @errors, { code => 'EMPTY_MARC' };
66 $template->param( errors => \@errors );
67 output_html_with_http_headers $input, $cookie, $template->output;
71 # Rewriting the leader
72 if( my $authrec = GetAuthority($recordid1) ) {
73 $record->leader( $authrec->leader() );
76 # Modifying the reference record
77 # This triggers a merge for the biblios attached to $recordid1
78 ModAuthority( $recordid1, $record, $typecode );
80 # Now merge for biblios attached to $recordid2
81 my $MARCfrom = GetAuthority( $recordid2 );
82 merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
84 # Delete the other record. No need to merge.
85 DelAuthority({ authid => $recordid2, skip_merge => 1 });
90 recordid1 => $recordid1
93 #-------------------------
94 # Show records to merge
95 #-------------------------
98 my $mergereference = $input->param('mergereference');
99 $template->{'VARS'}->{'mergereference'} = $mergereference;
101 if ( scalar(@authid) != 2 ) {
102 push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
103 } elsif( $authid[0] eq $authid[1] ) {
104 push @errors, { code => 'DESTRUCTIVE_MERGE' };
106 my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
108 push @errors, { code => "MISSING_RECORD", value => $authid[0] };
113 if (defined $mergereference && $mergereference eq 'breeding') {
114 $recordObj2 = Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
116 $recordObj2 = Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
119 push @errors, { code => "MISSING_RECORD", value => $authid[1] };
122 unless ( $recordObj1 && $recordObj2 ) {
124 $template->param( errors => \@errors );
126 output_html_with_http_headers $input, $cookie, $template->output;
130 if ($mergereference ) {
133 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
134 $framework = $input->param('frameworkcode');
137 $framework = $recordObj1->authtypecode;
139 if ($mergereference eq 'breeding') {
140 $mergereference = $authid[0];
143 # Getting MARC Structure
144 my $tagslib = GetTagsLabels( 1, $framework );
145 foreach my $field ( keys %$tagslib ) {
146 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
147 $tagslib->{$field}->{'tab'} = 0;
151 #Setting $notreference
152 my $notreference = $authid[1];
153 if($mergereference == $notreference){
154 $notreference = $authid[0];
155 #Swap so $recordObj1 is always the correct merge reference
156 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
159 # Creating a loop for display
163 recordid => $mergereference,
164 record => $recordObj1->record,
165 frameworkcode => $recordObj1->authtypecode,
166 display => $recordObj1->createMergeHash($tagslib),
170 recordid => $notreference,
171 record => $recordObj2->record,
172 frameworkcode => $recordObj2->authtypecode,
173 display => $recordObj2->createMergeHash($tagslib),
179 recordid1 => $mergereference,
180 recordid2 => $notreference,
181 records => \@records,
182 framework => $framework,
187 # Ask the user to choose which record will be the kept
189 choosereference => 1,
190 recordid1 => $authid[0],
191 recordid2 => $authid[1],
192 title1 => $recordObj1->authorized_heading,
193 title2 => $recordObj2->authorized_heading,
195 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
196 my $authority_types = Koha::Authority::Types->search( { authtypecode => { '!=' => '' } }, { order_by => ['authtypetext'] } );
198 frameworkselect => $authority_types->unblessed,
199 frameworkcode1 => $recordObj1->authtypecode,
200 frameworkcode2 => $recordObj2->authtypecode,
208 $template->param( errors => \@errors );
210 output_html_with_http_headers $input, $cookie, $template->output;