7 use List::MoreUtils qw( uniq );
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
14 # under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # Koha is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 C4::Templates - Object for manipulating templates for use with Koha
32 use base qw(Class::Accessor);
34 use C4::Languages qw( get_bidi getTranslatedLanguages regex_lang_subtags );
38 use Koha::Cache::Memory::Lite;
41 __PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
47 my $interface = shift;
50 my $query = @_? shift: undef;
52 if ( $interface ne "intranet" ) {
53 $htdocs = C4::Context->config('opachtdocs');
56 $htdocs = C4::Context->config('intrahtdocs');
58 my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query);
60 foreach (@$activethemes) {
61 push @includes, "$htdocs/$_/$lang/includes";
62 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
64 # Do not use template cache if script is called from commandline
65 my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
66 my $template = Template->new(
69 PLUGIN_BASE => 'Koha::Template::Plugin',
70 COMPILE_EXT => $use_template_cache ? '.ttc' : '',
71 COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
72 INCLUDE_PATH => \@includes,
76 ) or die Template->error();
78 TEMPLATE => $template,
84 $self->activethemes($activethemes);
85 $self->preferredtheme($activethemes->[0]);
86 $self->filename($filename);
87 $self->htdocs($htdocs);
88 $self->interface($interface);
89 $self->{VARS}->{"test"} = "value";
98 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
99 my $template = $self->{TEMPLATE};
100 if ( $self->interface eq 'intranet' ) {
101 $vars->{themelang} = '/intranet-tmpl';
104 $vars->{themelang} = '/opac-tmpl';
106 $vars->{lang} = $self->lang;
107 $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang;
109 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
110 $vars->{theme} = $self->theme;
111 $vars->{OpacAdditionalStylesheet} =
112 C4::Context->preference('OpacAdditionalStylesheet');
113 $vars->{opaclayoutstylesheet} =
114 C4::Context->preference('opaclayoutstylesheet');
116 if(exists $self->{VARS}{lang}) {
117 warn "Preventing \$template->lang='" . ($self->{vars}{lang}//'-undef-')
118 . "' to be overwritten by template->{VARS}{lang}='" . ($self->{VARS}{lang}//'-undef-') . "'";
119 delete $self->{VARS}{lang};
122 # add variables set via param to $vars for processing
123 $vars = { %$vars, %{ $self->{VARS} } };
126 binmode( STDOUT, ":encoding(UTF-8)" );
127 $template->process( $self->filename, $vars, \$data )
128 || die "Template process failed: ", $template->error();
132 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
138 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
139 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
141 $self->{VARS}->{$key} = $val;
143 warn "Problem = a value of $val has been passed to param without key";
151 C4::Templates - Functions for managing templates
157 # FIXME: this is a quick fix to stop rc1 installing broken
158 # Still trying to figure out the correct fix.
159 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
161 #---------------------------------------------------------------------------------------------------------
164 sub _get_template_file {
165 my ($tmplbase, $interface, $query) = @_;
167 my $is_intranet = $interface eq 'intranet';
168 my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
169 my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
172 $tmplbase = "$htdocs/$theme/$lang/modules/$tmplbase" if $tmplbase !~ /^\//;
173 # do not prefix an absolute path
175 return ( $htdocs, $theme, $lang, $tmplbase );
178 =head2 badtemplatecheck
180 badtemplatecheck( $template_path );
182 The sub will throw an exception if the template path is not allowed.
184 Note: At this moment the sub is actually a helper routine for
189 sub badtemplatecheck {
190 my ( $template ) = @_;
191 if( !$template || $template !~ m/^[a-zA-Z0-9_\-\/]+\.(tt|pref)$/ ) {
192 # This also includes two dots
193 Koha::Exceptions::NoPermission->throw( 'bad template path' );
195 # Check allowed dirs - make sure we operate on a copy of the config
196 my $dirs = C4::Context->config("pluginsdir");
203 unshift @$dirs, C4::Context->config('opachtdocs'), C4::Context->config('intrahtdocs');
205 foreach my $dir ( @$dirs ) {
206 $dir .= '/' if $dir !~ m/\/$/;
207 $found++ if $template =~ m/^$dir/;
210 Koha::Exceptions::NoPermission->throw( 'bad template path' ) if !$found;
215 my ( $tmplbase, $interface, $query ) = @_;
216 my ($htdocs, $theme, $lang, $filename)
217 = _get_template_file($tmplbase, $interface, $query);
218 badtemplatecheck( $filename ); # single trip for bad templates
219 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
221 # NOTE: Commenting these out rather than deleting them so that those who need
222 # to know how we previously shimmed these directories will be able to understand.
223 # my $is_intranet = $interface eq 'intranet';
225 # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
228 # themelang => $themelang,
229 # interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
234 # Bidirectionality, must be sent even if is the only language
235 my $current_lang = regex_lang_subtags($lang);
237 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
242 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
243 my $num_languages_enabled = 0;
244 foreach my $lang (@$languages_loop) {
245 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
246 $num_languages_enabled++ if $sublang->{enabled};
249 my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well
251 languages_loop => $languages_loop,
252 one_language_enabled => $one_language_enabled,
253 ) unless $one_language_enabled;
261 my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query);
263 This function returns the theme and language to be used for rendering the UI.
264 It also returns the list of themes that should be applied as a fallback. This is
265 used for the theme overlay feature (i.e. if a file doesn't exist on the requested
266 theme, fallback to the configured fallback).
268 Important: this function is used on the webinstaller too, so always consider
269 the use case where the DB is not populated already when rewriting/fixing.
274 my ($htdocs, $tmpl, $interface, $query) = @_;
276 # Select a language based on cookie, syspref available languages & browser
277 my $lang = C4::Languages::getlanguage($query);
279 return availablethemes($htdocs, $tmpl, $interface, $lang);
282 sub availablethemes {
283 my ($htdocs, $tmpl, $interface, $lang) = @_;
287 my $theme_syspref = ($interface eq 'intranet') ? 'template' : 'opacthemes';
288 my $fallback_syspref = ($interface eq 'intranet') ? 'template' : 'OPACFallback';
289 # Yeah, hardcoded, last resort if the DB is not populated
290 my $hardcoded_theme = ($interface eq 'intranet') ? 'prog' : 'bootstrap';
292 # Configured theme is the first one
293 push @themes, C4::Context->preference( $theme_syspref )
294 if C4::Context->preference( $theme_syspref );
295 # Configured fallback next
296 push @themes, C4::Context->preference( $fallback_syspref )
297 if C4::Context->preference( $fallback_syspref );
298 # The hardcoded fallback theme is the last one
299 push @themes, $hardcoded_theme;
301 # Try to find first theme for the selected theme/lang, then for fallback/lang
302 my $where = $tmpl =~ /xsl$/ ? 'xslt' : 'modules';
303 for my $theme (@themes) {
304 if ( -e "$htdocs/$theme/$lang/$where/$tmpl" ) {
305 return ( $theme, $lang, [ uniq(@themes) ] );
308 # Otherwise return theme/'en', last resort fallback/'en'
309 for my $theme (@themes) {
310 if ( -e "$htdocs/$theme/en/$where/$tmpl" ) {
311 return ( $theme, 'en', [ uniq(@themes) ] );
314 # tmpl is a full path, so this is a template for a plugin
315 if ( $tmpl =~ /^\// && -e $tmpl ) {
316 return ( $themes[0], $lang, [ uniq(@themes) ] );
320 sub setlanguagecookie {
321 my ( $query, $language, $uri ) = @_;
323 my $cookie = getlanguagecookie( $query, $language );
325 # We do not want to set getlanguage in cache, some additional checks are
326 # done in C4::Languages::getlanguage
327 Koha::Cache::Memory::Lite->get_instance()->clear_from_cache( 'getlanguage' );
329 print $query->redirect(
335 =head2 getlanguagecookie
337 my $cookie = getlanguagecookie($query,$language);
339 Returns a cookie object containing the calculated language to be used.
343 sub getlanguagecookie {
344 my ( $query, $language ) = @_;
345 my $cookie = $query->cookie(
346 -name => 'KohaOpacLanguage',
351 -secure => ( C4::Context->https_enabled() ? 1 : 0 ),