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 $op = $input->param('op') // q|form|;
33 my $recordtype = $input->param('recordtype') // 'biblio';
35 my ($template, $loggedinuser, $cookie) = get_template_and_user({
36 template_name => 'tools/batch_delete_records.tt',
40 flagsrequired => { tools => 'records_batchdel' },
45 if ( $op eq 'form' ) {
47 $template->param( op => 'form' );
48 } elsif ( $op eq 'list' ) {
49 # List all records to process
51 if ( my $bib_list = $input->param('bib_list') ) {
52 # Come from the basket
53 @record_ids = split /\//, $bib_list;
54 $recordtype = 'biblio';
55 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
56 # A file of id is given
57 while ( my $content = <$uploadfile> ) {
59 $content =~ s/[\r\n]*$//;
60 push @record_ids, $content if $content;
63 # The user enters manually the list of id
64 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
67 for my $record_id ( uniq @record_ids ) {
68 if ( $recordtype eq 'biblio' ) {
69 # Retrieve biblio information
70 my $biblio = C4::Biblio::GetBiblio( $record_id );
74 code => 'biblio_not_exists',
75 biblionumber => $record_id,
79 my $record = &GetMarcBiblio( $record_id );
80 $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) );
81 $biblio->{itemnumbers} = C4::Items::GetItemnumbersForBiblio( $record_id );
82 $biblio->{reserves} = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $record_id });
83 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
84 push @records, $biblio;
86 # Retrieve authority information
87 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
88 unless ( $authority ) {
91 code => 'authority_not_exists',
99 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
100 count_usage => C4::AuthoritiesMarc::CountUsage( $record_id ),
102 push @records, $authority;
106 records => \@records,
109 } elsif ( $op eq 'delete' ) {
110 # We want to delete selected records!
111 my @record_ids = $input->multi_param('record_id');
112 my $schema = Koha::Database->new->schema;
119 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
120 $report->{total_records}++;
121 next unless $record_id;
122 $schema->storage->txn_begin;
124 if ( $recordtype eq 'biblio' ) {
126 my $biblionumber = $record_id;
127 # First, checking if issues exist.
128 # If yes, nothing to do
129 if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
132 code => 'item_issued',
133 biblionumber => $biblionumber,
135 $schema->storage->txn_rollback;
140 my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber });
141 for my $reserve ( @$reserves ) {
143 C4::Reserves::CancelReserve( { reserve_id => $reserve->{reserve_id} } );
148 code => 'reserve_not_cancelled',
149 biblionumber => $biblionumber,
150 reserve_id => $reserve->{reserve_id},
153 $schema->storage->txn_rollback;
159 my @itemnumbers = @{ C4::Items::GetItemnumbersForBiblio( $biblionumber ) };
160 ITEMNUMBER: for my $itemnumber ( @itemnumbers ) {
161 my $error = eval { C4::Items::DelItemCheck( $biblionumber, $itemnumber ) };
162 if ( $error != 1 or $@ ) {
165 code => 'item_not_deleted',
166 biblionumber => $biblionumber,
167 itemnumber => $itemnumber,
168 error => ($@ ? $@ : $error),
170 $schema->storage->txn_rollback;
175 # Finally, delete the biblio
177 C4::Biblio::DelBiblio( $biblionumber );
179 if ( $error or $@ ) {
182 code => 'biblio_not_deleted',
183 biblionumber => $biblionumber,
184 error => ($@ ? $@ : $error),
186 $schema->storage->txn_rollback;
192 code => 'biblio_deleted',
193 biblionumber => $biblionumber,
195 $report->{total_success}++;
196 $schema->storage->txn_commit;
199 my $authid = $record_id;
200 my $r = eval { C4::AuthoritiesMarc::DelAuthority( $authid ) };
201 if ( $r eq '0E0' or $@ ) {
204 code => 'authority_not_deleted',
206 error => ($@ ? $@ : 0),
208 $schema->storage->txn_rollback;
213 code => 'authority_deleted',
216 $report->{total_success}++;
217 $schema->storage->txn_commit;
228 messages => \@messages,
229 recordtype => $recordtype,
232 output_html_with_http_headers $input, $cookie, $template->output;