1 package Koha::BackgroundJob::BatchUpdateItem;
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 List::MoreUtils qw( uniq );
28 use Koha::DateUtils qw( dt_from_string );
29 use Koha::SearchEngine::Indexer;
31 use Koha::UI::Table::Builder::Items;
33 use base 'Koha::BackgroundJob';
37 Koha::BackgroundJob::BatchUpdateItem - Background job derived class to process item modification in batch
45 Define the job type of this job: batch_item_record_modification
50 return 'batch_item_record_modification';
55 Koha::BackgroundJobs->find($id)->process(
57 record_ids => \@itemnumbers,
59 itemnotes => $new_item_notes,
63 itemnotes_nonpublic => {
69 exclude_from_local_holds_priority => 1|0
73 Process the modification.
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.
78 regex_mod allows to modify existing subfield's values using a regular expression.
83 my ( $self, $args ) = @_;
85 if ( $self->status eq 'cancelled' ) {
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
93 $self->started_on(dt_from_string)->progress($job_progress)
94 ->status('started')->store;
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};
103 total_records => scalar @record_ids,
104 modified_fields => 0,
109 Koha::Items->search( { itemnumber => \@record_ids } )
112 regex_mod => $regex_mod,
113 new_values => $new_values,
114 exclude_from_local_holds_priority =>
115 $exclude_from_local_holds_priority,
118 $self->progress($progress)->store;
122 $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
123 $report->{modified_fields} = $results->{modified_fields};
127 die "Something terrible has happened!"
128 if ( $_ =~ /Rollback failed/ ); # Rollback failed
131 my $json = $self->json;
132 $self->discard_changes;
133 my $job_data = $json->decode($self->data);
134 $job_data->{report} = $report;
136 $self->ended_on(dt_from_string)->data($json->encode($job_data));
137 $self->status('finished') if $self->status ne 'cancelled';
148 my ( $self, $args ) = @_;
150 # TODO Raise exception instead
151 return unless exists $args->{record_ids};
153 my @record_ids = @{ $args->{record_ids} };
155 $self->SUPER::enqueue(
157 job_size => scalar @record_ids,
158 job_args => {%$args},
159 queue => 'long_tasks',
164 =head3 additional_report
166 Sent the infos to generate the table containing the details of the modified items.
170 sub additional_report {
171 my ( $self, $args ) = @_;
173 return unless $self->report->{modified_itemnumbers};
175 my $itemnumbers = $self->report->{modified_itemnumbers};
176 if ( scalar(@$itemnumbers) > C4::Context->preference('MaxItemsToDisplayForBatchMod') ) {
177 return { too_many_items_display => 1 };
180 Koha::UI::Table::Builder::Items->new( { itemnumbers => $itemnumbers } )
184 items => $items_table->{items},
185 item_header_loop => $items_table->{headers},