use CGI;
use List::MoreUtils qw( uniq );
+use Try::Tiny;
-use C4::Auth;
-use C4::Output;
-use C4::AuthoritiesMarc;
+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;
+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 = new CGI;
-my $dbh = C4::Context->dbh;
+my $input = CGI->new;
my $op = $input->param('op') // q|form|;
my $recordtype = $input->param('recordtype') // 'biblio';
template_name => 'tools/batch_delete_records.tt',
query => $input,
type => "intranet",
- authnotrequired => 0,
flagsrequired => { tools => 'records_batchdel' },
});
my @messages;
if ( $op eq 'form' ) {
# Display the form
- $template->param( op => 'form' );
+ $template->param(
+ op => 'form',
+ lists => Koha::Virtualshelves->search(
+ [
+ { public => 0, owner => $loggedinuser },
+ { public => 1 }
+ ]
+ )
+ );
} elsif ( $op eq 'list' ) {
# List all records to process
my @record_ids;
$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') );
+ push @record_ids, split( /\s\n/, scalar $input->param('recordnumber_list') );
}
for my $record_id ( uniq @record_ids ) {
if ( $recordtype eq 'biblio' ) {
# Retrieve biblio information
- my $biblio = C4::Biblio::GetBiblio( $record_id );
- unless ( $biblio ) {
+ my $biblio_object = Koha::Biblios->find( $record_id );
+ unless ( $biblio_object ) {
push @messages, {
type => 'warning',
code => 'biblio_not_exists',
};
next;
}
- my $record = &GetMarcBiblio( $record_id );
- $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) );
- $biblio->{itemnumbers} = C4::Items::GetItemnumbersForBiblio( $record_id );
- $biblio->{reserves} = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $record_id });
+ my $biblio = $biblio_object->unblessed;
+ my $record = $biblio_object->metadata->record;
+ $biblio->{itemnumbers} = [Koha::Items->search({ biblionumber => $record_id })->get_column('itemnumber')];
+ $biblio->{holds_count} = $biblio_object->holds->count;
$biblio->{issues_count} = C4::Biblio::CountItemsIssued( $record_id );
+ $biblio->{subscriptions_count} = $biblio_object->subscriptions->count;
push @records, $biblio;
} else {
# Retrieve authority information
$authority = {
authid => $record_id,
summary => C4::AuthoritiesMarc::BuildSummary( $authority, $record_id ),
- count_usage => C4::AuthoritiesMarc::CountUsage( $record_id ),
+ count_usage => Koha::Authorities->get_usage_count({ authid => $record_id }),
};
push @records, $authority;
}
);
} elsif ( $op eq 'delete' ) {
# We want to delete selected records!
- my @record_ids = $input->param('record_id');
- my $dbh = C4::Context->dbh;
- $dbh->{AutoCommit} = 0;
- $dbh->{RaiseError} = 1;
-
- my $error;
- my $report = {
- total_records => 0,
- total_success => 0,
+ 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' );
};
- RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
- $report->{total_records}++;
- next unless $record_id;
- if ( $recordtype eq 'biblio' ) {
- # Biblios
- my $biblionumber = $record_id;
- # First, checking if issues exist.
- # If yes, nothing to do
- if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) {
- push @messages, {
- type => 'warning',
- code => 'item_issued',
- biblionumber => $biblionumber,
- };
- $dbh->rollback;
- next;
- }
-
- # Cancel reserves
- my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber });
- for my $reserve ( @$reserves ) {
- eval{
- C4::Reserves::CancelReserve( { reserve_id => $reserve->{reserve_id} } );
- };
- if ( $@ ) {
- push @messages, {
- type => 'error',
- code => 'reserve_not_cancelled',
- biblionumber => $biblionumber,
- reserve_id => $reserve->{reserve_id},
- error => $@,
- };
- $dbh->rollback;
- next RECORD_IDS;
- }
- }
-
- # Delete items
- my @itemnumbers = @{ C4::Items::GetItemnumbersForBiblio( $biblionumber ) };
- ITEMNUMBER: for my $itemnumber ( @itemnumbers ) {
- my $error = eval { C4::Items::DelItemCheck( $dbh, $biblionumber, $itemnumber ) };
- if ( $error != 1 or $@ ) {
- push @messages, {
- type => 'error',
- code => 'item_not_deleted',
- biblionumber => $biblionumber,
- itemnumber => $itemnumber,
- error => ($@ ? $@ : $error),
- };
- $dbh->rollback;
- next BIBLIONUMBER;
- }
- }
-
- # Finally, delete the biblio
- my $error = eval {
- C4::Biblio::DelBiblio( $biblionumber );
- };
- if ( $error or $@ ) {
- push @messages, {
- type => 'error',
- code => 'biblio_not_deleted',
- biblionumber => $biblionumber,
- error => ($@ ? $@ : $error),
- };
- $dbh->rollback;
- next;
- }
-
- push @messages, {
- type => 'success',
- code => 'biblio_deleted',
- biblionumber => $biblionumber,
- };
- $report->{total_success}++;
- $dbh->commit;
- } else {
- # Authorities
- my $authid = $record_id;
- my $r = eval { C4::AuthoritiesMarc::DelAuthority( $authid ) };
- if ( $r eq '0E0' or $@ ) {
- push @messages, {
- type => 'error',
- code => 'authority_not_deleted',
- authid => $authid,
- error => ($@ ? $@ : 0),
- };
- $dbh->rollback;
- next;
- } else {
- push @messages, {
- type => 'success',
- code => 'authority_deleted',
- authid => $authid,
- };
- $report->{total_success}++;
- $dbh->commit;
- }
- }
- }
- $template->param(
- op => 'report',
- report => $report,
- );
}
$template->param(