From 03feabe9261b4090e3b4e708b972dd7c3d9c678e Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Thu, 15 Nov 2012 14:47:29 +0100
Subject: [PATCH] Bug MT6536: Followup: Fix some issues for unimarc

1/ In the Breeding module, it is useless to parse 2x the xslt
transformation.

2/ Fix encoding in the xslt

3/ The as_xml method takes the marcflavour value.
---
 C4/Breeding.pm                                     |   30 ++++++++++++--------
 C4/ImportBatch.pm                                  |    6 +++-
 .../intranet-tmpl/prog/en/xslt/MARC21toUNIMARC.xsl |    4 +--
 3 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/C4/Breeding.pm b/C4/Breeding.pm
index cb04e14..f2ac82c 100644
--- a/C4/Breeding.pm
+++ b/C4/Breeding.pm
@@ -542,10 +542,14 @@ sub handle_one_result {
 	$hServer[$conn]->{syntax});
   }
   else { #z3950
-    ($marcrecord)= MarcToUTF8Record($rec->raw(),
-	marcflavour_from_syntax($conn), $hServer[$conn]->{encoding});
+      if ($hServer[$conn]->{encoding} eq 'utf8'){
+          $marcrecord= MARC::Record->new_from_usmarc($rec->raw());
+          C4::Charset::SetUTF8Flag $marcrecord;
+      }else{
+          ($marcrecord) = MarcToUTF8Record($rec->raw(), marcflavour_from_syntax($conn), $hServer[$conn]->{encoding})
+      }
   }
-  $marcrecord->encoding('UTF-8'); #force UTF-8
+
   #convert to system marc flavour and/or additional xslt proc.
   $marcrecord= do_xslt_processing($conn, $marcrecord);
 
@@ -570,15 +574,7 @@ sub do_xslt_processing {
   my ($conn, $marcrecord)= @_;
   my ($res, $newmarcrec, $parser, $style_doc, $stylesheet);
 
-  if($hServer[$conn]->{marc_conv} && defined $xsltconv) {
-    $parser = XML::LibXML->new();
-    $res= $parser->parse_string($marcrecord->as_xml());
-    $res= $xsltconv->transform($res);
-    $res= $xsltconv->output_string($res);
-    $newmarcrec= MARC::Record->new_from_xml($res, 'UTF-8');
-  }
-
-  if($hServer[$conn]->{add_xslt}) { #server specific xslt proc
+ if($hServer[$conn]->{add_xslt}) { #server specific xslt proc
     $res= $newmarcrec? $newmarcrec->as_xml(): $marcrecord->as_xml();
     $parser = XML::LibXML->new() unless $parser;
     $res= $parser->parse_string($res);
@@ -595,6 +591,16 @@ sub do_xslt_processing {
       warn "z3950_search: XSLT problem with ".$hServer[$conn]->{add_xslt};
     }
   }
+
+  if( not $newmarcrec and $hServer[$conn]->{marc_conv} and defined $xsltconv) {
+    $parser = XML::LibXML->new();
+    $res= $parser->parse_string($marcrecord->as_xml());
+    $res= $xsltconv->transform($res);
+    $res= $xsltconv->output_string($res);
+
+    $newmarcrec= MARC::Record->new_from_xml($res, 'UTF-8');
+  }
+
   return $newmarcrec // $marcrecord;
 }
 
diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index b6db406..22040d2 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -1137,7 +1137,11 @@ sub _create_import_record {
     my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, 
                                                          record_type, encoding, z3950random)
                                     VALUES (?, ?, ?, ?, ?, ?, ?)");
-    $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
+
+    my $usmarc = $marc_record->as_usmarc();
+    my $xml    = $marc_record->as_xml( C4::Context->preference("marcflavour") );
+
+    $sth->execute($batch_id, $record_sequence, $usmarc, $xml,
                   $record_type, $encoding, $z3950random);
     my $import_record_id = $dbh->{'mysql_insertid'};
     $sth->finish();
diff --git a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21toUNIMARC.xsl b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21toUNIMARC.xsl
index bb6cd09..790a3fa 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21toUNIMARC.xsl
+++ b/koha-tmpl/intranet-tmpl/prog/en/xslt/MARC21toUNIMARC.xsl
@@ -141,8 +141,8 @@ with Koha; if not, write to the Free Software Foundation, Inc.,
 								<xsl:otherwise>y</xsl:otherwise>
 							</xsl:choose>
 						</xsl:variable>
-						<!-- Demander à nicomo pour les 2 variables suivantes -->
-						<xsl:variable name="cs"><xsl:value-of select="'    '" /></xsl:variable>
+                        <!-- cs is the character set. 50 is utf8 the default value for Koha -->
+						<xsl:variable name="cs"><xsl:value-of select="'50  '" /></xsl:variable>
 						<xsl:variable name="acs"><xsl:value-of select="'    '" /></xsl:variable>
 						<xsl:variable name="aot" select="substring(text(),34,1)" />
 						<xsl:value-of select="concat($deof,$topd,$d1,$d2,$il,$gpc,$mrc,$loc,$tc,$cs,$acs,$aot)" />
-- 
1.7.10.4