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 );
30 use C4::Biblio qw( GetMarcBiblio );
31 use C4::AuthoritiesMarc;
32 use Koha::Virtualshelves;
34 use Koha::Authorities;
37 use Koha::BackgroundJob::BatchDeleteBiblio;
40 my $op = $input->param('op') // q|form|;
41 my $recordtype = $input->param('recordtype') // 'biblio';
43 my ($template, $loggedinuser, $cookie) = get_template_and_user({
44 template_name => 'tools/batch_delete_records.tt',
47 flagsrequired => { tools => 'records_batchdel' },
52 if ( $op eq 'form' ) {
56 lists => scalar Koha::Virtualshelves->search(
58 { category => 1, owner => $loggedinuser },
63 } elsif ( $op eq 'list' ) {
64 # List all records to process
66 if ( my $bib_list = $input->param('bib_list') ) {
67 # Come from the basket
68 @record_ids = split /\//, $bib_list;
69 $recordtype = 'biblio';
70 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
71 # A file of id is given
72 binmode $uploadfile, ':encoding(UTF-8)';
73 while ( my $content = <$uploadfile> ) {
75 $content =~ s/[\r\n]*$//;
76 push @record_ids, $content if $content;
78 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
79 my $shelf = Koha::Virtualshelves->find($shelf_number);
80 my $contents = $shelf->get_contents;
81 while ( my $content = $contents->next ) {
82 my $biblionumber = $content->biblionumber;
83 push @record_ids, $biblionumber;
86 # The user enters manually the list of id
87 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
90 for my $record_id ( uniq @record_ids ) {
91 if ( $recordtype eq 'biblio' ) {
92 # Retrieve biblio information
93 my $biblio = Koha::Biblios->find( $record_id );
97 code => 'biblio_not_exists',
98 biblionumber => $record_id,
102 my $holds_count = $biblio->holds->count;
103 $biblio = $biblio->unblessed;
104 my $record = &GetMarcBiblio({ biblionumber => $record_id });
105 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
106 $biblio->{holds_count} = $holds_count;
107 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
108 push @records, $biblio;
110 # Retrieve authority information
111 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
112 unless ( $authority ) {
115 code => 'authority_not_exists',
116 authid => $record_id,
122 authid => $record_id,
123 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
124 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
126 push @records, $authority;
130 records => \@records,
133 } elsif ( $op eq 'delete' ) {
134 # We want to delete selected records!
135 my @record_ids = $input->multi_param('record_id');
139 record_ids => \@record_ids,
143 $recordtype eq 'biblio'
144 ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
145 : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
154 code => 'cannot_enqueue_job',
157 $template->param( view => 'errors' );
162 messages => \@messages,
163 recordtype => $recordtype,
166 output_html_with_http_headers $input, $cookie, $template->output;