Bug 6755 Web browser default lang != syspref selected langs
[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         {
60             EVAL_PERL    => 1,
61             ABSOLUTE     => 1,
62             INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
63             FILTERS      => {},
64
65         }
66     ) or die Template->error();
67     my $self = {
68         TEMPLATE => $template,
69         VARS     => {},
70     };
71     bless $self, $class;
72     $self->theme($theme);
73     $self->lang($lang);
74     $self->filename($filename);
75     $self->htdocs($htdocs);
76     $self->interface($interface);
77     $self->{VARS}->{"test"} = "value";
78     return $self;
79
80 }
81
82 sub output {
83     my $self = shift;
84     my $vars = shift;
85
86 #    my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
87     my $template = $self->{TEMPLATE};
88     if ( $self->interface eq 'intranet' ) {
89         $vars->{themelang} = '/intranet-tmpl';
90     }
91     else {
92         $vars->{themelang} = '/opac-tmpl';
93     }
94     $vars->{lang} = $self->lang;
95     $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
96     $vars->{yuipath} =
97       ( C4::Context->preference("yuipath") eq "local"
98         ? $vars->{themelang} . "/lib/yui"
99         : C4::Context->preference("yuipath") );
100     $vars->{interface} =
101       ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
102     $vars->{theme} = $self->theme;
103     $vars->{opaccolorstylesheet} =
104       C4::Context->preference('opaccolorstylesheet');
105     $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
106     $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
107
108     # add variables set via param to $vars for processing
109     # and clean any utf8 mess
110     for my $k ( keys %{ $self->{VARS} } ) {
111         $vars->{$k} = $self->{VARS}->{$k};
112         if (ref($vars->{$k}) eq 'ARRAY'){
113             utf8_arrayref($vars->{$k});
114         }
115         elsif (ref($vars->{$k}) eq 'HASH'){
116             utf8_hashref($vars->{$k});
117         }
118         else {
119             utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
120         }
121     }
122     my $data;
123 #    binmode( STDOUT, ":utf8" );
124     $template->process( $self->filename, $vars, \$data )
125       || die "Template process failed: ", $template->error();
126     return $data;
127 }
128
129 sub utf8_arrayref {
130     my $arrayref = shift;
131     foreach my $element (@$arrayref){
132         if (ref($element) eq 'ARRAY'){
133             utf8_arrayref($element);
134             next;
135         }
136         if (ref($element) eq 'HASH'){
137             utf8_hashref($element);
138             next;
139         }
140         utf8::encode($element) if utf8::is_utf8($element);
141     }        
142 }         
143
144 sub utf8_hashref {
145     my $hashref = shift;
146     for my $key (keys %{$hashref}){
147         if (ref($hashref->{$key}) eq 'ARRAY'){
148             utf8_arrayref($hashref->{$key});
149             next;
150         }
151         if (ref($hashref->{$key}) eq 'HASH'){
152             utf8_hashref($hashref->{$key});
153             next;
154         }
155         utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
156     }
157 }
158         
159         
160 # FIXME - this is a horrible hack to cache
161 # the current known-good language, temporarily
162 # put in place to resolve bug 4403.  It is
163 # used only by C4::XSLT::XSLTParse4Display;
164 # the language is set via the usual call
165 # to themelanguage.
166 my $_current_language = 'en';
167
168 sub _current_language {
169     return $_current_language;
170 }
171
172
173 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
174 sub param {
175     my $self = shift;
176     while (@_) {
177         my $key = shift;
178         my $val = shift;
179         if    ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
180         elsif ( ref($val) eq 'HASH'  && !scalar %$val ) { $val = undef; }
181         $self->{VARS}->{$key} = $val;
182     }
183 }
184
185
186 =head1 NAME
187
188 C4::Templates - Functions for managing templates
189
190 =head1 FUNCTIONS
191
192 =cut
193
194 # FIXME: this is a quick fix to stop rc1 installing broken
195 # Still trying to figure out the correct fix.
196 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
197
198 #---------------------------------------------------------------------------------------------------------
199 # FIXME - POD
200
201 sub _get_template_file {
202     my ($tmplbase, $interface, $query) = @_;
203
204     my $is_intranet = $interface eq 'intranet';
205     my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
206     my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $interface, $query);
207     my $opacstylesheet = C4::Context->preference('opacstylesheet');
208
209     # if the template doesn't exist, load the English one as a last resort
210     my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
211     unless (-f $filename) {
212         $lang = 'en';
213         $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
214     }
215     return ($htdocs, $theme, $lang, $filename);
216 }
217
218
219 sub gettemplate {
220     my ( $tmplbase, $interface, $query ) = @_;
221     ($query) or warn "no query in gettemplate";
222     my $path = C4::Context->preference('intranet_includes') || 'includes';
223     my $opacstylesheet = C4::Context->preference('opacstylesheet');
224     $tmplbase =~ s/\.tmpl$/.tt/;
225     my ($htdocs, $theme, $lang, $filename)
226        =  _get_template_file($tmplbase, $interface, $query);
227     my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
228     my $is_intranet = $interface eq 'intranet';
229     my $themelang =
230         ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
231         "/$theme/$lang";
232     $template->param(
233         themelang => $themelang,
234         yuipath   => C4::Context->preference("yuipath") eq "local"
235                      ? "$themelang/lib/yui"
236                      : C4::Context->preference("yuipath"),
237         interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
238         theme     => $theme,
239         lang      => $lang
240     );
241
242     # Bidirectionality
243     my $current_lang = regex_lang_subtags($lang);
244     my $bidi;
245     $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
246     # Languages
247     my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
248     my $num_languages_enabled = 0;
249     foreach my $lang (@$languages_loop) {
250         foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
251             $num_languages_enabled++ if $sublang->{enabled};
252          }
253     }
254     $template->param(
255             languages_loop       => $languages_loop,
256             bidi                 => $bidi,
257             one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
258     ) unless @$languages_loop<2;
259
260     return $template;
261 }
262
263
264 #---------------------------------------------------------------------------------------------------------
265 # FIXME - POD
266 sub themelanguage {
267     my ($htdocs, $tmpl, $interface, $query) = @_;
268     ($query) or warn "no query in themelanguage";
269
270     # Select a language based on cookie, syspref available languages & browser
271     my $is_intranet = $interface eq 'intranet';
272     my @languages = split(",", C4::Context->preference(
273         $is_intranet ? 'language' : 'opaclanguages'));
274     my $lang;
275     $lang = $query->cookie('KohaOpacLanguage')
276         if defined $query and $query->cookie('KohaOpacLanguage');
277     unless ($lang) {
278         my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
279         $lang = accept_language( $http_accept_language, 
280             getTranslatedLanguages($interface,'prog') );
281     }
282     # Ignore a lang not selected in sysprefs
283     $lang = undef  unless first { $_ eq $lang } @languages;
284     # Fall back to English if necessary
285     $lang = 'en' unless $lang;
286
287     my @themes = split(" ", C4::Context->preference(
288         $is_intranet ? "template" : "opacthemes" ));
289     push @themes, 'prog';
290
291     # Try to find first theme for the selected language
292     for my $theme (@themes) {
293         if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
294             $_current_language = $lang;
295             return ($theme, $lang)
296         }
297     }
298     # Otherwise, return prog theme in English 'en'
299     return ('prog', 'en');
300 }
301
302
303 sub setlanguagecookie {
304     my ( $query, $language, $uri ) = @_;
305     my $cookie = $query->cookie(
306         -name    => 'KohaOpacLanguage',
307         -value   => $language,
308         -expires => ''
309     );
310     print $query->redirect(
311         -uri    => $uri,
312         -cookie => $cookie
313     );
314 }
315
316 sub getlanguagecookie {
317     my ($query) = @_;
318     my $lang;
319     if ($query->cookie('KohaOpacLanguage')){
320         $lang = $query->cookie('KohaOpacLanguage') ;
321     }else{
322         $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
323         
324     }
325     $lang = substr($lang, 0, 2);
326
327     return $lang;
328 }
329
330 1;
331