Bug 18417: Advanced Editor (Rancor) add shortcuts for copyright symbols (C) (P)
[koha.git] / cataloguing / merge.pl
blobee9f1fa067409cf45bc483b753a8743e3e2b38db
1 #!/usr/bin/perl
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>.
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
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/;
33 use Koha::BiblioFrameworks;
34 use Koha::MetadataRecord;
36 my $input = new CGI;
37 my @biblionumbers = $input->multi_param('biblionumber');
38 my $merge = $input->param('merge');
40 my @errors;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 template_name => "cataloguing/merge.tt",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => { editcatalogue => 'edit_catalogue' },
52 #------------------------
53 # Merging
54 #------------------------
55 if ($merge) {
57 my $dbh = C4::Context->dbh;
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;
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 ]
79 # Rewriting the leader
80 $record->leader(GetMarcBiblio($ref_biblionumber)->leader());
82 my $frameworkcode = $input->param('frameworkcode');
83 my @notmoveditems;
85 # Modifying the reference record
86 ModBiblio($record, $ref_biblionumber, $frameworkcode);
88 # Moving items from the other record to the reference record
89 foreach my $biblionumber (@biblionumbers) {
90 my $itemnumbers = get_itemnumbers_of($biblionumber);
91 foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) {
92 my $res = MoveItemFromBiblio($itemnumber, $biblionumber, $ref_biblionumber);
93 if (not defined $res) {
94 push @notmoveditems, $itemnumber;
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 };
104 my $sth_subscription = $dbh->prepare("
105 UPDATE subscription SET biblionumber = ? WHERE biblionumber = ?
107 my $sth_subscriptionhistory = $dbh->prepare("
108 UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ?
110 my $sth_serial = $dbh->prepare("
111 UPDATE serial SET biblionumber = ? WHERE biblionumber = ?
114 my $report_header = {};
115 foreach my $biblionumber ($ref_biblionumber, @biblionumbers) {
116 # build report
117 my $marcrecord = GetMarcBiblio($biblionumber);
118 my %report_record = (
119 biblionumber => $biblionumber,
120 fields => {},
122 foreach my $field (@report_fields) {
123 my @marcfields = $marcrecord->field($field->{tag});
124 foreach my $marcfield (@marcfields) {
125 my $tag = $marcfield->tag();
126 if (scalar @{$field->{subfields}}) {
127 foreach my $subfield (@{$field->{subfields}}) {
128 my @values = $marcfield->subfield($subfield);
129 $report_header->{ $tag . $subfield } = 1;
130 push @{ $report_record{fields}->{$tag . $subfield} }, @values;
132 } elsif ($field->{tag} gt '009') {
133 my @marcsubfields = $marcfield->subfields();
134 foreach my $marcsubfield (@marcsubfields) {
135 my ($code, $value) = @$marcsubfield;
136 $report_header->{ $tag . $code } = 1;
137 push @{ $report_record{fields}->{ $tag . $code } }, $value;
139 } else {
140 $report_header->{ $tag . '@' } = 1;
141 push @{ $report_record{fields}->{ $tag .'@' } }, $marcfield->data();
145 push @report_records, \%report_record;
148 foreach my $biblionumber (@biblionumbers) {
149 # Moving subscriptions from the other record to the reference record
150 my $subcount = CountSubscriptionFromBiblionumber($biblionumber);
151 if ($subcount > 0) {
152 $sth_subscription->execute($ref_biblionumber, $biblionumber);
153 $sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber);
156 # Moving serials
157 $sth_serial->execute($ref_biblionumber, $biblionumber);
159 # Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio)
160 my @allorders = GetOrdersByBiblionumber($biblionumber);
161 my @tobiblioitem = GetBiblioItemByBiblioNumber ($ref_biblionumber);
162 my $tobiblioitem_biblioitemnumber = $tobiblioitem [0]-> {biblioitemnumber };
163 foreach my $myorder (@allorders) {
164 $myorder->{'biblionumber'} = $ref_biblionumber;
165 ModOrder ($myorder);
166 # TODO : add error control (in ModOrder?)
169 # Deleting the other records
170 if (scalar(@errors) == 0) {
171 # Move holds
172 MergeHolds($dbh, $ref_biblionumber, $biblionumber);
173 my $error = DelBiblio($biblionumber);
174 push @errors, $error if ($error);
178 # Parameters
179 $template->param(
180 result => 1,
181 report_records => \@report_records,
182 report_header => $report_header,
183 ref_biblionumber => scalar $input->param('ref_biblionumber')
186 #-------------------------
187 # Show records to merge
188 #-------------------------
189 } else {
190 my $ref_biblionumber = $input->param('ref_biblionumber');
192 if ($ref_biblionumber) {
193 my $framework = $input->param('frameworkcode');
194 $framework //= GetFrameworkCode($ref_biblionumber);
196 # Getting MARC Structure
197 my $tagslib = GetMarcStructure(1, $framework);
199 my $marcflavour = lc(C4::Context->preference('marcflavour'));
201 # Creating a loop for display
202 my @records;
203 foreach my $biblionumber (@biblionumbers) {
204 my $marcrecord = GetMarcBiblio($biblionumber);
205 my $frameworkcode = GetFrameworkCode($biblionumber);
206 my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour});
207 my $record = {
208 recordid => $biblionumber,
209 record => $marcrecord,
210 frameworkcode => $frameworkcode,
211 display => $recordObj->createMergeHash($tagslib),
213 if ($ref_biblionumber and $ref_biblionumber == $biblionumber) {
214 $record->{reference} = 1;
215 $template->param(ref_record => $record);
216 unshift @records, $record;
217 } else {
218 push @records, $record;
222 my ($biblionumbertag) = GetMarcFromKohaField('biblio.biblionumber');
224 # Parameters
225 $template->param(
226 ref_biblionumber => $ref_biblionumber,
227 records => \@records,
228 ref_record => $records[0],
229 framework => $framework,
230 biblionumbertag => $biblionumbertag,
231 MergeReportFields => C4::Context->preference('MergeReportFields'),
233 } else {
234 my @records;
235 foreach my $biblionumber (@biblionumbers) {
236 my $frameworkcode = GetFrameworkCode($biblionumber);
237 my $record = {
238 biblionumber => $biblionumber,
239 data => GetBiblioData($biblionumber),
240 frameworkcode => $frameworkcode,
242 push @records, $record;
244 # Ask the user to choose which record will be the kept
245 $template->param(
246 choosereference => 1,
247 records => \@records,
250 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] });
251 $template->param( frameworks => $frameworks );
255 if (@errors) {
256 # Errors
257 $template->param( errors => \@errors );
260 output_html_with_http_headers $input, $cookie, $template->output;
261 exit;