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; }
183 $self->{VARS
}->{$key} = $val;
185 warn "Problem = a value of $val has been passed to param without key";
193 C4::Templates - Functions for managing templates
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/";
203 #---------------------------------------------------------------------------------------------------------
206 sub _get_template_file
{
207 my ($tmplbase, $interface, $query) = @_;
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');
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) {
218 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
220 return ($htdocs, $theme, $lang, $filename);
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';
235 ($is_intranet ?
'/intranet-tmpl' : '/opac-tmpl') .
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',
248 my $current_lang = regex_lang_subtags
($lang);
250 $bidi = get_bidi
($current_lang->{script
}) if $current_lang->{script
};
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
};
260 languages_loop
=> $languages_loop,
262 one_language_enabled
=> ($num_languages_enabled <= 1) ?
1 : 0, # deal with zero enabled langs as well
263 ) unless @
$languages_loop<2;
269 #---------------------------------------------------------------------------------------------------------
272 my ($htdocs, $tmpl, $interface, $query) = @_;
273 ($query) or warn "no query in themelanguage";
275 # Select a language based on cookie, syspref available languages & browser
276 my $is_intranet = $interface eq 'intranet';
277 my @languages = split(",", C4
::Context
->preference(
278 $is_intranet ?
'language' : 'opaclanguages'));
280 $lang = $query->cookie('KohaOpacLanguage')
281 if defined $query and $query->cookie('KohaOpacLanguage');
283 my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE
};
284 $lang = accept_language
( $http_accept_language,
285 getTranslatedLanguages
($interface,'prog') );
287 # Ignore a lang not selected in sysprefs
288 $lang = undef unless first
{ $_ eq $lang } @languages;
289 # Fall back to English if necessary
290 $lang = 'en' unless $lang;
292 my @themes = split(" ", C4
::Context
->preference(
293 $is_intranet ?
"template" : "opacthemes" ));
294 push @themes, 'prog';
296 # Try to find first theme for the selected language
297 for my $theme (@themes) {
298 if ( -e
"$htdocs/$theme/$lang/modules/$tmpl" ) {
299 $_current_language = $lang;
300 return ($theme, $lang)
303 # Otherwise, return prog theme in English 'en'
304 return ('prog', 'en');
308 sub setlanguagecookie
{
309 my ( $query, $language, $uri ) = @_;
310 my $cookie = $query->cookie(
311 -name
=> 'KohaOpacLanguage',
315 print $query->redirect(
321 sub getlanguagecookie
{
324 if ($query->cookie('KohaOpacLanguage')){
325 $lang = $query->cookie('KohaOpacLanguage') ;
327 $lang = $ENV{HTTP_ACCEPT_LANGUAGE
};
330 $lang = substr($lang, 0, 2);