Koha/C4/Templates.pm
Dobrica Pavlinusic d542740ab8 Bug 6554 - make Koha internally utf-8 clean
In current implementation (mostly commented out in this patch)
uses heuristic to guess which strings need decoding from utf-8
to binary representation and doesn't support utf-8 characters
in templates and has problems with utf-8 data from database.

With this changes, Koha perl code always uses utf-8 encoding
correctly. All incomming data from database is allready
correctly marked as utf-8, and decoding of utf8 is required
only from Zebra and XSLT transfers which don't set utf-8 flag
correctly.

For output, standard perl :encoding(utf8) handler is used
so it also removes various "wide character" warnings as side-effect.

Test scenario:
1. make sure that you have utf-8 characters in your biblio
   records, patrons, categories etc.
2. try to search records on intranet and opac which contain
   utf-8 characters
3. install language which has utf-8 characters, e.g. uk-UA
   dpavlin@koha-dev:/srv/koha/misc/translator(bug_6554) $
   PERL5LIB=/srv/koha/ perl translate install uk-UA
4. switch language to uk-UA and verify that templates
   display correctly
5. test search and Z39.50 search and verify that caracters
   are correct

Signed-off-by: Owen Leonard <oleonard@myacpl.org>

I followed the test plan, adding utf-8 characters to library names,
patron categories, titles, and authorized values. I tried the uk-UA
translation and everything looked good.

When performing Z39.50 searches for titles containing utf-8 characters I
got results which were still occasionally contaminated with dummy
characters [?] but I assume this is Z39.50's fault not the patch's.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Already signed, add mine.
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
2013-04-01 18:51:49 -04:00

