From fde147b306033c35207303632a35fafc924aba56 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 7 Mar 2023 19:23:44 +0000
Subject: [PATCH] Bug 33159: Correctly form thesaurus term for non-subject
 headings

Bug 30280 fixed things for subject heading, but didn't take into account
that all non-subject headings are considered LCSH - this means we can't
really handle different thesauri for controlled headings outside of
subjects - maybe a topic for a new bug, but we should fix matching for
them now

To test:
 1 - Set sysprefs:
    RequireChoosingExistingAuthority - don't require
    AutoCreateAuthorities - generate
    CatalogModuleRelink - Do
    AutoLinkBiblio - Do
    LinkerModule - first match
 2 - Find a record with an author attached to an authority, I used
  biblionumber 3 "Introduction to Attic Greek"
 3 - Edit in advanced editor
 4 - Delete the $9 link
 5 - Save the record
 6 - It generates a new number
 7 - Repeat 4-5, it does it again
 8 - Apply patch and restart all
 9 - Repeat 4-5
10 - It links to the original authority

Signed-off-by: Phil Ringnalda <phil@chetcolibrary.org>
Signed-off-by: Frank Hansen <frank.hansen@ub.lu.se>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 C4/Heading.pm | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)

diff --git a/C4/Heading.pm b/C4/Heading.pm
index 7541a56205..cce3abf6fc 100644
--- a/C4/Heading.pm
+++ b/C4/Heading.pm
@@ -194,7 +194,7 @@ sub _search {
     my $self         = shift;
     my $index        = shift || undef;
     my $skipmetadata = shift || undef;
-    my $ind2         = $self->{field}->{_ind2};
+    my $thesaurus = $self->{thesaurus};
     my $subject_heading_thesaurus = '';
     my @marclist;
     my @and_or;
@@ -209,23 +209,29 @@ sub _search {
         push @value,    $self->{'search_form'};
     }
 
-    if ($self->{'thesaurus'}) {
-        if ($ind2 eq '0') {
+    if ( $thesaurus ) {
+    # This is calculated in C4/Heading/MARC21.pm - not used for UNIMARC
+        if ($thesaurus eq 'lcsh') {
             $subject_heading_thesaurus = 'a';
-        } elsif ($ind2 eq '1') {
+        } elsif ($thesaurus eq 'lcac') {
             $subject_heading_thesaurus = 'b';
-        } elsif ($ind2 eq '2') {
+        } elsif ($thesaurus eq 'mesh') {
             $subject_heading_thesaurus = 'c';
-        } elsif ($ind2 eq '3') {
+        } elsif ($thesaurus eq 'nal') {
             $subject_heading_thesaurus = 'd';
-        } elsif ($ind2 eq '4') {
+        } elsif ($thesaurus eq 'notspecified') {
             $subject_heading_thesaurus = 'n';
-        } elsif ($ind2 eq '5') {
+        } elsif ($thesaurus eq 'cash') {
             $subject_heading_thesaurus = 'k';
-        } elsif ($ind2 eq '6') {
+        } elsif ($thesaurus eq 'rvm') {
             $subject_heading_thesaurus = 'v';
-        } else {
+        } else { # We stored the value from $7 as the thesaurus if there was one
             $subject_heading_thesaurus = 'z';
+            push @marclist, 'thesaurus-conventions';
+            push @and_or, 'and';
+            push @excluding, '';
+            push @operator, 'is';
+            push @value, $self->{'thesaurus'};
         }
         push @marclist, 'thesaurus';
         push @and_or, 'and';
@@ -234,14 +240,6 @@ sub _search {
         push @value, $subject_heading_thesaurus;
     }
 
-    if ($ind2 eq '7') {
-        push @marclist, 'thesaurus-conventions';
-        push @and_or, 'and';
-        push @excluding, '';
-        push @operator, 'is';
-        push @value, $self->{'thesaurus'};
-    }
-
     require Koha::SearchEngine::QueryBuilder;
     require Koha::SearchEngine::Search;
 
-- 
2.30.2