Merge branch '20.11.x-security' into 20.11.x
[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
26 use C4::Auth;
27 use C4::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Biblio;
30 use Koha::Virtualshelves;
31
32 use Koha::Authorities;
33 use Koha::Biblios;
34 use Koha::Items;
35
36 my $input = CGI->new;
37 my $op = $input->param('op') // q|form|;
38 my $recordtype = $input->param('recordtype') // 'biblio';
39
40 my ($template, $loggedinuser, $cookie) = get_template_and_user({
41         template_name => 'tools/batch_delete_records.tt',
42         query => $input,
43         type => "intranet",
44         flagsrequired => { tools => 'records_batchdel' },
45 });
46
47 my @records;
48 my @messages;
49 if ( $op eq 'form' ) {
50     # Display the form
51     $template->param(
52         op => 'form',
53         lists => scalar Koha::Virtualshelves->search(
54             [
55                 { category => 1, owner => $loggedinuser },
56                 { category => 2 }
57             ]
58         )
59     );
60 } elsif ( $op eq 'list' ) {
61     # List all records to process
62     my @record_ids;
63     if ( my $bib_list = $input->param('bib_list') ) {
64         # Come from the basket
65         @record_ids = split /\//, $bib_list;
66         $recordtype = 'biblio';
67     } elsif ( my $uploadfile = $input->param('uploadfile') ) {
68         # A file of id is given
69         binmode $uploadfile, ':encoding(UTF-8)';
70         while ( my $content = <$uploadfile> ) {
71             next unless $content;
72             $content =~ s/[\r\n]*$//;
73             push @record_ids, $content if $content;
74         }
75     } elsif ( my $shelf_number = $input->param('shelf_number') ) {
76         my $shelf = Koha::Virtualshelves->find($shelf_number);
77         my $contents = $shelf->get_contents;
78         while ( my $content = $contents->next ) {
79             my $biblionumber = $content->biblionumber;
80             push @record_ids, $biblionumber;
81         }
82     } else {
83         # The user enters manually the list of id
84         push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
85     }
86
87     for my $record_id ( uniq @record_ids ) {
88         if ( $recordtype eq 'biblio' ) {
89             # Retrieve biblio information
90             my $biblio = Koha::Biblios->find( $record_id );
91             unless ( $biblio ) {
92                 push @messages, {
93                     type => 'warning',
94                     code => 'biblio_not_exists',
95                     biblionumber => $record_id,
96                 };
97                 next;
98             }
99             my $holds_count = $biblio->holds->count;
100             $biblio = $biblio->unblessed;
101             my $record = &GetMarcBiblio({ biblionumber => $record_id });
102             $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
103             $biblio->{holds_count} = $holds_count;
104             $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
105             push @records, $biblio;
106         } else {
107             # Retrieve authority information
108             my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
109             unless ( $authority ) {
110                 push @messages, {
111                     type => 'warning',
112                     code => 'authority_not_exists',
113                     authid => $record_id,
114                 };
115                 next;
116             }
117
118             $authority = {
119                 authid => $record_id,
120                 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
121                 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
122             };
123             push @records, $authority;
124         }
125     }
126     $template->param(
127         records => \@records,
128         op => 'list',
129     );
130 } elsif ( $op eq 'delete' ) {
131     # We want to delete selected records!
132     my @record_ids = $input->multi_param('record_id');
133     my $schema = Koha::Database->new->schema;
134
135     my $error;
136     my $report = {
137         total_records => 0,
138         total_success => 0,
139     };
140     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
141         $report->{total_records}++;
142         next unless $record_id;
143         $schema->storage->txn_begin;
144
145         if ( $recordtype eq 'biblio' ) {
146             # Biblios
147             my $biblionumber = $record_id;
148             # First, checking if issues exist.
149             # If yes, nothing to do
150             my $biblio = Koha::Biblios->find( $biblionumber );
151
152             # TODO Replace with $biblio->get_issues->count
153             if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
154                 push @messages, {
155                     type => 'warning',
156                     code => 'item_issued',
157                     biblionumber => $biblionumber,
158                 };
159                 $schema->storage->txn_rollback;
160                 next;
161             }
162
163             # Cancel reserves
164             my $holds = $biblio->holds;
165             while ( my $hold = $holds->next ) {
166                 eval{
167                     $hold->cancel;
168                 };
169                 if ( $@ ) {
170                     push @messages, {
171                         type => 'error',
172                         code => 'reserve_not_cancelled',
173                         biblionumber => $biblionumber,
174                         reserve_id => $hold->reserve_id,
175                         error => $@,
176                     };
177                     $schema->storage->txn_rollback;
178                     next RECORD_IDS;
179                 }
180             }
181
182             # Delete items
183             my $items = Koha::Items->search({ biblionumber => $biblionumber });
184             while ( my $item = $items->next ) {
185                 my $deleted_item = eval { $item->safe_delete };
186                 if ( !ref($deleted_item) or $@ ) {
187                     push @messages, {
188                         type => 'error',
189                         code => 'item_not_deleted',
190                         biblionumber => $biblionumber,
191                         itemnumber => $item->itemnumber,
192                         error => ($@ ? $@ : $error),
193                     };
194                     $schema->storage->txn_rollback;
195                     next RECORD_IDS;
196                 }
197             }
198
199             # Finally, delete the biblio
200             my $error = eval {
201                 C4::Biblio::DelBiblio( $biblionumber );
202             };
203             if ( $error or $@ ) {
204                 push @messages, {
205                     type => 'error',
206                     code => 'biblio_not_deleted',
207                     biblionumber => $biblionumber,
208                     error => ($@ ? $@ : $error),
209                 };
210                 $schema->storage->txn_rollback;
211                 next;
212             }
213
214             push @messages, {
215                 type => 'success',
216                 code => 'biblio_deleted',
217                 biblionumber => $biblionumber,
218             };
219             $report->{total_success}++;
220             $schema->storage->txn_commit;
221         } else {
222             # Authorities
223             my $authid = $record_id;
224             eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
225             if ( $@ ) {
226                 push @messages, {
227                     type => 'error',
228                     code => 'authority_not_deleted',
229                     authid => $authid,
230                     error => ($@ ? $@ : 0),
231                 };
232                 $schema->storage->txn_rollback;
233                 next;
234             } else {
235                 push @messages, {
236                     type => 'success',
237                     code => 'authority_deleted',
238                     authid => $authid,
239                 };
240                 $report->{total_success}++;
241                 $schema->storage->txn_commit;
242             }
243         }
244     }
245     $template->param(
246         op => 'report',
247         report => $report,
248     );
249 }
250
251 $template->param(
252     messages => \@messages,
253     recordtype => $recordtype,
254 );
255
256 output_html_with_http_headers $input, $cookie, $template->output;