X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=tools%2Fbatch_delete_records.pl;h=2ec14ef653795373b2834dd2339a15c1b904080f;hb=f0108f4ff70bc351a2129ee670e07c8f79ae023e;hp=2b82796541a818c03004209df3dbc2cd9c400e73;hpb=17e84750d9d90f3d2df466d1c8d8a63bbd5ad09d;p=koha-ffzg.git diff --git a/tools/batch_delete_records.pl b/tools/batch_delete_records.pl index 2b82796541..2ec14ef653 100755 --- a/tools/batch_delete_records.pl +++ b/tools/batch_delete_records.pl @@ -23,13 +23,17 @@ use Modern::Perl; use CGI; use List::MoreUtils qw( uniq ); -use C4::Auth; -use C4::Output; -use C4::AuthoritiesMarc; -use C4::Biblio; +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 Koha::Virtualshelves; -my $input = new CGI; -my $dbh = C4::Context->dbh; +use Koha::Authorities; +use Koha::Biblios; +use Koha::Items; + +my $input = CGI->new; my $op = $input->param('op') // q|form|; my $recordtype = $input->param('recordtype') // 'biblio'; @@ -37,10 +41,11 @@ my ($template, $loggedinuser, $cookie) = get_template_and_user({ template_name => 'tools/batch_delete_records.tt', query => $input, type => "intranet", - authnotrequired => 0, flagsrequired => { tools => 'records_batchdel' }, }); +$template->param( lists => scalar Koha::Virtualshelves->search([{ category => 1, owner => $loggedinuser }, { category => 2 }]) ); + my @records; my @messages; if ( $op eq 'form' ) { @@ -55,11 +60,19 @@ if ( $op eq 'form' ) { $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') ); @@ -68,7 +81,7 @@ if ( $op eq 'form' ) { for my $record_id ( uniq @record_ids ) { if ( $recordtype eq 'biblio' ) { # Retrieve biblio information - my $biblio = C4::Biblio::GetBiblio( $record_id ); + my $biblio = Koha::Biblios->find( $record_id ); unless ( $biblio ) { push @messages, { type => 'warning', @@ -77,10 +90,11 @@ if ( $op eq 'form' ) { }; 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 $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 ); push @records, $biblio; } else { @@ -98,7 +112,7 @@ if ( $op eq 'form' ) { $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; } @@ -110,9 +124,7 @@ if ( $op eq 'form' ) { } elsif ( $op eq 'delete' ) { # We want to delete selected records! my @record_ids = $input->multi_param('record_id'); - my $dbh = C4::Context->dbh; - $dbh->{AutoCommit} = 0; - $dbh->{RaiseError} = 1; + my $schema = Koha::Database->new->schema; my $error; my $report = { @@ -122,53 +134,58 @@ if ( $op eq 'form' ) { RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) { $report->{total_records}++; next unless $record_id; + $schema->storage->txn_begin; + if ( $recordtype eq 'biblio' ) { # Biblios my $biblionumber = $record_id; # First, checking if issues exist. # If yes, nothing to do + my $biblio = Koha::Biblios->find( $biblionumber ); + + # TODO Replace with $biblio->get_issues->count if ( C4::Biblio::CountItemsIssued( $biblionumber ) ) { push @messages, { type => 'warning', code => 'item_issued', biblionumber => $biblionumber, }; - $dbh->rollback; + $schema->storage->txn_rollback; next; } # Cancel reserves - my $reserves = C4::Reserves::GetReservesFromBiblionumber({ biblionumber => $biblionumber }); - for my $reserve ( @$reserves ) { + my $holds = $biblio->holds; + while ( my $hold = $holds->next ) { eval{ - C4::Reserves::CancelReserve( { reserve_id => $reserve->{reserve_id} } ); + $hold->cancel; }; if ( $@ ) { push @messages, { type => 'error', code => 'reserve_not_cancelled', biblionumber => $biblionumber, - reserve_id => $reserve->{reserve_id}, + reserve_id => $hold->reserve_id, error => $@, }; - $dbh->rollback; + $schema->storage->txn_rollback; next RECORD_IDS; } } # Delete items - my @itemnumbers = @{ C4::Items::GetItemnumbersForBiblio( $biblionumber ) }; - ITEMNUMBER: for my $itemnumber ( @itemnumbers ) { - my $error = eval { C4::Items::DelItemCheck( $biblionumber, $itemnumber ) }; - if ( $error != 1 or $@ ) { + my $items = Koha::Items->search({ biblionumber => $biblionumber }); + while ( my $item = $items->next ) { + my $deleted_item = eval { $item->safe_delete }; + if ( !ref($deleted_item) or $@ ) { push @messages, { type => 'error', code => 'item_not_deleted', biblionumber => $biblionumber, - itemnumber => $itemnumber, + itemnumber => $item->itemnumber, error => ($@ ? $@ : $error), }; - $dbh->rollback; + $schema->storage->txn_rollback; next RECORD_IDS; } } @@ -184,7 +201,7 @@ if ( $op eq 'form' ) { biblionumber => $biblionumber, error => ($@ ? $@ : $error), }; - $dbh->rollback; + $schema->storage->txn_rollback; next; } @@ -194,11 +211,11 @@ if ( $op eq 'form' ) { biblionumber => $biblionumber, }; $report->{total_success}++; - $dbh->commit; + $schema->storage->txn_commit; } else { # Authorities my $authid = $record_id; - eval { C4::AuthoritiesMarc::DelAuthority( $authid ) }; + eval { C4::AuthoritiesMarc::DelAuthority({ authid => $authid }) }; if ( $@ ) { push @messages, { type => 'error', @@ -206,7 +223,7 @@ if ( $op eq 'form' ) { authid => $authid, error => ($@ ? $@ : 0), }; - $dbh->rollback; + $schema->storage->txn_rollback; next; } else { push @messages, { @@ -215,7 +232,7 @@ if ( $op eq 'form' ) { authid => $authid, }; $report->{total_success}++; - $dbh->commit; + $schema->storage->txn_commit; } } }