1 package Koha::BackgroundJob::BatchUpdateBiblio;
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 Koha::BackgroundJobs;
22 use Koha::DateUtils qw( dt_from_string );
23 use Koha::Virtualshelves;
27 use C4::MarcModificationTemplates;
29 use base 'Koha::BackgroundJob';
33 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
35 This is a subclass of Koha::BackgroundJob.
43 Define the job type of this job: batch_biblio_record_modification
48 return 'batch_biblio_record_modification';
53 Process the modification.
58 my ( $self, $args ) = @_;
60 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
62 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
66 # FIXME If the job has already been started, but started again (worker has been restart for instance)
67 # Then we will start from scratch and so double process the same records
70 $job->started_on(dt_from_string)
71 ->progress($job_progress)
75 my $mmtid = $args->{mmtid};
76 my @record_ids = @{ $args->{record_ids} };
79 total_records => scalar @record_ids,
83 RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
85 last if $job->get_from_storage->status eq 'cancelled';
87 next unless $biblionumber;
91 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
92 C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
93 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
94 C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode, {
95 overlay_context => $args->{overlay_context},
98 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
101 code => 'biblio_not_modified',
102 biblionumber => $biblionumber,
103 error => ($@ ? "$@" : $error),
108 code => 'biblio_modified',
109 biblionumber => $biblionumber,
111 $report->{total_success}++;
113 $job->progress( ++$job_progress )->store;
116 my $job_data = decode_json $job->data;
117 $job_data->{messages} = \@messages;
118 $job_data->{report} = $report;
120 $job->ended_on(dt_from_string)
121 ->data(encode_json $job_data);
122 $job->status('finished') if $job->status ne 'cancelled';
133 my ( $self, $args) = @_;
135 # TODO Raise exception instead
136 return unless exists $args->{mmtid};
137 return unless exists $args->{record_ids};
139 $self->SUPER::enqueue({
140 job_size => scalar @{$args->{record_ids}},
145 =head3 additional_report
147 Pass the list of lists/virtual shelves the logged in user has write permissions.
149 It will enable the "add modified records to list" feature.
153 sub additional_report {
156 my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
158 lists => Koha::Virtualshelves->search(
160 { public => 0, owner => $loggedinuser },