From e6a604c4277404db7580b970176933e3a8695ccd Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 9 Jun 2021 19:29:19 +0000
Subject: [PATCH] Bug 27734: Add OpacHiddenRecordRedirect and
 OpacHiddenRecordMessage system preferences

This patch adds options analogous to OpacSuppressionRedirect and OpacSuppressionMessage for OpacHiddtenItems

Test plan:
1 - Apply patch
2 - Test OpacSupressionsRedirect and OpacSuppressionMessage prefs to ensure they work as before
3 - Enable OpacHiddenItemsHidesRecord and hide a biblio using OpacHiddenItems:
    biblionumber: [303]
3 - Test OpacHiddenRecordRedirect and OpacHiddenRecordMessage to ensure they work as expected
4 - Hit the link: http://localhost:8080/cgi-bin/koha/opac-detail.pl?biblionumber=303
5 - When OpacHiddenRecordRedirect set to 404 you get a 404
6 - When OpacHiddenRecordRedirect set to 'an explanatory page' you get 'opac-blocked.pl'
7 - If you set a message in  OpacHiddenRecordMessage  it displays on opac-blocked.pl

Signed-off-by: marti <mfuerst@hmcpl.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
---
 installer/data/mysql/atomicupdate/bug_27734.perl     | 12 ++++++++++++
 installer/data/mysql/mandatory/sysprefs.sql          |  2 ++
 .../prog/en/modules/admin/preferences/opac.pref      |  9 +++++++++
 .../opac-tmpl/bootstrap/en/modules/opac-blocked.tt   | 10 ++++++++--
 opac/opac-blocked.pl                                 |  4 +++-
 opac/opac-detail.pl                                  |  6 +++++-
 6 files changed, 39 insertions(+), 4 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/bug_27734.perl

diff --git a/installer/data/mysql/atomicupdate/bug_27734.perl b/installer/data/mysql/atomicupdate/bug_27734.perl
new file mode 100644
index 0000000000..990b4b6672
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_27734.perl
@@ -0,0 +1,12 @@
+$DBversion = 'XXX';
+if( CheckVersion( $DBversion ) ) {
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
+        ('OpacHiddenRecordRedirect','0','Redirect the opac detail page for records hidden by OpacHiddenItems to an explanatory page (otherwise redirect to 404 error page)','','YesNo')
+    });
+    $dbh->do(q{
+        INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES
+        ('OpacHiddenRecordMessage','','Display this message on the redirect page for hidden biblios','70|10','Textarea')
+    });
+    NewVersion( $DBversion, 27734, "Add OpacHiddenRecordRedirect and OpacHiddenRecordMessage preferences");
+}
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index 0a86168ee0..a0d18d2dce 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -413,6 +413,8 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('OpacHiddenItems','','','This syspref allows to define custom rules for hiding specific items at the OPAC. See http://wiki.koha-community.org/wiki/OpacHiddenItems for more information.','Textarea'),
 ('OpacHiddenItemsExceptions','',NULL,'List of borrower categories, separated by |, that can see items otherwise hidden by OpacHiddenItems','Textarea'),
 ('OpacHiddenItemsHidesRecord','1','','Hide biblio record when all its items are hidden because of OpacHiddenItems','YesNo'),
+('OpacHiddenRecordRedirect','0','Redirect the opac detail page for records hidden by OpacHiddenItems to an explanatory page (otherwise redirect to 404 error page)','','YesNo'),
+('OpacHiddenRecordMessage','','Display this message on the redirect page for hidden biblios','70|10','Textarea'),
 ('OpacHighlightedWords','1','','If Set, then queried words are higlighted in OPAC','YesNo'),
 ('OPACHoldingsDefaultSortField','first_column','first_column|homebranch|holdingbranch','Default sort field for the holdings table at the OPAC','choice'),
 ('OPACHoldsHistory','0','','If ON, enables display of Patron Holds History in OPAC','YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
index 535f65bd87..107eee50d5 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref
@@ -662,6 +662,15 @@ OPAC:
                   0: "Don't hide"
                   1: Hide
             - "the bibliographic record when all its items are hidden by <a href='/cgi-bin/koha/admin/preferences.pl?op=search&searchfield=OpacHiddenItems'>OpacHiddenItems</a>."
+            - <br />Redirect the opac detail page for hidden records to
+            - pref: OpacHiddenRecordRedirect
+              choices:
+                  1: "an explanatory page ('This record is blocked')."
+                  0: "the 404 error page ('Not found')."
+            - "<br />Display the following message on the redirect page for suppressed bibliographic records:"
+            - pref: OpacHiddenRecordMessage
+              type: textarea
+              class: code
         -
             - pref: OpacAllowPublicListCreation
               default: 1
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-blocked.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-blocked.tt
index 43cd083839..7944bdee4a 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-blocked.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-blocked.tt
@@ -1,4 +1,5 @@
 [% USE raw %]
+[% USE Koha %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>
     Record blocked &rsaquo;
@@ -31,9 +32,14 @@
                 <div id="opac-blocked-message" class="maincontent">
                     <h1>Blocked</h1>
                     <p>You are not authorized to view this record.</p>
-                    [% IF ( OpacSuppressionMessage ) %]
+                    [% IF hidden %]
+                        [% SET OpacBlockedMessage = Koha.Preference("OpacHiddenRecordMessage") %]
+                    [% ELSE %]
+                        [% SET OpacBlockedMessage = Koha.Preference("OpacSuppressionMessage") %]
+                    [% END %]
+                    [% IF ( OpacBlockedMessage ) %]
                         <div id="opacsuppressionmessage">
-                            [% OpacSuppressionMessage | $raw %]
+                            [% OpacBlockedMessage | $raw %]
                         </div>
                     [% END %]
                 </div>
diff --git a/opac/opac-blocked.pl b/opac/opac-blocked.pl
index 2876823c46..5ea87fd3f9 100755
--- a/opac/opac-blocked.pl
+++ b/opac/opac-blocked.pl
@@ -33,6 +33,8 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
-$template->param ( OpacSuppressionMessage => C4::Context->preference('OpacSuppressionMessage'));
+if ( $query->param('hidden') ) {
+    $template->param( hidden => 1 );
+}
 
 output_with_http_headers $query, $cookie, $template->output, 'html';
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 6173b72862..2bdff8ee8b 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -103,7 +103,11 @@ unless ( $patron and $patron->category->override_hidden_items ) {
     # only skip this check if there's a logged in user
     # and its category overrides OpacHiddenItems
     if ( $biblio->hidden_in_opac({ rules => C4::Context->yaml_preference('OpacHiddenItems') }) ) {
-        print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
+        if ( C4::Context->preference("OpacHiddenRecordRedirect") ) {
+            print $query->redirect("/cgi-bin/koha/opac-blocked.pl?hidden=1");
+        } else {
+            print $query->redirect('/cgi-bin/koha/errors/404.pl'); # escape early
+        }
         exit;
     }
     if ( scalar @all_items >= 1 ) {
-- 
2.32.0