Koha/tools/batch_delete_records.pl
Marion Durand 0b66310580 Bug 9565: (follow-up) Adapt batch record deletion tool
Add a column "Subscriptions" to the batch deletion tools
Add a link on the number of subscription to the search page with all the subscriptions of the record
Add a button in the toolbar to select only biblio record without subscriptions

The changes are only on display
It is still possible to delete records that are attached to subscriptions from this tool (as it is possible for records with attached items)

To test:
1) Go to the batch record deletion (in tools)
2) Select a list of record numbers (select some with one or more subscription)
3) Click on Continue
4) Check that there is no column named "Subscription" and that there is no button "Select without subscription" in the toolbar
5) Apply patch
6) Repeat steps 1 to 3
7a) Check that there is a column named "Subscription" fill with the number of subscriptions attached to the record
7b) Check that the link in the subscriptions column send you to the search page with the subscriptions linked to this record
7c) Check that there is a button "Select without subscription" in the toolbar that selects record with no subscription attached
8) Sign off

Signed-off-by: Frank Hansen <frank.hansen@ub.lu.se>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2021-12-10 21:15:30 -10:00

169 lines
5.4 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2013 BibLibre
#
# Koha is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General
# Public License along with Koha; if not, see
# <http://www.gnu.org/licenses>
use Modern::Perl;
use CGI;
use List::MoreUtils qw( uniq );
use Try::Tiny;
use C4::Auth qw( get_template_and_user );
use C4::Output qw( output_html_with_http_headers );
use C4::Auth qw( get_template_and_user );
use C4::Biblio qw( GetMarcBiblio );
use C4::Serials qw( CountSubscriptionFromBiblionumber);
use C4::AuthoritiesMarc;
use Koha::Virtualshelves;
use Koha::Authorities;
use Koha::Biblios;
use Koha::Items;
use Koha::BackgroundJob::BatchDeleteBiblio;
use Koha::BackgroundJob::BatchDeleteAuthority;
my $input = CGI->new;
my $op = $input->param('op') // q|form|;
my $recordtype = $input->param('recordtype') // 'biblio';
my ($template, $loggedinuser, $cookie) = get_template_and_user({
template_name => 'tools/batch_delete_records.tt',
query => $input,
type => "intranet",
flagsrequired => { tools => 'records_batchdel' },
});
my @records;
my @messages;
if ( $op eq 'form' ) {
# Display the form
$template->param(
op => 'form',
lists => scalar Koha::Virtualshelves->search(
[
{ public => 0, owner => $loggedinuser },
{ public => 1 }
]
)
);
} elsif ( $op eq 'list' ) {
# List all records to process
my @record_ids;
if ( my $bib_list = $input->param('bib_list') ) {
# Come from the basket
@record_ids = split /\//, $bib_list;
$recordtype = 'biblio';
} elsif ( my $uploadfile = $input->param('uploadfile') ) {
# A file of id is given
binmode $uploadfile, ':encoding(UTF-8)';
while ( my $content = <$uploadfile> ) {
next unless $content;
$content =~ s/[\r\n]*$//;
push @record_ids, $content if $content;
}
} elsif ( my $shelf_number = $input->param('shelf_number') ) {
my $shelf = Koha::Virtualshelves->find($shelf_number);
my $contents = $shelf->get_contents;
while ( my $content = $contents->next ) {
my $biblionumber = $content->biblionumber;
push @record_ids, $biblionumber;
}
} else {
# The user enters manually the list of id
push @record_ids, split( /\s\n/, $input->param('recordnumber_list') );
}
for my $record_id ( uniq @record_ids ) {
if ( $recordtype eq 'biblio' ) {
# Retrieve biblio information
my $biblio = Koha::Biblios->find( $record_id );
unless ( $biblio ) {
push @messages, {
type => 'warning',
code => 'biblio_not_exists',
biblionumber => $record_id,
};
next;
}
my $holds_count = $biblio->holds->count;
$biblio = $biblio->unblessed;
my $record = &GetMarcBiblio({ biblionumber => $record_id });
$biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
$biblio->{holds_count} = $holds_count;
$biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
$biblio->{subscriptions_count} = CountSubscriptionFromBiblionumber( $record_id );
push @records, $biblio;
} else {
# Retrieve authority information
my $authority = C4::AuthoritiesMarc::GetAuthority( $record_id );
unless ( $authority ) {
push @messages, {
type => 'warning',
code => 'authority_not_exists',
authid => $record_id,
};
next;
}
$authority = {
authid => $record_id,
summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
};
push @records, $authority;
}
}
$template->param(
records => \@records,
op => 'list',
);
} elsif ( $op eq 'delete' ) {
# We want to delete selected records!
my @record_ids = $input->multi_param('record_id');
try {
my $params = {
record_ids => \@record_ids,
};
my $job_id =
$recordtype eq 'biblio'
? Koha::BackgroundJob::BatchDeleteBiblio->new->enqueue($params)
: Koha::BackgroundJob::BatchDeleteAuthority->new->enqueue($params);
$template->param(
op => 'enqueued',
job_id => $job_id,
);
} catch {
push @messages, {
type => 'error',
code => 'cannot_enqueue_job',
error => $_,
};
$template->param( view => 'errors' );
};
}
$template->param(
messages => \@messages,
recordtype => $recordtype,
);
output_html_with_http_headers $input, $cookie, $template->output;