311 lines
9.8 KiB
Perl
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package C4::Templates;
use strict;
use warnings;
use Carp;
use CGI;
use List::MoreUtils qw/any/;
# Copyright 2009 Chris Cormack and The Koha Dev Team
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
=head1 NAME
Koha::Templates - Object for manipulating templates for use with Koha
=cut
use base qw(Class::Accessor);
use Template;
use Template::Constants qw( :debug );
use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
use C4::Context;
__PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
sub new {
my $class = shift;
my $interface = shift;
my $filename = shift;
my $tmplbase = shift;
my $query = @_? shift: undef;
my $htdocs;
if ( $interface ne "intranet" ) {
$htdocs = C4::Context->config('opachtdocs');
}
else {
$htdocs = C4::Context->config('intrahtdocs');
}
my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query);
my @includes;
foreach (@$activethemes) {
push @includes, "$htdocs/$_/$lang/includes";
push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
}
my $template = Template->new(
{ EVAL_PERL => 1,
ABSOLUTE => 1,
PLUGIN_BASE => 'Koha::Template::Plugin',
COMPILE_EXT => C4::Context->config('template_cache_dir')?'.ttc':'',
COMPILE_DIR => C4::Context->config('template_cache_dir')?C4::Context->config('template_cache_dir'):'',,
INCLUDE_PATH => \@includes,
FILTERS => {},
ENCODING => 'utf8', # templates don't have BOM, see Template::FAQ
}
) or die Template->error();
my $self = {
TEMPLATE => $template,
VARS => {},
};
bless $self, $class;
$self->theme($theme);
$self->lang($lang);
$self->activethemes($activethemes);
$self->preferredtheme($activethemes->[0]);
$self->filename($filename);
$self->htdocs($htdocs);
$self->interface($interface);
$self->{VARS}->{"test"} = "value";
return $self;
}
sub output {
my $self = shift;
my $vars = shift;
# my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
my $template = $self->{TEMPLATE};
if ( $self->interface eq 'intranet' ) {
$vars->{themelang} = '/intranet-tmpl';
}
else {
$vars->{themelang} = '/opac-tmpl';
}
$vars->{lang} = $self->lang;
$vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang;
$vars->{yuipath} =
( C4::Context->preference("yuipath") eq "local"
? ( $self->interface eq 'intranet' ? $vars->{themelang} . "/lib/yui" : "/opac-tmpl/lib/yui" )
: C4::Context->preference("yuipath") );
$vars->{interface} =
( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
$vars->{theme} = $self->theme;
$vars->{opaccolorstylesheet} =
C4::Context->preference('opaccolorstylesheet');
$vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
$vars->{opaclayoutstylesheet} =
C4::Context->preference('opaclayoutstylesheet');
# add variables set via param to $vars for processing
for my $k ( keys %{ $self->{VARS} } ) {
$vars->{$k} = $self->{VARS}->{$k};
}
my $data;
$template->process( $self->filename, $vars, \$data )
|| die "Template process failed: ", $template->error();
return $data;
}
# FIXME - this is a horrible hack to cache
# the current known-good language, temporarily
# put in place to resolve bug 4403. It is
# used only by C4::XSLT::XSLTParse4Display;
# the language is set via the usual call
# to themelanguage.
my $_current_language = 'en';
sub _current_language {
return $_current_language;
}
# wrapper method to allow easier transition from HTML template pro to Template Toolkit
sub param {
my $self = shift;
while (@_) {
my $key = shift;
my $val = shift;
if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
if ( $key ) {
$self->{VARS}->{$key} = $val;
} else {
warn "Problem = a value of $val has been passed to param without key";
}
}
}
=head1 NAME
C4::Templates - Functions for managing templates
=head1 FUNCTIONS
=cut
# FIXME: this is a quick fix to stop rc1 installing broken
# Still trying to figure out the correct fix.
my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
#---------------------------------------------------------------------------------------------------------
# FIXME - POD
sub _get_template_file {
my ($tmplbase, $interface, $query) = @_;
my $is_intranet = $interface eq 'intranet';
my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
# if the template doesn't exist, load the English one as a last resort
my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
unless (-f $filename) {
$lang = 'en';
$filename = "$htdocs/$theme/$lang/modules/$tmplbase";
}
return ($htdocs, $theme, $lang, $filename);
}
sub gettemplate {
my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
($query) or warn "no query in gettemplate";
my $path = C4::Context->preference('intranet_includes') || 'includes';
$tmplbase =~ s/\.tmpl$/.tt/;
my ($htdocs, $theme, $lang, $filename)
= _get_template_file($tmplbase, $interface, $query);
$filename = $tmplbase if ( $is_plugin );
my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
# NOTE: Commenting these out rather than deleting them so that those who need
# to know how we previously shimmed these directories will be able to understand.
# my $is_intranet = $interface eq 'intranet';
# my $themelang =
# ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
# "/$theme/$lang";
# $template->param(
# themelang => $themelang,
# yuipath => C4::Context->preference("yuipath") eq "local"
# ? "$themelang/lib/yui"
# : C4::Context->preference("yuipath"),
# interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
# theme => $theme,
# lang => $lang
# );
# Bidirectionality
my $current_lang = regex_lang_subtags($lang);
my $bidi;
$bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
# Languages
my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
my $num_languages_enabled = 0;
foreach my $lang (@$languages_loop) {
foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
$num_languages_enabled++ if $sublang->{enabled};
}
}
$template->param(
languages_loop => $languages_loop,
bidi => $bidi,
one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
) unless @$languages_loop<2;
return $template;
}
#---------------------------------------------------------------------------------------------------------
# FIXME - POD
sub themelanguage {
my ($htdocs, $tmpl, $interface, $query) = @_;
($query) or warn "no query in themelanguage";
# Select a language based on cookie, syspref available languages & browser
my $lang = getlanguage($query, $interface);
# Select theme
my $is_intranet = $interface eq 'intranet';
my @themes = split(" ", C4::Context->preference(
$is_intranet ? "template" : "opacthemes" ));
push @themes, 'prog';
# Try to find first theme for the selected language
for my $theme (@themes) {
if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
$_current_language = $lang;
return ($theme, $lang, \@themes)
}
}
# Otherwise, return prog theme in English 'en'
return ('prog', 'en', \@themes);
}
sub setlanguagecookie {
my ( $query, $language, $uri ) = @_;
my $cookie = $query->cookie(
-name => 'KohaOpacLanguage',
-value => $language,
-HttpOnly => 1,
-expires => '+3y'
);
print $query->redirect(
-uri => $uri,
-cookie => $cookie
);
}
sub getlanguage {
my ($query, $interface) = @_;
# Select a language based on cookie, syspref available languages & browser
my $preference_to_check =
$interface eq 'intranet' ? 'language' : 'opaclanguages';
my @languages = split /,/, C4::Context->preference($preference_to_check);
my $lang;
# cookie
if ( $query->cookie('KohaOpacLanguage') ) {
$lang = $query->cookie('KohaOpacLanguage');
$lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie
}
# HTTP_ACCEPT_LANGUAGE
if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE} ) {
$lang = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE},
getTranslatedLanguages( $interface, 'prog' ) );
}
# Ignore a lang not selected in sysprefs
if ( $lang && any { $_ eq $lang } @languages ) {
return $lang;
}
# Fall back to English if necessary
return 'en';
}
1;