Bug 28782: Use query param list instead of splitting elements using '/'
[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::AuthoritiesMarc;
32 use Koha::Virtualshelves;
33
34 use Koha::Authorities;
35 use Koha::Biblios;
36 use Koha::Items;
37 use Koha::BackgroundJob::BatchDeleteBiblio;
38 use Koha::BackgroundJob::BatchDeleteAuthority;
39
40 my $input = CGI->new;
41 my $op = $input->param('op') // q|form|;
42 my $recordtype = $input->param('recordtype') // 'biblio';
43
44 my ($template, $loggedinuser, $cookie) = get_template_and_user({
45         template_name => 'tools/batch_delete_records.tt',
46         query => $input,
47         type => "intranet",
48         flagsrequired => { tools => 'records_batchdel' },
49 });
50
51 my @records;
52 my @messages;
53 if ( $op eq 'form' ) {
54     # Display the form
55     $template->param(
56         op => 'form',
57         lists => Koha::Virtualshelves->search(
58             [
59                 { public => 0, owner => $loggedinuser },
60                 { public => 1 }
61             ]
62         )
63     );
64 } elsif ( $op eq 'list' ) {
65     # List all records to process
66     my @record_ids;
67     if ( my $bib_list = $input->param('bib_list') ) {
68         # Come from the basket
69         @record_ids = split /\//, $bib_list;
70         $recordtype = 'biblio';
71     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
72         # A file of id is given
73         binmode $uploadfile, ':encoding(UTF-8)';
74         while ( my $content = <$uploadfile> ) {
75             next unless $content;
76             $content =~ s/[\r\n]*$//;
77             push @record_ids, $content if $content;
78         }
79     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
80         my $shelf = Koha::Virtualshelves->find($shelf_number);
81         my $contents = $shelf->get_contents;
82         while ( my $content = $contents->next ) {
83             my $biblionumber = $content->biblionumber;
84             push @record_ids, $biblionumber;
85         }
86     } else {
87         # The user enters manually the list of id
88         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
89     }
90
91     for my $record_id ( uniq @record_ids ) {
92         if ( $recordtype eq 'biblio' ) {
93             # Retrieve biblio information
94             my $biblio_object = Koha::Biblios->find( $record_id );
95             unless ( $biblio_object ) {
96                 push @messages, {
97                     type => 'warning',
98                     code => 'biblio_not_exists',
99                     biblionumber => $record_id,
100                 };
101                 next;
102             }
103             my $biblio = $biblio_object->unblessed;
104             my $record = &GetMarcBiblio({ biblionumber => $record_id });
105             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
106             $biblio->{holds_count} = $biblio_object->holds->count;
107             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
108             $biblio->{subscriptions_count} = $biblio_object->subscriptions->count;
109             push @records, $biblio;
110         } else {
111             # Retrieve authority information
112             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
113             unless ( $authority ) {
114                 push @messages, {
115                     type => 'warning',
116                     code => 'authority_not_exists',
117                     authid => $record_id,
118                 };
119                 next;
120             }
121
122             $authority = {
123                 authid => $record_id,
124                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
125                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
126             };
127             push @records, $authority;
128         }
129     }
130     $template->param(
131         records => \@records,
132         op => 'list',
133     );
134 } elsif ( $op eq 'delete' ) {
135     # We want to delete selected records!
136     my @record_ids = $input->multi_param('record_id');
137
138     try {
139         my $params = {
140             record_ids  => \@record_ids,
141         };
142
143         my $job_id =
144           $recordtype eq 'biblio'
145           ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
146           : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
147
148         $template->param(
149             op => 'enqueued',
150             job_id => $job_id,
151         );
152     } catch {
153         push @messages, {
154             type => 'error',
155             code => 'cannot_enqueue_job',
156             error => $_,
157         };
158         $template->param( view => 'errors' );
159     };
160 }
161
162 $template->param(
163     messages => \@messages,
164     recordtype => $recordtype,
165 );
166
167 output_html_with_http_headers $input, $cookie, $template->output;