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