3 # Copyright 2009 BibLibre
4 # Parts Copyright Catalyst IT 2011
6 # This file is part of Koha.
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.
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.
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>.
24 use C4::Output qw( output_html_with_http_headers );
25 use C4::Auth qw( get_template_and_user );
35 use C4::Serials qw( CountSubscriptionFromBiblionumber );
36 use C4::Reserves qw( MergeHolds );
37 use C4::Acquisition qw( ModOrder GetOrdersByBiblionumber );
39 use Koha::BiblioFrameworks;
42 use Koha::MetadataRecord;
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 template_name => "cataloguing/merge.tt",
50 flagsrequired => { editcatalogue => 'edit_catalogue' },
54 my @biblionumbers = $input->multi_param('biblionumber');
55 my $op = $input->param('op') || q{};
58 #------------------------
60 #------------------------
61 if ($op eq 'cud-merge') {
63 my $dbh = C4::Context->dbh;
65 # Creating a new record from the html code
66 my $record = TransformHtmlToMarc( $input, 1 );
67 my $ref_biblionumber = $input->param('ref_biblionumber');
68 @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers;
72 my $report_fields_str = $input->param('report_fields');
73 $report_fields_str ||= C4::Context->preference('MergeReportFields');
75 foreach my $field_str (split /,/, $report_fields_str) {
76 if ($field_str =~ /(\d{3})([0-9a-z]*)/) {
77 my ($field, $subfields) = ($1, $2);
78 push @report_fields, {
80 subfields => [ split //, $subfields ]
85 # Rewriting the leader
86 my $biblio = Koha::Biblios->find($ref_biblionumber);
87 $record->leader($biblio->metadata->record->leader());
88 #Take new framework code
89 my $frameworkcode = $input->param('frameworkcode');
91 # Modifying the reference record
92 ModBiblio($record, $ref_biblionumber, $frameworkcode);
94 my $report_header = {};
95 foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
97 my $biblio = Koha::Biblios->find($biblionumber);
98 my $marcrecord = $biblio->metadata->record;
100 biblionumber => $biblionumber,
103 foreach my $field (@report_fields) {
104 my @marcfields = $marcrecord->field($field->{tag});
105 foreach my $marcfield (@marcfields) {
106 my $tag = $marcfield->tag();
107 if (scalar @{$field->{subfields}}) {
108 foreach my $subfield (@{$field->{subfields}}) {
109 my @values = $marcfield->subfield($subfield);
110 $report_header->{ $tag . $subfield } = 1;
111 push @{ $report_record{fields}->{$tag . $subfield} }, @values;
113 } elsif ($field->{tag} gt '009') {
114 my @marcsubfields = $marcfield->subfields();
115 foreach my $marcsubfield (@marcsubfields) {
116 my ($code, $value) = @$marcsubfield;
117 $report_header->{ $tag . $code } = 1;
118 push @{ $report_record{fields}->{ $tag . $code } }, $value;
121 $report_header->{ $tag . '@' } = 1;
122 push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
126 push @report_records, \%report_record;
131 my $newbiblio = Koha::Biblios->find($ref_biblionumber);
132 $rmerge = $newbiblio->merge_with( \@biblionumbers );
141 report_records => \@report_records,
142 report_header => $report_header,
143 ref_biblionumber => scalar $input->param('ref_biblionumber')
146 #-------------------------
147 # Show records to merge
148 #-------------------------
150 my $ref_biblionumber = $input->param('ref_biblionumber');
152 if ($ref_biblionumber) {
153 my $framework = $input->param('frameworkcode');
154 $framework //= GetFrameworkCode($ref_biblionumber);
156 # Getting MARC Structure
157 my $tagslib = GetMarcStructure(1, $framework);
159 my $marcflavour = lc(C4::Context->preference('marcflavour'));
161 # Creating a loop for display
163 foreach my $biblionumber (@biblionumbers) {
164 my $biblio = Koha::Biblios->find($biblionumber);
165 my $marcrecord = $biblio->metadata->record;
166 my $frameworkcode = GetFrameworkCode($biblionumber);
167 my $recordObj = Koha::MetadataRecord->new({'record' => $marcrecord, schema => $marcflavour});
169 recordid => $biblionumber,
170 record => $marcrecord,
171 frameworkcode => $frameworkcode,
172 display => $recordObj->createMergeHash($tagslib),
174 if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
175 $record->{reference} = 1;
176 $template->param(ref_record => $record);
177 unshift @records, $record;
179 push @records, $record;
183 my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
187 ref_biblionumber => $ref_biblionumber,
188 records => \@records,
189 ref_record => $records[0],
190 framework => $framework,
191 biblionumbertag => $biblionumbertag,
192 MergeReportFields => C4::Context->preference('MergeReportFields'),
196 foreach my $biblionumber (@biblionumbers) {
197 my $frameworkcode = GetFrameworkCode($biblionumber);
199 biblionumber => $biblionumber,
200 data => GetBiblioData($biblionumber),
201 frameworkcode => $frameworkcode,
203 push @records, $record;
205 # Ask the user to choose which record will be the kept
207 choosereference => 1,
208 records => \@records,
211 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
212 $template->param( frameworks => $frameworks );
218 $template->param( errors => \@errors );
221 output_html_with_http_headers $input, $cookie, $template->output;