Merge branch 'new/enh/bug_4877'
[koha.git] / cataloguing / merge.pl
1 #!/usr/bin/perl 
2
3
4 # Copyright 2009 BibLibre
5 # Parts Copyright Catalyst IT 2011
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
17 #
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.
21
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/;
31
32 my $input = new CGI;
33 my @biblionumber = $input->param('biblionumber');
34 my $merge = $input->param('merge');
35
36 my @errors;
37
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39     {
40         template_name   => "cataloguing/merge.tmpl",
41         query           => $input,
42         type            => "intranet",
43         authnotrequired => 0,
44         flagsrequired   => { editcatalogue => 'edit_catalogue' },
45     }
46 );
47
48 #------------------------
49 # Merging
50 #------------------------
51 if ($merge) {
52
53     my $dbh = C4::Context->dbh;
54     my $sth;
55
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');
60
61     # Rewriting the leader
62     $record->leader(GetMarcBiblio($tobiblio)->leader());
63
64     my $frameworkcode = &GetFrameworkCode($tobiblio);
65     my @notmoveditems;
66
67     # Modifying the reference record
68     ModBiblio($record, $tobiblio, $frameworkcode);
69
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;
77             }
78         }
79     }
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";
84     }
85
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);
91
92         $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
93         $sth->execute($tobiblio, $frombiblio);
94
95     }
96
97     # Moving serials
98     $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
99     $sth->execute($tobiblio, $frombiblio);
100
101     # TODO : Moving reserves
102
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); 
109     }
110
111     # Errors
112     my @errors_loop  = map{{error => $_}}@errors;
113
114     # Parameters
115     $template->param(
116         errors  => \@errors_loop,
117         result => 1,
118         biblio1 => $input->param('biblio1')
119     );
120
121
122 #-------------------------
123 # Show records to merge
124 #-------------------------
125 } else {
126
127     my $mergereference = $input->param('mergereference');
128     my $biblionumber = $input->param('biblionumber');
129
130     my $data1 = GetBiblioData($biblionumber[0]);
131     my $data2 = GetBiblioData($biblionumber[1]);
132
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'}
141             );
142     } else {
143
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.";
146         }
147
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;       
156         }
157
158         # Getting MARC Structure
159         my $tagslib = GetMarcStructure(1, $framework);
160
161         my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
162
163         # Creating a loop for display
164         my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
165         my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
166
167         # Errors
168         my @errors_loop  = map{{error => $_}}@errors;
169
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
179             );
180     }
181 }
182 output_html_with_http_headers $input, $cookie, $template->output;
183 exit;
184
185 =head1 FUNCTIONS
186
187 =cut
188
189 # ------------------------
190 # Functions
191 # ------------------------
192 sub _createMarcHash {
193      my $record = shift;
194     my $tagslib = shift;
195     my @array;
196     my @fields = $record->fields();
197
198
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 => [ 
205                                     {
206                                         tag => $fieldtag, 
207                                         key => createKey(), 
208                                         value => $field->data(),
209                                     }
210                                 ]
211                             };    
212             }
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],
222                                       };
223                 }
224
225             }
226
227             if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
228                 push @array, {
229                         field => [  
230                                     {
231                                         tag => $fieldtag, 
232                                         key => createKey(), 
233                                         indicator1 => $field->indicator(1), 
234                                         indicator2 => $field->indicator(2), 
235                                         subfield   => [@subfield_array], 
236                                     }
237                                 ]
238                             };  
239             }
240
241         }
242     }
243     return [@array];
244
245 }
246
247 =head2 CreateKey
248
249 Create a random value to set it into the input name
250
251 =cut
252
253 sub createKey(){
254     return int(rand(1000000));
255 }
256
257
258