Bug 7360 Import Default framework into other deletes default Framework
[koha.git] / C4 / Templates.pm
1 package C4::Templates;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
7 use List::Util qw/first/;
8
9 # Copyright 2009 Chris Cormack and The Koha Dev Team
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 =head1 NAME 
27
28     Koha::Templates - Object for manipulating templates for use with Koha
29
30 =cut
31
32 use base qw(Class::Accessor);
33 use Template;
34 use Template::Constants qw( :debug );
35 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
36
37 use C4::Context;
38
39 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
40
41
42
43 sub new {
44     my $class     = shift;
45     my $interface = shift;
46     my $filename  = shift;
47     my $tmplbase  = shift;
48     my $query     = @_? shift: undef;
49     my $htdocs;
50     if ( $interface ne "intranet" ) {
51         $htdocs = C4::Context->config('opachtdocs');
52     }
53     else {
54         $htdocs = C4::Context->config('intrahtdocs');
55     }
56
57     my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query);
58     my $template = Template->new(
59         {   EVAL_PERL    => 1,
60             ABSOLUTE     => 1,
61             INCLUDE_PATH => [
62                 "$htdocs/$theme/$lang/includes",
63                 "$htdocs/$theme/en/includes"
64             ],
65             FILTERS => {},
66         }
67     ) or die Template->error();
68     my $self = {
69         TEMPLATE => $template,
70         VARS     => {},
71     };
72     bless $self, $class;
73     $self->theme($theme);
74     $self->lang($lang);
75     $self->filename($filename);
76     $self->htdocs($htdocs);
77     $self->interface($interface);
78     $self->{VARS}->{"test"} = "value";
79     return $self;
80
81 }
82
83 sub output {
84     my $self = shift;
85     my $vars = shift;
86
87 #    my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
88     my $template = $self->{TEMPLATE};
89     if ( $self->interface eq 'intranet' ) {
90         $vars->{themelang} = '/intranet-tmpl';
91     }
92     else {
93         $vars->{themelang} = '/opac-tmpl';
94     }
95     $vars->{lang} = $self->lang;
96     $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
97     $vars->{yuipath} =
98       ( C4::Context->preference("yuipath") eq "local"
99         ? $vars->{themelang} . "/lib/yui"
100         : C4::Context->preference("yuipath") );
101     $vars->{interface} =
102       ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
103     $vars->{theme} = $self->theme;
104     $vars->{opaccolorstylesheet} =
105       C4::Context->preference('opaccolorstylesheet');
106     $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
107     $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
108
109     # add variables set via param to $vars for processing
110     # and clean any utf8 mess
111     for my $k ( keys %{ $self->{VARS} } ) {
112         $vars->{$k} = $self->{VARS}->{$k};
113         if (ref($vars->{$k}) eq 'ARRAY'){
114             utf8_arrayref($vars->{$k});
115         }
116         elsif (ref($vars->{$k}) eq 'HASH'){
117             utf8_hashref($vars->{$k});
118         }
119         else {
120             utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
121         }
122     }
123     my $data;
124 #    binmode( STDOUT, ":utf8" );
125     $template->process( $self->filename, $vars, \$data )
126       || die "Template process failed: ", $template->error();
127     return $data;
128 }
129
130 sub utf8_arrayref {
131     my $arrayref = shift;
132     foreach my $element (@$arrayref){
133         if (ref($element) eq 'ARRAY'){
134             utf8_arrayref($element);
135             next;
136         }
137         if (ref($element) eq 'HASH'){
138             utf8_hashref($element);
139             next;
140         }
141         utf8::encode($element) if utf8::is_utf8($element);
142     }        
143 }         
144
145 sub utf8_hashref {
146     my $hashref = shift;
147     for my $key (keys %{$hashref}){
148         if (ref($hashref->{$key}) eq 'ARRAY'){
149             utf8_arrayref($hashref->{$key});
150             next;
151         }
152         if (ref($hashref->{$key}) eq 'HASH'){
153             utf8_hashref($hashref->{$key});
154             next;
155         }
156         utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
157     }
158 }
159         
160         
161 # FIXME - this is a horrible hack to cache
162 # the current known-good language, temporarily
163 # put in place to resolve bug 4403.  It is
164 # used only by C4::XSLT::XSLTParse4Display;
165 # the language is set via the usual call
166 # to themelanguage.
167 my $_current_language = 'en';
168
169 sub _current_language {
170     return $_current_language;
171 }
172
173
174 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
175 sub param {
176     my $self = shift;
177     while (@_) {
178         my $key = shift;
179         my $val = shift;
180         if    ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
181         elsif ( ref($val) eq 'HASH'  && !scalar %$val ) { $val = undef; }
182         if ( $key ) {
183             $self->{VARS}->{$key} = $val;
184         } else {
185             warn "Problem = a value of $val has been passed to param without key";
186         }
187     }
188 }
189
190
191 =head1 NAME
192
193 C4::Templates - Functions for managing templates
194
195 =head1 FUNCTIONS
196
197 =cut
198
199 # FIXME: this is a quick fix to stop rc1 installing broken
200 # Still trying to figure out the correct fix.
201 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
202
203 #---------------------------------------------------------------------------------------------------------
204 # FIXME - POD
205
206 sub _get_template_file {
207     my ($tmplbase, $interface, $query) = @_;
208
209     my $is_intranet = $interface eq 'intranet';
210     my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
211     my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $interface, $query);
212     my $opacstylesheet = C4::Context->preference('opacstylesheet');
213
214     # if the template doesn't exist, load the English one as a last resort
215     my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
216     unless (-f $filename) {
217         $lang = 'en';
218         $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
219     }
220     return ($htdocs, $theme, $lang, $filename);
221 }
222
223
224 sub gettemplate {
225     my ( $tmplbase, $interface, $query ) = @_;
226     ($query) or warn "no query in gettemplate";
227     my $path = C4::Context->preference('intranet_includes') || 'includes';
228     my $opacstylesheet = C4::Context->preference('opacstylesheet');
229     $tmplbase =~ s/\.tmpl$/.tt/;
230     my ($htdocs, $theme, $lang, $filename)
231        =  _get_template_file($tmplbase, $interface, $query);
232     my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
233     my $is_intranet = $interface eq 'intranet';
234     my $themelang =
235         ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
236         "/$theme/$lang";
237     $template->param(
238         themelang => $themelang,
239         yuipath   => C4::Context->preference("yuipath") eq "local"
240                      ? "$themelang/lib/yui"
241                      : C4::Context->preference("yuipath"),
242         interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
243         theme     => $theme,
244         lang      => $lang
245     );
246
247     # Bidirectionality
248     my $current_lang = regex_lang_subtags($lang);
249     my $bidi;
250     $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
251     # Languages
252     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
253     my $num_languages_enabled = 0;
254     foreach my $lang (@$languages_loop) {
255         foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
256             $num_languages_enabled++ if $sublang->{enabled};
257          }
258     }
259     $template->param(
260             languages_loop       => $languages_loop,
261             bidi                 => $bidi,
262             one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
263     ) unless @$languages_loop<2;
264
265     return $template;
266 }
267
268
269 #---------------------------------------------------------------------------------------------------------
270 # FIXME - POD
271 sub themelanguage {
272     my ($htdocs, $tmpl, $interface, $query) = @_;
273     ($query) or warn "no query in themelanguage";
274
275     # Select a language based on cookie, syspref available languages & browser
276     my $lang = getlanguage($query, $interface);
277
278     # Select theme
279     my $is_intranet = $interface eq 'intranet';
280     my @themes = split(" ", C4::Context->preference(
281         $is_intranet ? "template" : "opacthemes" ));
282     push @themes, 'prog';
283
284     # Try to find first theme for the selected language
285     for my $theme (@themes) {
286         if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
287             $_current_language = $lang;
288             return ($theme, $lang)
289         }
290     }
291     # Otherwise, return prog theme in English 'en'
292     return ('prog', 'en');
293 }
294
295
296 sub setlanguagecookie {
297     my ( $query, $language, $uri ) = @_;
298     my $cookie = $query->cookie(
299         -name    => 'KohaOpacLanguage',
300         -value   => $language,
301         -expires => ''
302     );
303     print $query->redirect(
304         -uri    => $uri,
305         -cookie => $cookie
306     );
307 }
308
309
310 sub getlanguage {
311     my ($query, $interface) = @_;
312
313     # Select a language based on cookie, syspref available languages & browser
314     my $is_intranet = $interface eq 'intranet';
315     my @languages = split(",", C4::Context->preference(
316         $is_intranet ? 'language' : 'opaclanguages'));
317
318     my $lang;
319
320     # cookie
321     if ( $query->cookie('KohaOpacLanguage') ) {
322         $lang = $query->cookie('KohaOpacLanguage');
323         $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie
324     }
325
326     # HTTP_ACCEPT_LANGUAGE
327     unless ($lang) {
328         my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
329         $lang = accept_language( $http_accept_language,
330             getTranslatedLanguages($interface,'prog') );
331     }
332
333     # Ignore a lang not selected in sysprefs
334     $lang = undef  unless first { $_ eq $lang } @languages;
335
336     # Fall back to English if necessary
337     $lang = 'en' unless $lang;
338
339     return $lang;
340 }
341
342 1;
343