From c46857b900d65f20d44fcf8bf697cfd4bd65c336 Mon Sep 17 00:00:00 2001
From: Andrew Nugged <nugged@gmail.com>
Date: Thu, 14 Nov 2019 13:17:13 +0200
Subject: [PATCH] Bug 24027: (follow-up) Don't combine long items-add POST page
 with list generation

When hundreds or thousands of items added, it takes time.

Then generation of items list also takes time, and more items
more items list generation needed, so sometimes user gets even browser
timeout. It's much more sensible on big databases and libraries
hosting/clouds with limited resources.

So - showing only counter of added items on POST-generated page instead of
list generation shortens page generation time and then user can navigate
further.

This stacks up with previous patches in this ticket for the combined effect.
---
 cataloguing/additem.pl                        | 36 +++++++++++++------
 .../prog/en/modules/cataloguing/additem.tt    |  9 +++++
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index ca987a41b0..51eef069b4 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -397,6 +397,9 @@ my $fa_duedatespec        = $input->param('duedatespec');
 
 my $frameworkcode = &GetFrameworkCode($biblionumber);
 
+# counter for multiple added items to put into template
+my $num_items_added = 0;
+
 # Defining which userflag is needing according to the framework currently used
 my $userflags;
 if (defined $input->param('frameworkcode')) {
@@ -517,6 +520,7 @@ if ($op eq "additem") {
                 $cookie = [ $cookie, $itemcookie ];
             }
 
+            $num_items_added++;
         }
         $nextop = "additem";
         if ($exist_itemnumber) {
@@ -609,6 +613,8 @@ if ($op eq "additem") {
             $i++;
             # Only increment copynumber if item was really added
             $copynumber++  if ( $copynumber && $copynumber =~ m/^\d+$/ );
+
+			$num_items_added++;
         }
 
 		# Preparing the next iteration
@@ -857,18 +863,25 @@ my ($holdingbrtagf,$holdingbrtagsubf) = &GetMarcFromKohaField( "items.holdingbra
 # First, the existing items for display
 my @item_value_loop;
 my @header_value_loop;
-for my $row ( @big_array ) {
-    my %row_data;
-    my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
-    $row_data{item_value} = [ @item_fields ];
-    $row_data{itemnumber} = $row->{itemnumber};
-    #reporting this_row values
-    $row_data{'nomod'} = $row->{'nomod'};
-    $row_data{'hostitemflag'} = $row->{'hostitemflag'};
-    $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
-#	$row_data{'countanalytics'} = $row->{'countanalytics'};
-    push(@item_value_loop,\%row_data);
+
+if( ! $num_items_added ) {
+    # do this only if we didn't just added number of items
+    # (to make added items POST page for not to load long):
+
+    for my $row (@big_array) {
+        my %row_data;
+        my @item_fields = map +{ field => $_ || '' }, @$row{ sort keys(%witness) };
+        $row_data{item_value} = [@item_fields];
+        $row_data{itemnumber} = $row->{itemnumber};
+        #reporting this_row values
+        $row_data{'nomod'}            = $row->{'nomod'};
+        $row_data{'hostitemflag'}     = $row->{'hostitemflag'};
+        $row_data{'hostbiblionumber'} = $row->{'hostbiblionumber'};
+        #	$row_data{'countanalytics'} = $row->{'countanalytics'};
+        push( @item_value_loop, \%row_data );
+    }
 }
+
 foreach my $subfield_code (sort keys(%witness)) {
     my %header_value;
     $header_value{header_value} = $witness{$subfield_code};
@@ -946,6 +959,7 @@ $template->param(
     biblionumber => $biblionumber,
     title        => $oldrecord->{title},
     author       => $oldrecord->{author},
+    num_items_added  => $num_items_added,
     item_loop        => \@item_value_loop,
     item_header_loop => \@header_value_loop,
     item             => \@loop_data,
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
index 39476f623a..45c1a02d90 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/additem.tt
@@ -41,6 +41,13 @@
 [% IF ( not_same_branch ) %]<div class="dialog alert"><strong>Cannot delete</strong>: The items do not belong to your library.</div>[% END %]
 [% IF ( linked_analytics ) %]<div class="dialog alert"><strong>Cannot delete</strong>: item has linked <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% biblionumber | uri %]&amp;analyze=1">analytics.</a>.</div>[% END %]
 
+[% IF ( num_items_added ) %]
+<div class="dialog message">
+    <h3><strong>[% num_items_added %]</strong> item(s) added.</h3>
+    <a href="/cgi-bin/koha/cataloguing/additem.pl?biblionumber=[% biblionumber %]">Return to the added list</a>
+</div>
+[% END %]
+
 <div id="cataloguing_additem_itemlist">
     [% IF ( item_loop ) %]
         <div>
@@ -120,6 +127,7 @@
     <div class="col-sm-10">
 
 <div id="cataloguing_additem_newitem">
+[% IF NOT ( num_items_added ) %]
     <form id="f" method="post" action="/cgi-bin/koha/cataloguing/additem.pl?biblionumber=[% biblionumber | html %]" name="f">
     <input type="hidden" name="op" value="[% op | html %]" />
     [% IF (popup) %]
@@ -247,6 +255,7 @@
 
 
     </form>
+[% END %]
                             </div> <!-- /#cataloguing_additem_newitem -->
                         </div> <!-- /.col-sm-10 -->
                     </div> <!-- /.row -->
-- 
2.17.1