From d55c011ac4a8272f0ead7021bee1c95a0ce44621 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Tue, 5 May 2020 15:51:25 +0200
Subject: [PATCH] Bug 23166: Call the methods from the .inc

We do not longer need the order variables to be passed from the
controllers, we can call the methods on the biblio object instead.

There is something wrong with our ->search method and TT behaviours, it
is hard to retrieve object list in a scalar context.
If [% objects.method.count %] is called, objects.method will get the
first object of the list and count will explode (Koha::Object->count
does not exist)

We need to force the call in a scalar context to retrieve an iterator
and prevent to fetch all the objects (we could have called all then
size, but it's not efficient)

If adopted I will move the plugin on a separate bug report to ease
backport
---
 Koha/Template/Plugin/Context.pm                    | 47 ++++++++++++++++++++++
 .../prog/en/includes/catalog-strings.inc           |  8 +++-
 2 files changed, 53 insertions(+), 2 deletions(-)
 create mode 100644 Koha/Template/Plugin/Context.pm

diff --git a/Koha/Template/Plugin/Context.pm b/Koha/Template/Plugin/Context.pm
new file mode 100644
index 0000000000..03c024b609
--- /dev/null
+++ b/Koha/Template/Plugin/Context.pm
@@ -0,0 +1,47 @@
+package Koha::Template::Plugin::Context;
+
+# 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 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.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use base qw( Template::Plugin );
+
+use C4::Context;
+
+=head1 NAME
+
+Koha::Template::Plugin::Scalar - Return object set in scalar context
+
+=head1 SYNOPSIS
+
+If you need to force scalar context when calling a method on a object set.
+Especially useful to call ->search
+
+=cut
+
+=head1 API
+
+=head2 Class Methods
+
+=cut
+
+sub Scalar {
+    my ( $self, $set, $method ) = @_;
+    $set = $set->$method;
+    return $set;
+}
+
+1;
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/catalog-strings.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/catalog-strings.inc
index 3e77d6a998..78980c334e 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/catalog-strings.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/catalog-strings.inc
@@ -1,3 +1,4 @@
+[% USE Context %]
 <!-- catalog-strings.inc -->
 <script>
     /* Strings for translation */
@@ -17,8 +18,11 @@
     var biblionumber = [% biblionumber | html %];
     var count = [% count | html %];
     var holdcount = [% holdcount | html %];
-    var countorders = [% countorders | html %];
-    var countdeletedorders = [% countdeletedorders | html %];
+    [% SET orders = biblio.orders %]
+    [% SET current   = Context.Scalar(orders, "filter_by_current") %]
+    [% SET cancelled = Context.Scalar(orders, "filter_by_cancelled") %]
+    var countorders = [% current.count || 0 | html %];
+    var countdeletedorders = [% cancelled.count || 0 | html %];
 
     /* provide Z3950 search points */
     function GetZ3950Terms(){
-- 
2.11.0