Bug 5202: QA follow-up - improve merge reference selection
[koha.git] / authorities / merge.pl
blob5682b5e9ce25d9748be92e0f933b5ed9cbea25b2
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 2 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;
23 use C4::Output;
24 use C4::Auth;
25 use C4::AuthoritiesMarc;
26 use Koha::Authority;
27 use C4::Koha;
28 use C4::Biblio;
30 my $input = new CGI;
31 my @authid = $input->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);
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 $error = (DelAuthority($recordid2) == 0);
73 push @errors, $error if ($error);
76 # Parameters
77 $template->param(
78 result => 1,
79 recordid1 => $recordid1
82 #-------------------------
83 # Show records to merge
84 #-------------------------
86 else {
87 my $mergereference = $input->param('mergereference');
88 $template->{'VARS'}->{'mergereference'} = $mergereference;
90 if ( scalar(@authid) != 2 ) {
91 push @errors, { code => "WRONG_COUNT", value => scalar(@authid) };
93 else {
94 my $recordObj1 = Koha::Authority->get_from_authid($authid[0]) || Koha::Authority->new();
95 my $recordObj2;
97 if (defined $mergereference && $mergereference eq 'breeding') {
98 $recordObj2 = Koha::Authority->get_from_breeding($authid[1]) || Koha::Authority->new();
99 } else {
100 $recordObj2 = Koha::Authority->get_from_authid($authid[1]) || Koha::Authority->new();
103 if ($mergereference) {
105 my $framework;
106 if ( $recordObj1->authtype ne $recordObj2->authtype && $mergereference ne 'breeding' ) {
107 $framework = $input->param('frameworkcode')
108 or push @errors, { code => 'FRAMEWORK_NOT_SELECTED' };
110 else {
111 $framework = $recordObj1->authtype;
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 my $notreference =
126 ( $authid[0] == $mergereference )
127 ? $authid[1]
128 : $authid[0];
130 # Creating a loop for display
132 my @record1 = $recordObj1->createMergeHash($tagslib);
133 my @record2 = $recordObj2->createMergeHash($tagslib);
135 # Parameters
136 $template->param(
137 recordid1 => $mergereference,
138 recordid2 => $notreference,
139 record1 => @record1,
140 record2 => @record2,
141 framework => $framework,
144 else {
146 # Ask the user to choose which record will be the kept
147 $template->param(
148 choosereference => 1,
149 recordid1 => $authid[0],
150 recordid2 => $authid[1],
151 title1 => $recordObj1->authorized_heading,
152 title2 => $recordObj2->authorized_heading,
154 if ( $recordObj1->authtype ne $recordObj2->authtype ) {
155 my $frameworks = getauthtypes;
156 my @frameworkselect;
157 foreach my $thisframeworkcode ( keys %$frameworks ) {
158 my %row = (
159 value => $thisframeworkcode,
160 frameworktext =>
161 $frameworks->{$thisframeworkcode}->{'authtypetext'},
163 if ( $recordObj1->authtype eq $thisframeworkcode ) {
164 $row{'selected'} = 1;
166 push @frameworkselect, \%row;
168 $template->param(
169 frameworkselect => \@frameworkselect,
170 frameworkcode1 => $recordObj1->authtype,
171 frameworkcode2 => $recordObj2->authtype,
172 frameworklabel1 => $frameworks->{$recordObj1->authtype}->{'authtypetext'},
173 frameworklabel2 => $frameworks->{$recordObj2->authtype}->{'authtypetext'},
180 my $authtypes = getauthtypes;
181 my @authtypesloop;
182 foreach my $thisauthtype (
183 sort {
184 $authtypes->{$a}{'authtypetext'} cmp $authtypes->{$b}{'authtypetext'}
186 keys %$authtypes
189 my %row = (
190 value => $thisauthtype,
191 authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
193 push @authtypesloop, \%row;
195 $template->{VARS}->{authtypesloop} = \@authtypesloop;
197 if (@errors) {
199 # Errors
200 $template->param( errors => \@errors );
203 output_html_with_http_headers $input, $cookie, $template->output;
204 exit;
206 =head1 FUNCTIONS
208 =cut
210 # ------------------------
211 # Functions
212 # ------------------------