Bug 25898: Prohibit indirect object notation
[koha.git] / tools / batch_delete_records.pl
blobea87d8b6c85aac44f95730ee92c9cd2b07cdabe4
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>
21 use Modern::Perl;
23 use CGI;
24 use List::MoreUtils qw( uniq );
26 use C4::Auth;
27 use C4::Output;
28 use C4::AuthoritiesMarc;
29 use C4::Biblio;
30 use Koha::Virtualshelves;
32 use Koha::Authorities;
33 use Koha::Biblios;
34 use Koha::Items;
36 my $input = CGI->new;
37 my $op = $input->param('op') // q|form|;
38 my $recordtype = $input->param('recordtype') // 'biblio';
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 });
47 $template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1, owner => $loggedinuser }, { category => 2 }]) );
49 my @records;
50 my @messages;
51 if ( $op eq 'form' ) {
52 # Display the form
53 $template->param( op => 'form' );
54 } elsif ( $op eq 'list' ) {
55 # List all records to process
56 my @record_ids;
57 if ( my $bib_list = $input->param('bib_list') ) {
58 # Come from the basket
59 @record_ids = split /\//, $bib_list;
60 $recordtype = 'biblio';
61 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
62 # A file of id is given
63 binmode $uploadfile, ':encoding(UTF-8)';
64 while ( my $content = <$uploadfile> ) {
65 next unless $content;
66 $content =~ s/[\r\n]*$//;
67 push @record_ids, $content if $content;
69 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
70 my $shelf = Koha::Virtualshelves->find($shelf_number);
71 my $contents = $shelf->get_contents;
72 while ( my $content = $contents->next ) {
73 my $biblionumber = $content->biblionumber;
74 push @record_ids, $biblionumber;
76 } else {
77 # The user enters manually the list of id
78 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
81 for my $record_id ( uniq @record_ids ) {
82 if ( $recordtype eq 'biblio' ) {
83 # Retrieve biblio information
84 my $biblio = Koha::Biblios->find( $record_id );
85 unless ( $biblio ) {
86 push @messages, {
87 type => 'warning',
88 code => 'biblio_not_exists',
89 biblionumber => $record_id,
91 next;
93 my $holds_count = $biblio->holds->count;
94 $biblio = $biblio->unblessed;
95 my $record = &GetMarcBiblio({ biblionumber => $record_id });
96 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
97 $biblio->{holds_count} = $holds_count;
98 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
99 push @records, $biblio;
100 } else {
101 # Retrieve authority information
102 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
103 unless ( $authority ) {
104 push @messages, {
105 type => 'warning',
106 code => 'authority_not_exists',
107 authid => $record_id,
109 next;
112 $authority = {
113 authid => $record_id,
114 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
115 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
117 push @records, $authority;
120 $template->param(
121 records => \@records,
122 op => 'list',
124 } elsif ( $op eq 'delete' ) {
125 # We want to delete selected records!
126 my @record_ids = $input->multi_param('record_id');
127 my $schema = Koha::Database->new->schema;
129 my $error;
130 my $report = {
131 total_records => 0,
132 total_success => 0,
134 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
135 $report->{total_records}++;
136 next unless $record_id;
137 $schema->storage->txn_begin;
139 if ( $recordtype eq 'biblio' ) {
140 # Biblios
141 my $biblionumber = $record_id;
142 # First, checking if issues exist.
143 # If yes, nothing to do
144 my $biblio = Koha::Biblios->find( $biblionumber );
146 # TODO Replace with $biblio->get_issues->count
147 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
148 push @messages, {
149 type => 'warning',
150 code => 'item_issued',
151 biblionumber => $biblionumber,
153 $schema->storage->txn_rollback;
154 next;
157 # Cancel reserves
158 my $holds = $biblio->holds;
159 while ( my $hold = $holds->next ) {
160 eval{
161 $hold->cancel;
163 if ( $@ ) {
164 push @messages, {
165 type => 'error',
166 code => 'reserve_not_cancelled',
167 biblionumber => $biblionumber,
168 reserve_id => $hold->reserve_id,
169 error => $@,
171 $schema->storage->txn_rollback;
172 next RECORD_IDS;
176 # Delete items
177 my $items = Koha::Items->search({ biblionumber => $biblionumber });
178 while ( my $item = $items->next ) {
179 my $deleted_item = eval { $item->safe_delete };
180 if ( !ref($deleted_item) or $@ ) {
181 push @messages, {
182 type => 'error',
183 code => 'item_not_deleted',
184 biblionumber => $biblionumber,
185 itemnumber => $item->itemnumber,
186 error => ($@ ? $@ : $error),
188 $schema->storage->txn_rollback;
189 next RECORD_IDS;
193 # Finally, delete the biblio
194 my $error = eval {
195 C4::Biblio::DelBiblio( $biblionumber );
197 if ( $error or $@ ) {
198 push @messages, {
199 type => 'error',
200 code => 'biblio_not_deleted',
201 biblionumber => $biblionumber,
202 error => ($@ ? $@ : $error),
204 $schema->storage->txn_rollback;
205 next;
208 push @messages, {
209 type => 'success',
210 code => 'biblio_deleted',
211 biblionumber => $biblionumber,
213 $report->{total_success}++;
214 $schema->storage->txn_commit;
215 } else {
216 # Authorities
217 my $authid = $record_id;
218 eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
219 if ( $@ ) {
220 push @messages, {
221 type => 'error',
222 code => 'authority_not_deleted',
223 authid => $authid,
224 error => ($@ ? $@ : 0),
226 $schema->storage->txn_rollback;
227 next;
228 } else {
229 push @messages, {
230 type => 'success',
231 code => 'authority_deleted',
232 authid => $authid,
234 $report->{total_success}++;
235 $schema->storage->txn_commit;
239 $template->param(
240 op => 'report',
241 report => $report,
245 $template->param(
246 messages => \@messages,
247 recordtype => $recordtype,
250 output_html_with_http_headers $input, $cookie, $template->output;