From 010e052bea28a57786837cf0894f61178dc74c56 Mon Sep 17 00:00:00 2001
From: Katrin Fischer <katrin.fischer.83@web.de>
Date: Sun, 12 Apr 2020 16:30:05 +0000
Subject: [PATCH] Bug 25082: Make 041 descriptions display, if authorised value
 list is used

When 041$a or another subfield is linked to an authorised value list,
Koha will replace the codes with the descriptions from the authorised
value in the MARCXML before passing it to the XSLT.

The XSLT was made to translate codes into descriptions - as there is
no code now, this fails and 'unknown language' is shown.

The patch changes the handling so that when the XSLT is handled something
that doesn't match a known code, it displays the subfield without
processing.

To test:
- Create some records with 041.
  Examples: http://www.loc.gov/marc/bibliographic/bd041.html
- Verify this displays nicely in staff and OPAC
- Apply patch
- Verify display is unchanged
- Remove the patch
- Change one of your MARC frameworks (use another framework than
  for your first test records)
  and link the 041 subfields to the LANG authorised value list
- Go to administration and create some entries for LANG with language
  codes and descriptions
- Catalog a 041 using the normal editor (not advanced)
- You can now use a pull down to select the values you want
- Verify the display is now 'Unknown language' for this record
- Apply patch
- Verify all records display the language descriptions now, using
  an authorised value or not
- Run misc/maintenance/generate_MARC21Languages.pl

Verify the output at the end reads like:
<xsl:otherwise>
    <!-- when 041\$a is mapped to an authorised value list, this will show the description -->
    <xsl:value-of select="\$code" />
</xsl:otherwise>

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Amended test plan (add remove patch) to verify the error
Work as described following test plan, no errors.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21Languages.xsl  | 3 ++-
 koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl  | 4 ++--
 koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21Languages.xsl | 3 ++-
 koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl | 4 ++--
 misc/maintenance/generate_MARC21Languages.pl              | 3 ++-
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21Languages.xsl b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21Languages.xsl
index a0889a49f1..bf70ed9445 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21Languages.xsl
+++ b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21Languages.xsl
@@ -491,7 +491,8 @@
             <xsl:when test="$code='zul'"><xsl:text>Zulu</xsl:text></xsl:when>
             <xsl:when test="$code='zun'"><xsl:text>Zuni</xsl:text></xsl:when>
             <xsl:otherwise>
-                <xsl:text>Unknown language code</xsl:text>
+                <!-- when 041$a is mapped to an authorised value list, this will show the description -->
+                <xsl:value-of select="$code" />
             </xsl:otherwise>
         </xsl:choose>
     </xsl:template>
diff --git a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl
index 0e1f4a1e6b..acb122b1c1 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl
+++ b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21slimUtils.xsl
@@ -383,9 +383,9 @@
 	  <span class="label"><xsl:value-of select="$langLabel"/></span>
 	  <xsl:for-each select="$langNode">
 	    <span>
-	      <xsl:attribute name="class">lang_code-<xsl:value-of select="substring(translate(., ' .-;|#', '_'),1,3)"/></xsl:attribute>
+	      <xsl:attribute name="class">lang_code-<xsl:value-of select="translate(., ' .-;|#', '_')"/></xsl:attribute>
 	      <xsl:call-template name="languageCodeText">
-		<xsl:with-param name="code" select="substring(.,1,3)"/>
+		<xsl:with-param name="code" select="translate(., ' .-;|#', '')"/>
 	      </xsl:call-template>
 	      <xsl:if test="position() != last()">
 	        <span class="sep"><xsl:text>, </xsl:text></span>
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21Languages.xsl b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21Languages.xsl
index a0889a49f1..bf70ed9445 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21Languages.xsl
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21Languages.xsl
@@ -491,7 +491,8 @@
             <xsl:when test="$code='zul'"><xsl:text>Zulu</xsl:text></xsl:when>
             <xsl:when test="$code='zun'"><xsl:text>Zuni</xsl:text></xsl:when>
             <xsl:otherwise>
-                <xsl:text>Unknown language code</xsl:text>
+                <!-- when 041$a is mapped to an authorised value list, this will show the description -->
+                <xsl:value-of select="$code" />
             </xsl:otherwise>
         </xsl:choose>
     </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 07b20dddbf..4b83326bfa 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/xslt/MARC21slimUtils.xsl
@@ -347,9 +347,9 @@
         <span class="label"><xsl:value-of select="$langLabel"/></span>
         <xsl:for-each select="$langNode">
         <span>
-            <xsl:attribute name="class">lang_code-<xsl:value-of select="substring(translate(., ' .-;|#', '_'),1,3)"/></xsl:attribute>
+            <xsl:attribute name="class">lang_code-<xsl:value-of select="translate(., ' .-;|#', '_')"/></xsl:attribute>
             <xsl:call-template name="languageCodeText">
-        <xsl:with-param name="code" select="substring(.,1,3)"/>
+        <xsl:with-param name="code" select="translate(., ' .-;|#', '')"/>
             </xsl:call-template>
             <xsl:if test="position() != last()">
             <span class="separator"><xsl:text>, </xsl:text></span>
diff --git a/misc/maintenance/generate_MARC21Languages.pl b/misc/maintenance/generate_MARC21Languages.pl
index 2394ada38e..e5a151679d 100755
--- a/misc/maintenance/generate_MARC21Languages.pl
+++ b/misc/maintenance/generate_MARC21Languages.pl
@@ -101,7 +101,8 @@ sub generate_footer {
     my ($file_handle) = @_;
     print {$file_handle} <<"FOOTER";
             <xsl:otherwise>
-                <xsl:text>Unknown language code</xsl:text>
+                <!-- when 041\$a is mapped to an authorised value list, this will show the description -->
+                <xsl:value-of select="\$code" />
             </xsl:otherwise>
         </xsl:choose>
     </xsl:template>
-- 
2.20.1