From fe85462bc8d5716bb4b915fd5532c5d5e5f2dc3a Mon Sep 17 00:00:00 2001
From: Katrin Fischer <katrin.fischer.83@web.de>
Date: Fri, 9 Jun 2023 11:19:45 +0000
Subject: [PATCH] Bug 32695: Fix search strings for 775, 780, 785 and 787
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In the past due to some misunderstanding of the MARC standard
we built the search links for these fields using $a and $t with
the title index. But we actually need to search $t as title
and $a as author.
This patch fixes the templates for the MARC 7xx fields:
* 775
* 780
* 785
* 787

To test:
* Ensure systme preference 'UseControlNumber' is set to don't
* Create a record with the linking fields above
* Look at the OPAC and staff interface detail views
* Verify the content of your fields shows and the links combine a+t as a title search
* Apply patch
* Verify you have nicely formatted links now

Example:
775 _ _ ‡asomeone‡ttitle775
780 0 2 ‡asomeone‡ttitle780
785 0 2 ‡asomeone‡ttitle785
787 _ _ ‡ilabel:‡ttitle787‡asomeone

Signed-off-by: Sabrina Kiehl <kiehl@mpis.mpg.de>
---
 .../en/xslt/MARC21slim2intranetDetail.xsl     | 105 +++++++++++-----
 .../en/xslt/MARC21slim2OPACDetail.xsl         | 115 ++++++++++++------
 2 files changed, 155 insertions(+), 65 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2intranetDetail.xsl b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2intranetDetail.xsl
index 2c0898235a..ca783fa4a2 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2intranetDetail.xsl
+++ b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slim2intranetDetail.xsl
@@ -1321,16 +1321,29 @@
         <xsl:if test="marc:datafield[@tag=775]">
         <span class="results_summary other_editions"><span class="label">Other editions: </span>
         <xsl:for-each select="marc:datafield[@tag=775]">
-            <xsl:variable name="f775">
-                <xsl:call-template name="chopPunctuation"><xsl:with-param name="chopString"><xsl:call-template name="subfieldSelect">
-                <xsl:with-param name="codes">t</xsl:with-param>
-                </xsl:call-template></xsl:with-param></xsl:call-template>
+
+            <xsl:variable name="relation_query">
+                <xsl:text>ti,phr:(</xsl:text>
+                <xsl:call-template name="quote_search_term">
+                    <xsl:with-param name="term"><xsl:value-of select="marc:subfield[@code='t']"/></xsl:with-param>
+                </xsl:call-template>
+                <xsl:text>)</xsl:text>
+                <xsl:if test="marc:subfield[@code='a']">
+                    <xsl:text> AND au:(</xsl:text>
+                    <xsl:call-template name="quote_search_term">
+                        <xsl:with-param name="term">
+                            <xsl:value-of select="marc:subfield[@code='a']"/>
+                        </xsl:with-param>
+                    </xsl:call-template>
+                    <xsl:text>)</xsl:text>
+                </xsl:if>
             </xsl:variable>
+
             <xsl:if test="marc:subfield[@code='i']">
                 <xsl:call-template name="subfieldSelect">
                     <xsl:with-param name="codes">i</xsl:with-param>
                 </xsl:call-template>
-                <xsl:text>: </xsl:text>
+                <xsl:text> </xsl:text>
             </xsl:if>
             <a>
             <xsl:choose>
@@ -1338,7 +1351,7 @@
                 <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
             </xsl:when>
             <xsl:otherwise>
-                <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=ti,phr:<xsl:value-of select="str:encode-uri(translate($f775, '()', ''), true())"/></xsl:attribute>
+                <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
             </xsl:otherwise>
             </xsl:choose>
             <xsl:call-template name="subfieldSelect">
@@ -1393,20 +1406,38 @@
                             </span>
                         </xsl:if>
 
