Merge remote-tracking branch 'kc/new/bug_6679' into kcmaster
[koha.git] / cataloguing / merge.pl
1 #!/usr/bin/perl 
2
3
4 # Copyright 2009 BibLibre
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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.
20
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
30 my $input = new CGI;
31 my @biblionumber = $input->param('biblionumber');
32 my $merge = $input->param('merge');
33
34 my @errors;
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "cataloguing/merge.tmpl",
39         query           => $input,
40         type            => "intranet",
41         authnotrequired => 0,
42         flagsrequired   => { editcatalogue => 'edit_catalogue' },
43     }
44 );
45
46 #------------------------
47 # Merging
48 #------------------------
49 if ($merge) {
50
51     my $dbh = C4::Context->dbh;
52     my $sth;
53
54     # Creating a new record from the html code
55     my $record       = TransformHtmlToMarc( $input );
56     my $tobiblio     =  $input->param('biblio1');
57     my $frombiblio   =  $input->param('biblio2');
58
59     # Rewriting the leader
60     $record->leader(GetMarcBiblio($tobiblio)->leader());
61
62     my $frameworkcode = &GetFrameworkCode($tobiblio);
63     my @notmoveditems;
64
65     # Modifying the reference record
66     ModBiblio($record, $tobiblio, $frameworkcode);
67
68     # Moving items from the other record to the reference record
69     my $itemnumbers = get_itemnumbers_of($frombiblio);
70     foreach my $itloop ($itemnumbers->{$frombiblio}) {
71         foreach my $itemnumber (@$itloop) {
72             my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
73             if (not defined $res) {
74                 push @notmoveditems, $itemnumber;
75             }
76         }
77     }
78     # If some items could not be moved :
79     if (scalar(@notmoveditems) > 0) {
80                 my $itemlist = join(' ',@notmoveditems);
81                 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
82     }
83
84     # Moving subscriptions from the other record to the reference record
85     my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
86     if ($subcount > 0) {
87         $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
88         $sth->execute($tobiblio, $frombiblio);
89
90         $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
91         $sth->execute($tobiblio, $frombiblio);
92
93     }
94
95     # Moving serials
96     $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
97     $sth->execute($tobiblio, $frombiblio);
98
99     # TODO : Moving reserves
100
101     # Deleting the other record
102     if (scalar(@errors) == 0) {
103         my $error = DelBiblio($frombiblio);
104         push @errors, $error if ($error); 
105     }
106
107     # Errors
108     my @errors_loop  = map{{error => $_}}@errors;
109
110     # Parameters
111     $template->param(
112         errors  => \@errors_loop,
113         result => 1,
114         biblio1 => $input->param('biblio1')
115     );
116
117
118 #-------------------------
119 # Show records to merge
120 #-------------------------
121 } else {
122
123     my $mergereference = $input->param('mergereference');
124     my $biblionumber = $input->param('biblionumber');
125
126     my $data1 = GetBiblioData($biblionumber[0]);
127     my $data2 = GetBiblioData($biblionumber[1]);
128
129     # Ask the user to choose which record will be the kept
130     if (not $mergereference) {
131         $template->param(
132             choosereference => 1,       
133             biblio1 => $biblionumber[0],
134             biblio2 => $biblionumber[1],
135             title1 => $data1->{'title'},
136             title2 => $data2->{'title'}
137             );
138     } else {
139
140         if (scalar(@biblionumber) != 2) {
141             push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
142         }
143
144         # Checks if both records use the same framework
145         my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
146         my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
147         my $framework;
148         if ($frameworkcode1 ne $frameworkcode2) {
149             push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
150         } else {
151             $framework = $frameworkcode1;       
152         }
153
154         # Getting MARC Structure
155         my $tagslib = GetMarcStructure(1, $framework);
156
157         my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
158
159         # Creating a loop for display
160         my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
161         my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
162
163         # Errors
164         my @errors_loop  = map{{error => $_}}@errors;
165
166         # Parameters
167         $template->param(
168             errors  => \@errors_loop,
169             biblio1 => $mergereference,
170             biblio2 => $notreference,
171             mergereference => $mergereference,
172             record1 => @record1,
173             record2 => @record2,
174             framework => $framework
175             );
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 # ------------------------
188 sub _createMarcHash {
189      my $record = shift;
190     my $tagslib = shift;
191     my @array;
192     my @fields = $record->fields();
193
194
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 => [ 
201                                     {
202                                         tag => $fieldtag, 
203                                         key => createKey(), 
204                                         value => $field->data(),
205                                     }
206                                 ]
207                             };    
208             }
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],
218                                       };
219                 }
220
221             }
222
223             if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
224                 push @array, {
225                         field => [  
226                                     {
227                                         tag => $fieldtag, 
228                                         key => createKey(), 
229                                         indicator1 => $field->indicator(1), 
230                                         indicator2 => $field->indicator(2), 
231                                         subfield   => [@subfield_array], 
232                                     }
233                                 ]
234                             };  
235             }
236
237         }
238     }
239     return [@array];
240
241 }
242
243 =head2 CreateKey
244
245 Create a random value to set it into the input name
246
247 =cut
248
249 sub createKey(){
250     return int(rand(1000000));
251 }
252
253