From 0e83b2aec118b285d0a49797c1581a7773fa14e2 Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 23 Sep 2021 08:52:50 -0300
Subject: [PATCH] Bug 29083: Fix OPAC listing of article requests

This patch makes the OPAC template reuse a precalculated value for the
active article requests for the patron (and its count).

The original code relied on the methods returning a list, which is not
the case for _new_from_dbic until bug 28883 is pushed.

This patch fixes that.

Note: there was an odd behavior when ArticleRequests was enabled but no
active article requests were present: the tab wasn't rendered but the
'empty table' with the 'You have no article requests currently.' message
was displayed below the Checkouts tab. I'm not sure that was caused by
this patches, or other. Fixed on this patch.

To test:
1. In the OPAC, go to 'your summary'
=> FAIL: Things don't show for article requests
2. Add some article requests and repeat 1
=> FAIL: Something's wrong there
3. Apply this patch and repeat 1
=> Yes! Things show correctly!
4. Cancel all your article requests
=> SUCCESS: Things render as they should
5. Re-enter the 'your summary' page (to force re-rendering)
=> SUCCESS: Things render correctly for empty article requests
6. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt | 8 ++++----
 opac/opac-user.pl                                     | 7 +++++++
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
index bc97a5c2ab5..a50609d9ccd 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt
@@ -217,7 +217,7 @@
                             [% END %]
 
                             [% IF ( RESERVES.count ) %]<li><a href="#opac-user-holds">Holds ([% RESERVES.count | html %])</a></li>[% END %]
-                            [% IF Koha.Preference('ArticleRequests') && logged_in_user.article_requests_current %]<li><a href="#opac-user-article-requests">Article requests ([% logged_in_user.article_requests_current.count | html %])</a></li>[% END %]
+                            [% IF Koha.Preference('ArticleRequests') %]<li><a href="#opac-user-article-requests">Article requests ([% current_article_requests_count || 0 | html %])</a></li>[% END %]
                             [% IF ( OverDriveCirculation ) %]
                             <li><a href="#opac-user-overdrive">OverDrive Account</a></li>
                             [% END %]
@@ -714,7 +714,7 @@
 
                         [% IF Koha.Preference('ArticleRequests') %]
                             <div id="opac-user-article-requests">
-                                [% IF logged_in_user.article_requests_current.count %]
+                                [% IF current_article_requests_count %]
                                     <table id="article-requests-table" class="table table-bordered table-striped">
                                         <caption>Article requests <span class="count"></span></caption>
                                         <thead>
@@ -737,7 +737,7 @@
                                         </thead>
 
                                         <tbody>
-                                        [% FOREACH ar IN logged_in_user.article_requests_current %]
+                                        [% FOREACH ar IN current_article_requests %]
                                                 <td class="article-request-record-title">
                                                     <a class="article-request-title" href="/cgi-bin/koha/opac-detail.pl?biblionumber=[% ar.biblionumber | html %]">
                                                         [% INCLUDE 'biblio-title.inc' biblio=ar.biblio %]
@@ -853,7 +853,7 @@
             }
         }
         $(document).ready(function(){
-            $('#opac-user-article-requests caption .count').html(AR_CAPTION_COUNT.format('[% logged_in_user.article_requests_current.count | html %]'));
+            $('#opac-user-article-requests caption .count').html(AR_CAPTION_COUNT.format('[% current_article_requests_count | html %]'));
             $('#opac-user-views').tabs();
             $(".modal-nojs").addClass("modal").addClass("hide").removeClass("modal-nojs");
             $(".suspend-until").prop("readonly",1);
diff --git a/opac/opac-user.pl b/opac/opac-user.pl
index 8deb054e721..1cd97b628e0 100755
--- a/opac/opac-user.pl
+++ b/opac/opac-user.pl
@@ -386,6 +386,13 @@ if (   C4::Context->preference('AllowPatronToSetFinesVisibilityForGuarantor')
     $template->param( relatives_with_fines => \@relatives_with_fines );
 }
 
+if ( C4::Context->preference("ArticleRequests") ) {
+    my @current_article_requests = $patron->article_requests_current->as_list;
+    $template->param(
+        current_article_requests => \@current_article_requests,
+        current_article_requests_count => scalar @current_article_requests,
+    );
+}
 
 $template->param(
     patron_messages          => $patron_messages,
-- 
2.20.1