-                        <xsl:variable name="f780">
-                            <xsl:call-template name="subfieldSelect">
-                                <xsl:with-param name="codes">a_t</xsl:with-param>
+                        <xsl:variable name="relation_query">
+                            <xsl:text>ti,phr:(</xsl:text>
+                            <xsl:call-template name="quote_search_term">
+                                <xsl:with-param name="term"><xsl:value-of select="marc:subfield[@code='t']"/></xsl:with-param>
                             </xsl:call-template>
+                            <xsl:text>)</xsl:text>
+                            <xsl:if test="marc:subfield[@code='a']">
+                                <xsl:text> AND au:(</xsl:text>
+                                <xsl:call-template name="quote_search_term">
+                                    <xsl:with-param name="term">
+                                        <xsl:value-of select="marc:subfield[@code='a']"/>
+                                    </xsl:with-param>
+                                </xsl:call-template>
+                                <xsl:text>)</xsl:text>
+                            </xsl:if>
                         </xsl:variable>
+
                         <xsl:choose>
                             <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
-                                <a><xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
-                                    <xsl:value-of select="$f780"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:when>
                             <xsl:otherwise>
-                                <a><xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=ti,phr:<xsl:value-of select="str:encode-uri(translate($f780, '()', ''), true())"/></xsl:attribute>
-                                    <xsl:value-of select="$f780"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                                     <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:otherwise>
                         </xsl:choose>
@@ -1463,21 +1494,38 @@
                             </span>
                         </xsl:if>
 
-                        <xsl:variable name="f785">
-                            <xsl:call-template name="subfieldSelect">
-                                <xsl:with-param name="codes">a_t</xsl:with-param>
+                        <xsl:variable name="relation_query">
+                            <xsl:text>ti,phr:(</xsl:text>
+                            <xsl:call-template name="quote_search_term">
+                                <xsl:with-param name="term"><xsl:value-of select="marc:subfield[@code='t']"/></xsl:with-param>
                             </xsl:call-template>
+                            <xsl:text>)</xsl:text>
+                            <xsl:if test="marc:subfield[@code='a']">
+                                <xsl:text> AND au:(</xsl:text>
+                                <xsl:call-template name="quote_search_term">
+                                    <xsl:with-param name="term">
+                                        <xsl:value-of select="marc:subfield[@code='a']"/>
+                                    </xsl:with-param>
+                                </xsl:call-template>
+                                <xsl:text>)</xsl:text>
+                            </xsl:if>
                         </xsl:variable>
 
                         <xsl:choose>
                             <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
-                                <a><xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
-                                    <xsl:value-of select="$f785"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:when>
                             <xsl:otherwise>
-                                <a><xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=ti,phr:<xsl:value-of select="str:encode-uri(translate($f785, '()', ''), true())"/></xsl:attribute>
-                                    <xsl:value-of select="$f785"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:otherwise>
                         </xsl:choose>
@@ -1492,20 +1540,16 @@
         <span class="results_summary other_relationship_entry"><span class="label">Other related works: </span>
         <xsl:for-each select="marc:datafield[@tag=787]">
             <span class="other_relationship_entry">
-                <xsl:variable name="f787">
-                    <xsl:call-template name="chopPunctuation"><xsl:with-param name="chopString"><xsl:call-template name="subfieldSelect">
-                    <xsl:with-param name="codes">a_t</xsl:with-param>
-                    </xsl:call-template></xsl:with-param></xsl:call-template>
-                </xsl:variable>
                 <xsl:if test="marc:subfield[@code='i']">
                     <xsl:call-template name="subfieldSelect">
                         <xsl:with-param name="codes">i</xsl:with-param>
                     </xsl:call-template>
-                    <xsl:text>: </xsl:text>
+                    <xsl:text> </xsl:text>
                 </xsl:if>
                 <xsl:choose>
                 <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
-                    <a><xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
+                    <a>
+                        <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
                         <xsl:value-of select="$f787"/>
                     </a>
                 </xsl:when>
@@ -1527,9 +1571,10 @@
                         </xsl:if>
                     </xsl:variable>
                     <a>
