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 );
37 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
41 my $interface = shift;
45 if ( $interface ne "intranet" ) {
46 $htdocs = C4::Context->config('opachtdocs');
49 $htdocs = C4::Context->config('intrahtdocs');
52 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface );
53 my $template = Template->new(
57 INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
61 ) or die Template->error();
63 TEMPLATE => $template,
69 $self->filename($filename);
70 $self->htdocs($htdocs);
71 $self->interface($interface);
72 $self->{VARS}->{"test"} = "value";
81 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
82 my $template = $self->{TEMPLATE};
83 if ( $self->interface eq 'intranet' ) {
84 $vars->{themelang} = '/intranet-tmpl';
87 $vars->{themelang} = '/opac-tmpl';
89 $vars->{lang} = $self->lang;
90 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
92 ( C4::Context->preference("yuipath") eq "local"
93 ? $self->{themelang} . "/lib/yui"
94 : C4::Context->preference("yuipath") );
96 ( $vars->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
97 $vars->{theme} = $self->theme;
98 $vars->{opaccolorstylesheet} =
99 C4::Context->preference('opaccolorstylesheet');
100 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
101 $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
103 #add variables set via param to $vars for processing
104 for my $k ( keys %{ $self->{VARS} } ) {
105 $vars->{$k} = $self->{VARS}->{$k};
108 $template->process( $self->filename, $vars, \$data )
109 || die "Template process failed: ", $template->error();
113 # FIXME - this is a horrible hack to cache
114 # the current known-good language, temporarily
115 # put in place to resolve bug 4403. It is
116 # used only by C4::XSLT::XSLTParse4Display;
117 # the language is set via the usual call
119 my $_current_language = 'en';
121 sub _current_language {
122 return $_current_language;
126 my ( $htdocs, $tmpl, $interface ) = @_;
129 # Set some defaults for language and theme
130 # First, check the user's preferences
133 # But, if there's a cookie set, obey it
134 $lang = $query->cookie('KohaOpacLanguage')
135 if ( defined $query and $query->cookie('KohaOpacLanguage') );
137 # Fall back to English
139 if ( $interface eq 'intranet' ) {
140 @languages = split ",", C4::Context->preference("language");
143 @languages = split ",", C4::Context->preference("opaclanguages");
146 @languages = ( $lang, @languages );
149 $lang = $languages[0];
151 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
152 my $dbh = C4::Context->dbh;
154 if ( $interface eq "intranet" ) {
155 @themes = split " ", C4::Context->preference("template");
158 @themes = split " ", C4::Context->preference("opacthemes");
161 # searches through the themes and languages. First template it find it returns.
162 # Priority is for getting the theme right.
164 foreach my $th (@themes) {
165 foreach my $la (@languages) {
166 if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
171 last unless $la =~ /[-_]/;
174 $_current_language = $lang; # FIXME part of bad hack to paper over bug 4403
175 return ( $theme, $lang );
178 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
184 utf8::encode($val) if utf8::is_utf8($val);
185 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
186 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
187 $self->{VARS}->{$key} = $val;