View | Details | Raw Unified | Return to bug 26080
Collapse All | Expand All

(-)a/Koha/BackgroundJob.pm (+3 lines)
Lines 27-32 use Koha::Exceptions; Link Here
27
use Koha::BackgroundJob::BatchUpdateBiblio;
27
use Koha::BackgroundJob::BatchUpdateBiblio;
28
use Koha::BackgroundJob::BatchUpdateAuthority;
28
use Koha::BackgroundJob::BatchUpdateAuthority;
29
use Koha::BackgroundJob::BatchDeleteBiblio;
29
use Koha::BackgroundJob::BatchDeleteBiblio;
30
use Koha::BackgroundJob::BatchDeleteAuthority;
30
31
31
use base qw( Koha::Object );
32
use base qw( Koha::Object );
32
33
Lines 158-163 sub process { Link Here
158
      ? Koha::BackgroundJob::BatchUpdateAuthority->process($args)
159
      ? Koha::BackgroundJob::BatchUpdateAuthority->process($args)
159
      : $job_type eq 'batch_biblio_record_deletion'
160
      : $job_type eq 'batch_biblio_record_deletion'
160
      ? Koha::BackgroundJob::BatchDeleteBiblio->process($args)
161
      ? Koha::BackgroundJob::BatchDeleteBiblio->process($args)
162
      : $job_type eq 'batch_authority_record_deletion'
163
      ? Koha::BackgroundJob::BatchDeleteAuthority->process($args)
161
      : Koha::Exceptions::Exception->throw('->process called without valid job_type');
164
      : Koha::Exceptions::Exception->throw('->process called without valid job_type');
162
}
165
}
163
166
(-)a/Koha/BackgroundJob/BatchDeleteAuthority.pm (+101 lines)
Line 0 Link Here
1
package Koha::BackgroundJob::BatchDeleteAuthority;
2
3
use Modern::Perl;
4
use JSON qw( encode_json decode_json );
5
6
use Koha::BackgroundJobs;
7
use Koha::DateUtils qw( dt_from_string );
8
use C4::AuthoritiesMarc;
9
10
use base 'Koha::BackgroundJob';
11
12
sub job_type {
13
    return 'batch_authority_record_deletion';
14
}
15
16
sub process {
17
    my ( $self, $args ) = @_;
18
19
    my $job_type = $args->{job_type};
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
    # FIXME If the job has already been started, but started again (worker has been restart for instance)
28
    # Then we will start from scratch and so double delete the same records
29
30
    my $job_progress = 0;
31
    $job->started_on(dt_from_string)
32
        ->progress($job_progress)
33
        ->status('started')
34
        ->store;
35
36
    my $mmtid = $args->{mmtid};
37
    my @record_ids = @{ $args->{record_ids} };
38
39
    my $report = {
40
        total_records => scalar @record_ids,
41
        total_success => 0,
42
    };
43
    my @messages;
44
    my $schema = Koha::Database->new->schema;
45
    RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
46
47
        last if $job->get_from_storage->status eq 'cancelled';
48
49
        next unless $record_id;
50
51
        $schema->storage->txn_begin;
52
53
        my $authid = $record_id;
54
        eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
55
        if ( $@ ) {
56
            push @messages, {
57
                type => 'error',
58
                code => 'authority_not_deleted',
59
                authid => $authid,
60
                error => ($@ ? $@ : 0),
61
            };
62
            $schema->storage->txn_rollback;
63
            next;
64
        } else {
65
            push @messages, {
66
                type => 'success',
67
                code => 'authority_deleted',
68
                authid => $authid,
69
            };
70
            $report->{total_success}++;
71
            $schema->storage->txn_commit;
72
        }
73
74
        $job->progress( ++$job_progress )->store;
75
    }
76
77
    my $job_data = decode_json $job->data;
78
    $job_data->{messages} = \@messages;
79
    $job_data->{report} = $report;
80
81
    $job->ended_on(dt_from_string)
82
        ->data(encode_json $job_data);
83
    $job->status('finished') if $job->status ne 'cancelled';
84
    $job->store;
85
}
86
87
sub enqueue {
88
    my ( $self, $args) = @_;
89
90
    # TODO Raise exception instead
91
    return unless exists $args->{record_ids};
92
93
    my @record_ids = @{ $args->{record_ids} };
94
95
    $self->SUPER::enqueue({
96
        job_size => scalar @record_ids,
97
        job_args => {record_ids => \@record_ids,}
98
    });
99
}
100
101
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/batch_authority_record_deletion.inc (+42 lines)
Line 0 Link Here
1
[% BLOCK report %]
2
    [% SET report = job.report %]
3
    [% IF report %]
4
        [% IF report.total_records == report.total_success %]
5
            <div class="dialog message">
6
                All records have been deleted successfully!
7
            </div>
8
        [% ELSIF report.total_success == 0 %]
9
            <div class="dialog message">
10
                No record has been deleted. An error occurred.
11
            </div>
12
        [% ELSE %]
13
            <div class="dialog message">
14
                [% report.total_success | html %] / [% report.total_records | html %] records have been deleted successfully but some errors occurred.
15
            </div>
16
        [% END %]
17
    [% END %]
18
[% END %]
19
20
[% BLOCK detail %]
21
    [% FOR m IN job.messages %]
22
        <div class="dialog message">
23
            [% IF m.type == 'success' %]
24
                <i class="fa fa-check success"></i>
25
            [% ELSIF m.type == 'warning' %]
