1 package Koha::BackgroundJob::BatchUpdateAuthority;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use JSON qw( decode_json encode_json );
21 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
22 use C4::AuthoritiesMarc;
23 use Koha::BackgroundJobs;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::MetadataRecord::Authority;
27 use base 'Koha::BackgroundJob';
31 Koha::BackgroundJob::BatchUpdateAuthority - Batch update authorities
33 This is a subclass of Koha::BackgroundJob.
41 Define the job type of this job: batch_authority_record_modification
46 return 'batch_authority_record_modification';
51 Process the modification.
56 my ( $self, $args ) = @_;
58 if ( $self->status eq 'cancelled' ) {
63 $self->started_on(dt_from_string)
64 ->progress($job_progress)
68 my $mmtid = $args->{mmtid};
69 my @record_ids = @{ $args->{record_ids} };
72 total_records => scalar @record_ids,
76 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
77 next unless $record_id;
79 my $authid = $record_id;
81 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
82 my $record = $authority->record;
83 ModifyRecordWithTemplate( $mmtid, $record );
84 ModAuthority( $authid, $record, $authority->authtypecode );
86 if ( $error and $error != $authid or $@ ) {
89 code => 'authority_not_modified',
91 error => ($@ ? "$@" : 0),
96 code => 'authority_modified',
99 $report->{total_success}++;
101 $self->progress( ++$job_progress )->store;
104 my $job_data = decode_json $self->data;
105 $job_data->{messages} = \@messages;
106 $job_data->{report} = $report;
108 $self->ended_on(dt_from_string)
109 ->data(encode_json $job_data);
110 $self->status('finished') if $self->status ne 'cancelled';
122 my ( $self, $args) = @_;
124 # TODO Raise exception instead
125 return unless exists $args->{mmtid};
126 return unless exists $args->{record_ids};
128 my $mmtid = $args->{mmtid};
129 my @record_ids = @{ $args->{record_ids} };
131 $self->SUPER::enqueue({
132 job_size => scalar @record_ids,
133 job_args => {mmtid => $mmtid, record_ids => \@record_ids,},
134 queue => 'long_tasks',