Bug 18201: Export data -Fix "Remove non-local items" option and add "Removes non...
[koha_ffzg] / tools / batch_delete_records.pl
index 195f767..8804e62 100755 (executable)
@@ -28,10 +28,10 @@ use C4::Output;
 use C4::AuthoritiesMarc;
 use C4::Biblio;
 
+use Koha::Authorities;
 use Koha::Biblios;
 
 my $input = new CGI;
-my $dbh = C4::Context->dbh;
 my $op = $input->param('op') // q|form|;
 my $recordtype = $input->param('recordtype') // 'biblio';
 
@@ -81,7 +81,7 @@ if ( $op eq 'form' ) {
             }
             my $holds_count = $biblio->holds->count;
             $biblio = $biblio->unblessed;
-            my $record = &GetMarcBiblio( $record_id );
+            my $record = &GetMarcBiblio({ biblionumber => $record_id });
             $biblio->{subtitle} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $record_id ) );
             $biblio->{itemnumbers} = C4::Items::GetItemnumbersForBiblio( $record_id );
             $biblio->{holds_count} = $holds_count;
@@ -102,7 +102,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;
         }
@@ -114,9 +114,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 = {
@@ -126,6 +124,8 @@ 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;
@@ -140,7 +140,7 @@ if ( $op eq 'form' ) {
                     code => 'item_issued',
                     biblionumber => $biblionumber,
                 };
-                $dbh->rollback;
+                $schema->storage->txn_rollback;
                 next;
             }
 
@@ -148,7 +148,7 @@ if ( $op eq 'form' ) {
             my $holds = $biblio->holds;
             while ( my $hold = $holds->next ) {
                 eval{
-                    C4::Reserves::CancelReserve( { reserve_id => $hold->reserve_id } );
+                    $hold->cancel;
                 };
                 if ( $@ ) {
                     push @messages, {
@@ -158,7 +158,7 @@ if ( $op eq 'form' ) {
                         reserve_id => $hold->reserve_id,
                         error => $@,
                     };
-                    $dbh->rollback;
+                    $schema->storage->txn_rollback;
                     next RECORD_IDS;
                 }
             }
@@ -175,7 +175,7 @@ if ( $op eq 'form' ) {
                         itemnumber => $itemnumber,
                         error => ($@ ? $@ : $error),
                     };
-                    $dbh->rollback;
+                    $schema->storage->txn_rollback;
                     next RECORD_IDS;
                 }
             }
@@ -191,7 +191,7 @@ if ( $op eq 'form' ) {
                     biblionumber => $biblionumber,
                     error => ($@ ? $@ : $error),
                 };
-                $dbh->rollback;
+                $schema->storage->txn_rollback;
                 next;
             }
 
@@ -201,11 +201,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',
@@ -213,7 +213,7 @@ if ( $op eq 'form' ) {
                     authid => $authid,
                     error => ($@ ? $@ : 0),
                 };
-                $dbh->rollback;
+                $schema->storage->txn_rollback;
                 next;
             } else {
                 push @messages, {
@@ -222,7 +222,7 @@ if ( $op eq 'form' ) {
                     authid => $authid,
                 };
                 $report->{total_success}++;
-                $dbh->commit;
+                $schema->storage->txn_commit;
             }
         }
     }