Bug 36652: Pass copy_form template variable
[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 qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Biblio qw(
27     DelBiblio
28     GetBiblioData
29     GetFrameworkCode
30     GetMarcFromKohaField
31     GetMarcStructure
32     ModBiblio
33     TransformHtmlToMarc
34 );
35 use C4::Serials qw( CountSubscriptionFromBiblionumber );
36 use C4::Reserves qw( MergeHolds );
37 use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
38
39 use Koha::BiblioFrameworks;
40 use Koha::Biblios;
41 use Koha::Items;
42 use Koha::MetadataRecord;
43
44 my $input = CGI->new;
45 my @biblionumbers = $input->multi_param('biblionumber');
46 my $merge = $input->param('merge');
47
48 my @errors;
49
50 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
51     {
52         template_name   => "cataloguing/merge.tt",
53         query           => $input,
54         type            => "intranet",
55         flagsrequired   => { editcatalogue => 'edit_catalogue' },
56     }
57 );
58
59 #------------------------
60 # Merging
61 #------------------------
62 if ($merge) {
63
64     my $dbh = C4::Context->dbh;
65
66     # Creating a new record from the html code
67     my $record       = TransformHtmlToMarc( $input, 1 );
68     my $ref_biblionumber = $input->param('ref_biblionumber');
69     @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
70
71     # prepare report
72     my @report_records;
73     my $report_fields_str = $input->param('report_fields');
74     $report_fields_str ||= C4::Context->preference('MergeReportFields');
75     my @report_fields;
76     foreach my $field_str (split /,/, $report_fields_str) {
77         if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
78             my ($field, $subfields) = ($1, $2);
79             push @report_fields, {
80                 tag => $field,
81                 subfields => [ split //, $subfields ]
82             }
83         }
84     }
85
86     # Rewriting the leader
87     my $biblio = Koha::Biblios->find($ref_biblionumber);
88     $record->leader($biblio->metadata->record->leader());
89     #Take new framework code
90     my $frameworkcode = $input->param('frameworkcode');
91
92     # Modifying the reference record
93     ModBiblio($record, $ref_biblionumber, $frameworkcode);
94
95     my $report_header = {};
96     foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
97         # build report
98         my $biblio = Koha::Biblios->find($biblionumber);
99         my $marcrecord = $biblio->metadata->record;
100         my %report_record = (
101             biblionumber => $biblionumber,
102             fields => {},
103         );
104         foreach my $field (@report_fields) {
105             my @marcfields = $marcrecord->field($field->{tag});
106             foreach my $marcfield (@marcfields) {
107                 my $tag = $marcfield->tag();
108                 if (scalar @{$field->{subfields}}) {
109                     foreach my $subfield (@{$field->{subfields}}) {
110                         my @values = $marcfield->subfield($subfield);
111                         $report_header->{ $tag . $subfield } = 1;
112                         push @{ $report_record{fields}->{$tag . $subfield} }, @values;
113                     }
114                 } elsif ($field->{tag} gt '009') {
115                     my @marcsubfields = $marcfield->subfields();
116                     foreach my $marcsubfield (@marcsubfields) {
117                         my ($code, $value) = @$marcsubfield;
118                         $report_header->{ $tag . $code } = 1;
119                         push @{ $report_record{fields}->{ $tag . $code } }, $value;
120                     }
121                 } else {
122                     $report_header->{ $tag . '@' } = 1;
123                     push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
124                 }
125             }
126         }
127         push @report_records, \%report_record;
128     }
129
130     my $rmerge;
131     eval {
132         my $newbiblio = Koha::Biblios->find($ref_biblionumber);
133         $rmerge = $newbiblio->merge_with( \@biblionumbers );
134     };
135     if ($@) {
136         push @errors, $@;
137     }
138
139     # Parameters
140     $template->param(
141         result           => 1,
142         report_records   => \@report_records,
143         report_header    => $report_header,
144         ref_biblionumber => scalar $input->param('ref_biblionumber')
145     );
146
147 #-------------------------
148 # Show records to merge
149 #-------------------------
150 } else {
151     my $ref_biblionumber = $input->param('ref_biblionumber');
152
153     if ($ref_biblionumber) {
154         my $framework = $input->param('frameworkcode');
155         $framework //= GetFrameworkCode($ref_biblionumber);
156
157         # Getting MARC Structure
158         my $tagslib = GetMarcStructure(1, $framework);
159
160         my $marcflavour = lc(C4::Context->preference('marcflavour'));
161
162         # Creating a loop for display
163         my @records;
164         foreach my $biblionumber (@biblionumbers) {
165             my $biblio = Koha::Biblios->find($biblionumber);
166             my $marcrecord = $biblio->metadata->record;
167             my $frameworkcode = GetFrameworkCode($biblionumber);
168             my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
169             my $record = {
170                 recordid => $biblionumber,
171                 record => $marcrecord,
172                 frameworkcode => $frameworkcode,
173                 display => $recordObj->createMergeHash($tagslib),
174             };
175             if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
176                 $record->{reference} = 1;
177                 $template->param(ref_record => $record);
178                 unshift @records, $record;
179             } else {
180                 push @records, $record;
181             }
182         }
183
184         my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
185
186         # Parameters
187         $template->param(
188             ref_biblionumber => $ref_biblionumber,
189             records => \@records,
190             ref_record => $records[0],
191             framework => $framework,
192             biblionumbertag => $biblionumbertag,
193             MergeReportFields => C4::Context->preference('MergeReportFields'),
194         );
195     } else {
196         my @records;
197         foreach my $biblionumber (@biblionumbers) {
198             my $frameworkcode = GetFrameworkCode($biblionumber);
199             my $record = {
200                 biblionumber => $biblionumber,
201                 data => GetBiblioData($biblionumber),
202                 frameworkcode => $frameworkcode,
203             };
204             push @records, $record;
205         }
206         # Ask the user to choose which record will be the kept
207         $template->param(
208             choosereference => 1,
209             records => \@records,
210         );
211
212         my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
213         $template->param( frameworks => $frameworks );
214     }
215 }
216
217 if (@errors) {
218     # Errors
219     $template->param( errors  => \@errors );
220 }
221
222 output_html_with_http_headers $input, $cookie, $template->output;
223 exit;