-                    <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" />
-                    </xsl:attribute>
-                        <xsl:value-of select="$f787"/>
+                        <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                        <xsl:call-template name="subfieldSelect">
+                            <xsl:with-param name="codes">a_t</xsl:with-param>
+                        </xsl:call-template>
                     </a>
                 </xsl:otherwise>
                 </xsl:choose>
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slim2OPACDetail.xsl b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slim2OPACDetail.xsl
index 98d3e4f9d0..515f954345 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slim2OPACDetail.xsl
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slim2OPACDetail.xsl
@@ -1094,21 +1094,19 @@
         <span class="results_summary other_relationship_entry"><span class="label">Other related works: </span>
         <xsl:for-each select="marc:datafield[@tag=787]">
             <span class="other_relationship_entry">
-                <xsl:variable name="f787">
-                    <xsl:call-template name="chopPunctuation"><xsl:with-param name="chopString"><xsl:call-template name="subfieldSelect">
-                    <xsl:with-param name="codes">a_t</xsl:with-param>
-                    </xsl:call-template></xsl:with-param></xsl:call-template>
-                </xsl:variable>
                 <xsl:if test="marc:subfield[@code='i']">
                     <xsl:call-template name="subfieldSelect">
                         <xsl:with-param name="codes">i</xsl:with-param>
                     </xsl:call-template>
-                    <xsl:text>: </xsl:text>
+                    <xsl:text> </xsl:text>
                 </xsl:if>
                 <xsl:choose>
                 <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
-                    <a><xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
-                        <xsl:value-of select="$f787"/>
+                    <a>
+                        <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
+                        <xsl:call-template name="subfieldSelect">
+                            <xsl:with-param name="codes">a_t</xsl:with-param>
+                        </xsl:call-template>
                     </a>
                 </xsl:when>
                 <xsl:otherwise>
@@ -1129,9 +1127,10 @@
                         </xsl:if>
                     </xsl:variable>
                     <a>
-                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" />
-                    </xsl:attribute>
-                        <xsl:value-of select="$f787"/>
+                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                    <xsl:call-template name="subfieldSelect">
+                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                    </xsl:call-template>
                     </a>
                 </xsl:otherwise>
                 </xsl:choose>
@@ -1532,25 +1531,38 @@
         <xsl:if test="marc:datafield[@tag=775]">
         <span class="results_summary other_editions"><span class="label">Other editions: </span>
         <xsl:for-each select="marc:datafield[@tag=775]">
-            <xsl:variable name="f775">
-                <xsl:call-template name="chopPunctuation"><xsl:with-param name="chopString"><xsl:call-template name="subfieldSelect">
-                <xsl:with-param name="codes">t</xsl:with-param>
-                </xsl:call-template></xsl:with-param></xsl:call-template>
+
+            <xsl:variable name="relation_query">
+                <xsl:text>ti,phr:(</xsl:text>
+                <xsl:call-template name="quote_search_term">
+                    <xsl:with-param name="term"><xsl:value-of select="marc:subfield[@code='t']"/></xsl:with-param>
+                </xsl:call-template>
+                <xsl:text>)</xsl:text>
+                <xsl:if test="marc:subfield[@code='a']">
+                    <xsl:text> AND au:(</xsl:text>
+                    <xsl:call-template name="quote_search_term">
+                        <xsl:with-param name="term">
+                            <xsl:value-of select="marc:subfield[@code='a']"/>
+                        </xsl:with-param>
+                    </xsl:call-template>
+                    <xsl:text>)</xsl:text>
+                </xsl:if>
             </xsl:variable>
+
             <xsl:if test="marc:subfield[@code='i']">
                 <xsl:call-template name="subfieldSelect">
                     <xsl:with-param name="codes">i</xsl:with-param>
                 </xsl:call-template>
-                <xsl:text>: </xsl:text>
+                <xsl:text> </xsl:text>
             </xsl:if>
             <a>
             <xsl:choose>
