8 # Copyright 2009 Chris Cormack and The Koha Dev Team
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
27 Koha::Templates - Object for manipulating templates for use with Koha
31 use base qw(Class::Accessor);
33 use Template::Constants qw( :debug );
34 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
38 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
44 my $interface = shift;
47 my $query = @_? shift: undef;
49 if ( $interface ne "intranet" ) {
50 $htdocs = C4::Context->config('opachtdocs');
53 $htdocs = C4::Context->config('intrahtdocs');
56 my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query);
57 my $template = Template->new(
61 "$htdocs/$theme/$lang/includes",
62 "$htdocs/$theme/en/includes"
66 ) or die Template->error();
68 TEMPLATE => $template,
74 $self->filename($filename);
75 $self->htdocs($htdocs);
76 $self->interface($interface);
77 $self->{VARS}->{"test"} = "value";
86 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
87 my $template = $self->{TEMPLATE};
88 if ( $self->interface eq 'intranet' ) {
89 $vars->{themelang} = '/intranet-tmpl';
92 $vars->{themelang} = '/opac-tmpl';
94 $vars->{lang} = $self->lang;
95 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
97 ( C4::Context->preference("yuipath") eq "local"
98 ? $vars->{themelang} . "/lib/yui"
99 : C4::Context->preference("yuipath") );
101 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
102 $vars->{theme} = $self->theme;
103 $vars->{opaccolorstylesheet} =
104 C4::Context->preference('opaccolorstylesheet');
105 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
106 $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
108 # add variables set via param to $vars for processing
109 # and clean any utf8 mess
110 for my $k ( keys %{ $self->{VARS} } ) {
111 $vars->{$k} = $self->{VARS}->{$k};
112 if (ref($vars->{$k}) eq 'ARRAY'){
113 utf8_arrayref($vars->{$k});
115 elsif (ref($vars->{$k}) eq 'HASH'){
116 utf8_hashref($vars->{$k});
119 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
123 # binmode( STDOUT, ":utf8" );
124 $template->process( $self->filename, $vars, \$data )
125 || die "Template process failed: ", $template->error();
130 my $arrayref = shift;
131 foreach my $element (@$arrayref){
132 if (ref($element) eq 'ARRAY'){
133 utf8_arrayref($element);
136 if (ref($element) eq 'HASH'){
137 utf8_hashref($element);
140 utf8::encode($element) if utf8::is_utf8($element);
146 for my $key (keys %{$hashref}){
147 if (ref($hashref->{$key}) eq 'ARRAY'){
148 utf8_arrayref($hashref->{$key});
151 if (ref($hashref->{$key}) eq 'HASH'){
152 utf8_hashref($hashref->{$key});
155 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
160 # FIXME - this is a horrible hack to cache
161 # the current known-good language, temporarily
162 # put in place to resolve bug 4403. It is
163 # used only by C4::XSLT::XSLTParse4Display;
164 # the language is set via the usual call
166 my $_current_language = 'en';
168 sub _current_language {
169 return $_current_language;
172 sub themelanguage_lite {
173 my ( $htdocs, $tmpl, $interface ) = @_;
176 # Set some defaults for language and theme
177 # First, check the user's preferences
180 # But, if there's a cookie set, obey it
181 $lang = $query->cookie('KohaOpacLanguage')
182 if ( defined $query and $query->cookie('KohaOpacLanguage') );
184 # Fall back to English
186 if ( $interface eq 'intranet' ) {
187 @languages = split ",", C4::Context->preference("language");
190 @languages = split ",", C4::Context->preference("opaclanguages");
193 @languages = ( $lang, @languages );
196 $lang = $languages[0] || 'en';
198 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
200 if ( $interface eq "intranet" ) {
201 @themes = split " ", C4::Context->preference("template");
204 @themes = split " ", C4::Context->preference("opacthemes");
207 # searches through the themes and languages. First template it find it returns.
208 # Priority is for getting the theme right.
210 foreach my $th (@themes) {
211 foreach my $la (@languages) {
212 if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
217 last unless $la =~ /[-_]/;
220 $_current_language = $lang; # FIXME part of bad hack to paper over bug 4403
221 return ( $theme, $lang );
224 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
230 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
231 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
232 $self->{VARS}->{$key} = $val;
239 C4::Templates - Functions for managing templates
245 # FIXME: this is a quick fix to stop rc1 installing broken
246 # Still trying to figure out the correct fix.
247 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
249 #---------------------------------------------------------------------------------------------------------
252 sub _get_template_file {
253 my ( $tmplbase, $interface, $query ) = @_;
254 my $htdocs = C4::Context->config( $interface ne 'intranet' ? 'opachtdocs' : 'intrahtdocs' );
255 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
256 my $opacstylesheet = C4::Context->preference('opacstylesheet');
258 # if the template doesn't exist, load the English one as a last resort
259 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
260 unless (-f $filename) {
262 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
265 return ( $htdocs, $theme, $lang, $filename );
269 my ( $tmplbase, $interface, $query ) = @_;
270 ($query) or warn "no query in gettemplate";
271 my $path = C4::Context->preference('intranet_includes') || 'includes';
272 my $opacstylesheet = C4::Context->preference('opacstylesheet');
273 $tmplbase =~ s/\.tmpl$/.tt/;
274 my ( $htdocs, $theme, $lang, $filename ) = _get_template_file( $tmplbase, $interface, $query );
275 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
276 my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
279 themelang => $themelang,
280 yuipath => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
281 interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
287 my $current_lang = regex_lang_subtags($lang);
289 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
291 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
292 my $num_languages_enabled = 0;
293 foreach my $lang (@$languages_loop) {
294 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
295 $num_languages_enabled++ if $sublang->{enabled};
299 languages_loop => $languages_loop,
301 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
302 ) unless @$languages_loop<2;
308 #---------------------------------------------------------------------------------------------------------
311 my ( $htdocs, $tmpl, $interface, $query ) = @_;
312 ($query) or warn "no query in themelanguage";
314 # Set some defaults for language and theme
315 # First, check the user's preferences
317 my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
318 $lang = accept_language( $http_accept_language,
319 getTranslatedLanguages($interface,'prog') )
320 if $http_accept_language;
321 # But, if there's a cookie set, obey it
322 $lang = $query->cookie('KohaOpacLanguage') if (defined $query and $query->cookie('KohaOpacLanguage'));
323 # Fall back to English
325 if ($interface eq 'intranet') {
326 @languages = split ",", C4::Context->preference("language");
328 @languages = split ",", C4::Context->preference("opaclanguages");
331 @languages=($lang,@languages);
333 $lang = $languages[0];
335 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
336 my $dbh = C4::Context->dbh;
338 if ( $interface eq "intranet" ) {
339 @themes = split " ", C4::Context->preference("template");
342 # we are in the opac here, what im trying to do is let the individual user
343 # set the theme they want to use.
344 # and perhaps the them as well.
345 #my $lang = $query->cookie('KohaOpacLanguage');
346 @themes = split " ", C4::Context->preference("opacthemes");
349 # searches through the themes and languages. First template it find it returns.
350 # Priority is for getting the theme right.
352 foreach my $th (@themes) {
353 foreach my $la (@languages) {
354 #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
355 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
356 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
357 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
358 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
363 last unless $la =~ /[-_]/;
368 $_current_language = $lang; # FIXME part of bad hack to paper over bug 4403
369 return ( $theme, $lang );
372 sub setlanguagecookie {
373 my ( $query, $language, $uri ) = @_;
374 my $cookie = $query->cookie(
375 -name => 'KohaOpacLanguage',
379 print $query->redirect(
385 sub getlanguagecookie {
388 if ($query->cookie('KohaOpacLanguage')){
389 $lang = $query->cookie('KohaOpacLanguage') ;
391 $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
394 $lang = substr($lang, 0, 2);