Revert "Bug 17249: Remove GetKohaAuthorisedValuesFromField - Add classes MarcSubfield...
[koha.git] / authorities / merge.pl
blobb423122efd63ab7bd775542e1bc74efdf4296ef7
1 #!/usr/bin/perl
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
10 # version.
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.
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 Koha::MetadataRecord::Authority;
27 use C4::Koha;
28 use C4::Biblio;
30 my $input = new CGI;
31 my @authid = $input->multi_param('authid');
32 my $merge = $input->param('merge');
34 my @errors;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 template_name => "authorities/merge.tt",
39 query => $input,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => { editauthorities => 1 },
46 #------------------------
47 # Merging
48 #------------------------
49 if ($merge) {
51 # Creating a new record from the html code
52 my $record = TransformHtmlToMarc($input, 0);
53 my $recordid1 = $input->param('recordid1');
54 my $recordid2 = $input->param('recordid2');
55 my $typecode = $input->param('frameworkcode');
57 # Rewriting the leader
58 $record->leader( GetAuthority($recordid1)->leader() );
60 # Modifying the reference record
61 ModAuthority( $recordid1, $record, $typecode );
63 # Deleting the other record
64 if ( scalar(@errors) == 0 ) {
66 my $error;
67 if ($input->param('mergereference') eq 'breeding') {
68 require C4::ImportBatch;
69 C4::ImportBatch::SetImportRecordStatus( $recordid2, 'imported' );
70 } else {
71 C4::AuthoritiesMarc::merge( $recordid2, GetAuthority($recordid2), $recordid1, $record );
72 $error = (DelAuthority($recordid2) == 0);
74 push @errors, $error if ($error);
77 # Parameters
78 $template->param(
79 result => 1,
80 recordid1 => $recordid1
83 #-------------------------
84 # Show records to merge
85 #-------------------------
87 else {
88 my $mergereference = $input->param('mergereference');
89 $template->{'VARS'}->{'mergereference'} = $mergereference;
91 if ( scalar(@authid) != 2 ) {
92 push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
94 else {
95 my $recordObj1 = Koha::MetadataRecord::Authority->get_from_authid($authid[0]) || Koha::MetadataRecord::Authority->new();
96 my $recordObj2;
98 if (defined $mergereference && $mergereference eq 'breeding') {
99 $recordObj2 = Koha::MetadataRecord::Authority->get_from_breeding($authid[1]) || Koha::MetadataRecord::Authority->new();
100 } else {
101 $recordObj2 = Koha::MetadataRecord::Authority->get_from_authid($authid[1]) || Koha::MetadataRecord::Authority->new();
104 if ($mergereference) {
106 my $framework;
107 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode && $mergereference ne 'breeding' ) {
108 $framework = $input->param('frameworkcode')
109 or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
111 else {
112 $framework = $recordObj1->authtypecode;
114 if ($mergereference eq 'breeding') {
115 $mergereference = $authid[0];
118 # Getting MARC Structure
119 my $tagslib = GetTagsLabels( 1, $framework );
120 foreach my $field ( keys %$tagslib ) {
121 if ( defined $tagslib->{$field}->{'tab'} && $tagslib->{$field}->{'tab'} eq ' ' ) {
122 $tagslib->{$field}->{'tab'} = 0;
126 #Setting $notreference
127 my $notreference = $authid[1];
128 if($mergereference == $notreference){
129 $notreference = $authid[0];
130 #Swap so $recordObj1 is always the correct merge reference
131 ($recordObj1, $recordObj2) = ($recordObj2, $recordObj1);
134 # Creating a loop for display
136 my @records = (
138 recordid => $mergereference,
139 record => $recordObj1->record,
140 frameworkcode => $recordObj1->authtypecode,
141 display => $recordObj1->createMergeHash($tagslib),
142 reference => 1,
145 recordid => $notreference,
146 record => $recordObj2->record,
147 frameworkcode => $recordObj2->authtypecode,
148 display => $recordObj2->createMergeHash($tagslib),
152 # Parameters
153 $template->param(
154 recordid1 => $mergereference,
155 recordid2 => $notreference,
156 records => \@records,
157 framework => $framework,
160 else {
162 # Ask the user to choose which record will be the kept
163 $template->param(
164 choosereference => 1,
165 recordid1 => $authid[0],
166 recordid2 => $authid[1],
167 title1 => $recordObj1->authorized_heading,
168 title2 => $recordObj2->authorized_heading,
170 if ( $recordObj1->authtypecode ne $recordObj2->authtypecode ) {
171 my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypecode'] } );
172 my @frameworkselect;
173 while ( my $authority_type = $authority_types->next ) {
174 my %row = (
175 value => $authority_type->authtypecode,
176 frameworktext => $authority_type->authtypetext,
178 push @frameworkselect, \%row;
180 $template->param(
181 frameworkselect => \@frameworkselect,
182 frameworkcode1 => $recordObj1->authtypecode,
183 frameworkcode2 => $recordObj2->authtypecode,
190 my $authority_types = Koha::Authority::Types->search({}, { order_by => ['authtypetext']});
191 $template->param( authority_types => $authority_types );
193 if (@errors) {
195 # Errors
196 $template->param( errors => \@errors );
199 output_html_with_http_headers $input, $cookie, $template->output;
200 exit;
202 =head1 FUNCTIONS
204 =cut
206 # ------------------------
207 # Functions
208 # ------------------------