1 package Koha::BackgroundJob::BatchUpdateAuthority;
4 use Koha::BackgroundJobs;
5 use Koha::DateUtils qw( dt_from_string );
6 use JSON qw( encode_json decode_json );
8 use base 'Koha::BackgroundJob';
12 my ( $self, $args, $channel ) = @_;
14 my $job_type = $args->{job_type};
16 return unless exists $args->{job_id};
18 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
21 $job->started_on(dt_from_string)
22 ->progress($job_progress)
26 my $mmtid = $args->{mmtid};
27 my $record_type = $args->{record_type};
28 my @record_ids = @{ $args->{record_ids} };
35 my $dbh = C4::Context->dbh;
36 $dbh->{RaiseError} = 1;
37 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
38 $report->{total_records}++;
39 next unless $record_id;
41 my $authid = $record_id;
43 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
44 my $record = $authority->record;
45 ModifyRecordWithTemplate( $mmtid, $record );
46 ModAuthority( $authid, $record, $authority->authtypecode );
48 if ( $error and $error != $authid or $@ ) {
51 code => 'authority_not_modified',
53 error => ($@ ? $@ : 0),
58 code => 'authority_modified',
61 $report->{total_success}++;
63 $job->progress( ++$job_progress )->store;
66 my $job_data = decode_json $job->data;
67 $job_data->{messages} = \@messages;
68 $job_data->{report} = $report;
70 $job->ended_on(dt_from_string)
72 ->data(encode_json $job_data)
75 $channel->ack(); # FIXME Is that ok even on failure?
79 my ( $self, $args) = @_;
81 # TODO Raise exception instead
82 return unless exists $args->{mmtid};
83 return unless exists $args->{record_type}; #FIXME RMME
84 return unless exists $args->{record_ids};
86 my $mmtid = $args->{mmtid};
87 my $record_type = $args->{record_type};
88 my @record_ids = @{ $args->{record_ids} };
90 $self->SUPER::enqueue({
91 job_type => 'batch_record_modification',
92 job_size => scalar @record_ids,
93 job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}