removed needless imports of the YAML module
[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 @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     foreach my $itloop ($itemnumbers->{$frombiblio}) {
72         foreach my $itemnumber (@$itloop) {
73             my $res = MoveItemFromBiblio($itemnumber, $frombiblio, $tobiblio);
74             if (not defined $res) {
75                 push @notmoveditems, $itemnumber;
76             }
77         }
78     }
79     # If some items could not be moved :
80     if (scalar(@notmoveditems) > 0) {
81                 my $itemlist = join(' ',@notmoveditems);
82                 push @errors, "The following items could not be moved from the old record to the new one: $itemlist";
83     }
84
85     # Moving subscriptions from the other record to the reference record
86     my $subcount = CountSubscriptionFromBiblionumber($frombiblio);
87     if ($subcount > 0) {
88         $sth = $dbh->prepare("UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?");
89         $sth->execute($tobiblio, $frombiblio);
90
91         $sth = $dbh->prepare("UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?");
92         $sth->execute($tobiblio, $frombiblio);
93
94     }
95
96     # Moving serials
97     $sth = $dbh->prepare("UPDATE serial SET biblionumber = ? WHERE biblionumber = ?");
98     $sth->execute($tobiblio, $frombiblio);
99
100     # TODO : Moving reserves
101
102     # Deleting the other record
103     if (scalar(@errors) == 0) {
104         my $error = DelBiblio($frombiblio);
105         push @errors, $error if ($error); 
106     }
107
108     # Errors
109     my @errors_loop  = map{{error => $_}}@errors;
110
111     # Parameters
112     $template->param({
113         errors  => \@errors_loop,
114         result => 1,
115         biblio1 => $input->param('biblio1')
116     });
117
118
119 #-------------------------
120 # Show records to merge
121 #-------------------------
122 } else {
123
124     my $mergereference = $input->param('mergereference');
125     my $biblionumber = $input->param('biblionumber');
126
127     my $data1 = GetBiblioData($biblionumber[0]);
128     my $data2 = GetBiblioData($biblionumber[1]);
129
130     # Ask the user to choose which record will be the kept
131     if (not $mergereference) {
132         $template->param({
133             choosereference => 1,       
134             biblio1 => $biblionumber[0],
135             biblio2 => $biblionumber[1],
136             title1 => $data1->{'title'},
137             title2 => $data2->{'title'}
138             });
139     } else {
140
141         if (scalar(@biblionumber) != 2) {
142             push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
143         }
144
145         # Checks if both records use the same framework
146         my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
147         my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
148         my $framework;
149         if ($frameworkcode1 ne $frameworkcode2) {
150             push @errors, "The records selected for merging are using different frameworks. Currently merging is only available for records using the same framework.";
151         } else {
152             $framework = $frameworkcode1;       
153         }
154
155         # Getting MARC Structure
156         my $tagslib = GetMarcStructure(1, $framework);
157
158         my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
159
160         # Creating a loop for display
161         my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
162         my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
163
164         # Errors
165         my @errors_loop  = map{{error => $_}}@errors;
166
167         # Parameters
168         $template->param({
169             errors  => \@errors_loop,
170             biblio1 => $mergereference,
171             biblio2 => $notreference,
172             mergereference => $mergereference,
173             record1 => @record1,
174             record2 => @record2,
175             framework => $framework
176             });
177     }
178 }
179 output_html_with_http_headers $input, $cookie, $template->output;
180 exit;
181
182
183 # ------------------------
184 # Functions
185 # ------------------------
186 sub _createMarcHash {
187      my $record = shift;
188     my $tagslib = shift;
189     my @array;
190     my @fields = $record->fields();
191
192
193     foreach my $field (@fields) {
194         my $fieldtag = $field->tag();
195         if ($fieldtag < 10) {
196             if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
197                 push @array, { 
198                         field => [ 
199                                     {
200                                         tag => $fieldtag, 
201                                         key => createKey(), 
202                                         value => $field->data(),
203                                     }
204                                 ]
205                             };    
206             }
207         } else {
208             my @subfields = $field->subfields();
209             my @subfield_array;
210             foreach my $subfield (@subfields) {
211                 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
212                     push @subfield_array, {  
213                                         subtag => @$subfield[0],
214                                         subkey => createKey(), 
215                                         value => @$subfield[1],
216                                       };
217                 }
218
219             }
220
221             if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
222                 push @array, {
223                         field => [  
224                                     {
225                                         tag => $fieldtag, 
226                                         key => createKey(), 
227                                         indicator1 => $field->indicator(1), 
228                                         indicator2 => $field->indicator(2), 
229                                         subfield   => [@subfield_array], 
230                                     }
231                                 ]
232                             };  
233             }
234
235         }
236     }
237     return [@array];
238
239 }
240
241 =item CreateKey
242
243     Create a random value to set it into the input name
244
245 =cut
246
247 sub createKey(){
248     return int(rand(1000000));
249 }
250
251