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;
32 my $dbh = C4::Context->dbh;
33 my $op = $input->param('op') // q|form|;
34 my $recordtype = $input->param('recordtype') // 'biblio';
36 my ($template, $loggedinuser, $cookie) = get_template_and_user({
37 template_name => 'tools/batch_delete_records.tt',
41 flagsrequired => { tools => 'biblio_batchdel' },
46 if ( $op eq 'form' ) {
48 $template->param( op => 'form' );
49 } elsif ( $op eq 'list' ) {
50 # List all records to process
52 if ( my $bib_list = $input->param('bib_list') ) {
53 # Come from the basket
54 @record_ids = split /\//, $bib_list;
55 $recordtype = 'biblio';
56 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
57 # A file of id is given
58 while ( my $content = <$uploadfile> ) {
60 $content =~ s/[\r\n]*$//;
61 push @record_ids, $content if $content;
64 # The user enters manually the list of id
65 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
68 for my $record_id ( uniq @record_ids ) {
69 if ( $recordtype eq 'biblio' ) {
70 # Retrieve biblio information
71 my $biblio = C4::Biblio::GetBiblio( $record_id );
75 code => 'biblio_not_exists',
76 biblionumber => $record_id,
80 $biblio->{itemnumbers} = C4::Items::GetItemnumbersForBiblio( $record_id );
81 $biblio->{reserves} = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $record_id });
82 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
83 push @records, $biblio;
85 # Retrieve authority information
86 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
87 unless ( $authority ) {
90 code => 'authority_not_exists',
98 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
99 count_usage => C4::AuthoritiesMarc::CountUsage( $record_id ),
101 push @records, $authority;
105 records => \@records,
108 } elsif ( $op eq 'delete' ) {
109 # We want to delete selected records!
110 my @record_ids = $input->param('record_id');
111 my $dbh = C4::Context->dbh;
112 $dbh->{AutoCommit} = 0;
113 $dbh->{RaiseError} = 1;
120 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
121 $report->{total_records}++;
122 next unless $record_id;
123 if ( $recordtype eq 'biblio' ) {
125 my $biblionumber = $record_id;
126 # First, checking if issues exist.
127 # If yes, nothing to do
128 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
131 code => 'item_issued',
132 biblionumber => $biblionumber,
139 my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber });
140 for my $reserve ( @$reserves ) {
142 C4::Reserves::CancelReserve( { reserve_id => $reserve->{reserve_id} } );
147 code => 'reserve_not_cancelled',
148 biblionumber => $biblionumber,
149 reserve_id => $reserve->{reserve_id},
158 my @itemnumbers = @{ C4::Items::GetItemnumbersForBiblio( $biblionumber ) };
159 ITEMNUMBER: for my $itemnumber ( @itemnumbers ) {
160 my $error = eval { C4::Items::DelItemCheck( $dbh, $biblionumber, $itemnumber ) };
161 if ( $error != 1 or $@ ) {
164 code => 'item_not_deleted',
165 biblionumber => $biblionumber,
166 itemnumber => $itemnumber,
167 error => ($@ ? $@ : $error),
174 # Finally, delete the biblio
176 C4::Biblio::DelBiblio( $biblionumber );
178 if ( $error or $@ ) {
181 code => 'biblio_not_deleted',
182 biblionumber => $biblionumber,
183 error => ($@ ? $@ : $error),
191 code => 'biblio_deleted',
192 biblionumber => $biblionumber,
194 $report->{total_success}++;
198 my $authid = $record_id;
199 my $r = eval { C4::AuthoritiesMarc::DelAuthority( $authid ) };
200 if ( $r eq '0E0' or $@ ) {
203 code => 'authority_not_deleted',
205 error => ($@ ? $@ : 0),
212 code => 'authority_deleted',
215 $report->{total_success}++;
227 messages => \@messages,
228 recordtype => $recordtype,
231 output_html_with_http_headers $input, $cookie, $template->output;