7 use List
::MoreUtils qw
/any/;
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 activethemes preferredtheme 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');
56 my ($theme, $lang, $activethemes)= themelanguage
( $htdocs, $tmplbase, $interface, $query);
58 foreach (@
$activethemes) {
59 push @includes, "$htdocs/$_/$lang/includes";
60 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
62 my $template = Template
->new(
65 PLUGIN_BASE
=> 'Koha::Template::Plugin',
66 COMPILE_EXT
=> C4
::Context
->config('template_cache_dir')?
'.ttc':'',
67 COMPILE_DIR
=> C4
::Context
->config('template_cache_dir')?C4
::Context
->config('template_cache_dir'):'',,
68 INCLUDE_PATH
=> \
@includes,
71 ) or die Template
->error();
73 TEMPLATE
=> $template,
79 $self->activethemes($activethemes);
80 $self->preferredtheme($activethemes->[0]);
81 $self->filename($filename);
82 $self->htdocs($htdocs);
83 $self->interface($interface);
84 $self->{VARS
}->{"test"} = "value";
93 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
94 my $template = $self->{TEMPLATE
};
95 if ( $self->interface eq 'intranet' ) {
96 $vars->{themelang
} = '/intranet-tmpl';
99 $vars->{themelang
} = '/opac-tmpl';
101 $vars->{lang
} = $self->lang;
102 $vars->{themelang
} .= '/' . $self->preferredtheme . '/' . $self->lang;
104 ( C4
::Context
->preference("yuipath") eq "local"
105 ?
( $self->interface eq 'intranet' ?
$vars->{themelang
} . "/lib/yui" : "/opac-tmpl/lib/yui" )
106 : C4
::Context
->preference("yuipath") );
108 ( $self->{interface
} ne 'intranet' ?
'/opac-tmpl' : '/intranet-tmpl' );
109 $vars->{theme
} = $self->theme;
110 $vars->{opaccolorstylesheet
} =
111 C4
::Context
->preference('opaccolorstylesheet');
112 $vars->{opacsmallimage
} = C4
::Context
->preference('opacsmallimage');
113 $vars->{opaclayoutstylesheet
} =
114 C4
::Context
->preference('opaclayoutstylesheet');
116 # add variables set via param to $vars for processing
117 # and clean any utf8 mess
118 for my $k ( keys %{ $self->{VARS
} } ) {
119 $vars->{$k} = $self->{VARS
}->{$k};
120 if (ref($vars->{$k}) eq 'ARRAY'){
121 utf8_arrayref
($vars->{$k});
123 elsif (ref($vars->{$k}) eq 'HASH'){
124 utf8_hashref
($vars->{$k});
127 utf8
::encode
($vars->{$k}) if utf8
::is_utf8
($vars->{$k});
131 # binmode( STDOUT, ":utf8" );
132 $template->process( $self->filename, $vars, \
$data )
133 || die "Template process failed: ", $template->error();
138 my $arrayref = shift;
139 foreach my $element (@
$arrayref){
140 if (ref($element) eq 'ARRAY'){
141 utf8_arrayref
($element);
144 if (ref($element) eq 'HASH'){
145 utf8_hashref
($element);
148 utf8
::encode
($element) if utf8
::is_utf8
($element);
154 for my $key (keys %{$hashref}){
155 if (ref($hashref->{$key}) eq 'ARRAY'){
156 utf8_arrayref
($hashref->{$key});
159 if (ref($hashref->{$key}) eq 'HASH'){
160 utf8_hashref
($hashref->{$key});
163 utf8
::encode
($hashref->{$key}) if utf8
::is_utf8
($hashref->{$key});
168 # FIXME - this is a horrible hack to cache
169 # the current known-good language, temporarily
170 # put in place to resolve bug 4403. It is
171 # used only by C4::XSLT::XSLTParse4Display;
172 # the language is set via the usual call
174 my $_current_language = 'en';
176 sub _current_language
{
177 return $_current_language;
181 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
187 if ( ref($val) eq 'ARRAY' && !scalar @
$val ) { $val = undef; }
188 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
190 $self->{VARS
}->{$key} = $val;
192 warn "Problem = a value of $val has been passed to param without key";
200 C4::Templates - Functions for managing templates
206 # FIXME: this is a quick fix to stop rc1 installing broken
207 # Still trying to figure out the correct fix.
208 my $path = C4
::Context
->config('intrahtdocs') . "/prog/en/includes/";
210 #---------------------------------------------------------------------------------------------------------
213 sub _get_template_file
{
214 my ($tmplbase, $interface, $query) = @_;
216 my $is_intranet = $interface eq 'intranet';
217 my $htdocs = C4
::Context
->config($is_intranet ?
'intrahtdocs' : 'opachtdocs');
218 my ($theme, $lang, $availablethemes) = themelanguage
($htdocs, $tmplbase, $interface, $query);
220 # if the template doesn't exist, load the English one as a last resort
221 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
222 unless (-f
$filename) {
224 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
226 return ($htdocs, $theme, $lang, $filename);
231 my ( $tmplbase, $interface, $query ) = @_;
232 ($query) or warn "no query in gettemplate";
233 my $path = C4
::Context
->preference('intranet_includes') || 'includes';
234 $tmplbase =~ s/\.tmpl$/.tt/;
235 my ($htdocs, $theme, $lang, $filename)
236 = _get_template_file
($tmplbase, $interface, $query);
237 my $template = C4
::Templates
->new($interface, $filename, $tmplbase, $query);
238 # NOTE: Commenting these out rather than deleting them so that those who need
239 # to know how we previously shimmed these directories will be able to understand.
240 # my $is_intranet = $interface eq 'intranet';
242 # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
245 # themelang => $themelang,
246 # yuipath => C4::Context->preference("yuipath") eq "local"
247 # ? "$themelang/lib/yui"
248 # : C4::Context->preference("yuipath"),
249 # interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
255 my $current_lang = regex_lang_subtags
($lang);
257 $bidi = get_bidi
($current_lang->{script
}) if $current_lang->{script
};
259 my $languages_loop = getTranslatedLanguages
($interface,$theme,$lang);
260 my $num_languages_enabled = 0;
261 foreach my $lang (@
$languages_loop) {
262 foreach my $sublang (@
{ $lang->{'sublanguages_loop'} }) {
263 $num_languages_enabled++ if $sublang->{enabled
};
267 languages_loop
=> $languages_loop,
269 one_language_enabled
=> ($num_languages_enabled <= 1) ?
1 : 0, # deal with zero enabled langs as well
270 ) unless @
$languages_loop<2;
276 #---------------------------------------------------------------------------------------------------------
279 my ($htdocs, $tmpl, $interface, $query) = @_;
280 ($query) or warn "no query in themelanguage";
282 # Select a language based on cookie, syspref available languages & browser
283 my $lang = getlanguage
($query, $interface);
286 my $is_intranet = $interface eq 'intranet';
287 my @themes = split(" ", C4
::Context
->preference(
288 $is_intranet ?
"template" : "opacthemes" ));
289 push @themes, 'prog';
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, \
@themes)
298 # Otherwise, return prog theme in English 'en'
299 return ('prog', 'en', \
@themes);
303 sub setlanguagecookie
{
304 my ( $query, $language, $uri ) = @_;
305 my $cookie = $query->cookie(
306 -name
=> 'KohaOpacLanguage',
310 print $query->redirect(
318 my ($query, $interface) = @_;
320 # Select a language based on cookie, syspref available languages & browser
321 my $preference_to_check =
322 $interface eq 'intranet' ?
'language' : 'opaclanguages';
323 my @languages = split /,/, C4
::Context
->preference($preference_to_check);
328 if ( $query->cookie('KohaOpacLanguage') ) {
329 $lang = $query->cookie('KohaOpacLanguage');
330 $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie
333 # HTTP_ACCEPT_LANGUAGE
334 if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE
} ) {
335 $lang = accept_language
( $ENV{HTTP_ACCEPT_LANGUAGE
},
336 getTranslatedLanguages
( $interface, 'prog' ) );
339 # Ignore a lang not selected in sysprefs
340 if ( $lang && any
{ $_ eq $lang } @languages ) {
344 # Fall back to English if necessary