From 768fe3492bda941d6330164a471311ffb1cfcf06 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 18 Oct 2023 12:00:05 +0000
Subject: [PATCH] Bug 34020: Preserve order of subfields in 264 display

When ordered $a$b$a$b$c in the MARC object, 264 subfields are displayed
$a$a$b$b$c.  This goes against the standard.

This patch preserves the order.

1) Edit a record and add/update a 264 fields to have subfields a, b, a, b, c in that order
1) Search for record in staff and opac, see subfields displayed in order "aabbc" in results
2) View the details page for the record in staff and opac, note same order
3) Apply the patch, restart all
4) Confirm staff and opac, results and details now follow the order of the subfields in the record's field
---
 .../prog/en/xslt/MARC21slimUtils.xsl          | 59 ++++++++-----------
 .../bootstrap/en/xslt/MARC21slimUtils.xsl     | 58 ++++++++----------
 2 files changed, 52 insertions(+), 65 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl
index 3c3cecb6ff..5da84927a0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl
+++ b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl
@@ -281,41 +281,34 @@
                 </xsl:when>
             </xsl:choose>
 
-            <xsl:if test="$field/marc:subfield[@code='a']">
-                <xsl:call-template name="subfieldSelect">
-                    <xsl:with-param name="codes">a</xsl:with-param>
-                </xsl:call-template>
-            </xsl:if>
-            <xsl:text> </xsl:text>
-
-            <xsl:choose>
-                <xsl:when test="$url='1'">
-                    <xsl:if test="$field/marc:subfield[@code='b']">
-                         <a>
-                         <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Provider:<xsl:value-of select="str:encode-uri($field/marc:subfield[@code='b'], true())"/></xsl:attribute>
-                         <xsl:call-template name="subfieldSelect">
-                             <xsl:with-param name="codes">b</xsl:with-param>
-                         </xsl:call-template>
-                         </a>
-                    </xsl:if>
-                </xsl:when>
-                <xsl:otherwise>
-                    <xsl:if test="$field/marc:subfield[@code='b']">
-                        <xsl:call-template name="subfieldSelect">
-                            <xsl:with-param name="codes">b</xsl:with-param>
-                        </xsl:call-template>
+            <xsl:for-each select="marc:subfield">
+                <xsl:if test="@code='a'">
+                    <xsl:value-of select="current()"/>
+                </xsl:if>
+                    <xsl:if test="@code='b'">
+                        <xsl:choose>
+                            <xsl:when test="$url='1'">
+                                     <a>
+                                     <xsl:attribute name="href">/cgi-bin/koha/catalogue/search.pl?q=Provider:<xsl:value-of select="str:encode-uri(current(), true())"/></xsl:attribute>
+                                     <xsl:value-of select="current()"/>
+                                     </a>
+                            </xsl:when>
+                            <xsl:otherwise>
+                                    <xsl:value-of select="current()"/>
+                            </xsl:otherwise>
+                        </xsl:choose>
                     </xsl:if>
-                </xsl:otherwise>
-            </xsl:choose>
-            <xsl:text> </xsl:text>
-            <xsl:call-template name="chopPunctuation">
-                <xsl:with-param name="chopString">
-                    <xsl:call-template name="subfieldSelect">
-                        <xsl:with-param name="codes">c</xsl:with-param>
+                <xsl:if test="@code='c'">
+                    <xsl:call-template name="chopPunctuation">
+                        <xsl:with-param name="chopString">
+                            <xsl:value-of select="current()"/>
+                        </xsl:with-param>
                     </xsl:call-template>
-                </xsl:with-param>
-            </xsl:call-template>
-
+                </xsl:if>
+                <xsl:if test="position() != last()">
+                    <xsl:text> </xsl:text>
+                </xsl:if>
+            </xsl:for-each>
         </span>
     </xsl:template>
 
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl
index b0a8ad1b59..62465adf27 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl
@@ -279,40 +279,34 @@
                 </xsl:when>
             </xsl:choose>
 
-            <xsl:if test="$field/marc:subfield[@code='a']">
-                <xsl:call-template name="subfieldSelect">
-                    <xsl:with-param name="codes">a</xsl:with-param>
-                </xsl:call-template>
-            </xsl:if>
-            <xsl:text> </xsl:text>
-
-            <xsl:choose>
-                <xsl:when test="$url='1'">
-                    <xsl:if test="$field/marc:subfield[@code='b']">
-                         <a>
-                         <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Provider:<xsl:value-of select="str:encode-uri($field/marc:subfield[@code='b'], true())"/></xsl:attribute>
-                         <xsl:call-template name="subfieldSelect">
-                             <xsl:with-param name="codes">b</xsl:with-param>
-                         </xsl:call-template>
-                         </a>
-                    </xsl:if>
-                </xsl:when>
-                <xsl:otherwise>
-                    <xsl:if test="$field/marc:subfield[@code='b']">
-                        <xsl:call-template name="subfieldSelect">
-                            <xsl:with-param name="codes">b</xsl:with-param>
-                        </xsl:call-template>
+            <xsl:for-each select="marc:subfield">
+                <xsl:if test="@code='a'">
+                    <xsl:value-of select="current()"/>
+                </xsl:if>
+                    <xsl:if test="@code='b'">
+                        <xsl:choose>
+                            <xsl:when test="$url='1'">
+                                     <a>
+                                     <xsl:attribute name="href">/cgi-bin/koha/opac-search.pl?q=Provider:<xsl:value-of select="str:encode-uri(current(), true())"/></xsl:attribute>
+                                     <xsl:value-of select="current()"/>
+                                     </a>
+                            </xsl:when>
+                            <xsl:otherwise>
+                                    <xsl:value-of select="current()"/>
+                            </xsl:otherwise>
+                        </xsl:choose>
                     </xsl:if>
-                </xsl:otherwise>
-            </xsl:choose>
-            <xsl:text> </xsl:text>
-            <xsl:call-template name="chopPunctuation">
-                <xsl:with-param name="chopString">
-                    <xsl:call-template name="subfieldSelect">
-                        <xsl:with-param name="codes">c</xsl:with-param>
+                <xsl:if test="@code='c'">
+                    <xsl:call-template name="chopPunctuation">
+                        <xsl:with-param name="chopString">
+                            <xsl:value-of select="current()"/>
+                        </xsl:with-param>
                     </xsl:call-template>
-                </xsl:with-param>
-            </xsl:call-template>
+                </xsl:if>
+                <xsl:if test="position() != last()">
+                    <xsl:text> </xsl:text>
+                </xsl:if>
+            </xsl:for-each>
         </span>
     </xsl:template>
 
-- 
2.30.2