From e740585f9abe01c3b67ded1f687a8f464449da27 Mon Sep 17 00:00:00 2001 From: Jacek Ablewicz Date: Wed, 12 Mar 2014 10:05:08 +0100 Subject: [PATCH] Bug 11302: Template::output should deal with objects If a ref is not HASH or ARRAY, C4::Template::output assumes it is a scalar. Which is wrong, it could be an object. Note: this is yet another / alternative take on the problem, with only bare minimum changes made to the code base, so this (counter-)patch - while not pretty - hopefully should be less regression-prone. Right now, the only white-listed object this patch deals with is 'Koha::AdditionalField' introduced by bug 10855. In case it tests OK, without any regressions, we can add another objects (C4::Category etc.) later on. Test plan: 1) apply bug 10855 first; observe that non-ascii characters (in authorized category names or additional field labels) are not properly handled, 2) apply patch, 3) ensure that encoding of non-ascii characters for features added by 10855 got fixed, 4) there should be no character-encoding-related regressions of any kind appearing anywhere in the system. --- C4/Templates.pm | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/C4/Templates.pm b/C4/Templates.pm index 2d2304f..1c7dbfe 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -117,10 +117,11 @@ sub output { # and clean any utf8 mess for my $k ( keys %{ $self->{VARS} } ) { $vars->{$k} = $self->{VARS}->{$k}; - if (ref($vars->{$k}) eq 'ARRAY'){ + my $rt = ref($vars->{$k}); + if ($rt eq 'ARRAY') { utf8_arrayref($vars->{$k}); } - elsif (ref($vars->{$k}) eq 'HASH'){ + elsif ($rt eq 'HASH' || _utf8_is_whitelisted_object($rt)) { utf8_hashref($vars->{$k}); } else { @@ -137,11 +138,12 @@ sub output { sub utf8_arrayref { my $arrayref = shift; foreach my $element (@$arrayref){ - if (ref($element) eq 'ARRAY'){ + my $rt = ref($element); + if ($rt eq 'ARRAY'){ utf8_arrayref($element); next; } - if (ref($element) eq 'HASH'){ + if ($rt eq 'HASH' || _utf8_is_whitelisted_object($rt)) { utf8_hashref($element); next; } @@ -152,19 +154,27 @@ sub utf8_arrayref { sub utf8_hashref { my $hashref = shift; for my $key (keys %{$hashref}){ - if (ref($hashref->{$key}) eq 'ARRAY'){ + my $rt = ref($hashref->{$key}); + if ($rt eq 'ARRAY'){ utf8_arrayref($hashref->{$key}); next; } - if (ref($hashref->{$key}) eq 'HASH'){ + if ($rt eq 'HASH' || _utf8_is_whitelisted_object($rt)) { utf8_hashref($hashref->{$key}); next; } utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key}); } } - - + +sub _utf8_is_whitelisted_object { + my $rt = shift; + $rt eq '' && return(0); + my @whitelist = qw( Koha::AdditionalField ); + map { $_ eq $rt && return(1); } @whitelist; + 0; +} + # FIXME - this is a horrible hack to cache # the current known-good language, temporarily # put in place to resolve bug 4403. It is -- 1.7.10.4