From 958708b26acb51deaa4e81c9c81058de2440c7c1 Mon Sep 17 00:00:00 2001
From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Date: Thu, 1 Mar 2018 15:38:48 +0100
Subject: [PATCH] Bug 17530: Use can_article_request to control sidebar link
Content-Type: text/plain; charset=utf-8

Before this patch, the 'Request article' link is displayed whenever the
pref is enabled. In many cases this might be useless. Instead of a guess
as in opac-search, we now call can_article_request to know for sure.
Note: at least this is the case when a user has logged in.

Update sidebar template with template variable artreqpossible.
Add code in opac-detail, MARCdetail and ISBDdetail to fill it.

Test plan:
[1] Look for two biblios with items: one that should allow article requests
    and one that should not (respecting branch, patron, item type).
[2] Verify on detail, ISBD and MARC that the link is displayed for
    the first biblio and hidden for the second biblio.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc | 2 +-
 opac/opac-ISBDdetail.pl                                           | 7 +++++++
 opac/opac-MARCdetail.pl                                           | 7 +++++++
 opac/opac-detail.pl                                               | 8 ++++++++
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc
index e408d0a..b3d44c1 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/opac-detail-sidebar.inc
@@ -13,7 +13,7 @@
     <li><a class="print-large" href="#">Print</a></li>
 
     [% IF Koha.Preference( 'opacuserlogin' ) == 1 %]
-        [% IF Koha.Preference('ArticleRequests') %]
+        [% IF artreqpossible %]
             <li><a class="article_request" href="/cgi-bin/koha/opac-request-article.pl?biblionumber=[% biblio.biblionumber %]">Request article</a></li>
         [% END %]
     [% END %]
diff --git a/opac/opac-ISBDdetail.pl b/opac/opac-ISBDdetail.pl
index 40e9b71..cdbd37b 100755
--- a/opac/opac-ISBDdetail.pl
+++ b/opac/opac-ISBDdetail.pl
@@ -217,4 +217,11 @@ if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
     $template->param('OPACSearchForTitleIn' => $search_for_title);
 }
 
+if( C4::Context->preference('ArticleRequests') ) {
+    my $artreqpossible = $patron
+        ? $biblio->can_article_request( $patron )
+        : $biblio->may_article_request;
+    $template->param( artreqpossible => $artreqpossible );
+}
+
 output_html_with_http_headers $query, $cookie, $template->output;
diff --git a/opac/opac-MARCdetail.pl b/opac/opac-MARCdetail.pl
index ebb78a2..926b70b 100755
--- a/opac/opac-MARCdetail.pl
+++ b/opac/opac-MARCdetail.pl
@@ -349,6 +349,13 @@ if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
     $template->param('OPACSearchForTitleIn' => $search_for_title);
 }
 
+if( C4::Context->preference('ArticleRequests') ) {
+    my $artreqpossible = $patron
+        ? $biblio->can_article_request( $patron )
+        : $biblio->may_article_request;
+    $template->param( artreqpossible => $artreqpossible );
+}
+
 $template->param(
     item_loop           => \@item_loop,
     item_header_loop    => \@item_header_loop,
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index e7653d9..00a05a6 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -756,6 +756,14 @@ if (!C4::Context->preference("OPACXSLTDetailsDisplay") ) {
 my $marcnotesarray   = GetMarcNotes   ($record,$marcflavour);
 my $subtitle         = GetRecordValue('subtitle', $record, GetFrameworkCode($biblionumber));
 
+if( C4::Context->preference('ArticleRequests') ) {
+    my $patron = $borrowernumber ? Koha::Patrons->find($borrowernumber) : undef;
+    my $artreqpossible = $patron
+        ? $biblio->can_article_request( $patron )
+        : $biblio->may_article_request;
+    $template->param( artreqpossible => $artreqpossible );
+}
+
     $template->param(
                      MARCNOTES               => $marcnotesarray,
                      norequests              => $norequests,
-- 
2.1.4