Lines 29-41
use C4::Serials;
Link Here
|
29 |
use C4::Koha; |
29 |
use C4::Koha; |
30 |
use C4::Reserves qw/MergeHolds/; |
30 |
use C4::Reserves qw/MergeHolds/; |
31 |
use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/; |
31 |
use C4::Acquisition qw/ModOrder GetOrdersByBiblionumber/; |
32 |
|
32 |
use C4::ImportBatch; |
33 |
use Koha::BiblioFrameworks; |
33 |
use Koha::BiblioFrameworks; |
34 |
use Koha::Items; |
34 |
use Koha::Items; |
35 |
use Koha::MetadataRecord; |
35 |
use Koha::MetadataRecord; |
36 |
|
36 |
|
37 |
my $input = new CGI; |
37 |
my $input = new CGI; |
38 |
my @biblionumbers = $input->multi_param('biblionumber'); |
38 |
my @record_paths = $input->multi_param('record'); |
39 |
my $merge = $input->param('merge'); |
39 |
my $merge = $input->param('merge'); |
40 |
|
40 |
|
41 |
my @errors; |
41 |
my @errors; |
Lines 50-66
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
Link Here
|
50 |
} |
50 |
} |
51 |
); |
51 |
); |
52 |
|
52 |
|
|
|
53 |
my $sources = { |
54 |
catalog => { |
55 |
delete => \&_delete_catalog, |
56 |
fetch => \&_fetch_catalog, |
57 |
get_link => \&_get_link_catalog, |
58 |
get_preview_link => \&_get_preview_link_catalog, |
59 |
merge_other => \&_merge_other_catalog, |
60 |
update => \&_update_catalog, |
61 |
}, |
62 |
batch => { |
63 |
fetch => \&_fetch_import_batch, |
64 |
get_link => \&_get_preview_link_import_batch, |
65 |
get_preview_link => \&_get_preview_link_import_batch, |
66 |
update => \&_update_import_batch, |
67 |
}, |
68 |
}; |
69 |
|
70 |
sub split_record_path { |
71 |
my ($source, $num) = split /\//, shift, 2; |
72 |
$source =~ s/-.*//; |
73 |
|
74 |
return ( $source, $num ); |
75 |
} |
76 |
|
53 |
#------------------------ |
77 |
#------------------------ |
54 |
# Merging |
78 |
# Merging |
55 |
#------------------------ |
79 |
#------------------------ |
56 |
if ($merge) { |
80 |
if ($merge) { |
57 |
|
81 |
|
58 |
my $dbh = C4::Context->dbh; |
|
|
59 |
|
60 |
# Creating a new record from the html code |
82 |
# Creating a new record from the html code |
61 |
my $record = TransformHtmlToMarc( $input, 1 ); |
83 |
my $record = TransformHtmlToMarc( $input ); |
62 |
my $ref_biblionumber = $input->param('ref_biblionumber'); |
84 |
my $ref_record_path = $input->param('ref_record'); |
63 |
@biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers; |
85 |
|
|
|
86 |
my ( $ref_record_source, $ref_record_num ) = split_record_path $ref_record_path; |
87 |
my @other_record_paths = map +[ split_record_path $_ ], grep { $_ ne $ref_record_path } @record_paths; |
64 |
|
88 |
|
65 |
# prepare report |
89 |
# prepare report |
66 |
my @report_records; |
90 |
my @report_records; |
Lines 78-126
if ($merge) {
Link Here
|
78 |
} |
102 |
} |
79 |
|
103 |
|
80 |
# Rewriting the leader |
104 |
# Rewriting the leader |
81 |
$record->leader(GetMarcBiblio({ biblionumber => $ref_biblionumber })->leader()); |
105 |
my $orig_ref_record = $sources->{$ref_record_source}->{fetch}($ref_record_num); |
|
|
106 |
$record->leader( $orig_ref_record->leader() ); |
82 |
|
107 |
|
83 |
my $frameworkcode = $input->param('frameworkcode'); |
108 |
my $frameworkcode = $input->param('frameworkcode'); |
84 |
my @notmoveditems; |
109 |
my @notmoveditems; |
85 |
|
110 |
|
86 |
# Modifying the reference record |
111 |
# Modifying the reference record |
87 |
ModBiblio($record, $ref_biblionumber, $frameworkcode); |
112 |
$sources->{$ref_record_source}->{update}( $record, $ref_record_num, $frameworkcode ); |
88 |
|
113 |
|
89 |
# Moving items from the other record to the reference record |
114 |
# Are all the records from the same source? |
90 |
foreach my $biblionumber (@biblionumbers) { |
115 |
my $all_records_same_source = 1; |
91 |
my $items = Koha::Items->search({ biblionumber => $biblionumber }); |
116 |
|
92 |
while ( my $item = $items->next) { |
117 |
# Merging in information from other records |
93 |
my $res = MoveItemFromBiblio( $item->itemnumber, $biblionumber, $ref_biblionumber ); |
118 |
foreach my $record_path (@other_record_paths) { |
94 |
if ( not defined $res ) { |
119 |
my ( $record_source, $record_num ) = @$record_path; |
95 |
push @notmoveditems, $item->itemnumber; |
120 |
|
|
|
121 |
if ( $record_source eq $ref_record_source ) { |
122 |
# We only want to merge in information from the other records if they're the same kind. |
123 |
# |
124 |
# We can't merge serials from an import bib to a catalog bib, for instance. |
125 |
my @record_errors = $sources->{$record_source}->{merge_other}( $record_num, $ref_record_num ); |
126 |
|
127 |
if ( @record_errors ) { |
128 |
push @errors, @record_errors; |
129 |
} else { |
130 |
push @errors, $sources->{$record_source}->{delete}( $record_num ); |
96 |
} |
131 |
} |
|
|
132 |
} else { |
133 |
$all_records_same_source = 0; |
97 |
} |
134 |
} |
98 |
} |
135 |
} |
99 |
# If some items could not be moved : |
|
|
100 |
if (scalar(@notmoveditems) > 0) { |
101 |
my $itemlist = join(' ',@notmoveditems); |
102 |
push @errors, { code => "CANNOT_MOVE", value => $itemlist }; |
103 |
} |
104 |
|
105 |
my $sth_subscription = $dbh->prepare(" |
106 |
UPDATE subscription SET biblionumber = ? WHERE biblionumber = ? |
107 |
"); |
108 |
my $sth_subscriptionhistory = $dbh->prepare(" |
109 |
UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ? |
110 |
"); |
111 |
my $sth_serial = $dbh->prepare(" |
112 |
UPDATE serial SET biblionumber = ? WHERE biblionumber = ? |
113 |
"); |
114 |
my $sth_suggestions = $dbh->prepare(" |
115 |
UPDATE suggestions SET biblionumber = ? WHERE biblionumber = ? |
116 |
"); |
117 |
|
136 |
|
118 |
my $report_header = {}; |
137 |
my $report_header = {}; |
119 |
foreach my $biblionumber ($ref_biblionumber, @biblionumbers) { |
138 |
foreach my $record_path ( [ $ref_record_source, $ref_record_num ], @other_record_paths ) { |
|
|
139 |
my ( $record_source, $record_num ) = @$record_path; |
140 |
|
120 |
# build report |
141 |
# build report |
121 |
my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber }); |
142 |
my $marcrecord = $sources->{$record_source}->{fetch}($record_num); |
122 |
my %report_record = ( |
143 |
my %report_record = ( |
123 |
biblionumber => $biblionumber, |
144 |
record_num => $record_num, |
124 |
fields => {}, |
145 |
fields => {}, |
125 |
); |
146 |
); |
126 |
foreach my $field (@report_fields) { |
147 |
foreach my $field (@report_fields) { |
Lines 149-202
if ($merge) {
Link Here
|
149 |
push @report_records, \%report_record; |
170 |
push @report_records, \%report_record; |
150 |
} |
171 |
} |
151 |
|
172 |
|
152 |
foreach my $biblionumber (@biblionumbers) { |
|
|
153 |
# Moving subscriptions from the other record to the reference record |
154 |
my $subcount = CountSubscriptionFromBiblionumber($biblionumber); |
155 |
if ($subcount > 0) { |
156 |
$sth_subscription->execute($ref_biblionumber, $biblionumber); |
157 |
$sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber); |
158 |
} |
159 |
|
160 |
# Moving serials |
161 |
$sth_serial->execute($ref_biblionumber, $biblionumber); |
162 |
|
163 |
# Moving suggestions |
164 |
$sth_suggestions->execute($ref_biblionumber, $biblionumber); |
165 |
|
166 |
# Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio) |
167 |
my @allorders = GetOrdersByBiblionumber($biblionumber); |
168 |
foreach my $myorder (@allorders) { |
169 |
$myorder->{'biblionumber'} = $ref_biblionumber; |
170 |
ModOrder ($myorder); |
171 |
# TODO : add error control (in ModOrder?) |
172 |
} |
173 |
|
174 |
# Deleting the other records |
175 |
if (scalar(@errors) == 0) { |
176 |
# Move holds |
177 |
MergeHolds($dbh, $ref_biblionumber, $biblionumber); |
178 |
my $error = DelBiblio($biblionumber); |
179 |
push @errors, $error if ($error); |
180 |
} |
181 |
} |
182 |
|
183 |
# Parameters |
173 |
# Parameters |
184 |
$template->param( |
174 |
$template->param( |
|
|
175 |
all_records_same_source => $all_records_same_source, |
185 |
result => 1, |
176 |
result => 1, |
186 |
report_records => \@report_records, |
177 |
report_records => \@report_records, |
187 |
report_header => $report_header, |
178 |
report_header => $report_header, |
188 |
ref_biblionumber => scalar $input->param('ref_biblionumber') |
179 |
ref_record_path => scalar $input->param('ref_record'), |
|
|
180 |
ref_record_id => "$ref_record_source-$ref_record_num", |
181 |
ref_record_link => $sources->{$ref_record_source}->{get_link}($ref_record_num), |
189 |
); |
182 |
); |
190 |
|
183 |
|
191 |
#------------------------- |
184 |
#------------------------- |
192 |
# Show records to merge |
185 |
# Show records to merge |
193 |
#------------------------- |
186 |
#------------------------- |
194 |
} else { |
187 |
} else { |
195 |
my $ref_biblionumber = $input->param('ref_biblionumber'); |
188 |
my $ref_record_path = $input->param('ref_record'); |
|
|
189 |
|
190 |
if ($ref_record_path) { |
191 |
my ( $ref_record_source, $ref_record_num ) = split_record_path $ref_record_path; |
196 |
|
192 |
|
197 |
if ($ref_biblionumber) { |
|
|
198 |
my $framework = $input->param('frameworkcode'); |
193 |
my $framework = $input->param('frameworkcode'); |
199 |
$framework //= GetFrameworkCode($ref_biblionumber); |
194 |
$framework //= GetFrameworkCode($ref_record_num) if $ref_record_source eq 'catalog'; |
|
|
195 |
$framework //= ''; |
200 |
|
196 |
|
201 |
# Getting MARC Structure |
197 |
# Getting MARC Structure |
202 |
my $tagslib = GetMarcStructure(1, $framework); |
198 |
my $tagslib = GetMarcStructure(1, $framework); |
Lines 205-223
if ($merge) {
Link Here
|
205 |
|
201 |
|
206 |
# Creating a loop for display |
202 |
# Creating a loop for display |
207 |
my @records; |
203 |
my @records; |
208 |
foreach my $biblionumber (@biblionumbers) { |
204 |
foreach my $record_path ( $input->param('record') ) { |
209 |
my $marcrecord = GetMarcBiblio({ biblionumber => $biblionumber }); |
205 |
my ( $record_source, $record_num ) = split_record_path $record_path; |
210 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
206 |
my $marcrecord = $sources->{$record_source}->{fetch}($record_num); |
|
|
207 |
# FIXME: We assume the default framework for import records. |
208 |
my $frameworkcode = $record_source eq 'catalog' ? GetFrameworkCode($record_num) : ''; |
211 |
my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour}); |
209 |
my $recordObj = new Koha::MetadataRecord({'record' => $marcrecord, schema => $marcflavour}); |
212 |
my $record = { |
210 |
my $record = { |
213 |
recordid => $biblionumber, |
211 |
record_path => $record_path, |
|
|
212 |
recordid => "$record_source-$record_num", |
213 |
record_source => $record_source, |
214 |
record_num => $record_num, |
214 |
record => $marcrecord, |
215 |
record => $marcrecord, |
215 |
frameworkcode => $frameworkcode, |
216 |
frameworkcode => $frameworkcode, |
216 |
display => $recordObj->createMergeHash($tagslib), |
217 |
display => $recordObj->createMergeHash($tagslib), |
217 |
}; |
218 |
}; |
218 |
if ($ref_biblionumber and $ref_biblionumber == $biblionumber) { |
219 |
if ($ref_record_path and $ref_record_path eq $record_path) { |
219 |
$record->{reference} = 1; |
220 |
$record->{reference} = 1; |
220 |
$template->param(ref_record => $record); |
221 |
$template->param(ref_record_path => $record); |
221 |
unshift @records, $record; |
222 |
unshift @records, $record; |
222 |
} else { |
223 |
} else { |
223 |
push @records, $record; |
224 |
push @records, $record; |
Lines 228-234
if ($merge) {
Link Here
|
228 |
|
229 |
|
229 |
# Parameters |
230 |
# Parameters |
230 |
$template->param( |
231 |
$template->param( |
231 |
ref_biblionumber => $ref_biblionumber, |
232 |
ref_record_path => $ref_record_path, |
|
|
233 |
ref_recordid => "$ref_record_source-$ref_record_num", |
232 |
records => \@records, |
234 |
records => \@records, |
233 |
ref_record => $records[0], |
235 |
ref_record => $records[0], |
234 |
framework => $framework, |
236 |
framework => $framework, |
Lines 237-247
if ($merge) {
Link Here
|
237 |
); |
239 |
); |
238 |
} else { |
240 |
} else { |
239 |
my @records; |
241 |
my @records; |
240 |
foreach my $biblionumber (@biblionumbers) { |
242 |
foreach my $record_path ( $input->multi_param('record') ) { |
241 |
my $frameworkcode = GetFrameworkCode($biblionumber); |
243 |
my ( $record_source, $record_num ) = split_record_path $record_path; |
|
|
244 |
|
245 |
my $marcrecord = $sources->{$record_source}->{fetch}($record_num); |
246 |
# FIXME: We assume the default framework for import records. |
247 |
my $frameworkcode = $record_source eq 'catalog' ? GetFrameworkCode($record_num) : ''; |
242 |
my $record = { |
248 |
my $record = { |
243 |
biblionumber => $biblionumber, |
249 |
record_path => $record_path, |
244 |
data => GetBiblioData($biblionumber), |
250 |
recordid => "$record_path-$record_num", |
|
|
251 |
record_source => $record_source, |
252 |
record_num => $record_num, |
253 |
preview_link => $sources->{$record_source}->{get_preview_link}($record_num), |
254 |
data => TransformMarcToKoha( $marcrecord, $frameworkcode ), |
245 |
frameworkcode => $frameworkcode, |
255 |
frameworkcode => $frameworkcode, |
246 |
}; |
256 |
}; |
247 |
push @records, $record; |
257 |
push @records, $record; |
Lines 263-266
if (@errors) {
Link Here
|
263 |
} |
273 |
} |
264 |
|
274 |
|
265 |
output_html_with_http_headers $input, $cookie, $template->output; |
275 |
output_html_with_http_headers $input, $cookie, $template->output; |
266 |
exit; |
276 |
|
|
|
277 |
sub _delete_catalog { |
278 |
my ( $biblionumber ) = @_; |
279 |
return DelBiblio($biblionumber); |
280 |
} |
281 |
|
282 |
sub _fetch_catalog { |
283 |
my ( $biblionumber ) = @_; |
284 |
|
285 |
return GetMarcBiblio({ biblionumber => $biblionumber }); |
286 |
} |
287 |
|
288 |
sub _get_link_catalog { |
289 |
my ( $biblionumber ) = @_; |
290 |
|
291 |
return "/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber"; |
292 |
} |
293 |
|
294 |
sub _get_preview_link_catalog { |
295 |
my ( $biblionumber ) = @_; |
296 |
|
297 |
return "/cgi-bin/koha/catalogue/showmarc.pl?id=$biblionumber"; |
298 |
} |
299 |
|
300 |
sub _merge_other_catalog { |
301 |
my ( $biblionumber, $ref_biblionumber ) = @_; |
302 |
|
303 |
my $dbh = C4::Context->dbh; |
304 |
my @errors; |
305 |
|
306 |
my @notmoveditems; |
307 |
my $itemnumbers = get_itemnumbers_of($biblionumber); |
308 |
foreach my $itemnumber (@{ $itemnumbers->{$biblionumber} }) { |
309 |
if ( !MoveItemFromBiblio( $itemnumber, $biblionumber, $ref_biblionumber ) ) { |
310 |
push @notmoveditems, $itemnumber; |
311 |
} |
312 |
} |
313 |
|
314 |
# If some items could not be moved : |
315 |
if (scalar(@notmoveditems) > 0) { |
316 |
my $itemlist = join(' ',@notmoveditems); |
317 |
push @errors, { code => "CANNOT_MOVE", value => $itemlist }; |
318 |
} |
319 |
|
320 |
my $sth_subscription = $dbh->prepare(" |
321 |
UPDATE subscription SET biblionumber = ? WHERE biblionumber = ? |
322 |
"); |
323 |
my $sth_subscriptionhistory = $dbh->prepare(" |
324 |
UPDATE subscriptionhistory SET biblionumber = ? WHERE biblionumber = ? |
325 |
"); |
326 |
my $sth_serial = $dbh->prepare(" |
327 |
UPDATE serial SET biblionumber = ? WHERE biblionumber = ? |
328 |
"); |
329 |
|
330 |
# Moving subscriptions from the other record to the reference record |
331 |
my $subcount = CountSubscriptionFromBiblionumber($biblionumber); |
332 |
if ($subcount > 0) { |
333 |
$sth_subscription->execute($ref_biblionumber, $biblionumber); |
334 |
$sth_subscriptionhistory->execute($ref_biblionumber, $biblionumber); |
335 |
} |
336 |
|
337 |
# Moving serials |
338 |
$sth_serial->execute($ref_biblionumber, $biblionumber); |
339 |
|
340 |
# Moving orders (orders linked to items of frombiblio have already been moved by MoveItemFromBiblio) |
341 |
foreach my $myorder( GetOrdersByBiblionumber($biblionumber) ) { |
342 |
$myorder->{'biblionumber'} = $ref_biblionumber; |
343 |
ModOrder ($myorder); |
344 |
# TODO : add error control (in ModOrder?) |
345 |
} |
346 |
|
347 |
if (scalar(@errors) == 0) { |
348 |
MergeHolds($dbh, $ref_biblionumber, $biblionumber); |
349 |
} |
350 |
|
351 |
return @errors; |
352 |
} |
353 |
|
354 |
sub _update_catalog { |
355 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
356 |
ModBiblio( $record, $biblionumber, $frameworkcode ); |
357 |
} |
358 |
|
359 |
sub _fetch_import_batch { |
360 |
my ( $import_recordid ) = @_; |
361 |
return C4::ImportBatch::GetRecordFromImportBiblio( $import_recordid ); |
362 |
} |
363 |
|
364 |
sub _get_preview_link_import_batch { |
365 |
my ( $import_recordid ) = @_; |
366 |
|
367 |
return "/cgi-bin/koha/catalogue/showmarc.pl?importid=$import_recordid"; |
368 |
} |
369 |
|
370 |
sub _update_import_batch { |
371 |
my ( $record, $import_recordid ) = @_; |
372 |
ModBiblioInBatch( $import_recordid, $record ); |
373 |
} |