Bug 6085 : utf8 fixed for xslt, and parameters, broken for utf8 included in templates
[koha.git] / C4 / Templates.pm
1 package C4::Templates;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
7
8 # Copyright 2009 Chris Cormack and The Koha Dev Team
9 #
10 # This file is part of Koha.
11 #
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
16 #
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 =head1 NAME 
26
27     Koha::Templates - Object for manipulating templates for use with Koha
28
29 =cut
30
31 use base qw(Class::Accessor);
32 use Template;
33 use Template::Constants qw( :debug );
34
35 use C4::Context;
36
37 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
38
39 sub new {
40     my $class     = shift;
41     my $interface = shift;
42     my $filename  = shift;
43     my $tmplbase  = shift;
44     my $htdocs;
45     if ( $interface ne "intranet" ) {
46         $htdocs = C4::Context->config('opachtdocs');
47     }
48     else {
49         $htdocs = C4::Context->config('intrahtdocs');
50     }
51
52     my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface );
53     my $template = Template->new(
54         {
55             EVAL_PERL    => 1,
56             ABSOLUTE     => 1,
57             INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
58             FILTERS      => {},
59
60         }
61     ) or die Template->error();
62     my $self = {
63         TEMPLATE => $template,
64         VARS     => {},
65     };
66     bless $self, $class;
67     $self->theme($theme);
68     $self->lang($lang);
69     $self->filename($filename);
70     $self->htdocs($htdocs);
71     $self->interface($interface);
72     $self->{VARS}->{"test"} = "value";
73     return $self;
74
75 }
76
77 sub output {
78     my $self = shift;
79     my $vars = shift;
80
81 #    my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
82     my $template = $self->{TEMPLATE};
83     if ( $self->interface eq 'intranet' ) {
84         $vars->{themelang} = '/intranet-tmpl';
85     }
86     else {
87         $vars->{themelang} = '/opac-tmpl';
88     }
89     $vars->{lang} = $self->lang;
90     $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
91     $vars->{yuipath} =
92       ( C4::Context->preference("yuipath") eq "local"
93         ? $vars->{themelang} . "/lib/yui"
94         : C4::Context->preference("yuipath") );
95     $vars->{interface} =
96       ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
97     $vars->{theme} = $self->theme;
98     $vars->{opaccolorstylesheet} =
99       C4::Context->preference('opaccolorstylesheet');
100     $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
101     $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
102
103     #add variables set via param to $vars for processing
104     for my $k ( keys %{ $self->{VARS} } ) {
105         $vars->{$k} = $self->{VARS}->{$k};
106     }
107     my $data;
108 #    binmode( STDOUT, ":utf8" );
109     $template->process( $self->filename, $vars, \$data )
110       || die "Template process failed: ", $template->error();
111     return $data;
112 }
113
114 # FIXME - this is a horrible hack to cache
115 # the current known-good language, temporarily
116 # put in place to resolve bug 4403.  It is
117 # used only by C4::XSLT::XSLTParse4Display;
118 # the language is set via the usual call
119 # to themelanguage.
120 my $_current_language = 'en';
121
122 sub _current_language {
123     return $_current_language;
124 }
125
126 sub themelanguage {
127     my ( $htdocs, $tmpl, $interface ) = @_;
128     my $query = new CGI;
129
130     # Set some defaults for language and theme
131     # First, check the user's preferences
132     my $lang;
133
134     # But, if there's a cookie set, obey it
135     $lang = $query->cookie('KohaOpacLanguage')
136       if ( defined $query and $query->cookie('KohaOpacLanguage') );
137
138     # Fall back to English
139     my @languages;
140     if ( $interface eq 'intranet' ) {
141         @languages = split ",", C4::Context->preference("language");
142     }
143     else {
144         @languages = split ",", C4::Context->preference("opaclanguages");
145     }
146     if ($lang) {
147         @languages = ( $lang, @languages );
148     }
149     else {
150         $lang = $languages[0];
151     }
152     my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
153     my $dbh = C4::Context->dbh;
154     my @themes;
155     if ( $interface eq "intranet" ) {
156         @themes = split " ", C4::Context->preference("template");
157     }
158     else {
159         @themes = split " ", C4::Context->preference("opacthemes");
160     }
161
162  # searches through the themes and languages. First template it find it returns.
163  # Priority is for getting the theme right.
164   THEME:
165     foreach my $th (@themes) {
166         foreach my $la (@languages) {
167             if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
168                 $theme = $th;
169                 $lang  = $la;
170                 last THEME;
171             }
172             last unless $la =~ /[-_]/;
173         }
174     }
175     $_current_language = $lang;  # FIXME part of bad hack to paper over bug 4403
176     return ( $theme, $lang );
177 }
178
179 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
180 sub param {
181     my $self = shift;
182     while (@_) {
183         my $key = shift;
184         my $val = shift;
185         utf8::encode($val) if utf8::is_utf8($val);
186         utf8::decode($val) if $key eq "XSLTBloc";
187         if    ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
188         elsif ( ref($val) eq 'HASH'  && !scalar %$val ) { $val = undef; }
189         $self->{VARS}->{$key} = $val;
190     }
191 }
192
193 1;
194