bug_6210: Select framework if merging two records with different frameworks
[koha-equinox.git] / cataloguing / merge.pl
1 #!/usr/bin/perl 
2
3
4 # Copyright 2009 BibLibre
5 # Parts Copyright Catalyst IT 2011
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI;
25 use C4::Output;
26 use C4::Auth;
27 use C4::Items;
28 use C4::Biblio;
29 use C4::Serials;
30 use C4::Koha;
31 use C4::Reserves qw/MergeHolds/;
32
33 my $input = new CGI;
34 my @biblionumber = $input->param('biblionumber');
35 my $merge = $input->param('merge');
36
37 my @errors;
38
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
40     {
41         template_name   => "cataloguing/merge.tmpl",
42         query           => $input,
43         type            => "intranet",
44         authnotrequired => 0,
45         flagsrequired   => { editcatalogue => 'edit_catalogue' },
46     }
47 );
48
49 #------------------------
50 # Merging
51 #------------------------
52 if ($merge) {
53
54     my $dbh = C4::Context->dbh;
55     my $sth;
56
57     # Creating a new record from the html code
58     my $record       = TransformHtmlToMarc( $input );
59     my $tobiblio     =  $input->param('biblio1');
60     my $frombiblio   =  $input->param('biblio2');
61
62     # Rewriting the leader
63     $record->leader(GetMarcBiblio($tobiblio)->leader());
64
65     my $frameworkcode = $input->param('frameworkcode');
66     my @notmoveditems;
67
68     # Modifying the reference record
69     ModBiblio($record, $tobiblio, $frameworkcode);
70
71     # Moving items from the other record to the reference record
72     my $itemnumbers = get_itemnumbers_of($frombiblio);
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         # Move holds
107         MergeHolds($dbh,$tobiblio,$frombiblio);
108         my $error = DelBiblio($frombiblio);
109         push @errors, $error if ($error); 
110     }
111
112     # Parameters
113     $template->param(
114         result => 1,
115         biblio1 => $input->param('biblio1')
116     );
117
118 #-------------------------
119 # Show records to merge
120 #-------------------------
121 } else {
122     my $mergereference = $input->param('mergereference');
123     my $biblionumber = $input->param('biblionumber');
124
125     if (scalar(@biblionumber) != 2) {
126         push @errors, "An unexpected number of records was provided for merging. Currently only two records at a time can be merged.";
127     }
128     else {
129         my $data1 = GetBiblioData($biblionumber[0]);
130         my $data2 = GetBiblioData($biblionumber[1]);
131
132         # Checks if both records use the same framework
133         my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
134         my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
135
136         if ($mergereference) {
137
138             my $framework;
139             if ($frameworkcode1 ne $frameworkcode2) {
140                 $framework = $input->param('frameworkcode')
141                   or push @errors, "Famework not selected.";
142             } else {
143                 $framework = $frameworkcode1;
144             }
145
146             # Getting MARC Structure
147             my $tagslib = GetMarcStructure(1, $framework);
148
149             my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
150
151             # Creating a loop for display
152             my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
153             my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
154
155             # Parameters
156             $template->param(
157                 biblio1 => $mergereference,
158                 biblio2 => $notreference,
159                 mergereference => $mergereference,
160                 record1 => @record1,
161                 record2 => @record2,
162                 framework => $framework,
163             );
164         }
165         else {
166
167         # Ask the user to choose which record will be the kept
168             $template->param(
169                 choosereference => 1,
170                 biblio1 => $biblionumber[0],
171                 biblio2 => $biblionumber[1],
172                 title1 => $data1->{'title'},
173                 title2 => $data2->{'title'}
174             );
175             if ($frameworkcode1 ne $frameworkcode2) {
176                 my $frameworks = getframeworks;
177                 my @frameworkselect;
178                 foreach my $thisframeworkcode ( keys %$frameworks ) {
179                     my %row = (
180                         value         => $thisframeworkcode,
181                         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
182                     );
183                     if ($frameworkcode1 eq $thisframeworkcode){
184                         $row{'selected'} = 1;
185                         }
186                     push @frameworkselect, \%row;
187                 }
188                 $template->param(
189                     frameworkselect => \@frameworkselect,
190                     frameworkcode1 => $frameworkcode1,
191                     frameworkcode2 => $frameworkcode2,
192                 );
193             }
194         }
195     }
196 }
197
198 if (@errors) {
199     # Errors
200     my @errors_loop  = map{{error => $_}}@errors;
201     $template->param( errors  => \@errors_loop );
202 }
203
204 output_html_with_http_headers $input, $cookie, $template->output;
205 exit;
206
207 =head1 FUNCTIONS
208
209 =cut
210
211 # ------------------------
212 # Functions
213 # ------------------------
214 sub _createMarcHash {
215      my $record = shift;
216     my $tagslib = shift;
217     my @array;
218     my @fields = $record->fields();
219
220
221     foreach my $field (@fields) {
222         my $fieldtag = $field->tag();
223         if ($fieldtag < 10) {
224             if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
225                 push @array, { 
226                         field => [ 
227                                     {
228                                         tag => $fieldtag, 
229                                         key => createKey(), 
230                                         value => $field->data(),
231                                     }
232                                 ]
233                             };    
234             }
235         } else {
236             my @subfields = $field->subfields();
237             my @subfield_array;
238             foreach my $subfield (@subfields) {
239                 if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
240                     push @subfield_array, {  
241                                         subtag => @$subfield[0],
242                                         subkey => createKey(), 
243                                         value => @$subfield[1],
244                                       };
245                 }
246
247             }
248
249             if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
250                 push @array, {
251                         field => [  
252                                     {
253                                         tag => $fieldtag, 
254                                         key => createKey(), 
255                                         indicator1 => $field->indicator(1), 
256                                         indicator2 => $field->indicator(2), 
257                                         subfield   => [@subfield_array], 
258                                     }
259                                 ]
260                             };  
261             }
262
263         }
264     }
265     return [@array];
266
267 }
268
269 =head2 CreateKey
270
271 Create a random value to set it into the input name
272
273 =cut
274
275 sub createKey {
276     return int(rand(1000000));
277 }
278
279
280