Merge branch 'bug_8200' into 3.12-master
[koha.git] / C4 / Templates.pm
blob7f50143279383b0d4d612fb624f4c6e2240ec4ca
1 package C4::Templates;
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
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
16 # version.
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
26 =head1 NAME
28 Koha::Templates - Object for manipulating templates for use with Koha
30 =cut
32 use base qw(Class::Accessor);
33 use Template;
34 use Template::Constants qw( :debug );
35 use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
37 use C4::Context;
39 __PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
43 sub new {
44 my $class = shift;
45 my $interface = shift;
46 my $filename = shift;
47 my $tmplbase = shift;
48 my $query = @_? shift: undef;
49 my $htdocs;
50 if ( $interface ne "intranet" ) {
51 $htdocs = C4::Context->config('opachtdocs');
53 else {
54 $htdocs = C4::Context->config('intrahtdocs');
56 my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query);
57 my @includes;
58 foreach (@$activethemes) {
59 push @includes, "$htdocs/$_/$lang/includes";
60 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
62 my $template = Template->new(
63 { EVAL_PERL => 1,
64 ABSOLUTE => 1,
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,
69 FILTERS => {},
70 ENCODING => 'utf8', # templates don't have BOM, see Template::FAQ
72 ) or die Template->error();
73 my $self = {
74 TEMPLATE => $template,
75 VARS => {},
77 bless $self, $class;
78 $self->theme($theme);
79 $self->lang($lang);
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";
86 return $self;
90 sub output {
91 my $self = shift;
92 my $vars = shift;
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';
99 else {
100 $vars->{themelang} = '/opac-tmpl';
102 $vars->{lang} = $self->lang;
103 $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang;
104 $vars->{yuipath} =
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") );
108 $vars->{interface} =
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};
121 my $data;
122 $template->process( $self->filename, $vars, \$data )
123 || die "Template process failed: ", $template->error();
124 return $data;
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
133 # to themelanguage.
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
142 sub param {
143 my $self = shift;
144 while (@_) {
145 my $key = shift;
146 my $val = shift;
147 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
148 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
149 if ( $key ) {
150 $self->{VARS}->{$key} = $val;
151 } else {
152 warn "Problem = a value of $val has been passed to param without key";
158 =head1 NAME
160 C4::Templates - Functions for managing templates
162 =head1 FUNCTIONS
164 =cut
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 #---------------------------------------------------------------------------------------------------------
171 # FIXME - POD
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) {
183 $lang = 'en';
184 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
186 return ($htdocs, $theme, $lang, $filename);
190 sub gettemplate {
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';
203 # my $themelang =
204 # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
205 # "/$theme/$lang";
206 # $template->param(
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',
212 # theme => $theme,
213 # lang => $lang
214 # );
216 # Bidirectionality
217 my $current_lang = regex_lang_subtags($lang);
218 my $bidi;
219 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
220 # Languages
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};
228 $template->param(
229 languages_loop => $languages_loop,
230 bidi => $bidi,
231 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
232 ) unless @$languages_loop<2;
234 return $template;
238 #---------------------------------------------------------------------------------------------------------
239 # FIXME - POD
240 sub themelanguage {
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);
247 # Select theme
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',
269 -value => $language,
270 -HttpOnly => 1,
271 -expires => '+3y'
273 print $query->redirect(
274 -uri => $uri,
275 -cookie => $cookie
280 sub getlanguage {
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);
288 my $lang;
290 # cookie
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 ) {
304 return $lang;
307 # Fall back to English if necessary
308 return 'en';