26
                <i class="fa fa-warning warn"></i>
27
            [% ELSIF m.type == 'error' %]
28
                <i class="fa fa-exclamation error"></i>
29
            [% END %]
30
            [% IF m.code == 'authority_not_exists' %]
31
                The authority id [% m.authid | html %] does not exist in the database.
32
            [% ELSIF m.code == 'authority_not_deleted' %]
33
                Authority record [% m.authid | html %] was not deleted. An error occurred.
34
            [% ELSIF m.code == 'authority_deleted' %]
35
                Authority [% m.authid | html %] has been deleted successfully.
36
            [% END %]
37
        </div>
38
    [% END %]
39
[% END %]
40
41
[% BLOCK js %]
42
[% END %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt (+1 lines)
Lines 111-116 Administration &rsaquo; Koha Link Here
111
                        [% CASE 'batch_biblio_record_modification' %]Batch bibliographic record modification
111
                        [% CASE 'batch_biblio_record_modification' %]Batch bibliographic record modification
112
                        [% CASE 'batch_biblio_record_deletion' %]Batch bibliographic record record deletion
112
                        [% CASE 'batch_biblio_record_deletion' %]Batch bibliographic record record deletion
113
                        [% CASE 'batch_authority_record_modification' %]Batch authority record modification
113
                        [% CASE 'batch_authority_record_modification' %]Batch authority record modification
114
                        [% CASE 'batch_authority_record_deletion' %]Batch authority record deletion
114
                        [% CASE %][% job.type | html %]
115
                        [% CASE %][% job.type | html %]
115
                        [% END %]
116
                        [% END %]
116
                    </td>
117
                    </td>
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_delete_records.tt (-13 / +5 lines)
Lines 43-55 Link Here
43
      The biblionumber [% message.biblionumber | html %] does not exist in the database.
43
      The biblionumber [% message.biblionumber | html %] does not exist in the database.
44
    [% ELSIF message.code == 'authority_not_exists' %]
44
    [% ELSIF message.code == 'authority_not_exists' %]
45
      The authority id [% message.authid | html %] does not exist in the database.
45
      The authority id [% message.authid | html %] does not exist in the database.
46
    [% ELSIF message.code == 'authority_not_deleted' %]
47
      Authority record [% message.authid | html %] was not deleted. An error occurred.
48
    [% ELSIF message.code == 'authority_deleted' %]
49
      Authority [% message.authid | html %] has been deleted successfully.
50
    [% ELSIF message.code == 'cannot_enqueue_job' %]
46
    [% ELSIF message.code == 'cannot_enqueue_job' %]
51
        Cannot enqueue this job.
47
        Cannot enqueue this job.
48
    [% ELSIF message.code == 'biblio_not_exists' %]
49
        Bibliographic record [% message.biblionumber | html %] does not exist in the database.
50
    [% ELSIF message.code == 'authority_not_exists' %]
51
        Authority record [% message.authid | html %] does not exist in the database.
52
    [% END %]
52
    [% END %]
53
53
    [% IF message.error %]
54
    [% IF message.error %]
54
      (The error was: [% message.error | html %], see the Koha log file for more information).
55
      (The error was: [% message.error | html %], see the Koha log file for more information).
55
    [% END %]
56
    [% END %]
Lines 198-212 Link Here
198
    [% ELSE %]
199
    [% ELSE %]
199
      There are no record ids defined.
200
      There are no record ids defined.
200
    [% END %]
201
    [% END %]
201
  [% ELSIF op == 'report' %]
202
    [% IF report.total_records == report.total_success %]
203
      All records have been deleted successfully!
204
    [% ELSIF report.total_success == 0 %]
205
      No record has been deleted. An error occurred.
206
    [% ELSE %]
207
      [% report.total_success | html %] / [% report.total_records | html %] records have been deleted successfully but some errors occurred.
208
    [% END %]
209
    <p><a href="/cgi-bin/koha/tools/batch_delete_records.pl" title="New batch record deletion">New batch record deletion</a></p>
210
  [% ELSIF op == 'enqueued' %]
202
  [% ELSIF op == 'enqueued' %]
211
    <div class="dialog message">
203
    <div class="dialog message">
212
      <p>The job has been enqueued! It will be processed as soon as possible.</p>
204
      <p>The job has been enqueued! It will be processed as soon as possible.</p>
(-)a/misc/background_jobs_worker.pl (+1 lines)
Lines 32-37 my @job_types = qw( Link Here
32
    batch_biblio_record_modification
32
    batch_biblio_record_modification
33
    batch_authority_record_modification
33
    batch_authority_record_modification
34
    batch_biblio_record_deletion
34
    batch_biblio_record_deletion
35
    batch_authority_record_deletion
35
);
36
);
36
37
37
if ( $conn ) {
38
if ( $conn ) {
(-)a/tools/batch_delete_records.pl (-1 / +1 lines)
Lines 35-40 use Koha::Authorities; Link Here
35
use Koha::Biblios;
35
use Koha::Biblios;
36
use Koha::Items;
36
use Koha::Items;
37
use Koha::BackgroundJob::BatchDeleteBiblio;
37
use Koha::BackgroundJob::BatchDeleteBiblio;
38
use Koha::BackgroundJob::BatchDeleteAuthority;
38
39
39
my $input = CGI->new;
40
my $input = CGI->new;
40
my $op = $input->param('op') // q|form|;
41
my $op = $input->param('op') // q|form|;
41
- 

Return to bug 26080