Merge branch 'bug_8117' into 3.12-master
[koha.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, { code => "CANNOT_MOVE", value => $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, { code => "WRONG_COUNT", value => scalar(@biblionumber) };
127     }
128     else {
129         my $data1 = GetBiblioData($biblionumber[0]);
130         my $record1 = GetMarcBiblio($biblionumber[0]);
131
132         my $data2 = GetBiblioData($biblionumber[1]);
133         my $record2 = GetMarcBiblio($biblionumber[1]);
134
135         # Checks if both records use the same framework
136         my $frameworkcode1 = &GetFrameworkCode($biblionumber[0]);
137         my $frameworkcode2 = &GetFrameworkCode($biblionumber[1]);
138
139
140         my $subtitle1 = GetRecordValue('subtitle', $record1, $frameworkcode1);
141         my $subtitle2 = GetRecordValue('subtitle', $record2, $frameworkcode1);
142
143         if ($mergereference) {
144
145             my $framework;
146             if ($frameworkcode1 ne $frameworkcode2) {
147                 $framework = $input->param('frameworkcode')
148                   or push @errors, "Famework not selected.";
149             } else {
150                 $framework = $frameworkcode1;
151             }
152
153             # Getting MARC Structure
154             my $tagslib = GetMarcStructure(1, $framework);
155
156             my $notreference = ($biblionumber[0] == $mergereference) ? $biblionumber[1] : $biblionumber[0];
157
158             # Creating a loop for display
159             my @record1 = _createMarcHash(GetMarcBiblio($mergereference), $tagslib);
160             my @record2 = _createMarcHash(GetMarcBiblio($notreference), $tagslib);
161
162             # Parameters
163             $template->param(
164                 biblio1 => $mergereference,
165                 biblio2 => $notreference,
166                 mergereference => $mergereference,
167                 record1 => @record1,
168                 record2 => @record2,
169                 framework => $framework,
170             );
171         }
172         else {
173
174         # Ask the user to choose which record will be the kept
175             $template->param(
176                 choosereference => 1,
177                 biblio1 => $biblionumber[0],
178                 biblio2 => $biblionumber[1],
179                 title1 => $data1->{'title'},
180                 subtitle1 => $subtitle1,
181                 title2 => $data2->{'title'},
182                 subtitle2 => $subtitle2
183             );
184             if ($frameworkcode1 ne $frameworkcode2) {
185                 my $frameworks = getframeworks;
186                 my @frameworkselect;
187                 foreach my $thisframeworkcode ( keys %$frameworks ) {
188                     my %row = (
189                         value         => $thisframeworkcode,
190                         frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
191                     );
192                     if ($frameworkcode1 eq $thisframeworkcode){
193                         $row{'selected'} = 1;
194                         }
195                     push @frameworkselect, \%row;
196                 }
197                 $template->param(
198                     frameworkselect => \@frameworkselect,
199                     frameworkcode1 => $frameworkcode1,
200                     frameworkcode2 => $frameworkcode2,
201                 );
202             }
203         }
204     }
205 }
206
207 if (@errors) {
208     # Errors
209     $template->param( errors  => \@errors );
210 }
211
212 output_html_with_http_headers $input, $cookie, $template->output;
213 exit;
214
215 =head1 FUNCTIONS
216
217 =cut
218
219 # ------------------------
220 # Functions
221 # ------------------------
222 sub _createMarcHash {
223      my $record = shift;
224     my $tagslib = shift;
225     my @array;
226     my @fields = $record->fields();
227
228
229     foreach my $field (@fields) {
230     my $fieldtag = $field->tag();
231     if ($fieldtag < 10) {
232         if ($tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0) {
233         push @array, {
234             field => [
235                     {
236                     tag => $fieldtag,
237                     key => createKey(),
238                     value => $field->data(),
239                     }
240                 ]
241                 };
242         }
243     } else {
244         my @subfields = $field->subfields();
245         my @subfield_array;
246         foreach my $subfield (@subfields) {
247         if ($tagslib->{$fieldtag}->{@$subfield[0]}->{'tab'} >= 0) {
248             push @subfield_array, {
249                                     subtag => @$subfield[0],
250                                     subkey => createKey(),
251                                     value => @$subfield[1],
252                                   };
253         }
254
255         }
256
257         if ($tagslib->{$fieldtag}->{'tab'} >= 0 && $fieldtag ne '995') {
258         push @array, {
259             field => [
260                 {
261                     tag => $fieldtag,
262                     key => createKey(),
263                     indicator1 => $field->indicator(1),
264                     indicator2 => $field->indicator(2),
265                     subfield   => [@subfield_array],
266                 }
267             ]
268             };
269         }
270
271     }
272     }
273     return [@array];
274
275 }
276
277 =head2 CreateKey
278
279 Create a random value to set it into the input name
280
281 =cut
282
283 sub createKey {
284     return int(rand(1000000));
285 }
286