Bug 19073: (bug 15758 follow-up) Dereference correct value from Koha::Object.
[koha.git] / tools / manage-marc-import.pl
index 961a327..55707aa 100755 (executable)
@@ -4,24 +4,24 @@
 #
 # This file is part of Koha.
 #
-# 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 2 of the License, or (at your option) any later
-# version.
+# 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.
+# 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, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
 
 # standard or CPAN modules used
-use CGI;
+use CGI qw ( -utf8 );
 use CGI::Cookie;
 use MARC::File::USMARC;
 
@@ -36,6 +36,7 @@ use C4::ImportBatch;
 use C4::Matcher;
 use C4::BackgroundJob;
 use C4::Labels::Batch;
+use Koha::BiblioFrameworks;
 
 my $script_name = "/cgi-bin/koha/tools/manage-marc-import.pl";
 
@@ -62,15 +63,8 @@ my %cookies = parse CGI::Cookie($cookie);
 our $sessionID = $cookies{'CGISESSID'}->value;
 our $dbh = C4::Context->dbh;
 
-# Frameworks selection loop
-{
-    my $frameworks = getframeworks;
-    my $arrayref = [];
-    while ( my ($key, $value) = each %$frameworks ) {
-        push @$arrayref, { value => $key, label => $value->{frameworktext} };
-    }
-    $template->param( frameworks => $arrayref );
-}
+my $frameworks = Koha::BiblioFrameworks->search({ tagfield => { 'not' => undef } }, { join => 'marc_tag_structure', group_by=>'frameworkcode',order_by => ['frameworktext'] });
+$template->param( frameworks => $frameworks );
 
 if ($op eq "create_labels") {
        #create a batch of labels, then lose $op & $import_batch_id so we get back to import batch list.
@@ -123,6 +117,12 @@ if ($op eq "") {
         did_clean       => 1,
         import_batch_id => $import_batch_id,
     );
+} elsif ($op eq "delete-batch") {
+    DeleteBatch($import_batch_id);
+    import_batches_list($template, $offset, $results_per_page);
+    $template->param(
+        did_delete      => 1,
+    );
 } elsif ($op eq "redo-matching") {
     my $new_matcher_id = $input->param('new_matcher_id');
     my $current_matcher_id = $input->param('current_matcher_id');
@@ -234,15 +234,24 @@ sub commit_batch {
     my ($template, $import_batch_id, $framework) = @_;
 
     my $job = undef;
-    $dbh->{AutoCommit} = 0;
-    my $callback = sub {};
-    if ($runinbackground) {
-        $job = put_in_background($import_batch_id);
-        $callback = progress_callback($job, $dbh);
-    }
-    my ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored) =
-        BatchCommitRecords($import_batch_id, $framework, 50, $callback);
-    $dbh->commit();
+    my ( $num_added, $num_updated, $num_items_added,
+        $num_items_replaced, $num_items_errored, $num_ignored );
+    my $schema = Koha::Database->new->schema;
+    $schema->storage->txn_do(
+        sub {
+            my $callback = sub { };
+            if ($runinbackground) {
+                $job = put_in_background($import_batch_id);
+                $callback = progress_callback( $job, $dbh );
+            }
+            (
+                $num_added, $num_updated, $num_items_added,
+                $num_items_replaced, $num_items_errored, $num_ignored
+              )
+              = BatchCommitRecords( $import_batch_id, $framework, 50,
+                $callback );
+        }
+    );
 
     my $results = {
         did_commit => 1,
@@ -263,16 +272,25 @@ sub commit_batch {
 sub revert_batch {
     my ($template, $import_batch_id) = @_;
 
-    $dbh->{AutoCommit} = 0;
     my $job = undef;
-    my $callback = sub {};
-    if ($runinbackground) {
-        $job = put_in_background($import_batch_id);
-        $callback = progress_callback($job, $dbh);
-    }
-    my ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored) = 
-        BatchRevertRecords($import_batch_id, 50, $callback);
-    $dbh->commit();
+            my (
+                $num_deleted,       $num_errors, $num_reverted,
+                $num_items_deleted, $num_ignored
+            );
+    my $schema = Koha::Database->new->schema;
+    $schema->txn_do(
+        sub {
+            my $callback = sub { };
+            if ($runinbackground) {
+                $job = put_in_background($import_batch_id);
+                $callback = progress_callback( $job, $dbh );
+            }
+            (
+                $num_deleted,       $num_errors, $num_reverted,
+                $num_items_deleted, $num_ignored
+            ) = BatchRevertRecords( $import_batch_id, 50, $callback );
+        }
+    );
 
     my $results = {
         did_revert => 1,
@@ -293,7 +311,7 @@ sub put_in_background {
     my $import_batch_id = shift;
 
     my $batch = GetImportBatch($import_batch_id);
-    my $job = C4::BackgroundJob->new($sessionID, $batch->{'file_name'}, $ENV{'SCRIPT_NAME'}, $batch->{'num_records'});
+    my $job = C4::BackgroundJob->new($sessionID, $batch->{'file_name'}, '/cgi-bin/koha/tools/manage-marc-import.pl', $batch->{'num_records'});
     my $jobID = $job->id();
 
     # fork off
@@ -318,7 +336,7 @@ sub put_in_background {
         close STDERR;
     } else {
         # fork failed, so exit immediately
-        warn "fork failed while attempting to run $ENV{'SCRIPT_NAME'} as a background job";
+        warn "fork failed while attempting to run tools/manage-marc-import.pl as a background job";
         exit 0;
     }
     return $job;