From 978e38dd4ef063067f2398cd5991080c968b6ee1 Mon Sep 17 00:00:00 2001
From: Andreas Jonsson <andreas.jonsson@kreablo.se>
Date: Wed, 17 Apr 2024 08:10:46 +0000
Subject: [PATCH] Bug 14007: Hide hidden itemtypes in opac facets.

Test plan:

* Start koha-testing-docker with elasticsearch or opensearch (e.g., ku-es7)
* Make sure there is a built index for Elastic/OpenSearch:

  > koha-elasticsearch --reset --rebuild kohadev

* Search for "book" in opac and note the facet values for itemtype
* Change systempreference SearchEngine to ElasticSearch
* Search for "book" in opac and note the facet values for itemtype
* Go to the adminstration view of one of the itemtypes (for instance
  Visual Materials) and check "Hide in OPAC".
* Search for "book" in opac and make sure the hidden itemtype do
  not appear as facet.
* Search for "book" in staff interface and make sure the hidden
  itemtype still does appear as a facet.
* Change back the systemprefence to Zebra
* Search for "book" in opac and make sure the hidden itemtype do
  not appear as facet.
* Search for "book" in staff interface and make sure the hidden
  itemtype still does appear as a facet.
---
 C4/Search.pm                              | 14 ++++++++++++++
 Koha/SearchEngine/Elasticsearch/Search.pm |  4 ++++
 2 files changed, 18 insertions(+)

diff --git a/C4/Search.pm b/C4/Search.pm
index 248856e42b..4ce25412d4 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -619,6 +619,20 @@ sub GetFacets {
         $facets = _get_facets_from_records( $rs );
     }
 
+    if ( C4::Context->interface eq 'opac' ) {
+        for my $k ( keys %$facets ) {
+            if ( $k eq 'itype' ) {
+                my $itemtypes = Koha::ItemTypes->new;
+                for my $fv ( keys %{ $facets->{$k} } ) {
+                    my $itemtype = $itemtypes->find($fv);
+                    if ( defined $itemtype && $itemtype->hideinopac ) {
+                        delete $facets->{$k}->{$fv};
+                    }
+                }
+            }
+        }
+    }
+
     return $facets;
 }
 
diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm
index 933b52f79d..1b263560cd 100644
--- a/Koha/SearchEngine/Elasticsearch/Search.pm
+++ b/Koha/SearchEngine/Elasticsearch/Search.pm
@@ -476,6 +476,9 @@ sub _convert_facets {
         holdingbranch => $library_names,
         homebranch => $library_names
     );
+
+    my @hidden_itemtypes = map { $_->itemtype } ( grep { $opac && $_->hideinopac } @itypes );
+
     my @facets;
     $exp_facet //= '';
     while ( my ( $type, $data ) = each %$es ) {
@@ -495,6 +498,7 @@ sub _convert_facets {
             next unless length($t); # FIXME Currently we cannot search for an empty faceted field i.e. ln:"" to find records missing languages, though ES does count them correctly
             my $c = $term->{doc_count};
             my $label;
+            next if $type eq 'itype' && grep { $_ eq $t } @hidden_itemtypes;
             if ( exists( $special{$type} ) ) {
                 $label = $special{$type}->{$t} // $t;
             }
-- 
2.39.2