7 use List
::MoreUtils qw
/ any 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 Template
::Constants
qw( :debug );
35 use C4
::Languages
qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language );
39 use Koha
::Cache
::Memory
::Lite
;
42 __PACKAGE__
->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
48 my $interface = shift;
51 my $query = @_?
shift: undef;
53 if ( $interface ne "intranet" ) {
54 $htdocs = C4
::Context
->config('opachtdocs');
57 $htdocs = C4
::Context
->config('intrahtdocs');
59 my ($theme, $lang, $activethemes)= themelanguage
( $htdocs, $tmplbase, $interface, $query);
61 foreach (@
$activethemes) {
62 push @includes, "$htdocs/$_/$lang/includes";
63 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
65 # Do not use template cache if script is called from commandline
66 my $use_template_cache = C4
::Context
->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE
};
67 my $template = Template
->new(
70 PLUGIN_BASE
=> 'Koha::Template::Plugin',
71 COMPILE_EXT
=> $use_template_cache ?
'.ttc' : '',
72 COMPILE_DIR
=> $use_template_cache ? C4
::Context
->config('template_cache_dir') : '',
73 INCLUDE_PATH
=> \
@includes,
77 ) or die Template
->error();
79 TEMPLATE
=> $template,
85 $self->activethemes($activethemes);
86 $self->preferredtheme($activethemes->[0]);
87 $self->filename($filename);
88 $self->htdocs($htdocs);
89 $self->interface($interface);
90 $self->{VARS
}->{"test"} = "value";
99 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
100 my $template = $self->{TEMPLATE
};
101 if ( $self->interface eq 'intranet' ) {
102 $vars->{themelang
} = '/intranet-tmpl';
105 $vars->{themelang
} = '/opac-tmpl';
107 $vars->{lang
} = $self->lang;
108 $vars->{themelang
} .= '/' . $self->preferredtheme . '/' . $self->lang;
110 ( $self->{interface
} ne 'intranet' ?
'/opac-tmpl' : '/intranet-tmpl' );
111 $vars->{theme
} = $self->theme;
112 $vars->{OpacAdditionalStylesheet
} =
113 C4
::Context
->preference('OpacAdditionalStylesheet');
114 $vars->{opaclayoutstylesheet
} =
115 C4
::Context
->preference('opaclayoutstylesheet');
117 # add variables set via param to $vars for processing
118 $vars = { %$vars, %{ $self->{VARS
} } };
121 binmode( STDOUT
, ":utf8" );
122 $template->process( $self->filename, $vars, \
$data )
123 || die "Template process failed: ", $template->error();
127 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
133 if ( ref($val) eq 'ARRAY' && !scalar @
$val ) { $val = undef; }
134 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
136 $self->{VARS
}->{$key} = $val;
138 warn "Problem = a value of $val has been passed to param without key";
146 C4::Templates - Functions for managing templates
152 # FIXME: this is a quick fix to stop rc1 installing broken
153 # Still trying to figure out the correct fix.
154 my $path = C4
::Context
->config('intrahtdocs') . "/prog/en/includes/";
156 #---------------------------------------------------------------------------------------------------------
159 sub _get_template_file
{
160 my ($tmplbase, $interface, $query) = @_;
162 my $is_intranet = $interface eq 'intranet';
163 my $htdocs = C4
::Context
->config($is_intranet ?
'intrahtdocs' : 'opachtdocs');
164 my ($theme, $lang, $availablethemes) = themelanguage
($htdocs, $tmplbase, $interface, $query);
167 $tmplbase = "$htdocs/$theme/$lang/modules/$tmplbase" if $tmplbase !~ /^\
//;
168 # do not prefix an absolute path
170 return ( $htdocs, $theme, $lang, $tmplbase );
173 =head2 badtemplatecheck
175 badtemplatecheck( $template_path );
177 The sub will throw an exception if the template path is not allowed.
179 Note: At this moment the sub is actually a helper routine for
184 sub badtemplatecheck
{
185 my ( $template ) = @_;
186 if( !$template || $template !~ m/^[a-zA-Z0-9_\-\/]+\
.(tt
|pref
)$/ ) {
187 # This also includes two dots
188 Koha
::Exceptions
::NoPermission
->throw( 'bad template path' );
191 my $dirs = C4
::Context
->config("pluginsdir");
192 $dirs = [ $dirs ] if !ref($dirs);
193 unshift @
$dirs, C4
::Context
->config('opachtdocs'), C4
::Context
->config('intrahtdocs');
195 foreach my $dir ( @
$dirs ) {
196 $dir .= '/' if $dir !~ m
/\
/$/;
197 $found++ if $template =~ m/^$dir/;
200 Koha
::Exceptions
::NoPermission
->throw( 'bad template path' ) if !$found;
205 my ( $tmplbase, $interface, $query ) = @_;
206 ($query) or warn "no query in gettemplate";
207 my ($htdocs, $theme, $lang, $filename)
208 = _get_template_file
($tmplbase, $interface, $query);
209 badtemplatecheck
( $filename ); # single trip for bad templates
210 my $template = C4
::Templates
->new($interface, $filename, $tmplbase, $query);
212 # NOTE: Commenting these out rather than deleting them so that those who need
213 # to know how we previously shimmed these directories will be able to understand.
214 # my $is_intranet = $interface eq 'intranet';
216 # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
219 # themelang => $themelang,
220 # interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
225 # Bidirectionality, must be sent even if is the only language
226 my $current_lang = regex_lang_subtags
($lang);
228 $bidi = get_bidi
($current_lang->{script
}) if $current_lang->{script
};
233 my $languages_loop = getTranslatedLanguages
($interface,$theme,$lang);
234 my $num_languages_enabled = 0;
235 foreach my $lang (@
$languages_loop) {
236 foreach my $sublang (@
{ $lang->{'sublanguages_loop'} }) {
237 $num_languages_enabled++ if $sublang->{enabled
};
240 my $one_language_enabled = ($num_languages_enabled <= 1) ?
1 : 0; # deal with zero enabled langs as well
242 languages_loop
=> $languages_loop,
243 one_language_enabled
=> $one_language_enabled,
244 ) unless $one_language_enabled;
246 KOHA_VERSION
=> C4
::Context
->preference('Version')
255 my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query);
257 This function returns the theme and language to be used for rendering the UI.
258 It also returns the list of themes that should be applied as a fallback. This is
259 used for the theme overlay feature (i.e. if a file doesn't exist on the requested
260 theme, fallback to the configured fallback).
262 Important: this function is used on the webinstaller too, so always consider
263 the use case where the DB is not populated already when rewriting/fixing.
268 my ($htdocs, $tmpl, $interface, $query) = @_;
269 ($query) or warn "no query in themelanguage";
271 # Select a language based on cookie, syspref available languages & browser
272 my $lang = C4
::Languages
::getlanguage
($query);
276 my $theme_syspref = ($interface eq 'intranet') ?
'template' : 'opacthemes';
277 my $fallback_syspref = ($interface eq 'intranet') ?
'template' : 'OPACFallback';
278 # Yeah, hardcoded, last resort if the DB is not populated
279 my $hardcoded_theme = ($interface eq 'intranet') ?
'prog' : 'bootstrap';
281 # Configured theme is the first one
282 push @themes, C4
::Context
->preference( $theme_syspref )
283 if C4
::Context
->preference( $theme_syspref );
284 # Configured fallback next
285 push @themes, C4
::Context
->preference( $fallback_syspref )
286 if C4
::Context
->preference( $fallback_syspref );
287 # The hardcoded fallback theme is the last one
288 push @themes, $hardcoded_theme;
290 # Try to find first theme for the selected theme/lang, then for fallback/lang
291 my $where = $tmpl =~ /xsl$/ ?
'xslt' : 'modules';
292 for my $theme (@themes) {
293 if ( -e
"$htdocs/$theme/$lang/$where/$tmpl" ) {
294 return ( $theme, $lang, [ uniq
(@themes) ] );
297 # Otherwise return theme/'en', last resort fallback/'en'
298 for my $theme (@themes) {
299 if ( -e
"$htdocs/$theme/en/$where/$tmpl" ) {
300 return ( $theme, 'en', [ uniq
(@themes) ] );
303 # tmpl is a full path, so this is a template for a plugin
304 if ( $tmpl =~ /^\// && -e
$tmpl ) {
305 return ( $themes[0], $lang, [ uniq
(@themes) ] );
310 sub setlanguagecookie
{
311 my ( $query, $language, $uri ) = @_;
313 my $cookie = getlanguagecookie
( $query, $language );
315 # We do not want to set getlanguage in cache, some additional checks are
316 # done in C4::Languages::getlanguage
317 Koha
::Cache
::Memory
::Lite
->get_instance()->clear_from_cache( 'getlanguage' );
319 print $query->redirect(
325 =head2 getlanguagecookie
327 my $cookie = getlanguagecookie($query,$language);
329 Returns a cookie object containing the calculated language to be used.
333 sub getlanguagecookie
{
334 my ( $query, $language ) = @_;
335 my $cookie = $query->cookie(
336 -name
=> 'KohaOpacLanguage',
347 my $columns = GetColumnDefs( $cgi )
349 It is passed a CGI object and returns a hash of hashes containing
350 the column names and descriptions for each table defined in the
351 columns.def file corresponding to the CGI object.
361 my $htdocs = C4
::Context
->config('intrahtdocs');
362 my $columns_file = 'columns.def';
364 # Get theme and language to build the path to columns.def
365 my ($theme, $lang, $availablethemes) =
366 themelanguage
($htdocs, 'about.tt', 'intranet', $query);
367 # Build columns.def path
368 my $path = "$htdocs/$theme/$lang/$columns_file";
370 if ( ! open ( $fh, q{<:encoding(utf-8)}, $path ) ) {
371 carp
"Error opening $path. Check your templates.";
374 # Loop through the columns.def file
375 while ( my $input = <$fh> ){
377 if ( $input =~ m
|<field name
="(.*)">(.*)</field
>| ) {
378 my ( $table, $column ) = split( '\.', $1);
379 my $description = $2;
380 # Initialize the table array if needed.
381 @
{$columns->{ $table }} = () if ! defined $columns->{ $table };
382 # Push field and description
383 push @
{$columns->{ $table }},
384 { field
=> $column, description
=> $description };