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>
24 use List::MoreUtils qw( uniq );
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
28 use C4::Biblio qw( GetMarcBiblio );
29 use C4::AuthoritiesMarc;
30 use Koha::Virtualshelves;
32 use Koha::Authorities;
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',
44 flagsrequired => { tools => 'records_batchdel' },
49 if ( $op eq 'form' ) {
53 lists => scalar Koha::Virtualshelves->search(
55 { category => 1, owner => $loggedinuser },
60 } elsif ( $op eq 'list' ) {
61 # List all records to process
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> ) {
72 $content =~ s/[\r\n]*$//;
73 push @record_ids, $content if $content;
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;
83 # The user enters manually the list of id
84 push @record_ids, split( /\s\n/, scalar $input->param('recordnumber_list') );
87 for my $record_id ( uniq @record_ids ) {
88 if ( $recordtype eq 'biblio' ) {
89 # Retrieve biblio information
90 my $biblio_object = Koha::Biblios->find( $record_id );
91 unless ( $biblio_object ) {
94 code => 'biblio_not_exists',
95 biblionumber => $record_id,
99 my $biblio = $biblio_object->unblessed;
100 my $record = &GetMarcBiblio({ biblionumber => $record_id });
101 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
102 $biblio->{holds_count} = $biblio_object->holds->count;
103 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
104 $biblio->{subscriptions_count} = $biblio_object->subscriptions->count;
105 push @records, $biblio;
107 # Retrieve authority information
108 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
109 unless ( $authority ) {
112 code => 'authority_not_exists',
113 authid => $record_id,
119 authid => $record_id,
120 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
121 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
123 push @records, $authority;
127 records => \@records,
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;
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;
145 if ( $recordtype eq 'biblio' ) {
147 my $biblionumber = $record_id;
148 # First, checking if issues exist.
149 # If yes, nothing to do
150 my $biblio = Koha::Biblios->find( $biblionumber );
152 # TODO Replace with $biblio->get_issues->count
153 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
156 code => 'item_issued',
157 biblionumber => $biblionumber,
159 $schema->storage->txn_rollback;
164 my $holds = $biblio->holds;
165 while ( my $hold = $holds->next ) {
172 code => 'reserve_not_cancelled',
173 biblionumber => $biblionumber,
174 reserve_id => $hold->reserve_id,
177 $schema->storage->txn_rollback;
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 $@ ) {
189 code => 'item_not_deleted',
190 biblionumber => $biblionumber,
191 itemnumber => $item->itemnumber,
192 error => ($@ ? $@ : $error),
194 $schema->storage->txn_rollback;
199 # Finally, delete the biblio
201 C4::Biblio::DelBiblio( $biblionumber );
203 if ( $error or $@ ) {
206 code => 'biblio_not_deleted',
207 biblionumber => $biblionumber,
208 error => ($@ ? $@ : $error),
210 $schema->storage->txn_rollback;
216 code => 'biblio_deleted',
217 biblionumber => $biblionumber,
219 $report->{total_success}++;
220 $schema->storage->txn_commit;
223 my $authid = $record_id;
224 eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
228 code => 'authority_not_deleted',
230 error => ($@ ? $@ : 0),
232 $schema->storage->txn_rollback;
237 code => 'authority_deleted',
240 $report->{total_success}++;
241 $schema->storage->txn_commit;
252 messages => \@messages,
253 recordtype => $recordtype,
256 output_html_with_http_headers $input, $cookie, $template->output;