From 8c4cdcc673e3b61928d44ada568516a6d67e1ab5 Mon Sep 17 00:00:00 2001
From: David Gustafsson <david.gustafsson@ub.gu.se>
Date: Thu, 11 May 2023 15:12:20 +0200
Subject: [PATCH] Bug 31856: Refactor GetAuthorisedValueDesc

---
 C4/Biblio.pm             | 32 +++++++++++---------------------
 Koha/AuthorisedValue.pm  | 14 ++++++--------
 Koha/AuthorisedValues.pm | 37 +++++++++++++++++++------------------
 3 files changed, 36 insertions(+), 47 deletions(-)

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 6f36813c986..c6aadc5180e 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -115,6 +115,7 @@ use Koha::SearchEngine::Indexer;
 use Koha::SimpleMARC;
 use Koha::Libraries;
 use Koha::Util::MARC;
+use Koha::AuthorisedValues;
 
 =head1 NAME
 
@@ -1509,28 +1510,17 @@ sub GetAuthorisedValueDesc {
     }
 
     my $dbh = C4::Context->dbh;
-    if ( $category ne "" ) {
-        $cache_key = "AV_descriptions:" . $category;
-        my $av_descriptions = $cache->get_from_cache( $cache_key, { unsafe => 1 } );
-        if ( !$av_descriptions ) {
-            $av_descriptions = {
-                map {
-                    $_->authorised_value =>
-                      { lib => $_->lib, lib_opac => $_->lib_opac }
-                } Koha::AuthorisedValues->search(
-                    { category => $category },
-                    {
-                        columns => [ 'authorised_value', 'lib_opac', 'lib' ]
-                    }
-                )->as_list
-            };
-            $cache->set_in_cache($cache_key, $av_descriptions);
-        }
-        return ( $opac && $av_descriptions->{$value}->{'lib_opac'} )
-          ? $av_descriptions->{$value}->{'lib_opac'}
-          : $av_descriptions->{$value}->{'lib'};
+    if ( $category ) {
+        my $description = Koha::AuthorisedValues->get_description_by_category_and_authorised_value(
+            {
+                category => $category,
+                authorised_value => $value
+            }
+        );
+        return $opac ?
+            $description->{'opac_description'} : $description->{'lib'};
     } else {
-        return $value;    # if nothing is found return the original value
+        return $value; # if nothing is found return the original value
     }
 }
 
diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm
index 5669ab6086a..0d792cccd22 100644
--- a/Koha/AuthorisedValue.pm
+++ b/Koha/AuthorisedValue.pm
@@ -58,12 +58,8 @@ sub store {
     }
     else {
         my %updated_columns = $self->_result->get_dirty_columns;
-
-        if (   exists $updated_columns{lib}
-            or exists $updated_columns{lib_opac} )
-        {
-            $flush = 1;
-        }
+        $flush = exists $updated_columns{lib}
+            or exists $updated_columns{lib_opac};
     }
 
     $self->_check_is_integer_only;
@@ -71,7 +67,8 @@ sub store {
     $self = $self->SUPER::store;
 
     if ($flush) {
-        my $key = "AV_descriptions:".$self->category;
+        my $category = $self->category;
+        my $key = "AV_descriptions:$category";
         $cache->clear_from_cache($key);
     }
 
@@ -86,7 +83,8 @@ AuthorisedValue specific C<delete> to clear relevant caches on delete.
 
 sub delete {
     my $self = shift @_;
-    my $key = "AV_descriptions:".$self->category;
+    my $category = $self->category;
+    my $key = "AV_descriptions:$category";
     $cache->clear_from_cache($key);
     $self->SUPER::delete(@_);
 }
diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm
index 039afc5cc36..5cadb4f1baa 100644
--- a/Koha/AuthorisedValues.pm
+++ b/Koha/AuthorisedValues.pm
@@ -143,30 +143,31 @@ sub get_descriptions_by_koha_field {
 
 sub get_description_by_category_and_authorised_value {
     my ( $self, $params ) = @_;
-    return unless defined $params->{category} && defined $params->{authorised_value};
+    return unless defined $params->{category} and defined $params->{authorised_value};
 
     my $category = $params->{category};
     my $value = $params->{authorised_value};
 
-    my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
-    my $cache_key = "AV_description_by_category_and_authorised_value:$category:$value";
-    my $description = $memory_cache->get_from_cache($cache_key);
+    my $cache = Koha::Caches->get_instance();
+    my $cache_key = "AV_descriptions:$category";
+    my $descriptions = $cache->get_from_cache($cache_key, { unsafe => 1 });
 
-    unless (defined $description) {
-        my $av = $self->search(
-            {
-                category => $category,
-                authorised_value => $value
-            }
-        )->next;
-        $description = defined $av ? {
-            lib => $av->lib,
-            opac_description => $av->opac_description
-        } : {};
-        $memory_cache->set_in_cache( $cache_key, $description );
+    unless (defined $descriptions) {
+        $descriptions = {
+            map {
+                $_->authorised_value => {
+                    lib => $_->lib,
+                    opac_description => $_->opac_description
+                }
+            } $self->search(
+                {
+                    category => $category
+                }
+            )->as_list
+        };
+        $cache->set_in_cache($cache_key, $descriptions);
     }
-
-    return $description;
+    return defined $descriptions->{$value} ? $descriptions->{$value} : {};
 }
 
 =head3 get_descriptions_by_marc_field
-- 
2.48.1