Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2013 C & P Bibliography Services |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
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. |
11 |
# |
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. |
15 |
# |
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. |
19 |
|
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 Koha::Record; |
29 |
use C4::Biblio; |
30 |
|
31 |
my $input = new CGI; |
32 |
my @authid = $input->param('authid'); |
33 |
my $merge = $input->param('merge'); |
34 |
|
35 |
my @errors; |
36 |
|
37 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
38 |
{ |
39 |
template_name => "authorities/merge.tmpl", |
40 |
query => $input, |
41 |
type => "intranet", |
42 |
authnotrequired => 0, |
43 |
flagsrequired => { editauthorities => 1 }, |
44 |
} |
45 |
); |
46 |
|
47 |
#------------------------ |
48 |
# Merging |
49 |
#------------------------ |
50 |
if ($merge) { |
51 |
|
52 |
# Creating a new record from the html code |
53 |
my $record = TransformHtmlToMarc($input); |
54 |
my $recordid1 = $input->param('recordid1'); |
55 |
my $recordid2 = $input->param('recordid2'); |
56 |
my $typecode = $input->param('frameworkcode'); |
57 |
|
58 |
# Rewriting the leader |
59 |
$record->leader( GetAuthority($recordid1)->leader() ); |
60 |
|
61 |
# Modifying the reference record |
62 |
ModAuthority( $recordid1, $record, $typecode ); |
63 |
|
64 |
# Deleting the other record |
65 |
if ( scalar(@errors) == 0 ) { |
66 |
|
67 |
my $error; |
68 |
if ($input->param('mergereference') eq 'breeding') { |
69 |
require C4::ImportBatch; |
70 |
C4::ImportBatch::SetImportRecordStatus( $recordid2, 'imported' ); |
71 |
} else { |
72 |
$error = DelAuthority($recordid2); |
73 |
} |
74 |
push @errors, $error if ($error); |
75 |
} |
76 |
|
77 |
# Parameters |
78 |
$template->param( |
79 |
result => 1, |
80 |
recordid1 => $recordid1 |
81 |
); |
82 |
|
83 |
#------------------------- |
84 |
# Show records to merge |
85 |
#------------------------- |
86 |
} |
87 |
else { |
88 |
my $mergereference = $input->param('mergereference'); |
89 |
$template->{'VARS'}->{'mergereference'} = $mergereference; |
90 |
|
91 |
if ( scalar(@authid) != 2 ) { |
92 |
push @errors, { code => "WRONG_COUNT", value => scalar(@authid) }; |
93 |
} |
94 |
else { |
95 |
my $recordObj1 = Koha::Authority->get_from_authid($authid[0]) || Koha::Authority->new(); |
96 |
my $recordObj2; |
97 |
|
98 |
if ($mergereference eq 'breeding') { |
99 |
$recordObj2 = Koha::Authority->get_from_breeding($authid[1]) || Koha::Authority->new(); |
100 |
$mergereference = $authid[0]; |
101 |
} else { |
102 |
$recordObj2 = Koha::Authority->get_from_authid($authid[1]) || Koha::Authority->new(); |
103 |
} |
104 |
|
105 |
if ($mergereference) { |
106 |
|
107 |
my $framework; |
108 |
if ( $recordObj1->authtype ne $recordObj2->authtype && $mergereference ne 'breeding' ) { |
109 |
$framework = $input->param('frameworkcode') |
110 |
or push @errors, "Framework not selected."; |
111 |
} |
112 |
else { |
113 |
$framework = $recordObj1->authtype; |
114 |
} |
115 |
|
116 |
# Getting MARC Structure |
117 |
my $tagslib = GetTagsLabels( 1, $framework ); |
118 |
|
119 |
my $notreference = |
120 |
( $authid[0] == $mergereference ) |
121 |
? $authid[1] |
122 |
: $authid[0]; |
123 |
|
124 |
# Creating a loop for display |
125 |
|
126 |
my @record1 = $recordObj1->createMarcHash($tagslib); |
127 |
my @record2 = $recordObj2->createMarcHash($tagslib); |
128 |
|
129 |
# Parameters |
130 |
$template->param( |
131 |
recordid1 => $mergereference, |
132 |
recordid2 => $notreference, |
133 |
record1 => @record1, |
134 |
record2 => @record2, |
135 |
framework => $framework, |
136 |
); |
137 |
} |
138 |
else { |
139 |
|
140 |
# Ask the user to choose which record will be the kept |
141 |
$template->param( |
142 |
choosereference => 1, |
143 |
recordid1 => $authid[0], |
144 |
recordid2 => $authid[1], |
145 |
title1 => $recordObj1->authorized_heading, |
146 |
title2 => $recordObj2->authorized_heading, |
147 |
); |
148 |
if ( $recordObj1->authtype ne $recordObj2->authtype ) { |
149 |
my $frameworks = getauthtypes; |
150 |
my @frameworkselect; |
151 |
foreach my $thisframeworkcode ( keys %$frameworks ) { |
152 |
my %row = ( |
153 |
value => $thisframeworkcode, |
154 |
frameworktext => |
155 |
$frameworks->{$thisframeworkcode}->{'authtypetext'}, |
156 |
); |
157 |
if ( $recordObj1->authtype eq $thisframeworkcode ) { |
158 |
$row{'selected'} = 1; |
159 |
} |
160 |
push @frameworkselect, \%row; |
161 |
} |
162 |
$template->param( |
163 |
frameworkselect => \@frameworkselect, |
164 |
frameworkcode1 => $recordObj1->authtype, |
165 |
frameworkcode2 => $recordObj2->authtype, |
166 |
); |
167 |
} |
168 |
} |
169 |
} |
170 |
} |
171 |
|
172 |
if (@errors) { |
173 |
|
174 |
# Errors |
175 |
$template->param( errors => \@errors ); |
176 |
} |
177 |
|
178 |
output_html_with_http_headers $input, $cookie, $template->output; |
179 |
exit; |
180 |
|
181 |
=head1 FUNCTIONS |
182 |
|
183 |
=cut |
184 |
|
185 |
# ------------------------ |
186 |
# Functions |
187 |
# ------------------------ |