Bug 18811: Atomic update to print warning at upgrade time
[koha.git] / C4 / Templates.pm
index d53d1a8..17746e8 100644 (file)
@@ -3,25 +3,25 @@ package C4::Templates;
 use strict;
 use warnings;
 use Carp;
-use CGI;
-use List::MoreUtils qw/any uniq/;
+use CGI qw ( -utf8 );
+use List::MoreUtils qw/ any uniq /;
 
 # 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 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 3 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.
+# 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
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 =head1 NAME 
 
@@ -36,6 +36,8 @@ use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language
 
 use C4::Context;
 
+use Koha::Cache::Memory::Lite;
+
 __PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
 
 
@@ -59,14 +61,17 @@ sub new {
         push @includes, "$htdocs/$_/$lang/includes";
         push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
     }
+    # Do not use template cache if script is called from commandline
+    my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
     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'):'',,
+            COMPILE_EXT => $use_template_cache ? '.ttc' : '',
+            COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
             INCLUDE_PATH => \@includes,
             FILTERS => {},
+            ENCODING => 'UTF-8',
         }
     ) or die Template->error();
     my $self = {
@@ -103,62 +108,21 @@ sub output {
     $vars->{interface} =
       ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
     $vars->{theme} = $self->theme;
-    $vars->{opaccolorstylesheet} =
-        C4::Context->preference('opaccolorstylesheet');
+    $vars->{OpacAdditionalStylesheet} =
+        C4::Context->preference('OpacAdditionalStylesheet');
     $vars->{opaclayoutstylesheet} =
         C4::Context->preference('opaclayoutstylesheet');
 
     # 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});
-        }
-    }
+    $vars = { %$vars, %{ $self->{VARS} } };
+
     my $data;
-#    binmode( STDOUT, ":utf8" );
+    binmode( STDOUT, ":utf8" );
     $template->process( $self->filename, $vars, \$data )
       || die "Template process failed: ", $template->error();
     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});
-    }
-}
-
 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
 sub param {
     my $self = shift;
@@ -197,6 +161,7 @@ sub _get_template_file {
     my $is_intranet = $interface eq 'intranet';
     my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
     my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
+    $lang //= 'en';
     my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
 
     return ($htdocs, $theme, $lang, $filename);
@@ -206,6 +171,7 @@ sub _get_template_file {
 sub gettemplate {
     my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
     ($query) or warn "no query in gettemplate";
+    die "bad template path" unless $tmplbase =~ m/^[a-zA-Z0-9_\-\/]+\.(tt|pref)$/; # Will be extended on bug 17989
     my $path = C4::Context->preference('intranet_includes') || 'includes';
     my ($htdocs, $theme, $lang, $filename)
        =  _get_template_file($tmplbase, $interface, $query);
@@ -291,27 +257,31 @@ sub themelanguage {
     my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules';
     for my $theme (@themes) {
         if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) {
-            return ( $theme, $lang, uniq( \@themes ) );
+            return ( $theme, $lang, [ uniq(@themes) ] );
         }
     }
     # Otherwise return theme/'en', last resort fallback/'en'
     for my $theme (@themes) {
         if ( -e "$htdocs/$theme/en/$where/$tmpl" ) {
-            return ( $theme, 'en', uniq( \@themes ) );
+            return ( $theme, 'en', [ uniq(@themes) ] );
         }
     }
+    # tmpl is a full path, so this is a template for a plugin
+    if ( $tmpl =~ /^\// && -e $tmpl ) {
+        return ( $themes[0], $lang, [ uniq(@themes) ] );
+    }
 }
 
 
 sub setlanguagecookie {
     my ( $query, $language, $uri ) = @_;
 
-    my $cookie = $query->cookie(
-        -name    => 'KohaOpacLanguage',
-        -value   => $language,
-        -HttpOnly => 1,
-        -expires => '+3y'
-    );
+    my $cookie = getlanguagecookie( $query, $language );
+
+    # We do not want to set getlanguage in cache, some additional checks are
+    # done in C4::Languages::getlanguage
+    Koha::Cache::Memory::Lite->get_instance()->clear_from_cache( 'getlanguage' );
+
     print $query->redirect(
         -uri    => $uri,
         -cookie => $cookie
@@ -363,7 +333,7 @@ sub GetColumnDefs {
     # Build columns.def path
     my $path = "$htdocs/$theme/$lang/$columns_file";
     my $fh;
-    if ( ! open ( $fh, q{<}, $path ) )  {
+    if ( ! open ( $fh, q{<:encoding(utf-8)}, $path ) )  {
         carp "Error opening $path. Check your templates.";
         return;
     }