Bug 25898: Prohibit indirect object notation
[koha.git] / cataloguing / merge.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 BibLibre
4 # Parts Copyright Catalyst IT 2011
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23
24 use C4::Output;
25 use C4::Auth;
26 use C4::Items;
27 use C4::Biblio;
28 use C4::Serials;
29 use C4::Koha;
30 use C4::Reserves qw/MergeHolds/;
31 use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/;
32
33 use Koha::BiblioFrameworks;
34 use Koha::Items;
35 use Koha::MetadataRecord;
36
37 my $input = CGI->new;
38 my @biblionumbers = $input->multi_param('biblionumber');
39 my $merge = $input->param('merge');
40
41 my @errors;
42
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44     {
45         template_name   => "cataloguing/merge.tt",
46         query           => $input,
47         type            => "intranet",
48         flagsrequired   => { editcatalogue => 'edit_catalogue' },
49     }
50 );
51
52 #------------------------
53 # Merging
54 #------------------------
55 if ($merge) {
56
57     my $dbh = C4::Context->dbh;
58
59     # Creating a new record from the html code
60     my $record       = TransformHtmlToMarc( $input, 1 );
61     my $ref_biblionumber = $input->param('ref_biblionumber');
62     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
63
64     # prepare report
65     my @report_records;
66     my $report_fields_str = $input->param('report_fields');
67     $report_fields_str ||= C4::Context->preference('MergeReportFields');
68     my @report_fields;
69     foreach my $field_str (split /,/, $report_fields_str) {
70         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
71             my ($field, $subfields) = ($1, $2);
72             push @report_fields, {
73                 tag => $field,
74                 subfields => [ split //, $subfields ]
75             }
76         }
77     }
78
79     # Rewriting the leader
80     $record->leader(GetMarcBiblio({ biblionumber => $ref_biblionumber })->leader());
81
82     my $frameworkcode = $input->param('frameworkcode');
83     my @notmoveditems;
84
85     # Modifying the reference record
86     ModBiblio($record, $ref_biblionumber, $frameworkcode);
87
88     # Moving items from the other record to the reference record
89     foreach my $biblionumber (@biblionumbers) {
90         my $items = Koha::Items->search({ biblionumber => $biblionumber });
91         while ( my $item = $items->next) {
92             my $res = MoveItemFromBiblio( $item->itemnumber, $biblionumber, $ref_biblionumber );
93             if ( not defined $res ) {
94                 push @notmoveditems, $item->itemnumber;
95             }
96         }
97     }
98     # If some items could not be moved :
99     if (scalar(@notmoveditems) > 0) {
100         my $itemlist = join(' ',@notmoveditems);
101         push @errors, { code => "CANNOT_MOVE", value => $itemlist };
102     }
103
104     my $sth_subscription = $dbh->prepare("
105         UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
106     ");
107     my $sth_subscriptionhistory = $dbh->prepare("
108         UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
109     ");
110     my $sth_serial = $dbh->prepare("
111         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
112     ");
113     my $sth_suggestions = $dbh->prepare("
114         UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ?
115     ");
116
117     my $report_header = {};
118     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
119         # build report
120         my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
121         my %report_record = (
122             biblionumber => $biblionumber,
123             fields => {},
124         );
125         foreach my $field (@report_fields) {
126             my @marcfields = $marcrecord->field($field->{tag});
127             foreach my $marcfield (@marcfields) {
128                 my $tag = $marcfield->tag();
129                 if (scalar @{$field->{subfields}}) {
130                     foreach my $subfield (@{$field->{subfields}}) {
131                         my @values = $marcfield->subfield($subfield);
132                         $report_header->{ $tag . $subfield } = 1;
133                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
134                     }
135                 } elsif ($field->{tag} gt '009') {
136                     my @marcsubfields = $marcfield->subfields();
137                     foreach my $marcsubfield (@marcsubfields) {
138                         my ($code, $value) = @$marcsubfield;
139                         $report_header->{ $tag . $code } = 1;
140                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
141                     }
142                 } else {
143                     $report_header->{ $tag . '@' } = 1;
144                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
145                 }
146             }
147         }
148         push @report_records, \%report_record;
149     }
150
151     foreach my $biblionumber (@biblionumbers) {
152         # Moving subscriptions from the other record to the reference record
153         my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
154         if ($subcount > 0) {
155             $sth_subscription->execute($ref_biblionumber, $biblionumber);
156             $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
157         }
158
159     # Moving serials
160     $sth_serial->execute($ref_biblionumber, $biblionumber);
161
162     # Moving suggestions
163     $sth_suggestions->execute($ref_biblionumber, $biblionumber);
164
165     # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
166     my @allorders = GetOrdersByBiblionumber($biblionumber);
167     foreach my $myorder (@allorders) {
168         $myorder->{'biblionumber'} = $ref_biblionumber;
169         ModOrder ($myorder);
170     # TODO : add error control (in ModOrder?)
171     }
172
173     # Deleting the other records
174     if (scalar(@errors) == 0) {
175         # Move holds
176         MergeHolds($dbh, $ref_biblionumber, $biblionumber);
177         my $error = DelBiblio($biblionumber);
178         push @errors, $error if ($error);
179     }
180 }
181
182     # Parameters
183     $template->param(
184         result => 1,
185         report_records => \@report_records,
186         report_header => $report_header,
187         ref_biblionumber => scalar $input->param('ref_biblionumber')
188     );
189
190 #-------------------------
191 # Show records to merge
192 #-------------------------
193 } else {
194     my $ref_biblionumber = $input->param('ref_biblionumber');
195
196     if ($ref_biblionumber) {
197         my $framework = $input->param('frameworkcode');
198         $framework //= GetFrameworkCode($ref_biblionumber);
199
200         # Getting MARC Structure
201         my $tagslib = GetMarcStructure(1, $framework);
202
203         my $marcflavour = lc(C4::Context->preference('marcflavour'));
204
205         # Creating a loop for display
206         my @records;
207         foreach my $biblionumber (@biblionumbers) {
208             my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber });
209             my $frameworkcode = GetFrameworkCode($biblionumber);
210             my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
211             my $record = {
212                 recordid => $biblionumber,
213                 record => $marcrecord,
214                 frameworkcode => $frameworkcode,
215                 display => $recordObj->createMergeHash($tagslib),
216             };
217             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
218                 $record->{reference} = 1;
219                 $template->param(ref_record => $record);
220                 unshift @records, $record;
221             } else {
222                 push @records, $record;
223             }
224         }
225
226         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
227
228         # Parameters
229         $template->param(
230             ref_biblionumber => $ref_biblionumber,
231             records => \@records,
232             ref_record => $records[0],
233             framework => $framework,
234             biblionumbertag => $biblionumbertag,
235             MergeReportFields => C4::Context->preference('MergeReportFields'),
236         );
237     } else {
238         my @records;
239         foreach my $biblionumber (@biblionumbers) {
240             my $frameworkcode = GetFrameworkCode($biblionumber);
241             my $record = {
242                 biblionumber => $biblionumber,
243                 data => GetBiblioData($biblionumber),
244                 frameworkcode => $frameworkcode,
245             };
246             push @records, $record;
247         }
248         # Ask the user to choose which record will be the kept
249         $template->param(
250             choosereference => 1,
251             records => \@records,
252         );
253
254         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
255         $template->param( frameworks => $frameworks );
256     }
257 }
258
259 if (@errors) {
260     # Errors
261     $template->param( errors  => \@errors );
262 }
263
264 output_html_with_http_headers $input, $cookie, $template->output;
265 exit;