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;
32 use Koha::Exceptions::BackgroundJob;
34 use base 'Koha::BackgroundJob';
38 Koha::BackgroundJob::BatchUpdateItem - Background job derived class to process item modification in batch
46 Define the job type of this job: batch_item_record_modification
51 return 'batch_item_record_modification';
56 Koha::BackgroundJobs->find($id)->process(
58 record_ids => \@itemnumbers,
60 itemnotes => $new_item_notes,
64 itemnotes_nonpublic => {
70 exclude_from_local_holds_priority => 1|0
74 Process the modification.
76 new_values allows to set a new value for given fields.
77 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field.
79 regex_mod allows to modify existing subfield's values using a regular expression.
84 my ( $self, $args ) = @_;
86 if ( $self->status eq 'cancelled' ) {
90 # FIXME If the job has already been started, but started again (worker has been restart for instance)
91 # Then we will start from scratch and so double process the same records
95 my @record_ids = @{ $args->{record_ids} };
96 my $regex_mod = $args->{regex_mod};
97 my $new_values = $args->{new_values};
98 my $exclude_from_local_holds_priority =
99 $args->{exclude_from_local_holds_priority};
100 my $mark_items_returned =
101 $args->{mark_items_returned};
104 total_records => scalar @record_ids,
105 modified_fields => 0,
109 my ($results) = Koha::Items->search( { itemnumber => \@record_ids } )->batch_update(
110 { regex_mod => $regex_mod,
111 new_values => $new_values,
112 exclude_from_local_holds_priority => $exclude_from_local_holds_priority,
113 mark_items_returned => $mark_items_returned,
114 callback => sub { $self->step; },
117 $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
118 $report->{modified_fields} = $results->{modified_fields};
122 die "Something terrible has happened!"
123 if ( $_ =~ /Rollback failed/ ); # Rollback failed
126 my $data = $self->decoded_data;
127 $data->{report} = $report;
129 $self->finish( $data );
139 my ( $self, $args ) = @_;
141 Koha::Exceptions::BackgroundJob->throw('Job has not been enqueued')
142 unless $args && exists $args->{record_ids};
144 my @record_ids = @{ $args->{record_ids} };
146 $self->SUPER::enqueue(
148 job_size => scalar @record_ids,
149 job_args => {%$args},
150 job_queue => 'long_tasks',
155 =head3 additional_report
157 Sent the infos to generate the table containing the details of the modified items.
161 sub additional_report {
162 my ( $self, $args ) = @_;
164 return unless $self->report->{modified_itemnumbers};
166 my $itemnumbers = $self->report->{modified_itemnumbers};
167 if ( scalar(@$itemnumbers) > C4::Context->preference('MaxItemsToDisplayForBatchMod') ) {
168 return { too_many_items_display => 1 };
171 Koha::UI::Table::Builder::Items->new( { itemnumbers => $itemnumbers } )
175 items => $items_table->{items},
176 item_header_loop => $items_table->{headers},