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
;
34 my $op = $input->param('op') // q
|form
|;
35 my $recordtype = $input->param('recordtype') // 'biblio';
37 my ($template, $loggedinuser, $cookie) = get_template_and_user
({
38 template_name
=> 'tools/batch_delete_records.tt',
42 flagsrequired
=> { tools
=> 'records_batchdel' },
47 if ( $op eq 'form' ) {
49 $template->param( op
=> 'form' );
50 } elsif ( $op eq 'list' ) {
51 # List all records to process
53 if ( my $bib_list = $input->param('bib_list') ) {
54 # Come from the basket
55 @record_ids = split /\//, $bib_list;
56 $recordtype = 'biblio';
57 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
58 # A file of id is given
59 while ( my $content = <$uploadfile> ) {
61 $content =~ s/[\r\n]*$//;
62 push @record_ids, $content if $content;
65 # The user enters manually the list of id
66 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
69 for my $record_id ( uniq
@record_ids ) {
70 if ( $recordtype eq 'biblio' ) {
71 # Retrieve biblio information
72 my $biblio = Koha
::Biblios
->find( $record_id );
76 code
=> 'biblio_not_exists',
77 biblionumber
=> $record_id,
81 my $holds_count = $biblio->holds->count;
82 $biblio = $biblio->unblessed;
83 my $record = &GetMarcBiblio
( $record_id );
84 $biblio->{subtitle
} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $record_id ) );
85 $biblio->{itemnumbers
} = C4
::Items
::GetItemnumbersForBiblio
( $record_id );
86 $biblio->{holds_count
} = $holds_count;
87 $biblio->{issues_count
} = C4
::Biblio
::CountItemsIssued
( $record_id );
88 push @records, $biblio;
90 # Retrieve authority information
91 my $authority = C4
::AuthoritiesMarc
::GetAuthority
( $record_id );
92 unless ( $authority ) {
95 code
=> 'authority_not_exists',
102 authid
=> $record_id,
103 summary
=> C4
::AuthoritiesMarc
::BuildSummary
( $authority, $record_id ),
104 count_usage
=> C4
::AuthoritiesMarc
::CountUsage
( $record_id ),
106 push @records, $authority;
110 records
=> \
@records,
113 } elsif ( $op eq 'delete' ) {
114 # We want to delete selected records!
115 my @record_ids = $input->multi_param('record_id');
116 my $schema = Koha
::Database
->new->schema;
123 RECORD_IDS
: for my $record_id ( sort { $a <=> $b } @record_ids ) {
124 $report->{total_records
}++;
125 next unless $record_id;
126 $schema->storage->txn_begin;
128 if ( $recordtype eq 'biblio' ) {
130 my $biblionumber = $record_id;
131 # First, checking if issues exist.
132 # If yes, nothing to do
133 my $biblio = Koha
::Biblios
->find( $biblionumber );
135 # TODO Replace with $biblio->get_issues->count
136 if ( C4
::Biblio
::CountItemsIssued
( $biblionumber ) ) {
139 code
=> 'item_issued',
140 biblionumber
=> $biblionumber,
142 $schema->storage->txn_rollback;
147 my $holds = $biblio->holds;
148 while ( my $hold = $holds->next ) {
150 C4
::Reserves
::CancelReserve
( { reserve_id
=> $hold->reserve_id } );
155 code
=> 'reserve_not_cancelled',
156 biblionumber
=> $biblionumber,
157 reserve_id
=> $hold->reserve_id,
160 $schema->storage->txn_rollback;
166 my @itemnumbers = @
{ C4
::Items
::GetItemnumbersForBiblio
( $biblionumber ) };
167 ITEMNUMBER
: for my $itemnumber ( @itemnumbers ) {
168 my $error = eval { C4
::Items
::DelItemCheck
( $biblionumber, $itemnumber ) };
169 if ( $error != 1 or $@
) {
172 code
=> 'item_not_deleted',
173 biblionumber
=> $biblionumber,
174 itemnumber
=> $itemnumber,
175 error
=> ($@ ?
$@
: $error),
177 $schema->storage->txn_rollback;
182 # Finally, delete the biblio
184 C4
::Biblio
::DelBiblio
( $biblionumber );
186 if ( $error or $@
) {
189 code
=> 'biblio_not_deleted',
190 biblionumber
=> $biblionumber,
191 error
=> ($@ ?
$@
: $error),
193 $schema->storage->txn_rollback;
199 code
=> 'biblio_deleted',
200 biblionumber
=> $biblionumber,
202 $report->{total_success
}++;
203 $schema->storage->txn_commit;
206 my $authid = $record_id;
207 eval { C4
::AuthoritiesMarc
::DelAuthority
({ authid
=> $authid }) };
211 code
=> 'authority_not_deleted',
213 error
=> ($@ ?
$@
: 0),
215 $schema->storage->txn_rollback;
220 code
=> 'authority_deleted',
223 $report->{total_success
}++;
224 $schema->storage->txn_commit;
235 messages
=> \
@messages,
236 recordtype
=> $recordtype,
239 output_html_with_http_headers
$input, $cookie, $template->output;