-            <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
-                <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
-            </xsl:when>
-            <xsl:otherwise>
-                <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=ti,phr:<xsl:value-of select="str:encode-uri(translate($f775, '()', ''), true())"/></xsl:attribute>
-            </xsl:otherwise>
+                <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
+                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
+                </xsl:when>
+                <xsl:otherwise>
+                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                </xsl:otherwise>
             </xsl:choose>
             <xsl:call-template name="subfieldSelect">
                 <xsl:with-param name="codes">a_t</xsl:with-param>
@@ -1604,21 +1616,38 @@
                             </span>
                         </xsl:if>
 
-                        <xsl:variable name="f780">
-                            <xsl:call-template name="subfieldSelect">
-                                <xsl:with-param name="codes">a_t</xsl:with-param>
+                        <xsl:variable name="relation_query">
+                            <xsl:text>ti,phr:(</xsl:text>
+                            <xsl:call-template name="quote_search_term">
+                                <xsl:with-param name="term"><xsl:value-of select="marc:subfield[@code='t']"/></xsl:with-param>
                             </xsl:call-template>
+                            <xsl:text>)</xsl:text>
+                            <xsl:if test="marc:subfield[@code='a']">
+                                <xsl:text> AND au:(</xsl:text>
+                                <xsl:call-template name="quote_search_term">
+                                    <xsl:with-param name="term">
+                                        <xsl:value-of select="marc:subfield[@code='a']"/>
+                                    </xsl:with-param>
+                                </xsl:call-template>
+                                <xsl:text>)</xsl:text>
+                            </xsl:if>
                         </xsl:variable>
 
                         <xsl:choose>
                             <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
-                                <a><xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
-                                    <xsl:value-of select="$f780"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:when>
                             <xsl:otherwise>
-                                <a><xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=ti,phr:<xsl:value-of select="str:encode-uri(translate($f780, '()', ''), true())"/></xsl:attribute>
-                                    <xsl:value-of select="$f780"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:otherwise>
                         </xsl:choose>
@@ -1675,21 +1704,37 @@
                             </span>
                         </xsl:if>
 
-                        <xsl:variable name="f785">
-                            <xsl:call-template name="subfieldSelect">
-                                <xsl:with-param name="codes">a_t</xsl:with-param>
+                        <xsl:variable name="relation_query">
+                            <xsl:text>ti,phr:(</xsl:text>
+                            <xsl:call-template name="quote_search_term">
+                                <xsl:with-param name="term"><xsl:value-of select="marc:subfield[@code='t']"/></xsl:with-param>
                             </xsl:call-template>
+                            <xsl:text>)</xsl:text>
+                            <xsl:if test="marc:subfield[@code='a']">
+                                <xsl:text> AND au:(</xsl:text>
+                                <xsl:call-template name="quote_search_term">
+                                    <xsl:with-param name="term">
+                                        <xsl:value-of select="marc:subfield[@code='a']"/>
+                                    </xsl:with-param>
+                                </xsl:call-template>
+                                <xsl:text>)</xsl:text>
+                            </xsl:if>
                         </xsl:variable>
 
                         <xsl:choose>
                             <xsl:when test="$UseControlNumber = '1' and marc:subfield[@code='w']">
                                 <a><xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Control-number:<xsl:call-template name="extractControlNumber"><xsl:with-param name="subfieldW" select="marc:subfield[@code='w']"/></xsl:call-template></xsl:attribute>
-                                    <xsl:value-of select="$f785"/>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:when>
                             <xsl:otherwise>
-                                <a><xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=ti,phr:<xsl:value-of select="str:encode-uri(translate($f785, '()', ''), true())"/></xsl:attribute>
-                                    <xsl:value-of select="$f785"/>
+                                <a>
+                                    <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=<xsl:value-of select="str:encode-uri($relation_query, true())" /></xsl:attribute>
+                                    <xsl:call-template name="subfieldSelect">
+                                        <xsl:with-param name="codes">a_t</xsl:with-param>
+                                    </xsl:call-template>
                                 </a>
                             </xsl:otherwise>
                         </xsl:choose>
-- 
2.30.2