Lines 48-53
BEGIN {
Link Here
|
48 |
AddItemsToImportBiblio |
48 |
AddItemsToImportBiblio |
49 |
ModAuthorityInBatch |
49 |
ModAuthorityInBatch |
50 |
ModBiblioInBatch |
50 |
ModBiblioInBatch |
|
|
51 |
CheckRecordControlNumberInBatch |
51 |
|
52 |
|
52 |
BatchStageMarcRecords |
53 |
BatchStageMarcRecords |
53 |
BatchFindDuplicates |
54 |
BatchFindDuplicates |
Lines 309-314
sub ModBiblioInBatch {
Link Here
|
309 |
|
310 |
|
310 |
} |
311 |
} |
311 |
|
312 |
|
|
|
313 |
=head2 CheckRecordControlNumberInBatch |
314 |
|
315 |
CheckRecordControlNumberInBatch( $marc_record, $batch_id[, $import_record_id] ); |
316 |
|
317 |
Check to see if the given record's control number is already in use in the given batch. If provided, |
318 |
will check only for conflicts in records besides the given C<$import_batch_id>. |
319 |
|
320 |
If a record with the given control number in the given batch exists, will return its C<import_record_id>. |
321 |
|
322 |
=cut |
323 |
|
324 |
sub CheckRecordControlNumberInBatch { |
325 |
my ( $marc_record, $batch_id, $import_record_id ) = @_; |
326 |
|
327 |
return unless ( $marc_record->field('001') ); |
328 |
|
329 |
my %extra_conditions; |
330 |
|
331 |
$extra_conditions{'import_record.import_record_id'} = { '!=', $import_record_id } if ( $import_record_id ); |
332 |
|
333 |
my $control_number = $marc_record->field('001')->data; |
334 |
|
335 |
my $schema = Koha::Database->new()->schema(); |
336 |
return $schema->resultset('ImportBiblio')->search( |
337 |
{ |
338 |
control_number => $control_number, |
339 |
'import_record.import_batch_id' => $batch_id, |
340 |
%extra_conditions |
341 |
}, |
342 |
{ |
343 |
join => 'import_record', |
344 |
} |
345 |
)->get_column('import_record.import_batch_id')->first(); |
346 |
} |
347 |
|
312 |
=head2 AddAuthToBatch |
348 |
=head2 AddAuthToBatch |
313 |
|
349 |
|
314 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
350 |
my $import_record_id = AddAuthToBatch($batch_id, $record_sequence, |
Lines 348-354
sub ModAuthInBatch {
Link Here
|
348 |
|
384 |
|
349 |
=head2 BatchStageMarcRecords |
385 |
=head2 BatchStageMarcRecords |
350 |
|
386 |
|
351 |
( $batch_id, $num_records, $num_items, @invalid_records ) = |
387 |
{ |
|
|
388 |
batch_id => $batch_id, |
389 |
num_records => $num_records, |
390 |
num_items => $num_items, |
391 |
invalid_record => \@invalid_records |
392 |
} = BatchStageMarcRecords({ |
393 |
record_type => $record_type, |
394 |
encoding => $encoding, |
395 |
marc_records => $marc_records, |
396 |
file_name => $file_name, |
397 |
comments => $comments, |
398 |
[ control_number_handling => 'preserve' | 'overwrite' ], |
399 |
[ leave_as_staging => 1 ] |
400 |
[ marc_modification_template => $marc_modification_template, ] |
401 |
[ parse_items => 1 ] |
402 |
[ progress_interval => $progress_interval, |
403 |
progress_callback => \&progress_callback, ] |
404 |
[ timestamp_update => 'now' ], |
405 |
}); |
406 |
|
407 |
Any optional parameters are assumed to be no / off if omitted. If C<progress_interval> is specified, C<progress_callback> must be as well. |
408 |
|
352 |
BatchStageMarcRecords( |
409 |
BatchStageMarcRecords( |
353 |
$record_type, $encoding, |
410 |
$record_type, $encoding, |
354 |
$marc_records, $file_name, |
411 |
$marc_records, $file_name, |
Lines 361-383
sub ModAuthInBatch {
Link Here
|
361 |
=cut |
418 |
=cut |
362 |
|
419 |
|
363 |
sub BatchStageMarcRecords { |
420 |
sub BatchStageMarcRecords { |
364 |
my $record_type = shift; |
421 |
my $params = shift; |
365 |
my $encoding = shift; |
|
|
366 |
my $marc_records = shift; |
367 |
my $file_name = shift; |
368 |
my $marc_modification_template = shift; |
369 |
my $comments = shift; |
370 |
my $branch_code = shift; |
371 |
my $parse_items = shift; |
372 |
my $leave_as_staging = shift; |
373 |
|
422 |
|
374 |
# optional callback to monitor status |
423 |
# optional callback to monitor status |
375 |
# of job |
424 |
# of job |
376 |
my $progress_interval = 0; |
425 |
my $progress_interval = 0; |
377 |
my $progress_callback = undef; |
426 |
my $progress_callback = undef; |
378 |
if ($#_ == 1) { |
427 |
if ( $params->{progress_interval} ) { |
379 |
$progress_interval = shift; |
428 |
$progress_interval = $params->{progress_interval}; |
380 |
$progress_callback = shift; |
429 |
$progress_callback = $params->{progress_callback}; |
381 |
$progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0; |
430 |
$progress_interval = 0 unless $progress_interval =~ /^\d+$/ and $progress_interval > 0; |
382 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
431 |
$progress_interval = 0 unless 'CODE' eq ref $progress_callback; |
383 |
} |
432 |
} |
Lines 386-404
sub BatchStageMarcRecords {
Link Here
|
386 |
overlay_action => 'create_new', |
435 |
overlay_action => 'create_new', |
387 |
import_status => 'staging', |
436 |
import_status => 'staging', |
388 |
batch_type => 'batch', |
437 |
batch_type => 'batch', |
389 |
file_name => $file_name, |
438 |
file_name => $params->{file_name}, |
390 |
comments => $comments, |
439 |
comments => $params->{comments}, |
391 |
record_type => $record_type, |
440 |
record_type => $params->{record_type}, |
392 |
} ); |
441 |
} ); |
393 |
if ($parse_items) { |
442 |
if ($params->{parse_items}) { |
394 |
SetImportBatchItemAction($batch_id, 'always_add'); |
443 |
SetImportBatchItemAction($batch_id, 'always_add'); |
395 |
} else { |
444 |
} else { |
396 |
SetImportBatchItemAction($batch_id, 'ignore'); |
445 |
SetImportBatchItemAction($batch_id, 'ignore'); |
397 |
} |
446 |
} |
398 |
|
447 |
|
399 |
|
|
|
400 |
my $marc_type = C4::Context->preference('marcflavour'); |
448 |
my $marc_type = C4::Context->preference('marcflavour'); |
401 |
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $record_type eq 'auth'); |
449 |
$marc_type .= 'AUTH' if ($marc_type eq 'UNIMARC' && $params->{record_type} eq 'auth'); |
402 |
my @invalid_records = (); |
450 |
my @invalid_records = (); |
403 |
my $num_valid = 0; |
451 |
my $num_valid = 0; |
404 |
my $num_items = 0; |
452 |
my $num_items = 0; |
Lines 410-416
sub BatchStageMarcRecords {
Link Here
|
410 |
&$progress_callback($rec_num); |
458 |
&$progress_callback($rec_num); |
411 |
} |
459 |
} |
412 |
|
460 |
|
413 |
ModifyRecordWithTemplate( $marc_modification_template, $marc_record ) if ( $marc_modification_template ); |
461 |
ModifyRecordWithTemplate( $params->{marc_modification_template}, $marc_record ) if ( $params->{marc_modification_template} ); |
414 |
|
462 |
|
415 |
my $import_record_id; |
463 |
my $import_record_id; |
416 |
if (scalar($marc_record->fields()) == 0) { |
464 |
if (scalar($marc_record->fields()) == 0) { |
Lines 419-443
sub BatchStageMarcRecords {
Link Here
|
419 |
|
467 |
|
420 |
# Normalize the record so it doesn't have separated diacritics |
468 |
# Normalize the record so it doesn't have separated diacritics |
421 |
SetUTF8Flag($marc_record); |
469 |
SetUTF8Flag($marc_record); |
422 |
|
|
|
423 |
$num_valid++; |
470 |
$num_valid++; |
424 |
if ($record_type eq 'biblio') { |
471 |
|
425 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0); |
472 |
C4::Biblio::UpdateMarcTimestamp( $marc_record ) if ( $params->{timestamp_update} eq 'now' ); |
426 |
if ($parse_items) { |
473 |
my $existing_import_record_id; |
|
|
474 |
|
475 |
if ( $params->{control_number_handling} ) { |
476 |
$existing_import_record_id = CheckRecordControlNumberInBatch( $marc_record, $batch_id ); |
477 |
|
478 |
if ( $existing_import_record_id ) { |
479 |
$num_matched_control_number++; |
480 |
|
481 |
next if ( $params->{control_number_handling} eq 'preserve' ); |
482 |
} |
483 |
} |
484 |
|
485 |
if ($params->{record_type} eq 'biblio') { |
486 |
if ( $existing_import_record_id ) { |
487 |
$import_record_id = $existing_import_record_id; |
488 |
ModBiblioInBatch( $import_record_id, $marc_record ); |
489 |
} else { |
490 |
$import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $params->{encoding}, int(rand(99999)), 0); |
491 |
} |
492 |
|
493 |
if ($params->{parse_items}) { |
427 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
494 |
my @import_items_ids = AddItemsToImportBiblio($batch_id, $import_record_id, $marc_record, 0); |
428 |
$num_items += scalar(@import_items_ids); |
495 |
$num_items += scalar(@import_items_ids); |
429 |
} |
496 |
} |
430 |
} elsif ($record_type eq 'auth') { |
497 |
} elsif ($params->{record_type} eq 'auth') { |
431 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $encoding, int(rand(99999)), 0, $marc_type); |
498 |
if ( $existing_import_record_id ) { |
|
|
499 |
$import_record_id = $existing_import_record_id; |
500 |
ModAuthInBatch( $import_record_id, $marc_record ); |
501 |
} else { |
502 |
$import_record_id = AddAuthToBatch($batch_id, $rec_num, $marc_record, $params->{encoding}, int(rand(99999)), 0, $marc_type); |
503 |
} |
432 |
} |
504 |
} |
433 |
} |
505 |
} |
434 |
} |
506 |
} |
435 |
unless ($leave_as_staging) { |
507 |
unless ($params->{leave_as_staging}) { |
436 |
SetImportBatchStatus($batch_id, 'staged'); |
508 |
SetImportBatchStatus($batch_id, 'staged'); |
437 |
} |
509 |
} |
438 |
# FIXME branch_code, number of bibs, number of items |
510 |
# FIXME branch_code, number of bibs, number of items |
439 |
_update_batch_record_counts($batch_id); |
511 |
_update_batch_record_counts($batch_id); |
440 |
return ($batch_id, $num_valid, $num_items, @invalid_records); |
512 |
return { |
|
|
513 |
batch_id => $batch_id, |
514 |
num_items => $num_items, |
515 |
num_valid => $num_valid, |
516 |
num_matched_control_number => $num_matched_control_number, |
517 |
invalid_records => \@invalid_records |
518 |
}; |
441 |
} |
519 |
} |
442 |
|
520 |
|
443 |
=head2 AddItemsToImportBiblio |
521 |
=head2 AddItemsToImportBiblio |
Lines 456-461
sub AddItemsToImportBiblio {
Link Here
|
456 |
my @import_items_ids = (); |
534 |
my @import_items_ids = (); |
457 |
|
535 |
|
458 |
my $dbh = C4::Context->dbh; |
536 |
my $dbh = C4::Context->dbh; |
|
|
537 |
$dbh->do( "DELETE FROM import_items WHERE import_record_id = ?", undef, $import_record_id ); |
459 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" ); |
538 |
my ($item_tag,$item_subfield) = &GetMarcFromKohaField( "items.itemnumber" ); |
460 |
foreach my $item_field ($marc_record->field($item_tag)) { |
539 |
foreach my $item_field ($marc_record->field($item_tag)) { |
461 |
my $item_marc = MARC::Record->new(); |
540 |
my $item_marc = MARC::Record->new(); |