From de9adc1aedbd6399d3e6d2051f385fff6bb48ead Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Tue, 16 Jul 2013 17:28:43 +0200
Subject: [PATCH] Bug 10601: Add a counter on batch items modifications

After executing a batch items modifications, a dialog is displayed above
the result table. It contains the number of items (and total fields)
which has been modified.

Test plan:
1/ Go to tools/batchMod.pl
2/ Enter a barcodes list
3/ Check/uncheck items and fill some values to apply
4/ Save
5/ The table summary will be displayed with a dialog box on top:
 XX item(s) modified (with YY fields modified)
Check that XX and YY correspond with what you expected.

Signed-off-by: Liz Rea <liz@catalyst.net.nz>
batch modification still seems to work correctly, with the helpful addition of the counter. Thanks!

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 C4/BackgroundJob.pm                                |   38 +++++++++++
 .../prog/en/modules/tools/batchMod-edit.tt         |    9 ++-
 tools/batchMod.pl                                  |   70 +++++++++++++-------
 3 files changed, 93 insertions(+), 24 deletions(-)

diff --git a/C4/BackgroundJob.pm b/C4/BackgroundJob.pm
index 6830565..05327f0 100644
--- a/C4/BackgroundJob.pm
+++ b/C4/BackgroundJob.pm
@@ -260,6 +260,44 @@ sub fetch {
     return $self;
 }
 
+=head2 set
+
+=over 4
+
+$job->set($hashref);
+
+=back
+
+Set some variables into the hashref.
+These variables can be retrieved using the get method.
+
+=cut
+
+sub set {
+    my ($self, $hashref) = @_;
+    while ( my ($k, $v) = each %$hashref ) {
+        $self->{$k} = $v;
+    }
+    $self->_serialize();
+}
+
+=head2 get
+
+=over 4
+
+$job->get($hashref);
+
+=back
+
+Get a variable which has been previously stored with the set method.
+
+=cut
+
+sub get {
+    my ($self, $key) = @_;
+    return $self->{$key};
+}
+
 1;
 __END__
 
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt
index de67517..7c839a5 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-edit.tt
@@ -64,7 +64,14 @@ $(document).ready(function(){
 <div id="doc3" class="yui-t7">
     <div id="bd">
         <div id="yui-main">
-            [% IF ( show ) %]<h1>Batch item modification</h1>[% ELSE %]<h1>Batch item modification results</h1>[% END %]
+            [% IF ( show ) %]
+              <h1>Batch item modification</h1>
+            [% ELSE %]
+              <h1>Batch item modification results</h1>
+              <div class="dialog message">
+              [% modified_items %] item(s) modified (with [% modified_fields %] fields modified)
+              </div>
+            [% END %]
 [% IF ( barcode_not_unique ) %]<div class="dialog alert"><strong>Error saving item</strong>: Barcode must be unique.</div>[% END %]
 [% IF ( no_next_barcode ) %]<div class="dialog alert"><strong>Error saving items</strong>: Unable to automatically determine values for barcodes. No item has been inserted.</div>[% END %]
 [% IF ( book_on_loan ) %]<div class="dialog alert"><strong>Cannot Delete</strong>: item is checked out.</div>[% END %]
diff --git a/tools/batchMod.pl b/tools/batchMod.pl
index acb4f0b..e8d3bd7 100755
--- a/tools/batchMod.pl
+++ b/tools/batchMod.pl
@@ -84,6 +84,8 @@ my $deleted_items = 0;     # Number of deleted items
 my $deleted_records = 0;   # Number of deleted records ( with no items attached )
 my $not_deleted_items = 0; # Number of items that could not be deleted
 my @not_deleted;           # List of the itemnumbers that could not be deleted
+my $modified_items = 0;    # Numbers of modified items
+my $modified_fields = 0;   # Numbers of modified fields
 
 my %cookies = parse CGI::Cookie($cookie);
 my $sessionID = $cookies{'CGISESSID'}->value;
@@ -160,7 +162,7 @@ if ($op eq "action") {
 
 		$job->progress($i) if $runinbackground;
 		my $itemdata = GetItem($itemnumber);
-		if ($input->param("del")){
+        if ( $del ){
 			my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
 			if ($return == 1) {
 			    $deleted_items++;
@@ -184,14 +186,25 @@ if ($op eq "action") {
                             }
                         }
 		} else {
-		    if ($values_to_modify || $values_to_blank) {
-			my $localmarcitem = Item2Marc($itemdata);
-			UpdateMarcWith( $marcitem, $localmarcitem );
-			eval{
-                            if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
-                                LostItem($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost};
-                            }
-                        };
+            if ($values_to_modify || $values_to_blank) {
+                my $localmarcitem = Item2Marc($itemdata);
+
+                my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
+                if ( $modified ) {
+                    eval {
+                        if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
+                            LostItem($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost};
+                        }
+                    };
+                }
+                if ( $runinbackground ) {
+                    $modified_items++ if $modified;
+                    $modified_fields += $modified;
+                    $job->set({
+                        modified_items  => $modified_items,
+                        modified_fields => $modified_fields,
+                    });
+                }
 		    }
 		}
 		$i++;
@@ -559,21 +572,25 @@ sub BuildItemsData{
 # And $tag>10
 sub UpdateMarcWith {
   my ($marcfrom,$marcto)=@_;
-  #warn "FROM :",$marcfrom->as_formatted;
-	my (  $itemtag,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
-	my $fieldfrom=$marcfrom->field($itemtag);
-	my @fields_to=$marcto->field($itemtag);
-    foreach my $subfield ($fieldfrom->subfields()){
-		foreach my $field_to_update (@fields_to){
-		    if ($subfield->[1]){
-			$field_to_update->update($subfield->[0]=>$subfield->[1]);
-		    }
-		    else {
-			$field_to_update->delete_subfield(code=> $subfield->[0]);
-		    }
-		}
+    my (  $itemtag,   $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
+    my $fieldfrom=$marcfrom->field($itemtag);
+    my @fields_to=$marcto->field($itemtag);
+    my $modified = 0;
+    foreach my $subfield ( $fieldfrom->subfields() ) {
+        foreach my $field_to_update ( @fields_to ) {
+            if ( $subfield->[1] ) {
+                unless ( $field_to_update->subfield($subfield->[0]) ~~ $subfield->[1] ) {
+                    $modified++;
+                    $field_to_update->update( $subfield->[0] => $subfield->[1] );
+                }
+            }
+            else {
+                $modified++;
+                $field_to_update->delete_subfield( code => $subfield->[0] );
+            }
+        }
     }
-  #warn "TO edited:",$marcto->as_formatted;
+    return $modified;
 }
 
 sub find_value {
@@ -608,6 +625,13 @@ sub add_saved_job_results_to_template {
     my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
     my $results = $job->results();
     add_results_to_template($template, $results);
+
+    my $fields = $job->get("modified_fields");
+    my $items = $job->get("modified_items");
+    $template->param(
+        modified_items => $items,
+        modified_fields => $fields,
+    );
 }
 
 sub put_in_background {
-- 
1.7.2.5