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 JSON qw( encode_json decode_json );
20 use Encode qw( encode_utf8 );
21 use List::MoreUtils qw( uniq );
30 use Koha::BackgroundJobs;
31 use Koha::DateUtils qw( dt_from_string );
32 use Koha::SearchEngine::Indexer;
34 use Koha::UI::Table::Builder::Items;
36 use base 'Koha::BackgroundJob';
40 Koha::BackgroundJob::BatchUpdateItem - Background job derived class to process item modification in batch
48 Define the job type of this job: batch_item_record_modification
53 return 'batch_item_record_modification';
58 Koha::BackgroundJobs->find($id)->process(
60 record_ids => \@itemnumbers,
62 itemnotes => $new_item_notes,
66 itemnotes_nonpublic => {
72 exclude_from_local_holds_priority => 1|0
76 Process the modification.
78 new_values allows to set a new value for given fields.
79 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field.
81 regex_mod allows to modify existing subfield's values using a regular expression.
86 my ( $self, $args ) = @_;
88 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
90 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
94 # FIXME If the job has already been started, but started again (worker has been restart for instance)
95 # Then we will start from scratch and so double process the same records
98 $job->started_on(dt_from_string)->progress($job_progress)
99 ->status('started')->store;
101 my @record_ids = @{ $args->{record_ids} };
102 my $regex_mod = $args->{regex_mod};
103 my $new_values = $args->{new_values};
104 my $exclude_from_local_holds_priority =
105 $args->{exclude_from_local_holds_priority};
108 total_records => scalar @record_ids,
109 modified_itemitemnumbers => [],
110 modified_fields => 0,
114 my $schema = Koha::Database->new->schema;
118 Koha::Items->search( { itemnumber => \@record_ids } )
121 regex_mod => $regex_mod,
122 new_values => $new_values,
123 exclude_from_local_holds_priority =>
124 $exclude_from_local_holds_priority,
127 $job->progress($progress)->store;
131 $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
132 $report->{modified_fields} = $results->{modified_fields};
138 die "Something terrible has happened!"
139 if ( $_ =~ /Rollback failed/ ); # Rollback failed
142 my $job_data = decode_json encode_utf8 $job->data;
143 $job_data->{report} = $report;
145 $job->ended_on(dt_from_string)->data( encode_json $job_data);
146 $job->status('finished') if $job->status ne 'cancelled';
157 my ( $self, $args ) = @_;
159 # TODO Raise exception instead
160 return unless exists $args->{record_ids};
162 my @record_ids = @{ $args->{record_ids} };
164 $self->SUPER::enqueue(
166 job_size => scalar @record_ids,
167 job_args => {%$args},
172 =head3 additional_report
174 Sent the infos to generate the table containing the details of the modified items.
178 sub additional_report {
179 my ( $self, $args ) = @_;
181 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
183 my $itemnumbers = $job->report->{modified_itemnumbers};
185 Koha::UI::Table::Builder::Items->new( { itemnumbers => $itemnumbers } )
189 items => $items_table->{items},
190 item_header_loop => $items_table->{headers},