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::Auth qw( get_template_and_user );
29 use C4::Biblio qw( GetMarcBiblio );
30 use C4::AuthoritiesMarc;
31 use Koha::Virtualshelves;
33 use Koha::Authorities;
38 my $op = $input->param('op') // q|form|;
39 my $recordtype = $input->param('recordtype') // 'biblio';
41 my ($template, $loggedinuser, $cookie) = get_template_and_user({
42 template_name => 'tools/batch_delete_records.tt',
45 flagsrequired => { tools => 'records_batchdel' },
50 if ( $op eq 'form' ) {
54 lists => scalar Koha::Virtualshelves->search(
56 { category => 1, owner => $loggedinuser },
61 } elsif ( $op eq 'list' ) {
62 # List all records to process
64 if ( my $bib_list = $input->param('bib_list') ) {
65 # Come from the basket
66 @record_ids = split /\//, $bib_list;
67 $recordtype = 'biblio';
68 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
69 # A file of id is given
70 binmode $uploadfile, ':encoding(UTF-8)';
71 while ( my $content = <$uploadfile> ) {
73 $content =~ s/[\r\n]*$//;
74 push @record_ids, $content if $content;
76 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
77 my $shelf = Koha::Virtualshelves->find($shelf_number);
78 my $contents = $shelf->get_contents;
79 while ( my $content = $contents->next ) {
80 my $biblionumber = $content->biblionumber;
81 push @record_ids, $biblionumber;
84 # The user enters manually the list of id
85 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
88 for my $record_id ( uniq @record_ids ) {
89 if ( $recordtype eq 'biblio' ) {
90 # Retrieve biblio information
91 my $biblio = Koha::Biblios->find( $record_id );
95 code => 'biblio_not_exists',
96 biblionumber => $record_id,
100 my $holds_count = $biblio->holds->count;
101 $biblio = $biblio->unblessed;
102 my $record = &GetMarcBiblio({ biblionumber => $record_id });
103 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
104 $biblio->{holds_count} = $holds_count;
105 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
106 push @records, $biblio;
108 # Retrieve authority information
109 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
110 unless ( $authority ) {
113 code => 'authority_not_exists',
114 authid => $record_id,
120 authid => $record_id,
121 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
122 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
124 push @records, $authority;
128 records => \@records,
131 } elsif ( $op eq 'delete' ) {
132 # We want to delete selected records!
133 my @record_ids = $input->multi_param('record_id');
134 my $schema = Koha::Database->new->schema;
141 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
142 $report->{total_records}++;
143 next unless $record_id;
144 $schema->storage->txn_begin;
146 if ( $recordtype eq 'biblio' ) {
148 my $biblionumber = $record_id;
149 # First, checking if issues exist.
150 # If yes, nothing to do
151 my $biblio = Koha::Biblios->find( $biblionumber );
153 # TODO Replace with $biblio->get_issues->count
154 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
157 code => 'item_issued',
158 biblionumber => $biblionumber,
160 $schema->storage->txn_rollback;
165 my $holds = $biblio->holds;
166 while ( my $hold = $holds->next ) {
173 code => 'reserve_not_cancelled',
174 biblionumber => $biblionumber,
175 reserve_id => $hold->reserve_id,
178 $schema->storage->txn_rollback;
184 my $items = Koha::Items->search({ biblionumber => $biblionumber });
185 while ( my $item = $items->next ) {
186 my $deleted_item = eval { $item->safe_delete };
187 if ( !ref($deleted_item) or $@ ) {
190 code => 'item_not_deleted',
191 biblionumber => $biblionumber,
192 itemnumber => $item->itemnumber,
193 error => ($@ ? $@ : $error),
195 $schema->storage->txn_rollback;
200 # Finally, delete the biblio
202 C4::Biblio::DelBiblio( $biblionumber );
204 if ( $error or $@ ) {
207 code => 'biblio_not_deleted',
208 biblionumber => $biblionumber,
209 error => ($@ ? $@ : $error),
211 $schema->storage->txn_rollback;
217 code => 'biblio_deleted',
218 biblionumber => $biblionumber,
220 $report->{total_success}++;
221 $schema->storage->txn_commit;
224 my $authid = $record_id;
225 eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
229 code => 'authority_not_deleted',
231 error => ($@ ? $@ : 0),
233 $schema->storage->txn_rollback;
238 code => 'authority_deleted',
241 $report->{total_success}++;
242 $schema->storage->txn_commit;
253 messages => \@messages,
254 recordtype => $recordtype,
257 output_html_with_http_headers $input, $cookie, $template->output;