Stop unnecessary warnings in get_language
[koha.git] / C4 / Templates.pm
index 9169f41..d6e3ce4 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use Carp;
 use CGI;
+use List::MoreUtils qw/any/;
 
 # Copyright 2009 Chris Cormack and The Koha Dev Team
 #
@@ -31,16 +32,20 @@ use CGI;
 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 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');
@@ -48,15 +53,18 @@ sub new {
     else {
         $htdocs = C4::Context->config('intrahtdocs');
     }
-
-    my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface );
+    my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query);
     my $template = Template->new(
-        {
-            EVAL_PERL    => 1,
+        {   EVAL_PERL    => 1,
             ABSOLUTE     => 1,
-            INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
-            FILTERS      => {},
-
+            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 => [
+                "$htdocs/$theme/$lang/includes",
+                "$htdocs/$theme/en/includes"
+            ],
+            FILTERS => {},
         }
     ) or die Template->error();
     my $self = {
@@ -100,9 +108,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'){
+            utf8_hashref($vars->{$k});
+        }
+        else {
+            utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
+        }
     }
     my $data;
 #    binmode( STDOUT, ":utf8" );
@@ -111,6 +129,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
@@ -123,72 +172,172 @@ sub _current_language {
     return $_current_language;
 }
 
-sub themelanguage {
-    my ( $htdocs, $tmpl, $interface ) = @_;
-    my $query = new CGI;
 
-    # Set some defaults for language and theme
-    # First, check the user's preferences
-    my $lang;
+# 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";
+        }
+    }
+}
 
-    # But, if there's a cookie set, obey it
-    $lang = $query->cookie('KohaOpacLanguage')
-      if ( defined $query and $query->cookie('KohaOpacLanguage') );
 
-    # Fall back to English
-    my @languages;
-    if ( $interface eq 'intranet' ) {
-        @languages = split ",", C4::Context->preference("language");
-    }
-    else {
-        @languages = split ",", C4::Context->preference("opaclanguages");
-    }
-    if ($lang) {
-        @languages = ( $lang, @languages );
-    }
-    else {
-        $lang = $languages[0];
-    }
-    my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
-    my $dbh = C4::Context->dbh;
-    my @themes;
-    if ( $interface eq "intranet" ) {
-        @themes = split " ", C4::Context->preference("template");
+=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) = themelanguage($htdocs, $tmplbase, $interface, $query);
+    my $opacstylesheet = C4::Context->preference('opacstylesheet');
+
+    # 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";
     }
-    else {
-        @themes = split " ", C4::Context->preference("opacthemes");
+    return ($htdocs, $theme, $lang, $filename);
+}
+
+
+sub gettemplate {
+    my ( $tmplbase, $interface, $query ) = @_;
+    ($query) or warn "no query in gettemplate";
+    my $path = C4::Context->preference('intranet_includes') || 'includes';
+    my $opacstylesheet = C4::Context->preference('opacstylesheet');
+    $tmplbase =~ s/\.tmpl$/.tt/;
+    my ($htdocs, $theme, $lang, $filename)
+       =  _get_template_file($tmplbase, $interface, $query);
+    my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
+    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;
 
- # searches through the themes and languages. First template it find it returns.
- # Priority is for getting the theme right.
-  THEME:
-    foreach my $th (@themes) {
-        foreach my $la (@languages) {
-            if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
-                $theme = $th;
-                $lang  = $la;
-                last THEME;
-            }
-            last unless $la =~ /[-_]/;
+    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)
         }
     }
-    $_current_language = $lang;  # FIXME part of bad hack to paper over bug 4403
-    return ( $theme, $lang );
+    # Otherwise, return prog theme in English 'en'
+    return ('prog', 'en');
 }
 
-# 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;
-        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;
+
+sub setlanguagecookie {
+    my ( $query, $language, $uri ) = @_;
+    my $cookie = $query->cookie(
+        -name    => 'KohaOpacLanguage',
+        -value   => $language,
+        -expires => ''
+    );
+    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;
-