Bug 32163: (bug 32030 follow-up) Fix koha_object[s]_class for ErmUserRole
[koha.git] / Koha / BackgroundJob / BatchUpdateBiblio.pm
1 package Koha::BackgroundJob::BatchUpdateBiblio;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Koha::Biblios;
21 use Koha::DateUtils qw( dt_from_string );
22 use Koha::Virtualshelves;
23 use Koha::SearchEngine;
24 use Koha::SearchEngine::Indexer;
25
26 use C4::Context;
27 use C4::Biblio;
28 use C4::MarcModificationTemplates;
29
30 use base 'Koha::BackgroundJob';
31
32 =head1 NAME
33
34 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
35
36 This is a subclass of Koha::BackgroundJob.
37
38 =head1 API
39
40 =head2 Class methods
41
42 =head3 job_type
43
44 Define the job type of this job: batch_biblio_record_modification
45
46 =cut
47
48 sub job_type {
49     return 'batch_biblio_record_modification';
50 }
51
52 =head3 process
53
54 Process the modification.
55
56 =cut
57
58 sub process {
59     my ( $self, $args ) = @_;
60
61     if ( $self->status eq 'cancelled' ) {
62         return;
63     }
64
65     # FIXME If the job has already been started, but started again (worker has been restart for instance)
66     # Then we will start from scratch and so double process the same records
67
68     my $job_progress = 0;
69     $self->started_on(dt_from_string)
70         ->progress($job_progress)
71         ->status('started')
72         ->store;
73
74     my $mmtid = $args->{mmtid};
75     my @record_ids = @{ $args->{record_ids} };
76
77     my $report = {
78         total_records => scalar @record_ids,
79         total_success => 0,
80     };
81     my @messages;
82     RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
83
84         last if $self->get_from_storage->status eq 'cancelled';
85
86         next unless $biblionumber;
87
88         # Modify the biblio
89         my $error = eval {
90             my $biblio = Koha::Biblios->find($biblionumber);
91             my $record = $biblio->metadata->record;
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},
96                 skip_record_index => 1,
97             });
98         };
99         if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
100             push @messages, {
101                 type => 'error',
102                 code => 'biblio_not_modified',
103                 biblionumber => $biblionumber,
104                 error => ($@ ? "$@" : $error),
105             };
106         } else {
107             push @messages, {
108                 type => 'success',
109                 code => 'biblio_modified',
110                 biblionumber => $biblionumber,
111             };
112             $report->{total_success}++;
113         }
114         $self->progress( ++$job_progress )->store;
115     }
116
117     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
118     $indexer->index_records( \@record_ids, "specialUpdate", "biblioserver" );
119
120     my $json = $self->json;
121     my $job_data = $json->decode($self->data);
122     $job_data->{messages} = \@messages;
123     $job_data->{report} = $report;
124
125     $self->ended_on(dt_from_string)
126         ->data($json->encode($job_data));
127     $self->status('finished') if $self->status ne 'cancelled';
128     $self->store;
129 }
130
131 =head3 enqueue
132
133 Enqueue the new job
134
135 =cut
136
137 sub enqueue {
138     my ( $self, $args) = @_;
139
140     # TODO Raise exception instead
141     return unless exists $args->{mmtid};
142     return unless exists $args->{record_ids};
143
144     $self->SUPER::enqueue({
145         job_size => scalar @{$args->{record_ids}},
146         job_args => $args,
147         queue    => 'long_tasks',
148     });
149 }
150
151 =head3 additional_report
152
153 Pass the list of lists/virtual shelves the logged in user has write permissions.
154
155 It will enable the "add modified records to list" feature.
156
157 =cut
158
159 sub additional_report {
160     my ($self) = @_;
161
162     my $loggedinuser = C4::Context->userenv ? C4::Context->userenv->{'number'} : undef;
163     return {
164         lists => Koha::Virtualshelves->search(
165             [
166                 { public => 0, owner => $loggedinuser },
167                 { public => 1 }
168             ]
169         ),
170     };
171 }
172
173 1;