X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FTemplates.pm;h=6a9d48b3450a57e4288a6fadaa60d544336568a4;hb=e0c1476123603e9cfe9486ad67c507ff5b1574c3;hp=73d94c239952262f71153197cb9f4baa9269996d;hpb=f0489763d66dde3309396755f7c1d0c6359be31b;p=koha.git diff --git a/C4/Templates.pm b/C4/Templates.pm index 73d94c2399..6a9d48b345 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -4,7 +4,7 @@ use strict; use warnings; use Carp; use CGI; -use List::Util qw/first/; +use List::MoreUtils qw/any/; # Copyright 2009 Chris Cormack and The Koha Dev Team # @@ -36,7 +36,7 @@ use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language use C4::Context; -__PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars)); +__PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars)); @@ -53,15 +53,19 @@ sub new { else { $htdocs = C4::Context->config('intrahtdocs'); } - - my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query); + 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, - INCLUDE_PATH => [ - "$htdocs/$theme/$lang/includes", - "$htdocs/$theme/en/includes" - ], + 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 => {}, } ) or die Template->error(); @@ -72,6 +76,8 @@ sub new { 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); @@ -93,18 +99,18 @@ sub output { $vars->{themelang} = '/opac-tmpl'; } $vars->{lang} = $self->lang; - $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang; + $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang; $vars->{yuipath} = ( C4::Context->preference("yuipath") eq "local" - ? $vars->{themelang} . "/lib/yui" + ? ( $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->{opacstylesheet} = C4::Context->preference('opacstylesheet'); + C4::Context->preference('opaccolorstylesheet'); + $vars->{opaclayoutstylesheet} = + C4::Context->preference('opaclayoutstylesheet'); # add variables set via param to $vars for processing # and clean any utf8 mess @@ -156,20 +162,6 @@ sub utf8_hashref { 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 -# 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 { @@ -208,8 +200,7 @@ sub _get_template_file { 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'); + 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"; @@ -222,32 +213,21 @@ sub _get_template_file { sub gettemplate { - my ( $tmplbase, $interface, $query ) = @_; + my ( $tmplbase, $interface, $query, $is_plugin ) = @_; ($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); + $filename = $tmplbase if ( $is_plugin ); 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 + # Bidirectionality, must be sent even if is the only language my $current_lang = regex_lang_subtags($lang); my $bidi; $bidi = get_bidi($current_lang->{script}) if $current_lang->{script}; + $template->param( + bidi => $bidi, + ); # Languages my $languages_loop = getTranslatedLanguages($interface,$theme,$lang); my $num_languages_enabled = 0; @@ -256,11 +236,11 @@ sub gettemplate { $num_languages_enabled++ if $sublang->{enabled}; } } + my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well $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; + one_language_enabled => $one_language_enabled, + ) unless $one_language_enabled; return $template; } @@ -273,21 +253,10 @@ sub themelanguage { ($query) or warn "no query in themelanguage"; # Select a language based on cookie, syspref available languages & browser - my $is_intranet = $interface eq 'intranet'; - my @languages = split(",", C4::Context->preference( - $is_intranet ? 'language' : 'opaclanguages')); - my $lang; - $lang = getlanguagecookie($query); - unless ($lang) { - my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE }; - $lang = accept_language( $http_accept_language, - getTranslatedLanguages($interface,'prog') ); - } - # Ignore a lang not selected in sysprefs - $lang = undef unless first { $_ eq $lang } @languages; - # Fall back to English if necessary - $lang = 'en' unless $lang; + my $lang = C4::Languages::getlanguage($query); + # Select theme + my $is_intranet = $interface eq 'intranet'; my @themes = split(" ", C4::Context->preference( $is_intranet ? "template" : "opacthemes" )); push @themes, 'prog'; @@ -295,21 +264,22 @@ sub themelanguage { # 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) + return ($theme, $lang, \@themes) } } # Otherwise, return prog theme in English 'en' - return ('prog', 'en'); + return ('prog', 'en', \@themes); } sub setlanguagecookie { my ( $query, $language, $uri ) = @_; + my $cookie = $query->cookie( -name => 'KohaOpacLanguage', -value => $language, - -expires => '' + -HttpOnly => 1, + -expires => '+3y' ); print $query->redirect( -uri => $uri, @@ -317,18 +287,71 @@ sub setlanguagecookie { ); } +=head2 getlanguagecookie + + my $cookie = getlanguagecookie($query,$language); + +Returns a cookie object containing the calculated language to be used. + +=cut + sub getlanguagecookie { - my ($query) = @_; - my $lang; - if ($query->cookie('KohaOpacLanguage')){ - $lang = $query->cookie('KohaOpacLanguage') ; - }else{ - $lang = $ENV{HTTP_ACCEPT_LANGUAGE}; - + my ( $query, $language ) = @_; + my $cookie = $query->cookie( + -name => 'KohaOpacLanguage', + -value => $language, + -HttpOnly => 1, + -expires => '+3y' + ); + + return $cookie; +} + +=head2 GetColumnDefs + + my $columns = GetColumnDefs( $cgi ) + +It is passed a CGI object and returns a hash of hashes containing +the column names and descriptions for each table defined in the +columns.def file corresponding to the CGI object. + +=cut + +sub GetColumnDefs { + + my $query = shift; + + my $columns = {}; + + my $htdocs = C4::Context->config('intrahtdocs'); + my $columns_file = 'columns.def'; + + # Get theme and language to build the path to columns.def + my ($theme, $lang, $availablethemes) = + themelanguage($htdocs, 'about.tt', 'intranet', $query); + # Build columns.def path + my $path = "$htdocs/$theme/$lang/$columns_file"; + my $fh; + if ( ! open ( $fh, q{<}, $path ) ) { + carp "Error opening $path. Check your templates."; + return; + } + # Loop through the columns.def file + while ( my $input = <$fh> ){ + chomp $input; + if ( $input =~ m|(.*)| ) { + my ( $table, $column ) = split( '\.', $1); + my $description = $2; + # Initialize the table array if needed. + @{$columns->{ $table }} = () if ! defined $columns->{ $table }; + # Push field and description + push @{$columns->{ $table }}, + { field => $column, description => $description }; + } } - $lang =~ s/[^a-zA-Z_-]*//; #sanitzie - return $lang; + close $fh; + + return $columns; } 1; -