Bug 16154: CGI->multi_param - Force scalar context
[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 use Koha::MetadataRecord;
33
34 my $input = new CGI;
35 my @biblionumbers = $input->multi_param('biblionumber');
36 my $merge = $input->param('merge');
37
38 my @errors;
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "cataloguing/merge.tt",
43         query           => $input,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { editcatalogue => 'edit_catalogue' },
47     }
48 );
49
50 #------------------------
51 # Merging
52 #------------------------
53 if ($merge) {
54
55     my $dbh = C4::Context->dbh;
56
57     # Creating a new record from the html code
58     my $record       = TransformHtmlToMarc( $input, 1 );
59     my $ref_biblionumber = $input->param('ref_biblionumber');
60     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
61
62     # prepare report
63     my @report_records;
64     my $report_fields_str = $input->param('report_fields');
65     $report_fields_str ||= C4::Context->preference('MergeReportFields');
66     my @report_fields;
67     foreach my $field_str (split /,/, $report_fields_str) {
68         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
69             my ($field, $subfields) = ($1, $2);
70             push @report_fields, {
71                 tag => $field,
72                 subfields => [ split //, $subfields ]
73             }
74         }
75     }
76
77     # Rewriting the leader
78     $record->leader(GetMarcBiblio($ref_biblionumber)->leader());
79
80     my $frameworkcode = $input->param('frameworkcode');
81     my @notmoveditems;
82
83     # Modifying the reference record
84     ModBiblio($record, $ref_biblionumber, $frameworkcode);
85
86     # Moving items from the other record to the reference record
87     foreach my $biblionumber (@biblionumbers) {
88         my $itemnumbers = get_itemnumbers_of($biblionumber);
89         foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) {
90         my $res = MoveItemFromBiblio($itemnumber, $biblionumber, $ref_biblionumber);
91         if (not defined $res) {
92             push @notmoveditems, $itemnumber;
93         }
94     }
95     }
96     # If some items could not be moved :
97     if (scalar(@notmoveditems) > 0) {
98         my $itemlist = join(' ',@notmoveditems);
99         push @errors, { code => "CANNOT_MOVE", value => $itemlist };
100     }
101
102     my $sth_subscription = $dbh->prepare("
103         UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
104     ");
105     my $sth_subscriptionhistory = $dbh->prepare("
106         UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
107     ");
108     my $sth_serial = $dbh->prepare("
109         UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
110     ");
111
112     my $report_header = {};
113     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
114         # build report
115         my $marcrecord = GetMarcBiblio($biblionumber);
116         my %report_record = (
117             biblionumber => $biblionumber,
118             fields => {},
119         );
120         foreach my $field (@report_fields) {
121             my @marcfields = $marcrecord->field($field->{tag});
122             foreach my $marcfield (@marcfields) {
123                 my $tag = $marcfield->tag();
124                 if (scalar @{$field->{subfields}}) {
125                     foreach my $subfield (@{$field->{subfields}}) {
126                         my @values = $marcfield->subfield($subfield);
127                         $report_header->{ $tag . $subfield } = 1;
128                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
129                     }
130                 } elsif ($field->{tag} gt '009') {
131                     my @marcsubfields = $marcfield->subfields();
132                     foreach my $marcsubfield (@marcsubfields) {
133                         my ($code, $value) = @$marcsubfield;
134                         $report_header->{ $tag . $code } = 1;
135                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
136                     }
137                 } else {
138                     $report_header->{ $tag . '@' } = 1;
139                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
140                 }
141             }
142         }
143         push @report_records, \%report_record;
144     }
145
146     foreach my $biblionumber (@biblionumbers) {
147         # Moving subscriptions from the other record to the reference record
148         my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
149         if ($subcount > 0) {
150             $sth_subscription->execute($ref_biblionumber, $biblionumber);
151             $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
152         }
153
154     # Moving serials
155         $sth_serial->execute($ref_biblionumber, $biblionumber);
156
157     # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
158     my @allorders = GetOrdersByBiblionumber($biblionumber);
159     my @tobiblioitem = GetBiblioItemByBiblioNumber ($ref_biblionumber);
160     my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
161     foreach my $myorder (@allorders) {
162         $myorder->{'biblionumber'} = $ref_biblionumber;
163         ModOrder ($myorder);
164     # TODO : add error control (in ModOrder?)
165     }
166
167     # Deleting the other records
168     if (scalar(@errors) == 0) {
169         # Move holds
170         MergeHolds($dbh, $ref_biblionumber, $biblionumber);
171         my $error = DelBiblio($biblionumber);
172         push @errors, $error if ($error);
173     }
174 }
175
176     # Parameters
177     $template->param(
178         result => 1,
179         report_records => \@report_records,
180         report_header => $report_header,
181         ref_biblionumber => scalar $input->param('ref_biblionumber')
182     );
183
184 #-------------------------
185 # Show records to merge
186 #-------------------------
187 } else {
188     my $ref_biblionumber = $input->param('ref_biblionumber');
189
190     if ($ref_biblionumber) {
191         my $framework = $input->param('frameworkcode');
192         $framework //= GetFrameworkCode($ref_biblionumber);
193
194         # Getting MARC Structure
195         my $tagslib = GetMarcStructure(1, $framework);
196
197         my $marcflavour = lc(C4::Context->preference('marcflavour'));
198
199         # Creating a loop for display
200         my @records;
201         foreach my $biblionumber (@biblionumbers) {
202             my $marcrecord = GetMarcBiblio($biblionumber);
203             my $frameworkcode = GetFrameworkCode($biblionumber);
204             my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour});
205             my $record = {
206                 recordid => $biblionumber,
207                 record => $marcrecord,
208                 frameworkcode => $frameworkcode,
209                 display => $recordObj->createMergeHash($tagslib),
210             };
211             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
212                 $record->{reference} = 1;
213                 $template->param(ref_record => $record);
214                 unshift @records, $record;
215             } else {
216                 push @records, $record;
217             }
218         }
219
220         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
221
222         # Parameters
223         $template->param(
224             ref_biblionumber => $ref_biblionumber,
225             records => \@records,
226             ref_record => $records[0],
227             framework => $framework,
228             biblionumbertag => $biblionumbertag,
229             MergeReportFields => C4::Context->preference('MergeReportFields'),
230         );
231     } else {
232         my @records;
233         foreach my $biblionumber (@biblionumbers) {
234             my $frameworkcode = GetFrameworkCode($biblionumber);
235             my $record = {
236                 biblionumber => $biblionumber,
237                 data => GetBiblioData($biblionumber),
238                 frameworkcode => $frameworkcode,
239             };
240             push @records, $record;
241         }
242         # Ask the user to choose which record will be the kept
243         $template->param(
244             choosereference => 1,
245             records => \@records,
246         );
247
248         my $frameworks = getframeworks;
249         my @frameworkselect;
250         foreach my $thisframeworkcode ( keys %$frameworks ) {
251             my %row = (
252                 value         => $thisframeworkcode,
253                 frameworktext => $frameworks->{$thisframeworkcode}->{'frameworktext'},
254             );
255             push @frameworkselect, \%row;
256         }
257         $template->param(
258             frameworkselect => \@frameworkselect,
259         );
260     }
261 }
262
263 if (@errors) {
264     # Errors
265     $template->param( errors  => \@errors );
266 }
267
268 output_html_with_http_headers $input, $cookie, $template->output;
269 exit;