Bug 13601: get rid of DateTime::Format::DateParse
[koha.git] / C4 / Templates.pm
index 2c57718..52a7229 100644 (file)
@@ -3,8 +3,8 @@ package C4::Templates;
 use strict;
 use warnings;
 use Carp;
-use CGI;
-use List::MoreUtils qw/any/;
+use CGI qw ( -utf8 );
+use List::MoreUtils qw/ any uniq /;
 
 # Copyright 2009 Chris Cormack and The Koha Dev Team
 #
@@ -59,14 +59,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 = {
@@ -100,10 +103,6 @@ sub output {
     }
     $vars->{lang} = $self->lang;
     $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang;
-    $vars->{yuipath} =
-      ( C4::Context->preference("yuipath") eq "local"
-        ? ( $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;
@@ -113,56 +112,17 @@ sub output {
         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});
-        }
     }
+
     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;
@@ -216,6 +176,19 @@ sub gettemplate {
     $filename = $tmplbase if ( $is_plugin );
     my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
 
+# NOTE: Commenting these out rather than deleting them so that those who need
+# to know how we previously shimmed these directories will be able to understand.
+#    my $is_intranet = $interface eq 'intranet';
+#    my $themelang =
+#        ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
+#        "/$theme/$lang";
+#    $template->param(
+#        themelang => $themelang,
+#        interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
+#        theme     => $theme,
+#        lang      => $lang
+#    );
+
     # Bidirectionality, must be sent even if is the only language
     my $current_lang = regex_lang_subtags($lang);
     my $bidi;
@@ -241,9 +214,20 @@ sub gettemplate {
 }
 
 
-#---------------------------------------------------------------------------------------------------------
-# FIXME - POD
-# FIXME - Rewritten to remove hardcoded theme with minimal changes, need to be rethinked
+=head2 themelanguage
+
+    my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query);
+
+This function returns the theme and language to be used for rendering the UI.
+It also returns the list of themes that should be applied as a fallback. This is
+used for the theme overlay feature (i.e. if a file doesn't exist on the requested
+theme, fallback to the configured fallback).
+
+Important: this function is used on the webinstaller too, so always consider
+the use case where the DB is not populated already when rewriting/fixing.
+
+=cut
+
 sub themelanguage {
     my ($htdocs, $tmpl, $interface, $query) = @_;
     ($query) or warn "no query in themelanguage";
@@ -252,18 +236,38 @@ sub themelanguage {
     my $lang = C4::Languages::getlanguage($query);
 
     # Get theme
-    my @themes   = ( C4::Context->preference( ($interface eq 'intranet') ? 'template' : 'opacthemes' ) );
-    my $fallback =   C4::Context->preference( ($interface eq 'intranet') ? 'template' : 'OPACFallback' );
-    push @themes, $fallback;
-
-    # Try to find first theme for the selected language
+    my @themes;
+    my $theme_syspref    = ($interface eq 'intranet') ? 'template' : 'opacthemes';
+    my $fallback_syspref = ($interface eq 'intranet') ? 'template' : 'OPACFallback';
+    # Yeah, hardcoded, last resort if the DB is not populated
+    my $hardcoded_theme = ($interface eq 'intranet') ? 'prog' : 'bootstrap';
+
+    # Configured theme is the first one
+    push @themes, C4::Context->preference( $theme_syspref )
+        if C4::Context->preference( $theme_syspref );
+    # Configured fallback next
+    push @themes, C4::Context->preference( $fallback_syspref )
+        if C4::Context->preference( $fallback_syspref );
+    # The hardcoded fallback theme is the last one
+    push @themes, $hardcoded_theme;
+
+    # Try to find first theme for the selected theme/lang, then for fallback/lang
+    my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules';
+    for my $theme (@themes) {
+        if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) {
+            return ( $theme, $lang, uniq( \@themes ) );
+        }
+    }
+    # Otherwise return theme/'en', last resort fallback/'en'
     for my $theme (@themes) {
-        if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
-            return ($theme, $lang, \@themes);
+        if ( -e "$htdocs/$theme/en/$where/$tmpl" ) {
+            return ( $theme, 'en', uniq( \@themes ) );
         }
     }
-    # Otherwise, return fallback theme in English 'en'
-    return ($fallback, 'en', \@themes);
+    # tmpl is a full path, so this is a template for a plugin
+    if ( $tmpl =~ /^\// && -e $tmpl ) {
+        return ( $themes[0], $lang, uniq( \@themes ) );
+    }
 }