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
;
31 use Koha
::Authorities
;
36 my $op = $input->param('op') // q
|form
|;
37 my $recordtype = $input->param('recordtype') // 'biblio';
39 my ($template, $loggedinuser, $cookie) = get_template_and_user
({
40 template_name
=> 'tools/batch_delete_records.tt',
44 flagsrequired
=> { tools
=> 'records_batchdel' },
49 if ( $op eq 'form' ) {
51 $template->param( op
=> 'form' );
52 } elsif ( $op eq 'list' ) {
53 # List all records to process
55 if ( my $bib_list = $input->param('bib_list') ) {
56 # Come from the basket
57 @record_ids = split /\//, $bib_list;
58 $recordtype = 'biblio';
59 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
60 # A file of id is given
61 binmode $uploadfile, ':encoding(UTF-8)';
62 while ( my $content = <$uploadfile> ) {
64 $content =~ s/[\r\n]*$//;
65 push @record_ids, $content if $content;
68 # The user enters manually the list of id
69 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
72 for my $record_id ( uniq
@record_ids ) {
73 if ( $recordtype eq 'biblio' ) {
74 # Retrieve biblio information
75 my $biblio = Koha
::Biblios
->find( $record_id );
79 code
=> 'biblio_not_exists',
80 biblionumber
=> $record_id,
84 my $holds_count = $biblio->holds->count;
85 $biblio = $biblio->unblessed;
86 my $record = &GetMarcBiblio
({ biblionumber
=> $record_id });
87 $biblio->{subtitle
} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $record_id ) );
88 $biblio->{itemnumbers
} = [Koha
::Items
->search({ biblionumber
=> $record_id })->get_column('itemnumber')];
89 $biblio->{holds_count
} = $holds_count;
90 $biblio->{issues_count
} = C4
::Biblio
::CountItemsIssued
( $record_id );
91 push @records, $biblio;
93 # Retrieve authority information
94 my $authority = C4
::AuthoritiesMarc
::GetAuthority
( $record_id );
95 unless ( $authority ) {
98 code
=> 'authority_not_exists',
105 authid
=> $record_id,
106 summary
=> C4
::AuthoritiesMarc
::BuildSummary
( $authority, $record_id ),
107 count_usage
=> Koha
::Authorities
->get_usage_count({ authid
=> $record_id }),
109 push @records, $authority;
113 records
=> \
@records,
116 } elsif ( $op eq 'delete' ) {
117 # We want to delete selected records!
118 my @record_ids = $input->multi_param('record_id');
119 my $schema = Koha
::Database
->new->schema;
126 RECORD_IDS
: for my $record_id ( sort { $a <=> $b } @record_ids ) {
127 $report->{total_records
}++;
128 next unless $record_id;
129 $schema->storage->txn_begin;
131 if ( $recordtype eq 'biblio' ) {
133 my $biblionumber = $record_id;
134 # First, checking if issues exist.
135 # If yes, nothing to do
136 my $biblio = Koha
::Biblios
->find( $biblionumber );
138 # TODO Replace with $biblio->get_issues->count
139 if ( C4
::Biblio
::CountItemsIssued
( $biblionumber ) ) {
142 code
=> 'item_issued',
143 biblionumber
=> $biblionumber,
145 $schema->storage->txn_rollback;
150 my $holds = $biblio->holds;
151 while ( my $hold = $holds->next ) {
158 code
=> 'reserve_not_cancelled',
159 biblionumber
=> $biblionumber,
160 reserve_id
=> $hold->reserve_id,
163 $schema->storage->txn_rollback;
169 my @itemnumbers = Koha
::Items
->search({ biblionumber
=> $biblionumber })->get_column('itemnumber');
170 ITEMNUMBER
: for my $itemnumber ( @itemnumbers ) {
171 my $error = eval { C4
::Items
::DelItemCheck
( $biblionumber, $itemnumber ) };
172 if ( $error != 1 or $@
) {
175 code
=> 'item_not_deleted',
176 biblionumber
=> $biblionumber,
177 itemnumber
=> $itemnumber,
178 error
=> ($@ ?
$@
: $error),
180 $schema->storage->txn_rollback;
185 # Finally, delete the biblio
187 C4
::Biblio
::DelBiblio
( $biblionumber );
189 if ( $error or $@
) {
192 code
=> 'biblio_not_deleted',
193 biblionumber
=> $biblionumber,
194 error
=> ($@ ?
$@
: $error),
196 $schema->storage->txn_rollback;
202 code
=> 'biblio_deleted',
203 biblionumber
=> $biblionumber,
205 $report->{total_success
}++;
206 $schema->storage->txn_commit;
209 my $authid = $record_id;
210 eval { C4
::AuthoritiesMarc
::DelAuthority
({ authid
=> $authid }) };
214 code
=> 'authority_not_deleted',
216 error
=> ($@ ?
$@
: 0),
218 $schema->storage->txn_rollback;
223 code
=> 'authority_deleted',
226 $report->{total_success
}++;
227 $schema->storage->txn_commit;
238 messages
=> \
@messages,
239 recordtype
=> $recordtype,
242 output_html_with_http_headers
$input, $cookie, $template->output;