f965f8da430f0c7b7e9b673db8179c18ecee47e2
[koha-ffzg.git] / Koha / BackgroundJob / BatchUpdateAuthority.pm
1 package Koha::BackgroundJob::BatchUpdateAuthority;
2
3 use Modern::Perl;
4 use JSON qw( encode_json decode_json );
5
6 use C4::MarcModificationTemplates;
7 use C4::AuthoritiesMarc;
8 use Koha::BackgroundJobs;
9 use Koha::DateUtils qw( dt_from_string );
10 use Koha::MetadataRecord::Authority;
11
12 use base 'Koha::BackgroundJob';
13
14 sub job_type {
15     return 'batch_authority_record_modification';
16 }
17
18 sub process {
19     my ( $self, $args ) = @_;
20
21     my $job = Koha::BackgroundJobs->find( $args->{job_id} );
22
23     if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
24         return;
25     }
26
27     my $job_progress = 0;
28     $job->started_on(dt_from_string)
29         ->progress($job_progress)
30         ->status('started')
31         ->store;
32
33     my $mmtid = $args->{mmtid};
34     my @record_ids = @{ $args->{record_ids} };
35
36     my $report = {
37         total_records => scalar @record_ids,
38         total_success => 0,
39     };
40     my @messages;
41     my $dbh = C4::Context->dbh;
42     $dbh->{RaiseError} = 1;
43     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
44         next unless $record_id;
45         # Authorities
46         my $authid = $record_id;
47         my $error = eval {
48             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
49             my $record = $authority->record;
50             ModifyRecordWithTemplate( $mmtid, $record );
51             ModAuthority( $authid, $record, $authority->authtypecode );
52         };
53         if ( $error and $error != $authid or $@ ) {
54             push @messages, {
55                 type => 'error',
56                 code => 'authority_not_modified',
57                 authid => $authid,
58                 error => ($@ ? $@ : 0),
59             };
60         } else {
61             push @messages, {
62                 type => 'success',
63                 code => 'authority_modified',
64                 authid => $authid,
65             };
66             $report->{total_success}++;
67         }
68         $job->progress( ++$job_progress )->store;
69     }
70
71     my $job_data = decode_json $job->data;
72     $job_data->{messages} = \@messages;
73     $job_data->{report} = $report;
74
75     $job->ended_on(dt_from_string)
76         ->data(encode_json $job_data);
77     $job->status('finished') if $job->status ne 'cancelled';
78     $job->store;
79
80 }
81
82 sub enqueue {
83     my ( $self, $args) = @_;
84
85     # TODO Raise exception instead
86     return unless exists $args->{mmtid};
87     return unless exists $args->{record_ids};
88
89     my $mmtid = $args->{mmtid};
90     my @record_ids = @{ $args->{record_ids} };
91
92     $self->SUPER::enqueue({
93         job_size => scalar @record_ids,
94         job_args => {mmtid => $mmtid, record_ids => \@record_ids,}
95     });
96 }
97
98 1;