Bug 32019: Add option to mark items returned via batch modification
[koha.git] / Koha / BackgroundJob / BatchUpdateItem.pm
1 package Koha::BackgroundJob::BatchUpdateItem;
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 use List::MoreUtils qw( uniq );
20 use Try::Tiny;
21
22 use MARC::Record;
23 use MARC::Field;
24
25 use C4::Biblio;
26 use C4::Items;
27
28 use Koha::DateUtils qw( dt_from_string );
29 use Koha::SearchEngine::Indexer;
30 use Koha::Items;
31 use Koha::UI::Table::Builder::Items;
32
33 use base 'Koha::BackgroundJob';
34
35 =head1 NAME
36
37 Koha::BackgroundJob::BatchUpdateItem - Background job derived class to process item modification in batch
38
39 =head1 API
40
41 =head2 Class methods
42
43 =head3 job_type
44
45 Define the job type of this job: batch_item_record_modification
46
47 =cut
48
49 sub job_type {
50     return 'batch_item_record_modification';
51 }
52
53 =head3 process
54
55     Koha::BackgroundJobs->find($id)->process(
56         {
57             record_ids => \@itemnumbers,
58             new_values => {
59                 itemnotes => $new_item_notes,
60                 k         => $k,
61             },
62             regex_mod => {
63                 itemnotes_nonpublic => {
64                     search => 'foo',
65                     replace => 'bar',
66                     modifiers => 'gi',
67                 },
68             },
69             exclude_from_local_holds_priority => 1|0
70         }
71     );
72
73 Process the modification.
74
75 new_values allows to set a new value for given fields.
76 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field.
77
78 regex_mod allows to modify existing subfield's values using a regular expression.
79
80 =cut
81
82 sub process {
83     my ( $self, $args ) = @_;
84
85     if ( $self->status eq 'cancelled' ) {
86         return;
87     }
88
89     # FIXME If the job has already been started, but started again (worker has been restart for instance)
90     # Then we will start from scratch and so double process the same records
91
92     my $job_progress = 0;
93     $self->started_on(dt_from_string)->progress($job_progress)
94       ->status('started')->store;
95
96     my @record_ids = @{ $args->{record_ids} };
97     my $regex_mod  = $args->{regex_mod};
98     my $new_values = $args->{new_values};
99     my $exclude_from_local_holds_priority =
100       $args->{exclude_from_local_holds_priority};
101     my $mark_items_returned =
102       $args->{mark_items_returned};
103
104     my $report = {
105         total_records            => scalar @record_ids,
106         modified_fields          => 0,
107     };
108
109     try {
110         my ($results) =
111           Koha::Items->search( { itemnumber => \@record_ids } )
112           ->batch_update(
113             {
114                 regex_mod  => $regex_mod,
115                 new_values => $new_values,
116                 exclude_from_local_holds_priority =>
117                   $exclude_from_local_holds_priority,
118                 mark_items_returned => $mark_items_returned,
119                 callback => sub {
120                     my ($progress) = @_;
121                     $self->progress($progress)->store;
122                 },
123             }
124           );
125         $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
126         $report->{modified_fields}      = $results->{modified_fields};
127     }
128     catch {
129         warn $_;
130         die "Something terrible has happened!"
131           if ( $_ =~ /Rollback failed/ );    # Rollback failed
132     };
133
134     my $json = $self->json;
135     $self->discard_changes;
136     my $job_data = $json->decode($self->data);
137     $job_data->{report} = $report;
138
139     $self->ended_on(dt_from_string)->data($json->encode($job_data));
140     $self->status('finished') if $self->status ne 'cancelled';
141     $self->store;
142 }
143
144 =head3 enqueue
145
146 Enqueue the new job
147
148 =cut
149
150 sub enqueue {
151     my ( $self, $args ) = @_;
152
153     # TODO Raise exception instead
154     return unless exists $args->{record_ids};
155
156     my @record_ids = @{ $args->{record_ids} };
157
158     $self->SUPER::enqueue(
159         {
160             job_size  => scalar @record_ids,
161             job_args  => {%$args},
162             job_queue => 'long_tasks',
163         }
164     );
165 }
166
167 =head3 additional_report
168
169 Sent the infos to generate the table containing the details of the modified items.
170
171 =cut
172
173 sub additional_report {
174     my ( $self, $args ) = @_;
175
176     return unless $self->report->{modified_itemnumbers};
177
178     my $itemnumbers = $self->report->{modified_itemnumbers};
179     if ( scalar(@$itemnumbers) > C4::Context->preference('MaxItemsToDisplayForBatchMod') ) {
180         return { too_many_items_display => 1 };
181     } else {
182         my $items_table =
183           Koha::UI::Table::Builder::Items->new( { itemnumbers => $itemnumbers } )
184           ->build_table;
185
186         return {
187             items            => $items_table->{items},
188             item_header_loop => $items_table->{headers},
189         };
190     }
191 }
192
193 1;