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 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)= themelanguage( $htdocs, $tmplbase, $interface, $query);
57 my $template = Template->new(
60 PLUGIN_BASE => 'Koha::Template::Plugin',
61 COMPILE_EXT => C4::Context->config('template_cache_dir')?'.ttc':'',
62 COMPILE_DIR => C4::Context->config('template_cache_dir')?C4::Context->config('template_cache_dir'):'',,
64 "$htdocs/$theme/$lang/includes",
65 "$htdocs/$theme/en/includes"
69 ) or die Template->error();
71 TEMPLATE => $template,
77 $self->filename($filename);
78 $self->htdocs($htdocs);
79 $self->interface($interface);
80 $self->{VARS}->{"test"} = "value";
89 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
90 my $template = $self->{TEMPLATE};
91 if ( $self->interface eq 'intranet' ) {
92 $vars->{themelang} = '/intranet-tmpl';
95 $vars->{themelang} = '/opac-tmpl';
97 $vars->{lang} = $self->lang;
98 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
100 ( C4::Context->preference("yuipath") eq "local"
101 ? $vars->{themelang} . "/lib/yui"
102 : C4::Context->preference("yuipath") );
104 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
105 $vars->{theme} = $self->theme;
106 $vars->{opaccolorstylesheet} =
107 C4::Context->preference('opaccolorstylesheet');
108 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
109 $vars->{opaclayoutstylesheet} =
110 C4::Context->preference('opaclayoutstylesheet');
112 # add variables set via param to $vars for processing
113 # and clean any utf8 mess
114 for my $k ( keys %{ $self->{VARS} } ) {
115 $vars->{$k} = $self->{VARS}->{$k};
116 if (ref($vars->{$k}) eq 'ARRAY'){
117 utf8_arrayref($vars->{$k});
119 elsif (ref($vars->{$k}) eq 'HASH'){
120 utf8_hashref($vars->{$k});
123 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
127 # binmode( STDOUT, ":utf8" );
128 $template->process( $self->filename, $vars, \$data )
129 || die "Template process failed: ", $template->error();
134 my $arrayref = shift;
135 foreach my $element (@$arrayref){
136 if (ref($element) eq 'ARRAY'){
137 utf8_arrayref($element);
140 if (ref($element) eq 'HASH'){
141 utf8_hashref($element);
144 utf8::encode($element) if utf8::is_utf8($element);
150 for my $key (keys %{$hashref}){
151 if (ref($hashref->{$key}) eq 'ARRAY'){
152 utf8_arrayref($hashref->{$key});
155 if (ref($hashref->{$key}) eq 'HASH'){
156 utf8_hashref($hashref->{$key});
159 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
164 # FIXME - this is a horrible hack to cache
165 # the current known-good language, temporarily
166 # put in place to resolve bug 4403. It is
167 # used only by C4::XSLT::XSLTParse4Display;
168 # the language is set via the usual call
170 my $_current_language = 'en';
172 sub _current_language {
173 return $_current_language;
177 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
183 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
184 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
186 $self->{VARS}->{$key} = $val;
188 warn "Problem = a value of $val has been passed to param without key";
196 C4::Templates - Functions for managing templates
202 # FIXME: this is a quick fix to stop rc1 installing broken
203 # Still trying to figure out the correct fix.
204 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
206 #---------------------------------------------------------------------------------------------------------
209 sub _get_template_file {
210 my ($tmplbase, $interface, $query) = @_;
212 my $is_intranet = $interface eq 'intranet';
213 my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
214 my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $interface, $query);
216 # if the template doesn't exist, load the English one as a last resort
217 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
218 unless (-f $filename) {
220 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
222 return ($htdocs, $theme, $lang, $filename);
227 my ( $tmplbase, $interface, $query ) = @_;
228 ($query) or warn "no query in gettemplate";
229 my $path = C4::Context->preference('intranet_includes') || 'includes';
230 $tmplbase =~ s/\.tmpl$/.tt/;
231 my ($htdocs, $theme, $lang, $filename)
232 = _get_template_file($tmplbase, $interface, $query);
233 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
234 my $is_intranet = $interface eq 'intranet';
236 ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
239 themelang => $themelang,
240 yuipath => C4::Context->preference("yuipath") eq "local"
241 ? "$themelang/lib/yui"
242 : C4::Context->preference("yuipath"),
243 interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
249 my $current_lang = regex_lang_subtags($lang);
251 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
253 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
254 my $num_languages_enabled = 0;
255 foreach my $lang (@$languages_loop) {
256 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
257 $num_languages_enabled++ if $sublang->{enabled};
261 languages_loop => $languages_loop,
263 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
264 ) unless @$languages_loop<2;
270 #---------------------------------------------------------------------------------------------------------
273 my ($htdocs, $tmpl, $interface, $query) = @_;
274 ($query) or warn "no query in themelanguage";
276 # Select a language based on cookie, syspref available languages & browser
277 my $lang = getlanguage($query, $interface);
280 my $is_intranet = $interface eq 'intranet';
281 my @themes = split(" ", C4::Context->preference(
282 $is_intranet ? "template" : "opacthemes" ));
283 push @themes, 'prog';
285 # Try to find first theme for the selected language
286 for my $theme (@themes) {
287 if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
288 $_current_language = $lang;
289 return ($theme, $lang)
292 # Otherwise, return prog theme in English 'en'
293 return ('prog', 'en');
297 sub setlanguagecookie {
298 my ( $query, $language, $uri ) = @_;
299 my $cookie = $query->cookie(
300 -name => 'KohaOpacLanguage',
304 print $query->redirect(
312 my ($query, $interface) = @_;
314 # Select a language based on cookie, syspref available languages & browser
315 my $preference_to_check =
316 $interface eq 'intranet' ? 'language' : 'opaclanguages';
317 my @languages = split /,/, C4::Context->preference($preference_to_check);
322 if ( $query->cookie('KohaOpacLanguage') ) {
323 $lang = $query->cookie('KohaOpacLanguage');
324 $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie
327 # HTTP_ACCEPT_LANGUAGE
328 if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE} ) {
329 $lang = accept_language( $ENV{HTTP_ACCEPT_LANGUAGE},
330 getTranslatedLanguages( $interface, 'prog' ) );
333 # Ignore a lang not selected in sysprefs
334 if ( $lang && any { $_ eq $lang } @languages ) {
338 # Fall back to English if necessary