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;
38 use Koha::BackgroundJob::BatchDeleteAuthority;
41 my $op = $input->param('op') // q|form|;
42 my $recordtype = $input->param('recordtype') // 'biblio';
44 my ($template, $loggedinuser, $cookie) = get_template_and_user({
45 template_name => 'tools/batch_delete_records.tt',
48 flagsrequired => { tools => 'records_batchdel' },
53 if ( $op eq 'form' ) {
57 lists => scalar Koha::Virtualshelves->search(
59 { category => 1, owner => $loggedinuser },
64 } elsif ( $op eq 'list' ) {
65 # List all records to process
67 if ( my $bib_list = $input->param('bib_list') ) {
68 # Come from the basket
69 @record_ids = split /\//, $bib_list;
70 $recordtype = 'biblio';
71 } elsif ( my $uploadfile = $input->param('uploadfile') ) {
72 # A file of id is given
73 binmode $uploadfile, ':encoding(UTF-8)';
74 while ( my $content = <$uploadfile> ) {
76 $content =~ s/[\r\n]*$//;
77 push @record_ids, $content if $content;
79 } elsif ( my $shelf_number = $input->param('shelf_number') ) {
80 my $shelf = Koha::Virtualshelves->find($shelf_number);
81 my $contents = $shelf->get_contents;
82 while ( my $content = $contents->next ) {
83 my $biblionumber = $content->biblionumber;
84 push @record_ids, $biblionumber;
87 # The user enters manually the list of id
88 push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
91 for my $record_id ( uniq @record_ids ) {
92 if ( $recordtype eq 'biblio' ) {
93 # Retrieve biblio information
94 my $biblio = Koha::Biblios->find( $record_id );
98 code => 'biblio_not_exists',
99 biblionumber => $record_id,
103 my $holds_count = $biblio->holds->count;
104 $biblio = $biblio->unblessed;
105 my $record = &GetMarcBiblio({ biblionumber => $record_id });
106 $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
107 $biblio->{holds_count} = $holds_count;
108 $biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
109 push @records, $biblio;
111 # Retrieve authority information
112 my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
113 unless ( $authority ) {
116 code => 'authority_not_exists',
117 authid => $record_id,
123 authid => $record_id,
124 summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
125 count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
127 push @records, $authority;
131 records => \@records,
134 } elsif ( $op eq 'delete' ) {
135 # We want to delete selected records!
136 my @record_ids = $input->multi_param('record_id');
140 record_ids => \@record_ids,
144 $recordtype eq 'biblio'
145 ? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
146 : Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
155 code => 'cannot_enqueue_job',
158 $template->param( view => 'errors' );
163 messages => \@messages,
164 recordtype => $recordtype,
167 output_html_with_http_headers $input, $cookie, $template->output;