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 Koha::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
;
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 # add variables set via param to $vars for processing
117 $vars = { %$vars, %{ $self->{VARS
} } };
120 binmode( STDOUT
, ":utf8" );
121 $template->process( $self->filename, $vars, \
$data )
122 || die "Template process failed: ", $template->error();
126 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
132 if ( ref($val) eq 'ARRAY' && !scalar @
$val ) { $val = undef; }
133 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
135 $self->{VARS
}->{$key} = $val;
137 warn "Problem = a value of $val has been passed to param without key";
145 C4::Templates - Functions for managing templates
151 # FIXME: this is a quick fix to stop rc1 installing broken
152 # Still trying to figure out the correct fix.
153 my $path = C4
::Context
->config('intrahtdocs') . "/prog/en/includes/";
155 #---------------------------------------------------------------------------------------------------------
158 sub _get_template_file
{
159 my ($tmplbase, $interface, $query) = @_;
161 my $is_intranet = $interface eq 'intranet';
162 my $htdocs = C4
::Context
->config($is_intranet ?
'intrahtdocs' : 'opachtdocs');
163 my ($theme, $lang, $availablethemes) = themelanguage
($htdocs, $tmplbase, $interface, $query);
165 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
167 return ($htdocs, $theme, $lang, $filename);
172 my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
173 ($query) or warn "no query in gettemplate";
174 die "bad template path" unless $tmplbase =~ m/^[a-zA-Z0-9_\-\/]+\
.(tt
|pref
)$/; # Will be extended on bug 17989
175 my $path = C4
::Context
->preference('intranet_includes') || 'includes';
176 my ($htdocs, $theme, $lang, $filename)
177 = _get_template_file
($tmplbase, $interface, $query);
178 $filename = $tmplbase if ( $is_plugin );
179 my $template = C4
::Templates
->new($interface, $filename, $tmplbase, $query);
181 # NOTE: Commenting these out rather than deleting them so that those who need
182 # to know how we previously shimmed these directories will be able to understand.
183 # my $is_intranet = $interface eq 'intranet';
185 # ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
188 # themelang => $themelang,
189 # interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
194 # Bidirectionality, must be sent even if is the only language
195 my $current_lang = regex_lang_subtags
($lang);
197 $bidi = get_bidi
($current_lang->{script
}) if $current_lang->{script
};
202 my $languages_loop = getTranslatedLanguages
($interface,$theme,$lang);
203 my $num_languages_enabled = 0;
204 foreach my $lang (@
$languages_loop) {
205 foreach my $sublang (@
{ $lang->{'sublanguages_loop'} }) {
206 $num_languages_enabled++ if $sublang->{enabled
};
209 my $one_language_enabled = ($num_languages_enabled <= 1) ?
1 : 0; # deal with zero enabled langs as well
211 languages_loop
=> $languages_loop,
212 one_language_enabled
=> $one_language_enabled,
213 ) unless $one_language_enabled;
221 my ($theme,$lang,\@themes) = themelanguage($htdocs,$tmpl,$interface,query);
223 This function returns the theme and language to be used for rendering the UI.
224 It also returns the list of themes that should be applied as a fallback. This is
225 used for the theme overlay feature (i.e. if a file doesn't exist on the requested
226 theme, fallback to the configured fallback).
228 Important: this function is used on the webinstaller too, so always consider
229 the use case where the DB is not populated already when rewriting/fixing.
234 my ($htdocs, $tmpl, $interface, $query) = @_;
235 ($query) or warn "no query in themelanguage";
237 # Select a language based on cookie, syspref available languages & browser
238 my $lang = C4
::Languages
::getlanguage
($query);
242 my $theme_syspref = ($interface eq 'intranet') ?
'template' : 'opacthemes';
243 my $fallback_syspref = ($interface eq 'intranet') ?
'template' : 'OPACFallback';
244 # Yeah, hardcoded, last resort if the DB is not populated
245 my $hardcoded_theme = ($interface eq 'intranet') ?
'prog' : 'bootstrap';
247 # Configured theme is the first one
248 push @themes, C4
::Context
->preference( $theme_syspref )
249 if C4
::Context
->preference( $theme_syspref );
250 # Configured fallback next
251 push @themes, C4
::Context
->preference( $fallback_syspref )
252 if C4
::Context
->preference( $fallback_syspref );
253 # The hardcoded fallback theme is the last one
254 push @themes, $hardcoded_theme;
256 # Try to find first theme for the selected theme/lang, then for fallback/lang
257 my $where = $tmpl =~ /xsl$/ ?
'xslt' : 'modules';
258 for my $theme (@themes) {
259 if ( -e
"$htdocs/$theme/$lang/$where/$tmpl" ) {
260 return ( $theme, $lang, [ uniq
(@themes) ] );
263 # Otherwise return theme/'en', last resort fallback/'en'
264 for my $theme (@themes) {
265 if ( -e
"$htdocs/$theme/en/$where/$tmpl" ) {
266 return ( $theme, 'en', [ uniq
(@themes) ] );
269 # tmpl is a full path, so this is a template for a plugin
270 if ( $tmpl =~ /^\// && -e
$tmpl ) {
271 return ( $themes[0], $lang, [ uniq
(@themes) ] );
276 sub setlanguagecookie
{
277 my ( $query, $language, $uri ) = @_;
279 my $cookie = getlanguagecookie
( $query, $language );
281 # We do not want to set getlanguage in cache, some additional checks are
282 # done in C4::Languages::getlanguage
283 Koha
::Cache
::Memory
::Lite
->get_instance()->clear_from_cache( 'getlanguage' );
285 print $query->redirect(
291 =head2 getlanguagecookie
293 my $cookie = getlanguagecookie($query,$language);
295 Returns a cookie object containing the calculated language to be used.
299 sub getlanguagecookie
{
300 my ( $query, $language ) = @_;
301 my $cookie = $query->cookie(
302 -name
=> 'KohaOpacLanguage',
313 my $columns = GetColumnDefs( $cgi )
315 It is passed a CGI object and returns a hash of hashes containing
316 the column names and descriptions for each table defined in the
317 columns.def file corresponding to the CGI object.
327 my $htdocs = C4
::Context
->config('intrahtdocs');
328 my $columns_file = 'columns.def';
330 # Get theme and language to build the path to columns.def
331 my ($theme, $lang, $availablethemes) =
332 themelanguage
($htdocs, 'about.tt', 'intranet', $query);
333 # Build columns.def path
334 my $path = "$htdocs/$theme/$lang/$columns_file";
336 if ( ! open ( $fh, q{<:encoding(utf-8)}, $path ) ) {
337 carp
"Error opening $path. Check your templates.";
340 # Loop through the columns.def file
341 while ( my $input = <$fh> ){
343 if ( $input =~ m
|<field name
="(.*)">(.*)</field
>| ) {
344 my ( $table, $column ) = split( '\.', $1);
345 my $description = $2;
346 # Initialize the table array if needed.
347 @
{$columns->{ $table }} = () if ! defined $columns->{ $table };
348 # Push field and description
349 push @
{$columns->{ $table }},
350 { field
=> $column, description
=> $description };