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,
70 ENCODING
=> 'utf8', # templates don't have BOM, see Template::FAQ
72 ) or die Template
->error();
74 TEMPLATE
=> $template,
80 $self->activethemes($activethemes);
81 $self->preferredtheme($activethemes->[0]);
82 $self->filename($filename);
83 $self->htdocs($htdocs);
84 $self->interface($interface);
85 $self->{VARS
}->{"test"} = "value";
94 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
95 my $template = $self->{TEMPLATE
};
96 if ( $self->interface eq 'intranet' ) {
97 $vars->{themelang
} = '/intranet-tmpl';
100 $vars->{themelang
} = '/opac-tmpl';
102 $vars->{lang
} = $self->lang;
103 $vars->{themelang
} .= '/' . $self->preferredtheme . '/' . $self->lang;
105 ( C4
::Context
->preference("yuipath") eq "local"
106 ?
( $self->interface eq 'intranet' ?
$vars->{themelang
} . "/lib/yui" : "/opac-tmpl/lib/yui" )
107 : C4
::Context
->preference("yuipath") );
109 ( $self->{interface
} ne 'intranet' ?
'/opac-tmpl' : '/intranet-tmpl' );
110 $vars->{theme
} = $self->theme;
111 $vars->{opaccolorstylesheet
} =
112 C4
::Context
->preference('opaccolorstylesheet');
113 $vars->{opacsmallimage
} = C4
::Context
->preference('opacsmallimage');
114 $vars->{opaclayoutstylesheet
} =
115 C4
::Context
->preference('opaclayoutstylesheet');
117 # add variables set via param to $vars for processing
118 for my $k ( keys %{ $self->{VARS
} } ) {
119 $vars->{$k} = $self->{VARS
}->{$k};
122 $template->process( $self->filename, $vars, \
$data )
123 || die "Template process failed: ", $template->error();
128 # FIXME - this is a horrible hack to cache
129 # the current known-good language, temporarily
130 # put in place to resolve bug 4403. It is
131 # used only by C4::XSLT::XSLTParse4Display;
132 # the language is set via the usual call
134 my $_current_language = 'en';
136 sub _current_language
{
137 return $_current_language;
141 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
147 if ( ref($val) eq 'ARRAY' && !scalar @
$val ) { $val = undef; }
148 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
150 $self->{VARS
}->{$key} = $val;
152 warn "Problem = a value of $val has been passed to param without key";
160 C4::Templates - Functions for managing templates
166 # FIXME: this is a quick fix to stop rc1 installing broken
167 # Still trying to figure out the correct fix.
168 my $path = C4
::Context
->config('intrahtdocs') . "/prog/en/includes/";
170 #---------------------------------------------------------------------------------------------------------
173 sub _get_template_file
{
174 my ($tmplbase, $interface, $query) = @_;
176 my $is_intranet = $interface eq 'intranet';
177 my $htdocs = C4
::Context
->config($is_intranet ?
'intrahtdocs' : 'opachtdocs');
178 my ($theme, $lang, $availablethemes) = themelanguage
($htdocs, $tmplbase, $interface, $query);
180 # if the template doesn't exist, load the English one as a last resort
181 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
182 unless (-f
$filename) {
184 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
186 return ($htdocs, $theme, $lang, $filename);
191 my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
192 ($query) or warn "no query in gettemplate";
193 my $path = C4
::Context
->preference('intranet_includes') || 'includes';
194 $tmplbase =~ s/\.tmpl$/.tt/;
195 my ($htdocs, $theme, $lang, $filename)
196 = _get_template_file
($tmplbase, $interface, $query);
197 $filename = $tmplbase if ( $is_plugin );
198 my $template = C4
::Templates
->new($interface, $filename, $tmplbase, $query);
200 # NOTE: Commenting these out rather than deleting them so that those who need
201 # to know how we previously shimmed these directories will be able to understand.
202 # my $is_intranet = $interface eq 'intranet';
204 # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
207 # themelang => $themelang,
208 # yuipath => C4::Context->preference("yuipath") eq "local"
209 # ? "$themelang/lib/yui"
210 # : C4::Context->preference("yuipath"),
211 # interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
217 my $current_lang = regex_lang_subtags
($lang);
219 $bidi = get_bidi
($current_lang->{script
}) if $current_lang->{script
};
221 my $languages_loop = getTranslatedLanguages
($interface,$theme,$lang);
222 my $num_languages_enabled = 0;
223 foreach my $lang (@
$languages_loop) {
224 foreach my $sublang (@
{ $lang->{'sublanguages_loop'} }) {
225 $num_languages_enabled++ if $sublang->{enabled
};
229 languages_loop
=> $languages_loop,
231 one_language_enabled
=> ($num_languages_enabled <= 1) ?
1 : 0, # deal with zero enabled langs as well
232 ) unless @
$languages_loop<2;
238 #---------------------------------------------------------------------------------------------------------
241 my ($htdocs, $tmpl, $interface, $query) = @_;
242 ($query) or warn "no query in themelanguage";
244 # Select a language based on cookie, syspref available languages & browser
245 my $lang = getlanguage
($query, $interface);
248 my $is_intranet = $interface eq 'intranet';
249 my @themes = split(" ", C4
::Context
->preference(
250 $is_intranet ?
"template" : "opacthemes" ));
251 push @themes, 'prog';
253 # Try to find first theme for the selected language
254 for my $theme (@themes) {
255 if ( -e
"$htdocs/$theme/$lang/modules/$tmpl" ) {
256 $_current_language = $lang;
257 return ($theme, $lang, \
@themes)
260 # Otherwise, return prog theme in English 'en'
261 return ('prog', 'en', \
@themes);
265 sub setlanguagecookie
{
266 my ( $query, $language, $uri ) = @_;
267 my $cookie = $query->cookie(
268 -name
=> 'KohaOpacLanguage',
273 print $query->redirect(
281 my ($query, $interface) = @_;
283 # Select a language based on cookie, syspref available languages & browser
284 my $preference_to_check =
285 $interface eq 'intranet' ?
'language' : 'opaclanguages';
286 my @languages = split /,/, C4
::Context
->preference($preference_to_check);
291 if ( $query->cookie('KohaOpacLanguage') ) {
292 $lang = $query->cookie('KohaOpacLanguage');
293 $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie
296 # HTTP_ACCEPT_LANGUAGE
297 if ( !$lang && $ENV{HTTP_ACCEPT_LANGUAGE
} ) {
298 $lang = accept_language
( $ENV{HTTP_ACCEPT_LANGUAGE
},
299 getTranslatedLanguages
( $interface, 'prog' ) );
302 # Ignore a lang not selected in sysprefs
303 if ( $lang && any
{ $_ eq $lang } @languages ) {
307 # Fall back to English if necessary