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