Facets (current) translatable via template
[koha.git] / C4 / Output.pm
index c588e14..fc1a840 100644 (file)
@@ -26,16 +26,25 @@ package C4::Output;
 # templates.
 
 use strict;
-require Exporter;
 
 use C4::Context;
-use C4::Languages qw(getTranslatedLanguages);
+use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
 
 use HTML::Template::Pro;
 use vars qw($VERSION @ISA @EXPORT);
 
-# set the version for version checking
-$VERSION = 3.00;
+BEGIN {
+    # set the version for version checking
+    $VERSION = 3.01;
+    require Exporter;
+    @ISA    = qw(Exporter);
+    push @EXPORT, qw(
+        &themelanguage &gettemplate setlanguagecookie pagination_bar
+    );
+    push @EXPORT, qw(
+        &output_html_with_http_headers
+    );
+}
 
 =head1 NAME
 
@@ -47,17 +56,6 @@ C4::Output - Functions for managing templates
 
 =cut
 
-@ISA    = qw(Exporter);
-push @EXPORT, qw(
-  &themelanguage &gettemplate setlanguagecookie pagination_bar
-);
-
-#Output
-push @EXPORT, qw(
-    &output_html_with_http_headers
-);
-
-
 #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/";
@@ -66,9 +64,7 @@ my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
 # FIXME - POD
 sub gettemplate {
     my ( $tmplbase, $interface, $query ) = @_;
-    if ( !$query ) {
-        warn "no query in gettemplate";
-    }
+    ($query) or warn "no query in gettemplate";
     my $htdocs;
     if ( $interface ne "intranet" ) {
         $htdocs = C4::Context->config('opachtdocs');
@@ -78,20 +74,27 @@ sub gettemplate {
     }
     my $path = C4::Context->preference('intranet_includes') || 'includes';
 
-    #    warn "PATH : $path";
     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";
+    }
     my $template       = HTML::Template::Pro->new(
-        filename          => "$htdocs/$theme/$lang/modules/$tmplbase",
+        filename          => $filename,
         die_on_bad_params => 1,
         global_vars       => 1,
         case_sensitive    => 1,
         path              => ["$htdocs/$theme/$lang/$path"]
     );
-
+    my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
+          . "/$theme/$lang";
     $template->param(
-        themelang => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
-          . "/$theme/$lang",
+        themelang => $themelang,
+        yuipath => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
         interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
         theme => $theme,
         opacstylesheet      => $opacstylesheet,
@@ -100,8 +103,16 @@ sub gettemplate {
         lang                => $lang
     );
 
-       # load the languages ( for switching from one template to another )
-       $template->param(languages_loop => getTranslatedLanguages($interface,$theme));
+    # 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);
+    $template->param(
+            languages_loop => $languages_loop,
+            bidi => $bidi
+    ) unless @$languages_loop<2;
 
     return $template;
 }
@@ -109,59 +120,58 @@ sub gettemplate {
 #---------------------------------------------------------------------------------------------------------
 # FIXME - POD
 sub themelanguage {
-    my ( $htdocs, $tmpl, $section, $query ) = @_;
-
-    #   if (!$query) {
-    #     warn "no query";
-    #   }
+    my ( $htdocs, $tmpl, $interface, $query ) = @_;
+    ($query) or warn "no query in themelanguage";
+
+    # Set some defaults for language and theme
+    # First, check the user's preferences
+    my $lang;
+    my $http_accept_language = regex_lang_subtags($ENV{HTTP_ACCEPT_LANGUAGE})->{language};
+    if ($http_accept_language) {
+        $lang = accept_language($http_accept_language,getTranslatedLanguages($interface,'prog'));
+    } 
+    # But, if there's a cookie set, obey it
+    $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage');
+    # Fall back to English
+    my @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 @languages;
     my @themes;
-    if ( $section eq "intranet" ) {
-        @languages = split " ", C4::Context->preference("opaclanguages");
+    if ( $interface eq "intranet" ) {
         @themes    = split " ", C4::Context->preference("template");
     }
     else {
-
       # we are in the opac here, what im trying to do is let the individual user
       # set the theme they want to use.
       # and perhaps the them as well.
-        my $lang = $query->cookie('KohaOpacLanguage');
-        if ($lang) {
-
-            push @languages, $lang;
-            @themes = split " ", C4::Context->preference("opacthemes");
-        }
-        else {
-            @languages = split " ", C4::Context->preference("opaclanguages");
-            @themes    = split " ", C4::Context->preference("opacthemes");
-        }
+        #my $lang = $query->cookie('KohaOpacLanguage');
+        @themes = split " ", C4::Context->preference("opacthemes");
     }
 
-    my ( $theme, $lang );
-
  # searches through the themes and languages. First template it find it returns.
  # Priority is for getting the theme right.
-  THEME:
+    THEME:
     foreach my $th (@themes) {
         foreach my $la (@languages) {
-            for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
-                $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
-                if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
+            #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
+                # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
+                #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
+                               if ( -e "$htdocs/$th/$la/modules/$tmpl") {
+                #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
                     $theme = $th;
                     $lang  = $la;
                     last THEME;
                 }
                 last unless $la =~ /[-_]/;
-            }
+            #}
         }
     }
-    if ( $theme and $lang ) {
-        return ( $theme, $lang );
-    }
-    else {
-        return ( 'prog', 'en' );
-    }
+    return ( $theme, $lang );
 }
 
 sub setlanguagecookie {
@@ -332,8 +342,8 @@ sub output_html_with_http_headers ($$$) {
         -type    => 'text/html',
         -charset => 'UTF-8',
         -cookie  => $cookie,
-               -Pragma => 'no-cache',
-               -'Cache-Control' => 'no-cache',
+        -Pragma => 'no-cache',
+        -'Cache-Control' => 'no-cache',
     ), $html;
 }