Merge branch 'bug2505_patches' of git://git.catalyst.net.nz/koha into to-push
[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 use YAML;
30
31 my $input = new CGI;
32 my @biblionumber = $input->param('biblionumber');
33 my $merge = $input->param('merge');
34
35 my @errors;
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {
39         template_name   => "cataloguing/merge.tmpl",
40         query           => $input,
41         type            => "intranet",
42         authnotrequired => 0,
43         flagsrequired   => { editcatalogue => 'edit_catalogue' },
44     }
45 );
46
47 #------------------------
48 # Merging
49 #------------------------
50 if ($merge) {
51
52     my @params = $input->param();
53     my $dbh = C4::Context->dbh;
54     my $sth;
55
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');
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     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;
78             }
79         }
80     }
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";
85     }
86
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);
92
93         $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
94         $sth->execute($tobiblio, $frombiblio);
95
96     }
97
98     # Moving serials
99     $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
100     $sth->execute($tobiblio, $frombiblio);
101
102     # TODO : Moving reserves
103
104     # Deleting the other record
105     if (scalar(@errors) == 0) {
106         my $error = DelBiblio($frombiblio);
107         push @errors, $error if ($error); 
108     }
109
110     # Errors
111     my @errors_loop  = map{{error => $_}}@errors;
112
113     # Parameters
114     $template->param({
115         errors  => \@errors_loop,
116         result => 1,
117         biblio1 => $input->param('biblio1')
118     });
119
120
121 #-------------------------
122 # Show records to merge
123 #-------------------------
124 } else {
125
126     my $mergereference = $input->param('mergereference');
127     my $biblionumber = $input->param('biblionumber');
128
129     my $data1 = GetBiblioData($biblionumber[0]);
130     my $data2 = GetBiblioData($biblionumber[1]);
131
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'}
140             });
141     } else {
142
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.";
145         }
146
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;       
155         }
156
157         # Getting MARC Structure
158         my $tagslib = GetMarcStructure(1, $framework);
159
160         my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
161
162         # Creating a loop for display
163         my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
164         my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
165
166         # Errors
167         my @errors_loop  = map{{error => $_}}@errors;
168
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
178             });
179     }
180 }
181 output_html_with_http_headers $input, $cookie, $template->output;
182 exit;
183
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 =item 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