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 INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
65 ) or die Template->error();
67 TEMPLATE => $template,
73 $self->filename($filename);
74 $self->htdocs($htdocs);
75 $self->interface($interface);
76 $self->{VARS}->{"test"} = "value";
85 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
86 my $template = $self->{TEMPLATE};
87 if ( $self->interface eq 'intranet' ) {
88 $vars->{themelang} = '/intranet-tmpl';
91 $vars->{themelang} = '/opac-tmpl';
93 $vars->{lang} = $self->lang;
94 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
96 ( C4::Context->preference("yuipath") eq "local"
97 ? $vars->{themelang} . "/lib/yui"
98 : C4::Context->preference("yuipath") );
100 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
101 $vars->{theme} = $self->theme;
102 $vars->{opaccolorstylesheet} =
103 C4::Context->preference('opaccolorstylesheet');
104 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
105 $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
107 # add variables set via param to $vars for processing
108 # and clean any utf8 mess
109 for my $k ( keys %{ $self->{VARS} } ) {
110 $vars->{$k} = $self->{VARS}->{$k};
111 if (ref($vars->{$k}) eq 'ARRAY'){
112 utf8_arrayref($vars->{$k});
114 elsif (ref($vars->{$k}) eq 'HASH'){
115 utf8_hashref($vars->{$k});
118 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
122 # binmode( STDOUT, ":utf8" );
123 $template->process( $self->filename, $vars, \$data )
124 || die "Template process failed: ", $template->error();
129 my $arrayref = shift;
130 foreach my $element (@$arrayref){
131 if (ref($element) eq 'ARRAY'){
132 utf8_arrayref($element);
135 if (ref($element) eq 'HASH'){
136 utf8_hashref($element);
139 utf8::encode($element) if utf8::is_utf8($element);
145 for my $key (keys %{$hashref}){
146 if (ref($hashref->{$key}) eq 'ARRAY'){
147 utf8_arrayref($hashref->{$key});
150 if (ref($hashref->{$key}) eq 'HASH'){
151 utf8_hashref($hashref->{$key});
154 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
159 # FIXME - this is a horrible hack to cache
160 # the current known-good language, temporarily
161 # put in place to resolve bug 4403. It is
162 # used only by C4::XSLT::XSLTParse4Display;
163 # the language is set via the usual call
165 my $_current_language = 'en';
167 sub _current_language {
168 return $_current_language;
171 sub themelanguage_lite {
172 my ( $htdocs, $tmpl, $interface ) = @_;
175 # Set some defaults for language and theme
176 # First, check the user's preferences
179 # But, if there's a cookie set, obey it
180 $lang = $query->cookie('KohaOpacLanguage')
181 if ( defined $query and $query->cookie('KohaOpacLanguage') );
183 # Fall back to English
185 if ( $interface eq 'intranet' ) {
186 @languages = split ",", C4::Context->preference("language");
189 @languages = split ",", C4::Context->preference("opaclanguages");
192 @languages = ( $lang, @languages );
195 $lang = $languages[0] || 'en';
197 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
199 if ( $interface eq "intranet" ) {
200 @themes = split " ", C4::Context->preference("template");
203 @themes = split " ", C4::Context->preference("opacthemes");
206 # searches through the themes and languages. First template it find it returns.
207 # Priority is for getting the theme right.
209 foreach my $th (@themes) {
210 foreach my $la (@languages) {
211 if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
216 last unless $la =~ /[-_]/;
219 $_current_language = $lang; # FIXME part of bad hack to paper over bug 4403
220 return ( $theme, $lang );
223 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
229 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
230 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
231 $self->{VARS}->{$key} = $val;
238 C4::Templates - Functions for managing templates
244 # FIXME: this is a quick fix to stop rc1 installing broken
245 # Still trying to figure out the correct fix.
246 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
248 #---------------------------------------------------------------------------------------------------------
251 sub _get_template_file {
252 my ( $tmplbase, $interface, $query ) = @_;
253 my $htdocs = C4::Context->config( $interface ne 'intranet' ? 'opachtdocs' : 'intrahtdocs' );
254 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
255 my $opacstylesheet = C4::Context->preference('opacstylesheet');
257 # if the template doesn't exist, load the English one as a last resort
258 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
259 unless (-f $filename) {
261 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
264 return ( $htdocs, $theme, $lang, $filename );
268 my ( $tmplbase, $interface, $query ) = @_;
269 ($query) or warn "no query in gettemplate";
270 my $path = C4::Context->preference('intranet_includes') || 'includes';
271 my $opacstylesheet = C4::Context->preference('opacstylesheet');
272 $tmplbase =~ s/\.tmpl$/.tt/;
273 my ( $htdocs, $theme, $lang, $filename ) = _get_template_file( $tmplbase, $interface, $query );
274 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
275 my $themelang=( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' )
278 themelang => $themelang,
279 yuipath => (C4::Context->preference("yuipath") eq "local"?"$themelang/lib/yui":C4::Context->preference("yuipath")),
280 interface => ( $interface ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' ),
286 my $current_lang = regex_lang_subtags($lang);
288 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
290 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
291 my $num_languages_enabled = 0;
292 foreach my $lang (@$languages_loop) {
293 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
294 $num_languages_enabled++ if $sublang->{enabled};
298 languages_loop => $languages_loop,
300 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
301 ) unless @$languages_loop<2;
307 #---------------------------------------------------------------------------------------------------------
310 my ( $htdocs, $tmpl, $interface, $query ) = @_;
311 ($query) or warn "no query in themelanguage";
313 # Set some defaults for language and theme
314 # First, check the user's preferences
316 my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
317 $lang = accept_language( $http_accept_language,
318 getTranslatedLanguages($interface,'prog') )
319 if $http_accept_language;
320 # But, if there's a cookie set, obey it
321 $lang = $query->cookie('KohaOpacLanguage') if (defined $query and $query->cookie('KohaOpacLanguage'));
322 # Fall back to English
324 if ($interface eq 'intranet') {
325 @languages = split ",", C4::Context->preference("language");
327 @languages = split ",", C4::Context->preference("opaclanguages");
330 @languages=($lang,@languages);
332 $lang = $languages[0];
334 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
335 my $dbh = C4::Context->dbh;
337 if ( $interface eq "intranet" ) {
338 @themes = split " ", C4::Context->preference("template");
341 # we are in the opac here, what im trying to do is let the individual user
342 # set the theme they want to use.
343 # and perhaps the them as well.
344 #my $lang = $query->cookie('KohaOpacLanguage');
345 @themes = split " ", C4::Context->preference("opacthemes");
348 # searches through the themes and languages. First template it find it returns.
349 # Priority is for getting the theme right.
351 foreach my $th (@themes) {
352 foreach my $la (@languages) {
353 #for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) {
354 # warn "$htdocs/$th/$la/modules/$interface-"."tmpl";
355 #$la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2;
356 if ( -e "$htdocs/$th/$la/modules/$tmpl") {
357 #".($interface eq 'intranet'?"modules":"")."/$tmpl" ) {
362 last unless $la =~ /[-_]/;
367 $_current_language = $lang; # FIXME part of bad hack to paper over bug 4403
368 return ( $theme, $lang );
371 sub setlanguagecookie {
372 my ( $query, $language, $uri ) = @_;
373 my $cookie = $query->cookie(
374 -name => 'KohaOpacLanguage',
378 print $query->redirect(
384 sub getlanguagecookie {
387 if ($query->cookie('KohaOpacLanguage')){
388 $lang = $query->cookie('KohaOpacLanguage') ;
390 $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
393 $lang = substr($lang, 0, 2);