Merge branch 'patroncards-wip' of git://git.foundations.edu/koha into community
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use CGI;
23 use C4::Output;
24 use C4::Auth;
25 use C4::Items;
26 use C4::Biblio;
27 use C4::Serials;
28 use YAML;
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 @params = $input->param();
52     my $dbh = C4::Context->dbh;
53     my $sth;
54
55     # Creating a new record from the html code
56     my $record       = TransformHtmlToMarc( \@params , $input );
57     my $tobiblio     =  $input->param('biblio1');
58     my $frombiblio   =  $input->param('biblio2');
59
60     # Rewriting the leader
61     $record->leader(GetMarcBiblio($tobiblio)->leader());
62
63     my $frameworkcode = &GetFrameworkCode($tobiblio);
64     my @notmoveditems;
65
66     # Modifying the reference record
67     ModBiblio($record, $tobiblio, $frameworkcode);
68
69     # Moving items from the other record to the reference record
70     my $itemnumbers = get_itemnumbers_of($frombiblio);
71     use Data::Dumper;
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         my $error = DelBiblio($frombiblio);
106         push @errors, $error if ($error); 
107     }
108
109     # Errors
110     my @errors_loop  = map{{error => $_}}@errors;
111
112     # Parameters
113     $template->param({
114         errors  => \@errors_loop,
115         result => 1,
116         biblio1 => $input->param('biblio1')
117     });
118
119
120 #-------------------------
121 # Show records to merge
122 #-------------------------
123 } else {
124
125     my $mergereference = $input->param('mergereference');
126     my $biblionumber = $input->param('biblionumber');
127
128     my $data1 = GetBiblioData($biblionumber[0]);
129     my $data2 = GetBiblioData($biblionumber[1]);
130
131     # Ask the user to choose which record will be the kept
132     if (not $mergereference) {
133         $template->param({
134             choosereference => 1,       
135             biblio1 => $biblionumber[0],
136             biblio2 => $biblionumber[1],
137             title1 => $data1->{'title'},
138             title2 => $data2->{'title'}
139             });
140     } else {
141
142         if (scalar(@biblionumber) != 2) {
143             push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
144         }
145
146         # Checks if both records use the same framework
147         my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
148         my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
149         my $framework;
150         if ($frameworkcode1 ne $frameworkcode2) {
151             push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
152         } else {
153             $framework = $frameworkcode1;       
154         }
155
156         # Getting MARC Structure
157         my $tagslib = GetMarcStructure(1, $framework);
158
159         my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
160
161         # Creating a loop for display
162         my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
163         my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
164
165         # Errors
166         my @errors_loop  = map{{error => $_}}@errors;
167
168         # Parameters
169         $template->param({
170             errors  => \@errors_loop,
171             biblio1 => $mergereference,
172             biblio2 => $notreference,
173             mergereference => $mergereference,
174             record1 => @record1,
175             record2 => @record2,
176             framework => $framework
177             });
178     }
179 }
180 output_html_with_http_headers $input, $cookie, $template->output;
181 exit;
182
183
184 # ------------------------
185 # Functions
186 # ------------------------
187 sub _createMarcHash {
188      my $record = shift;
189     my $tagslib = shift;
190     my @array;
191     my @fields = $record->fields();
192
193
194     foreach my $field (@fields) {
195         my $fieldtag = $field->tag();
196         if ($fieldtag < 10) {
197             if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
198                 push @array, { 
199                         field => [ 
200                                     {
201                                         tag => $fieldtag, 
202                                         key => createKey(), 
203                                         value => $field->data(),
204                                     }
205                                 ]
206                             };    
207             }
208         } else {
209             my @subfields = $field->subfields();
210             my @subfield_array;
211             foreach my $subfield (@subfields) {
212                 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
213                     push @subfield_array, {  
214                                         subtag => @$subfield[0],
215                                         subkey => createKey(), 
216                                         value => @$subfield[1],
217                                       };
218                 }
219
220             }
221
222             if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
223                 push @array, {
224                         field => [  
225                                     {
226                                         tag => $fieldtag, 
227                                         key => createKey(), 
228                                         indicator1 => $field->indicator(1), 
229                                         indicator2 => $field->indicator(2), 
230                                         subfield   => [@subfield_array], 
231                                     }
232                                 ]
233                             };  
234             }
235
236         }
237     }
238     return [@array];
239
240 }
241
242 =item CreateKey
243
244     Create a random value to set it into the input name
245
246 =cut
247
248 sub createKey(){
249     return int(rand(1000000));
250 }
251
252