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 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
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.
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.
25 use C4::AuthoritiesMarc;
26 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');
56 my $recordid2 = $input->param('recordid2');
57 my $typecode = $input->param('frameworkcode');
59 # Rewriting the leader
60 $record->leader( GetAuthority($recordid1)->leader() );
62 # Modifying the reference record
63 # This triggers a merge for the biblios attached to $recordid1
64 ModAuthority( $recordid1, $record, $typecode );
66 # Now merge for biblios attached to $recordid2
67 my $MARCfrom = GetAuthority( $recordid2 );
68 merge({ mergefrom => $recordid2, MARCfrom => $MARCfrom, mergeto => $recordid1, MARCto => $record });
70 # Delete the other record. Do not merge. It is unneeded and could under
71 # special circumstances have unwanted side-effects.
72 DelAuthority({ authid => $recordid2, skip_merge => 1 });
77 recordid1 => $recordid1
80 #-------------------------
81 # Show records to merge
82 #-------------------------
85 my $mergereference = $input->param('mergereference');
86 $template->{'VARS'}->{'mergereference'} = $mergereference;
88 if ( scalar(@authid) != 2 ) {
89 push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
92 my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]);
93 Koha::Exceptions::ObjectNotFound->throw( "No authority record found for authid $authid[0]\n" ) if !$recordObj1;
96 if (defined $mergereference && $mergereference eq 'breeding') {
97 $recordObj2 = Koha::MetadataRecord::Authority->get_from_breeding($authid[1]);
99 $recordObj2 = Koha::MetadataRecord::Authority->get_from_authid($authid[1]);
101 Koha::Exceptions::ObjectNotFound->throw( "No authority record found for authid $authid[1]\n" ) if !$recordObj2;
103 if ($mergereference) {
106 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
107 $framework = $input->param('frameworkcode')
108 or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
111 $framework = $recordObj1->authtypecode;
113 if ($mergereference eq 'breeding') {
114 $mergereference = $authid[0];
117 # Getting MARC Structure
118 my $tagslib = GetTagsLabels( 1, $framework );
119 foreach my $field ( keys %$tagslib ) {
120 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
121 $tagslib->{$field}->{'tab'} = 0;
125 #Setting $notreference
126 my $notreference = $authid[1];
127 if($mergereference == $notreference){
128 $notreference = $authid[0];
129 #Swap so $recordObj1 is always the correct merge reference
130 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
133 # Creating a loop for display
137 recordid => $mergereference,
138 record => $recordObj1->record,
139 frameworkcode => $recordObj1->authtypecode,
140 display => $recordObj1->createMergeHash($tagslib),
144 recordid => $notreference,
145 record => $recordObj2->record,
146 frameworkcode => $recordObj2->authtypecode,
147 display => $recordObj2->createMergeHash($tagslib),
153 recordid1 => $mergereference,
154 recordid2 => $notreference,
155 records => \@records,
156 framework => $framework,
161 # Ask the user to choose which record will be the kept
163 choosereference => 1,
164 recordid1 => $authid[0],
165 recordid2 => $authid[1],
166 title1 => $recordObj1->authorized_heading,
167 title2 => $recordObj2->authorized_heading,
169 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
170 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
172 while ( my $authority_type = $authority_types->next ) {
174 value => $authority_type->authtypecode,
175 frameworktext => $authority_type->authtypetext,
177 push @frameworkselect, \%row;
180 frameworkselect => \@frameworkselect,
181 frameworkcode1 => $recordObj1->authtypecode,
182 frameworkcode2 => $recordObj2->authtypecode,
189 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
190 $template->param( authority_types => $authority_types );
195 $template->param( errors => \@errors );
198 output_html_with_http_headers $input, $cookie, $template->output;
205 # ------------------------
207 # ------------------------