Enhancement Bug 4444: Centralize Code Handling Perl Dependencies
[koha.git] / cataloguing / merge.pl
blobb1cddf23eefe2ab65828fb404003adc766a45837
1 #!/usr/bin/perl
4 # Copyright 2009 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Serials;
29 use YAML;
31 my $input = new CGI;
32 my @biblionumber = $input->param('biblionumber');
33 my $merge = $input->param('merge');
35 my @errors;
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 template_name => "cataloguing/merge.tmpl",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => { editcatalogue => 'edit_catalogue' },
47 #------------------------
48 # Merging
49 #------------------------
50 if ($merge) {
52 my @params = $input->param();
53 my $dbh = C4::Context->dbh;
54 my $sth;
56 # Creating a new record from the html code
57 my $record = TransformHtmlToMarc( \@params , $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 use Data::Dumper;
73 foreach my $itloop ($itemnumbers->{$frombiblio}) {
74 foreach my $itemnumber (@$itloop) {
75 my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
76 if (not defined $res) {
77 push @notmoveditems, $itemnumber;
81 # If some items could not be moved :
82 if (scalar(@notmoveditems) > 0) {
83 my $itemlist = join(' ',@notmoveditems);
84 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
87 # Moving subscriptions from the other record to the reference record
88 my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
89 if ($subcount > 0) {
90 $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
91 $sth->execute($tobiblio, $frombiblio);
93 $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
94 $sth->execute($tobiblio, $frombiblio);
98 # Moving serials
99 $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
100 $sth->execute($tobiblio, $frombiblio);
102 # TODO : Moving reserves
104 # Deleting the other record
105 if (scalar(@errors) == 0) {
106 my $error = DelBiblio($frombiblio);
107 push @errors, $error if ($error);
110 # Errors
111 my @errors_loop = map{{error => $_}}@errors;
113 # Parameters
114 $template->param({
115 errors => \@errors_loop,
116 result => 1,
117 biblio1 => $input->param('biblio1')
121 #-------------------------
122 # Show records to merge
123 #-------------------------
124 } else {
126 my $mergereference = $input->param('mergereference');
127 my $biblionumber = $input->param('biblionumber');
129 my $data1 = GetBiblioData($biblionumber[0]);
130 my $data2 = GetBiblioData($biblionumber[1]);
132 # Ask the user to choose which record will be the kept
133 if (not $mergereference) {
134 $template->param({
135 choosereference => 1,
136 biblio1 => $biblionumber[0],
137 biblio2 => $biblionumber[1],
138 title1 => $data1->{'title'},
139 title2 => $data2->{'title'}
141 } else {
143 if (scalar(@biblionumber) != 2) {
144 push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
147 # Checks if both records use the same framework
148 my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
149 my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
150 my $framework;
151 if ($frameworkcode1 ne $frameworkcode2) {
152 push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
153 } else {
154 $framework = $frameworkcode1;
157 # Getting MARC Structure
158 my $tagslib = GetMarcStructure(1, $framework);
160 my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
162 # Creating a loop for display
163 my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
164 my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
166 # Errors
167 my @errors_loop = map{{error => $_}}@errors;
169 # Parameters
170 $template->param({
171 errors => \@errors_loop,
172 biblio1 => $mergereference,
173 biblio2 => $notreference,
174 mergereference => $mergereference,
175 record1 => @record1,
176 record2 => @record2,
177 framework => $framework
181 output_html_with_http_headers $input, $cookie, $template->output;
182 exit;
185 # ------------------------
186 # Functions
187 # ------------------------
188 sub _createMarcHash {
189 my $record = shift;
190 my $tagslib = shift;
191 my @array;
192 my @fields = $record->fields();
195 foreach my $field (@fields) {
196 my $fieldtag = $field->tag();
197 if ($fieldtag < 10) {
198 if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
199 push @array, {
200 field => [
202 tag => $fieldtag,
203 key => createKey(),
204 value => $field->data(),
209 } else {
210 my @subfields = $field->subfields();
211 my @subfield_array;
212 foreach my $subfield (@subfields) {
213 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
214 push @subfield_array, {
215 subtag => @$subfield[0],
216 subkey => createKey(),
217 value => @$subfield[1],
223 if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
224 push @array, {
225 field => [
227 tag => $fieldtag,
228 key => createKey(),
229 indicator1 => $field->indicator(1),
230 indicator2 => $field->indicator(2),
231 subfield => [@subfield_array],
239 return [@array];
243 =item CreateKey
245 Create a random value to set it into the input name
247 =cut
249 sub createKey(){
250 return int(rand(1000000));