Bug 7309 - Add NORMARCslim2intranetDetail.xsl for detail view in intranet
[koha.git] / cataloguing / merge.pl
blob0b881f1c1391851689cb4e9a1f153ec9d0afbe4d
1 #!/usr/bin/perl
4 # Copyright 2009 BibLibre
5 # Parts Copyright Catalyst IT 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Output;
26 use C4::Auth;
27 use C4::Items;
28 use C4::Biblio;
29 use C4::Serials;
30 use C4::Reserves qw/MergeHolds/;
32 my $input = new CGI;
33 my @biblionumber = $input->param('biblionumber');
34 my $merge = $input->param('merge');
36 my @errors;
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40 template_name => "cataloguing/merge.tmpl",
41 query => $input,
42 type => "intranet",
43 authnotrequired => 0,
44 flagsrequired => { editcatalogue => 'edit_catalogue' },
48 #------------------------
49 # Merging
50 #------------------------
51 if ($merge) {
53 my $dbh = C4::Context->dbh;
54 my $sth;
56 # Creating a new record from the html code
57 my $record = TransformHtmlToMarc( $input );
58 my $tobiblio = $input->param('biblio1');
59 my $frombiblio = $input->param('biblio2');
61 # Rewriting the leader
62 $record->leader(GetMarcBiblio($tobiblio)->leader());
64 my $frameworkcode = &GetFrameworkCode($tobiblio);
65 my @notmoveditems;
67 # Modifying the reference record
68 ModBiblio($record, $tobiblio, $frameworkcode);
70 # Moving items from the other record to the reference record
71 my $itemnumbers = get_itemnumbers_of($frombiblio);
72 foreach my $itloop ($itemnumbers->{$frombiblio}) {
73 foreach my $itemnumber (@$itloop) {
74 my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
75 if (not defined $res) {
76 push @notmoveditems, $itemnumber;
80 # If some items could not be moved :
81 if (scalar(@notmoveditems) > 0) {
82 my $itemlist = join(' ',@notmoveditems);
83 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
86 # Moving subscriptions from the other record to the reference record
87 my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
88 if ($subcount > 0) {
89 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
90 $sth->execute($tobiblio, $frombiblio);
92 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
93 $sth->execute($tobiblio, $frombiblio);
97 # Moving serials
98 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
99 $sth->execute($tobiblio, $frombiblio);
101 # TODO : Moving reserves
103 # Deleting the other record
104 if (scalar(@errors) == 0) {
105 # Move holds
106 MergeHolds($dbh,$tobiblio,$frombiblio);
107 my $error = DelBiblio($frombiblio);
108 push @errors, $error if ($error);
111 # Errors
112 my @errors_loop = map{{error => $_}}@errors;
114 # Parameters
115 $template->param(
116 errors => \@errors_loop,
117 result => 1,
118 biblio1 => $input->param('biblio1')
122 #-------------------------
123 # Show records to merge
124 #-------------------------
125 } else {
127 my $mergereference = $input->param('mergereference');
128 my $biblionumber = $input->param('biblionumber');
130 my $data1 = GetBiblioData($biblionumber[0]);
131 my $data2 = GetBiblioData($biblionumber[1]);
133 # Ask the user to choose which record will be the kept
134 if (not $mergereference) {
135 $template->param(
136 choosereference => 1,
137 biblio1 => $biblionumber[0],
138 biblio2 => $biblionumber[1],
139 title1 => $data1->{'title'},
140 title2 => $data2->{'title'}
142 } else {
144 if (scalar(@biblionumber) != 2) {
145 push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
148 # Checks if both records use the same framework
149 my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
150 my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
151 my $framework;
152 if ($frameworkcode1 ne $frameworkcode2) {
153 push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
154 } else {
155 $framework = $frameworkcode1;
158 # Getting MARC Structure
159 my $tagslib = GetMarcStructure(1, $framework);
161 my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
163 # Creating a loop for display
164 my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
165 my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
167 # Errors
168 my @errors_loop = map{{error => $_}}@errors;
170 # Parameters
171 $template->param(
172 errors => \@errors_loop,
173 biblio1 => $mergereference,
174 biblio2 => $notreference,
175 mergereference => $mergereference,
176 record1 => @record1,
177 record2 => @record2,
178 framework => $framework
182 output_html_with_http_headers $input, $cookie, $template->output;
183 exit;
185 =head1 FUNCTIONS
187 =cut
189 # ------------------------
190 # Functions
191 # ------------------------
192 sub _createMarcHash {
193 my $record = shift;
194 my $tagslib = shift;
195 my @array;
196 my @fields = $record->fields();
199 foreach my $field (@fields) {
200 my $fieldtag = $field->tag();
201 if ($fieldtag < 10) {
202 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
203 push @array, {
204 field => [
206 tag => $fieldtag,
207 key => createKey(),
208 value => $field->data(),
213 } else {
214 my @subfields = $field->subfields();
215 my @subfield_array;
216 foreach my $subfield (@subfields) {
217 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
218 push @subfield_array, {
219 subtag => @$subfield[0],
220 subkey => createKey(),
221 value => @$subfield[1],
227 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
228 push @array, {
229 field => [
231 tag => $fieldtag,
232 key => createKey(),
233 indicator1 => $field->indicator(1),
234 indicator2 => $field->indicator(2),
235 subfield => [@subfield_array],
243 return [@array];
247 =head2 CreateKey
249 Create a random value to set it into the input name
251 =cut
253 sub createKey {
254 return int(rand(1000000));