refactoring changelanguage, better var names, etc.
[koha.git] / C4 / Output.pm
index b9279e1..2873c64 100644 (file)
@@ -21,7 +21,6 @@ package C4::Output;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id$
 
 # NOTE: I'm pretty sure this module is deprecated in favor of
 # templates.
@@ -30,12 +29,13 @@ use strict;
 require Exporter;
 
 use C4::Context;
-use HTML::Template::Pro;
+use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description);
 
+use HTML::Template::Pro;
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
+$VERSION = 3.00;
 
 =head1 NAME
 
@@ -54,25 +54,23 @@ push @EXPORT, qw(
 
 #Output
 push @EXPORT, qw(
-               &guesscharset
-               &guesstype
-               &output_html_with_http_headers
-               );
+    &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') . "/default/en/includes/";
+my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
 
 #---------------------------------------------------------------------------------------------------------
 # FIXME - POD
 sub gettemplate {
-    my ( $tmplbase, $opac, $query ) = @_;
+    my ( $tmplbase, $interface, $query ) = @_;
     if ( !$query ) {
         warn "no query in gettemplate";
     }
     my $htdocs;
-    if ( $opac ne "intranet" ) {
+    if ( $interface ne "intranet" ) {
         $htdocs = C4::Context->config('opachtdocs');
     }
     else {
@@ -81,10 +79,17 @@ sub gettemplate {
     my $path = C4::Context->preference('intranet_includes') || 'includes';
 
     #    warn "PATH : $path";
-    my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $opac, $query );
+    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/".($interface eq 'intranet'?"modules":"")."/$tmplbase";
+       unless (-f $filename) {
+               $lang = 'en';
+               $filename = "$htdocs/$theme/$lang/".($interface eq 'intranet'?"modules":"")."/$tmplbase";
+       }
     my $template       = HTML::Template::Pro->new(
-        filename          => "$htdocs/$theme/$lang/$tmplbase",
+               filename          => $filename,
         die_on_bad_params => 1,
         global_vars       => 1,
         case_sensitive    => 1,
@@ -92,9 +97,9 @@ sub gettemplate {
     );
 
     $template->param(
-        themelang => ( $opac ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
+        themelang => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
           . "/$theme/$lang",
-        interface => ( $opac ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
+        interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
         theme => $theme,
         opacstylesheet      => $opacstylesheet,
         opaccolorstylesheet => C4::Context->preference('opaccolorstylesheet'),
@@ -102,6 +107,35 @@ sub gettemplate {
         lang                => $lang
     );
 
+       # Bidirectionality
+       my $language_subtags_hashref = regex_lang_subtags($lang);
+       my $bidi;
+       $bidi = get_bidi($language_subtags_hashref->{script}) if $language_subtags_hashref->{script};
+
+       # Languages
+       my $current_lang = regex_lang_subtags($lang);
+       my @template_languages;
+       my $languages_loop = getTranslatedLanguages($interface,$theme);
+       for my $language_hashref (@$languages_loop) {
+                       $language_hashref->{'current_lang'} = $current_lang->{'language'};
+                       $language_hashref->{'native_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'language_code'},'language');
+                       warn "($language_hashref->{'language_code'},$language_hashref->{'current_lang'},$language_hashref->{'script_code'}";
+                       $language_hashref->{'locale_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'current_lang'},'language');
+                       $language_hashref->{'language_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'current_lang'},'language');
+                       $language_hashref->{'script_description'} = language_get_description($language_hashref->{'script_code'},$language_hashref->{'current_lang'},'script');
+                       $language_hashref->{'region_description'} = language_get_description($language_hashref->{'region_code'},$language_hashref->{'current_lang'},'region');
+                       $language_hashref->{'variant_description'} = language_get_description($language_hashref->{'variant_code'},$language_hashref->{'current_lang'},'variant');
+
+               if ($language_hashref->{'language_lang'} eq $lang) {
+                       $language_hashref->{current}++;
+               }
+               push @template_languages, $language_hashref;
+       }
+       # load the languages ( for switching from one template to another )
+       $template->param(       languages_loop => \@template_languages,
+                                               bidi => $bidi
+       );
+
     return $template;
 }
 
@@ -113,19 +147,26 @@ sub themelanguage {
     #   if (!$query) {
     #     warn "no query";
     #   }
+
+       # set some defaults for language and theme
+       my $lang = $query->cookie('KohaOpacLanguage');
+       $lang = 'en' unless $lang;
+       my $theme = 'prog';
+
     my $dbh = C4::Context->dbh;
     my @languages;
     my @themes;
     if ( $section eq "intranet" ) {
         @languages = split " ", C4::Context->preference("opaclanguages");
         @themes    = split " ", C4::Context->preference("template");
+        pop @languages, $lang if $lang;
     }
     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');
+        #my $lang = $query->cookie('KohaOpacLanguage');
         if ($lang) {
 
             push @languages, $lang;
@@ -137,8 +178,6 @@ sub themelanguage {
         }
     }
 
-    my ( $theme, $lang );
-
  # searches through the themes and languages. First template it find it returns.
  # Priority is for getting the theme right.
   THEME:
@@ -146,7 +185,7 @@ sub themelanguage {
         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/$tmpl" ) {
+                if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
                     $theme = $th;
                     $lang  = $la;
                     last THEME;
@@ -155,12 +194,7 @@ sub themelanguage {
             }
         }
     }
-    if ( $theme and $lang ) {
-        return ( $theme, $lang );
-    }
-    else {
-        return ( 'prog', 'en' );
-    }
+    return ( $theme, $lang );
 }
 
 sub setlanguagecookie {
@@ -315,42 +349,6 @@ sub pagination_bar {
     return $pagination_bar;
 }
 
-
-=item guesscharset
-
-   &guesscharset($output)
-
-"Guesses" the charset from the some HTML that would be output.
-
-C<$output> is the HTML page to be output. If it contains a META tag
-with a Content-Type, the tag will be scanned for a language code.
-This code is returned if it is found; undef is returned otherwise.
-
-This function only does sloppy guessing; it will be confused by
-unexpected things like SGML comments. What it basically does is to
-grab something that looks like a META tag and scan it.
-
-=cut
-
-sub guesscharset ($) {
-    my($html) = @_;
-    my $charset = undef;
-    local($`, $&, $', $1, $2, $3);
-    # FIXME... These regular expressions will miss a lot of valid tags!
-    if ($html =~ /<meta\s+http-equiv=(["']?)Content-Type\1\s+content=(["'])text\/html\s*;\s*charset=([^\2\s\r\n]+)\2\s*(?:\/?)>/is) {
-        $charset = $3;
-    } elsif ($html =~ /<meta\s+content=(["'])text\/html\s*;\s*charset=([^\1\s\r\n]+)\1\s+http-equiv=(["']?)Content-Type\3\s*(?:\/?)>/is) {
-        $charset = $2;
-    }
-    return $charset;
-} # guess
-
-sub guesstype ($) {
-    my($html) = @_;
-    my $charset = guesscharset($html);
-    return defined $charset? "text/html; charset=$charset": "text/html";
-}
-
 =item output_html_with_http_headers
 
    &output_html_with_http_headers($query, $cookie, $html)
@@ -364,8 +362,11 @@ corresponds to the HTML page $html.
 sub output_html_with_http_headers ($$$) {
     my($query, $cookie, $html) = @_;
     print $query->header(
-       -type   => guesstype($html),
-       -cookie => $cookie,
+        -type    => 'text/html',
+        -charset => 'UTF-8',
+        -cookie  => $cookie,
+               -Pragma => 'no-cache',
+               -'Cache-Control' => 'no-cache',
     ), $html;
 }