1 package Koha::BackgroundJob::BatchUpdateAuthority;
4 use JSON qw( encode_json decode_json );
6 use C4::MarcModificationTemplates;
7 use C4::AuthoritiesMarc;
8 use Koha::BackgroundJobs;
9 use Koha::DateUtils qw( dt_from_string );
10 use Koha::MetadataRecord::Authority;
12 use base 'Koha::BackgroundJob';
15 return 'batch_authority_record_modification';
19 my ( $self, $args ) = @_;
21 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
23 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
28 $job->started_on(dt_from_string)
29 ->progress($job_progress)
33 my $mmtid = $args->{mmtid};
34 my @record_ids = @{ $args->{record_ids} };
37 total_records => scalar @record_ids,
41 my $dbh = C4::Context->dbh;
42 $dbh->{RaiseError} = 1;
43 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
44 next unless $record_id;
46 my $authid = $record_id;
48 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
49 my $record = $authority->record;
50 ModifyRecordWithTemplate( $mmtid, $record );
51 ModAuthority( $authid, $record, $authority->authtypecode );
53 if ( $error and $error != $authid or $@ ) {
56 code => 'authority_not_modified',
58 error => ($@ ? $@ : 0),
63 code => 'authority_modified',
66 $report->{total_success}++;
68 $job->progress( ++$job_progress )->store;
71 my $job_data = decode_json $job->data;
72 $job_data->{messages} = \@messages;
73 $job_data->{report} = $report;
75 $job->ended_on(dt_from_string)
76 ->data(encode_json $job_data);
77 $job->status('finished') if $job->status ne 'cancelled';
83 my ( $self, $args) = @_;
85 # TODO Raise exception instead
86 return unless exists $args->{mmtid};
87 return unless exists $args->{record_ids};
89 my $mmtid = $args->{mmtid};
90 my @record_ids = @{ $args->{record_ids} };
92 $self->SUPER::enqueue({
93 job_size => scalar @record_ids,
94 job_args => {mmtid => $mmtid, record_ids => \@record_ids,}