From 7efaafe1323fa064083c8b597a2c0699353dcd8c Mon Sep 17 00:00:00 2001 From: Chris Cormack Date: Mon, 11 Apr 2011 15:58:18 +1200 Subject: [PATCH] Bug 6085 : Fixing the utf8 weirdness I am not very happy with this fix, but it does mean the template are translatable and we don't get double encoding issues with them anymore Please test Signed-off-by: Chris Cormack --- C4/Output.pm | 5 ++++- C4/Templates.pm | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/C4/Output.pm b/C4/Output.pm index 0adc163f9d..98f2d7a7c2 100644 --- a/C4/Output.pm +++ b/C4/Output.pm @@ -464,7 +464,10 @@ sub output_with_http_headers($$$$;$) { $data =~ s/\x{C2}\x{98}|\x{C2}\x{9C}/ /g; $data =~ s/\x{C2}\x{88}|\x{C2}\x{89}/ /g; - utf8::encode($data) if utf8::is_utf8($data); +# We can't encode here, that will double encode our templates, and xslt +# We need to fix the encoding as it comes out of the database, or when we pass the variables to templates + +# utf8::encode($data) if utf8::is_utf8($data); print $query->header($options), $data; } diff --git a/C4/Templates.pm b/C4/Templates.pm index 9169f41a5a..54ad9fd26e 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -100,9 +100,19 @@ sub output { $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage'); $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet'); - #add variables set via param to $vars for processing + # add variables set via param to $vars for processing + # and clean any utf8 mess for my $k ( keys %{ $self->{VARS} } ) { $vars->{$k} = $self->{VARS}->{$k}; + if (ref($vars->{$k}) eq 'ARRAY'){ + utf8_arrayref($vars->{$k}); + } + elsif (ref($vars->{$k}) eq 'HASH'){ + uft8_hashref($vars->{$k}); + } + else { + utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k}); + } } my $data; # binmode( STDOUT, ":utf8" ); @@ -111,6 +121,37 @@ sub output { return $data; } +sub utf8_arrayref { + my $arrayref = shift; + foreach my $element (@$arrayref){ + if (ref($element) eq 'ARRAY'){ + utf8_arrayref($element); + next; + } + if (ref($element) eq 'HASH'){ + utf8_hashref($element); + next; + } + utf8::encode($element) if utf8::is_utf8($element); + } +} + +sub utf8_hashref { + my $hashref = shift; + for my $key (keys %{$hashref}){ + if (ref($hashref->{$key}) eq 'ARRAY'){ + utf8_arrayref($hashref->{$key}); + next; + } + if (ref($hashref->{$key}) eq 'HASH'){ + utf8_hashref($hashref->{$key}); + next; + } + utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key}); + } +} + + # FIXME - this is a horrible hack to cache # the current known-good language, temporarily # put in place to resolve bug 4403. It is @@ -182,8 +223,6 @@ sub param { while (@_) { my $key = shift; my $val = shift; - utf8::encode($val) if utf8::is_utf8($val); - utf8::decode($val) if $key eq "XSLTBloc"; if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; } elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; } $self->{VARS}->{$key} = $val; -- 2.39.5