7 use List::Util qw/first/;
9 # Copyright 2009 Chris Cormack and The Koha Dev Team
11 # This file is part of Koha.
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
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.
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
28 Koha::Templates - Object for manipulating templates for use with Koha
32 use base qw(Class::Accessor);
34 use Template::Constants qw( :debug );
35 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
39 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
45 my $interface = shift;
48 my $query = @_? shift: undef;
50 if ( $interface ne "intranet" ) {
51 $htdocs = C4::Context->config('opachtdocs');
54 $htdocs = C4::Context->config('intrahtdocs');
57 my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query);
58 my $template = Template->new(
62 "$htdocs/$theme/$lang/includes",
63 "$htdocs/$theme/en/includes"
67 ) or die Template->error();
69 TEMPLATE => $template,
75 $self->filename($filename);
76 $self->htdocs($htdocs);
77 $self->interface($interface);
78 $self->{VARS}->{"test"} = "value";
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';
93 $vars->{themelang} = '/opac-tmpl';
95 $vars->{lang} = $self->lang;
96 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
98 ( C4::Context->preference("yuipath") eq "local"
99 ? $vars->{themelang} . "/lib/yui"
100 : C4::Context->preference("yuipath") );
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');
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});
116 elsif (ref($vars->{$k}) eq 'HASH'){
117 utf8_hashref($vars->{$k});
120 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
124 # binmode( STDOUT, ":utf8" );
125 $template->process( $self->filename, $vars, \$data )
126 || die "Template process failed: ", $template->error();
131 my $arrayref = shift;
132 foreach my $element (@$arrayref){
133 if (ref($element) eq 'ARRAY'){
134 utf8_arrayref($element);
137 if (ref($element) eq 'HASH'){
138 utf8_hashref($element);
141 utf8::encode($element) if utf8::is_utf8($element);
147 for my $key (keys %{$hashref}){
148 if (ref($hashref->{$key}) eq 'ARRAY'){
149 utf8_arrayref($hashref->{$key});
152 if (ref($hashref->{$key}) eq 'HASH'){
153 utf8_hashref($hashref->{$key});
156 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
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
167 my $_current_language = 'en';
169 sub _current_language {
170 return $_current_language;
174 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
180 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
181 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
182 $self->{VARS}->{$key} = $val;
189 C4::Templates - Functions for managing templates
195 # FIXME: this is a quick fix to stop rc1 installing broken
196 # Still trying to figure out the correct fix.
197 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
199 #---------------------------------------------------------------------------------------------------------
202 sub _get_template_file {
203 my ($tmplbase, $interface, $query) = @_;
205 my $is_intranet = $interface eq 'intranet';
206 my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
207 my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $interface, $query);
208 my $opacstylesheet = C4::Context->preference('opacstylesheet');
210 # if the template doesn't exist, load the English one as a last resort
211 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
212 unless (-f $filename) {
214 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
216 return ($htdocs, $theme, $lang, $filename);
221 my ( $tmplbase, $interface, $query ) = @_;
222 ($query) or warn "no query in gettemplate";
223 my $path = C4::Context->preference('intranet_includes') || 'includes';
224 my $opacstylesheet = C4::Context->preference('opacstylesheet');
225 $tmplbase =~ s/\.tmpl$/.tt/;
226 my ($htdocs, $theme, $lang, $filename)
227 = _get_template_file($tmplbase, $interface, $query);
228 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
229 my $is_intranet = $interface eq 'intranet';
231 ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
234 themelang => $themelang,
235 yuipath => C4::Context->preference("yuipath") eq "local"
236 ? "$themelang/lib/yui"
237 : C4::Context->preference("yuipath"),
238 interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
244 my $current_lang = regex_lang_subtags($lang);
246 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
248 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
249 my $num_languages_enabled = 0;
250 foreach my $lang (@$languages_loop) {
251 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
252 $num_languages_enabled++ if $sublang->{enabled};
256 languages_loop => $languages_loop,
258 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
259 ) unless @$languages_loop<2;
265 #---------------------------------------------------------------------------------------------------------
268 my ($htdocs, $tmpl, $interface, $query) = @_;
269 ($query) or warn "no query in themelanguage";
271 # Select a language based on cookie, syspref available languages & browser
272 my $is_intranet = $interface eq 'intranet';
273 my @languages = split(",", C4::Context->preference(
274 $is_intranet ? 'language' : 'opaclanguages'));
276 $lang = $query->cookie('KohaOpacLanguage')
277 if defined $query and $query->cookie('KohaOpacLanguage');
279 my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
280 $lang = accept_language( $http_accept_language,
281 getTranslatedLanguages($interface,'prog') );
283 # Ignore a lang not selected in sysprefs
284 $lang = undef unless first { $_ eq $lang } @languages;
285 # Fall back to English if necessary
286 $lang = 'en' unless $lang;
288 my @themes = split(" ", C4::Context->preference(
289 $is_intranet ? "template" : "opacthemes" ));
290 push @themes, 'prog';
292 # Try to find first theme for the selected language
293 for my $theme (@themes) {
294 if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
295 $_current_language = $lang;
296 return ($theme, $lang)
299 # Otherwise, return prog theme in English 'en'
300 return ('prog', 'en');
304 sub setlanguagecookie {
305 my ( $query, $language, $uri ) = @_;
306 my $cookie = $query->cookie(
307 -name => 'KohaOpacLanguage',
311 print $query->redirect(
317 sub getlanguagecookie {
320 if ($query->cookie('KohaOpacLanguage')){
321 $lang = $query->cookie('KohaOpacLanguage') ;
323 $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
326 $lang = substr($lang, 0, 2);