Bug 9565: (follow-up) Adapt batch record deletion tool
[koha.git] / tools / batch_delete_records.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General
18 # Public License along with Koha; if not, see
19 # <http://www.gnu.org/licenses>
20
21 use Modern::Perl;
22
23 use CGI;
24 use List::MoreUtils qw( uniq );
25 use Try::Tiny;
26
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Auth qw( get_template_and_user );
30 use C4::Biblio qw( GetMarcBiblio );
31 use C4::Serials qw( CountSubscriptionFromBiblionumber);
32 use C4::AuthoritiesMarc;
33 use Koha::Virtualshelves;
34
35 use Koha::Authorities;
36 use Koha::Biblios;
37 use Koha::Items;
38 use Koha::BackgroundJob::BatchDeleteBiblio;
39 use Koha::BackgroundJob::BatchDeleteAuthority;
40
41 my $input = CGI->new;
42 my $op = $input->param('op') // q|form|;
43 my $recordtype = $input->param('recordtype') // 'biblio';
44
45 my ($template, $loggedinuser, $cookie) = get_template_and_user({
46         template_name => 'tools/batch_delete_records.tt',
47         query => $input,
48         type => "intranet",
49         flagsrequired => { tools => 'records_batchdel' },
50 });
51
52 my @records;
53 my @messages;
54 if ( $op eq 'form' ) {
55     # Display the form
56     $template->param(
57         op => 'form',
58         lists => scalar Koha::Virtualshelves->search(
59             [
60                 { public => 0, owner => $loggedinuser },
61                 { public => 1 }
62             ]
63         )
64     );
65 } elsif ( $op eq 'list' ) {
66     # List all records to process
67     my @record_ids;
68     if ( my $bib_list = $input->param('bib_list') ) {
69         # Come from the basket
70         @record_ids = split /\//, $bib_list;
71         $recordtype = 'biblio';
72     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
73         # A file of id is given
74         binmode $uploadfile, ':encoding(UTF-8)';
75         while ( my $content = <$uploadfile> ) {
76             next unless $content;
77             $content =~ s/[\r\n]*$//;
78             push @record_ids, $content if $content;
79         }
80     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
81         my $shelf = Koha::Virtualshelves->find($shelf_number);
82         my $contents = $shelf->get_contents;
83         while ( my $content = $contents->next ) {
84             my $biblionumber = $content->biblionumber;
85             push @record_ids, $biblionumber;
86         }
87     } else {
88         # The user enters manually the list of id
89         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
90     }
91
92     for my $record_id ( uniq @record_ids ) {
93         if ( $recordtype eq 'biblio' ) {
94             # Retrieve biblio information
95             my $biblio = Koha::Biblios->find( $record_id );
96             unless ( $biblio ) {
97                 push @messages, {
98                     type => 'warning',
99                     code => 'biblio_not_exists',
100                     biblionumber => $record_id,
101                 };
102                 next;
103             }
104             my $holds_count = $biblio->holds->count;
105             $biblio = $biblio->unblessed;
106             my $record = &GetMarcBiblio({ biblionumber => $record_id });
107             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
108             $biblio->{holds_count} = $holds_count;
109             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
110             $biblio->{subscriptions_count} = CountSubscriptionFromBiblionumber( $record_id );
111             push @records, $biblio;
112         } else {
113             # Retrieve authority information
114             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
115             unless ( $authority ) {
116                 push @messages, {
117                     type => 'warning',
118                     code => 'authority_not_exists',
119                     authid => $record_id,
120                 };
121                 next;
122             }
123
124             $authority = {
125                 authid => $record_id,
126                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
127                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
128             };
129             push @records, $authority;
130         }
131     }
132     $template->param(
133         records => \@records,
134         op => 'list',
135     );
136 } elsif ( $op eq 'delete' ) {
137     # We want to delete selected records!
138     my @record_ids = $input->multi_param('record_id');
139
140     try {
141         my $params = {
142             record_ids  => \@record_ids,
143         };
144
145         my $job_id =
146           $recordtype eq 'biblio'
147           ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
148           : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
149
150         $template->param(
151             op => 'enqueued',
152             job_id => $job_id,
153         );
154     } catch {
155         push @messages, {
156             type => 'error',
157             code => 'cannot_enqueue_job',
158             error => $_,
159         };
160         $template->param( view => 'errors' );
161     };
162 }
163
164 $template->param(
165     messages => \@messages,
166     recordtype => $recordtype,
167 );
168
169 output_html_with_http_headers $input, $cookie, $template->output;