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 );
28 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' },
47 $template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1, owner => $loggedinuser }, { category => 2 }]) );
51 if ( $op eq 'form' ) {
53 $template->param( op => 'form' );
54 } elsif ( $op eq 'list' ) {
55 # List all records to process
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> ) {
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;
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 );
88 code => 'biblio_not_exists',
89 biblionumber => $record_id,
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;
101 # Retrieve authority information
102 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
103 unless ( $authority ) {
106 code => 'authority_not_exists',
107 authid => $record_id,
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;
121 records => \@records,
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;
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' ) {
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 ) ) {
150 code => 'item_issued',
151 biblionumber => $biblionumber,
153 $schema->storage->txn_rollback;
158 my $holds = $biblio->holds;
159 while ( my $hold = $holds->next ) {
166 code => 'reserve_not_cancelled',
167 biblionumber => $biblionumber,
168 reserve_id => $hold->reserve_id,
171 $schema->storage->txn_rollback;
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 $@ ) {
183 code => 'item_not_deleted',
184 biblionumber => $biblionumber,
185 itemnumber => $item->itemnumber,
186 error => ($@ ? $@ : $error),
188 $schema->storage->txn_rollback;
193 # Finally, delete the biblio
195 C4::Biblio::DelBiblio( $biblionumber );
197 if ( $error or $@ ) {
200 code => 'biblio_not_deleted',
201 biblionumber => $biblionumber,
202 error => ($@ ? $@ : $error),
204 $schema->storage->txn_rollback;
210 code => 'biblio_deleted',
211 biblionumber => $biblionumber,
213 $report->{total_success}++;
214 $schema->storage->txn_commit;
217 my $authid = $record_id;
218 eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) };
222 code => 'authority_not_deleted',
224 error => ($@ ? $@ : 0),
226 $schema->storage->txn_rollback;
231 code => 'authority_deleted',
234 $report->{total_success}++;
235 $schema->storage->txn_commit;
246 messages => \@messages,
247 recordtype => $recordtype,
250 output_html_with_http_headers $input, $cookie, $template->output;