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 );
27 use C4::Auth qw( get_template_and_user );
28 use C4::Output qw( output_html_with_http_headers );
29 use C4::Auth qw( get_template_and_user );
31 use C4::AuthoritiesMarc;
32 use Koha::Acquisition::Orders;
33 use Koha::Virtualshelves;
35 use Koha::Authorities;
38 use Koha::BackgroundJob::BatchDeleteBiblio;
39 use Koha::BackgroundJob::BatchDeleteAuthority;
42 my $op = $input->param('op') // q|form|;
43 my $recordtype = $input->param('recordtype') // 'biblio';
44 my $skip_open_orders = $input->param('skip_open_orders') // 0;
46 my ($template, $loggedinuser, $cookie) = get_template_and_user({
47 template_name => 'tools/batch_delete_records.tt',
50 flagsrequired => { tools => 'records_batchdel' },
55 if ( $op eq 'form' ) {
59 lists => Koha::Virtualshelves->search(
61 { public => 0, owner => $loggedinuser },
66 } elsif ( $op eq 'cud-list' ) {
67 # List all records to process
69 if ( my $bib_list = $input->param('bib_list') ) {
70 # Come from the basket
71 @record_ids = split /\//, $bib_list;
72 $recordtype = 'biblio';
73 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
74 # A file of id is given
75 binmode $uploadfile, ':encoding(UTF-8)';
76 while ( my $content = <$uploadfile> ) {
78 $content =~ s/[\r\n]*$//;
79 push @record_ids, $content if $content;
81 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
82 my $shelf = Koha::Virtualshelves->find($shelf_number);
83 my $contents = $shelf->get_contents;
84 while ( my $content = $contents->next ) {
85 my $biblionumber = $content->biblionumber;
86 push @record_ids, $biblionumber;
89 # The user enters manually the list of id
90 push @record_ids, split( /\s\n/, scalar $input->param('recordnumber_list') );
93 for my $record_id ( uniq @record_ids ) {
94 if ( $recordtype eq 'biblio' ) {
95 # Retrieve biblio information
96 my $biblio_object = Koha::Biblios->find( $record_id );
97 unless ( $biblio_object ) {
100 code => 'biblio_not_exists',
101 biblionumber => $record_id,
105 my $biblio = $biblio_object->unblessed;
106 my $record = $biblio_object->metadata->record;
107 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
108 $biblio->{holds_count} = $biblio_object->holds->count;
109 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
110 $biblio->{subscriptions_count} = $biblio_object->subscriptions->count;
112 # Respect skip_open_orders
115 && Koha::Acquisition::Orders->search(
116 { biblionumber => $record_id, orderstatus => [ 'new', 'ordered', 'partial' ] } )->count;
118 push @records, $biblio;
120 # Retrieve authority information
121 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
122 unless ( $authority ) {
125 code => 'authority_not_exists',
126 authid => $record_id,
132 authid => $record_id,
133 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
134 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
136 push @records, $authority;
140 records => \@records,
143 } elsif ( $op eq 'cud-delete' ) {
144 # We want to delete selected records!
145 my @record_ids = $input->multi_param('record_id');
149 record_ids => \@record_ids,
153 $recordtype eq 'biblio'
154 ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
155 : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
164 code => 'cannot_enqueue_job',
167 $template->param( view => 'errors' );
172 messages => \@messages,
173 recordtype => $recordtype,
176 output_html_with_http_headers $input, $cookie, $template->output;