Bug 11413: Fix field_numbers
[koha.git] / C4 / Templates.pm
index 8d13a49..d53d1a8 100644 (file)
@@ -4,7 +4,7 @@ use strict;
 use warnings;
 use Carp;
 use CGI;
-use List::MoreUtils qw/any/;
+use List::MoreUtils qw/any uniq/;
 
 # Copyright 2009 Chris Cormack and The Koha Dev Team
 #
@@ -100,16 +100,11 @@ 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;
     $vars->{opaccolorstylesheet} =
         C4::Context->preference('opaccolorstylesheet');
-    $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
     $vars->{opaclayoutstylesheet} =
         C4::Context->preference('opaclayoutstylesheet');
 
@@ -163,20 +158,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 {
@@ -216,13 +197,8 @@ 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);
-
-    # 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";
-    }
+
     return ($htdocs, $theme, $lang, $filename);
 }
 
@@ -231,7 +207,6 @@ sub gettemplate {
     my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
     ($query) or warn "no query in gettemplate";
     my $path = C4::Context->preference('intranet_includes') || 'includes';
-    $tmplbase =~ s/\.tmpl$/.tt/;
     my ($htdocs, $theme, $lang, $filename)
        =  _get_template_file($tmplbase, $interface, $query);
     $filename = $tmplbase if ( $is_plugin );
@@ -245,9 +220,6 @@ sub gettemplate {
 #        "/$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
@@ -278,8 +250,20 @@ sub gettemplate {
 }
 
 
-#---------------------------------------------------------------------------------------------------------
-# FIXME - POD
+=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";
@@ -287,21 +271,35 @@ sub themelanguage {
     # Select a language based on cookie, syspref available languages & browser
     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';
-
-    # Try to find first theme for the selected language
+    # Get theme
+    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/modules/$tmpl" ) {
-            $_current_language = $lang;
-            return ($theme, $lang, \@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/en/$where/$tmpl" ) {
+            return ( $theme, 'en', uniq( \@themes ) );
         }
     }
-    # Otherwise, return prog theme in English 'en'
-    return ('prog', 'en', \@themes);
 }
 
 
@@ -340,4 +338,51 @@ sub getlanguagecookie {
     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|<field name="(.*)">(.*)</field>| ) {
+            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 };
+        }
+    }
+    close $fh;
+
+    return $columns;
+}
+
 1;