Bug 7191 Remove GetBorrowerIssues from @EXPORT
[koha.git] / C4 / Templates.pm
blobc2a5911e4c4567457781e3c7037d382c331c0653
1 package C4::Templates;
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
7 use List::Util qw/first/;
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 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');
57 my ($theme, $lang)= themelanguage( $htdocs, $tmplbase, $interface, $query);
58 my $template = Template->new(
59 { EVAL_PERL => 1,
60 ABSOLUTE => 1,
61 INCLUDE_PATH => [
62 "$htdocs/$theme/$lang/includes",
63 "$htdocs/$theme/en/includes"
65 FILTERS => {},
67 ) or die Template->error();
68 my $self = {
69 TEMPLATE => $template,
70 VARS => {},
72 bless $self, $class;
73 $self->theme($theme);
74 $self->lang($lang);
75 $self->filename($filename);
76 $self->htdocs($htdocs);
77 $self->interface($interface);
78 $self->{VARS}->{"test"} = "value";
79 return $self;
83 sub output {
84 my $self = shift;
85 my $vars = shift;
87 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
88 my $template = $self->{TEMPLATE};
89 if ( $self->interface eq 'intranet' ) {
90 $vars->{themelang} = '/intranet-tmpl';
92 else {
93 $vars->{themelang} = '/opac-tmpl';
95 $vars->{lang} = $self->lang;
96 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
97 $vars->{yuipath} =
98 ( C4::Context->preference("yuipath") eq "local"
99 ? $vars->{themelang} . "/lib/yui"
100 : C4::Context->preference("yuipath") );
101 $vars->{interface} =
102 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
103 $vars->{theme} = $self->theme;
104 $vars->{opaccolorstylesheet} =
105 C4::Context->preference('opaccolorstylesheet');
106 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
107 $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
109 # add variables set via param to $vars for processing
110 # and clean any utf8 mess
111 for my $k ( keys %{ $self->{VARS} } ) {
112 $vars->{$k} = $self->{VARS}->{$k};
113 if (ref($vars->{$k}) eq 'ARRAY'){
114 utf8_arrayref($vars->{$k});
116 elsif (ref($vars->{$k}) eq 'HASH'){
117 utf8_hashref($vars->{$k});
119 else {
120 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
123 my $data;
124 # binmode( STDOUT, ":utf8" );
125 $template->process( $self->filename, $vars, \$data )
126 || die "Template process failed: ", $template->error();
127 return $data;
130 sub utf8_arrayref {
131 my $arrayref = shift;
132 foreach my $element (@$arrayref){
133 if (ref($element) eq 'ARRAY'){
134 utf8_arrayref($element);
135 next;
137 if (ref($element) eq 'HASH'){
138 utf8_hashref($element);
139 next;
141 utf8::encode($element) if utf8::is_utf8($element);
145 sub utf8_hashref {
146 my $hashref = shift;
147 for my $key (keys %{$hashref}){
148 if (ref($hashref->{$key}) eq 'ARRAY'){
149 utf8_arrayref($hashref->{$key});
150 next;
152 if (ref($hashref->{$key}) eq 'HASH'){
153 utf8_hashref($hashref->{$key});
154 next;
156 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
161 # FIXME - this is a horrible hack to cache
162 # the current known-good language, temporarily
163 # put in place to resolve bug 4403. It is
164 # used only by C4::XSLT::XSLTParse4Display;
165 # the language is set via the usual call
166 # to themelanguage.
167 my $_current_language = 'en';
169 sub _current_language {
170 return $_current_language;
174 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
175 sub param {
176 my $self = shift;
177 while (@_) {
178 my $key = shift;
179 my $val = shift;
180 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
181 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
182 if ( $key ) {
183 $self->{VARS}->{$key} = $val;
184 } else {
185 warn "Problem = a value of $val has been passed to param without key";
191 =head1 NAME
193 C4::Templates - Functions for managing templates
195 =head1 FUNCTIONS
197 =cut
199 # FIXME: this is a quick fix to stop rc1 installing broken
200 # Still trying to figure out the correct fix.
201 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
203 #---------------------------------------------------------------------------------------------------------
204 # FIXME - POD
206 sub _get_template_file {
207 my ($tmplbase, $interface, $query) = @_;
209 my $is_intranet = $interface eq 'intranet';
210 my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
211 my ($theme, $lang) = themelanguage($htdocs, $tmplbase, $interface, $query);
212 my $opacstylesheet = C4::Context->preference('opacstylesheet');
214 # if the template doesn't exist, load the English one as a last resort
215 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
216 unless (-f $filename) {
217 $lang = 'en';
218 $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
220 return ($htdocs, $theme, $lang, $filename);
224 sub gettemplate {
225 my ( $tmplbase, $interface, $query ) = @_;
226 ($query) or warn "no query in gettemplate";
227 my $path = C4::Context->preference('intranet_includes') || 'includes';
228 my $opacstylesheet = C4::Context->preference('opacstylesheet');
229 $tmplbase =~ s/\.tmpl$/.tt/;
230 my ($htdocs, $theme, $lang, $filename)
231 = _get_template_file($tmplbase, $interface, $query);
232 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
233 my $is_intranet = $interface eq 'intranet';
234 my $themelang =
235 ($is_intranet ? '/intranet-tmpl' : '/opac-tmpl') .
236 "/$theme/$lang";
237 $template->param(
238 themelang => $themelang,
239 yuipath => C4::Context->preference("yuipath") eq "local"
240 ? "$themelang/lib/yui"
241 : C4::Context->preference("yuipath"),
242 interface => $is_intranet ? '/intranet-tmpl' : '/opac-tmpl',
243 theme => $theme,
244 lang => $lang
247 # Bidirectionality
248 my $current_lang = regex_lang_subtags($lang);
249 my $bidi;
250 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
251 # Languages
252 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
253 my $num_languages_enabled = 0;
254 foreach my $lang (@$languages_loop) {
255 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
256 $num_languages_enabled++ if $sublang->{enabled};
259 $template->param(
260 languages_loop => $languages_loop,
261 bidi => $bidi,
262 one_language_enabled => ($num_languages_enabled <= 1) ? 1 : 0, # deal with zero enabled langs as well
263 ) unless @$languages_loop<2;
265 return $template;
269 #---------------------------------------------------------------------------------------------------------
270 # FIXME - POD
271 sub themelanguage {
272 my ($htdocs, $tmpl, $interface, $query) = @_;
273 ($query) or warn "no query in themelanguage";
275 # Select a language based on cookie, syspref available languages & browser
276 my $is_intranet = $interface eq 'intranet';
277 my @languages = split(",", C4::Context->preference(
278 $is_intranet ? 'language' : 'opaclanguages'));
279 my $lang;
280 $lang = $query->cookie('KohaOpacLanguage')
281 if defined $query and $query->cookie('KohaOpacLanguage');
282 unless ($lang) {
283 my $http_accept_language = $ENV{ HTTP_ACCEPT_LANGUAGE };
284 $lang = accept_language( $http_accept_language,
285 getTranslatedLanguages($interface,'prog') );
287 # Ignore a lang not selected in sysprefs
288 $lang = undef unless first { $_ eq $lang } @languages;
289 # Fall back to English if necessary
290 $lang = 'en' unless $lang;
292 my @themes = split(" ", C4::Context->preference(
293 $is_intranet ? "template" : "opacthemes" ));
294 push @themes, 'prog';
296 # Try to find first theme for the selected language
297 for my $theme (@themes) {
298 if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
299 $_current_language = $lang;
300 return ($theme, $lang)
303 # Otherwise, return prog theme in English 'en'
304 return ('prog', 'en');
308 sub setlanguagecookie {
309 my ( $query, $language, $uri ) = @_;
310 my $cookie = $query->cookie(
311 -name => 'KohaOpacLanguage',
312 -value => $language,
313 -expires => ''
315 print $query->redirect(
316 -uri => $uri,
317 -cookie => $cookie
321 sub getlanguagecookie {
322 my ($query) = @_;
323 my $lang;
324 if ($query->cookie('KohaOpacLanguage')){
325 $lang = $query->cookie('KohaOpacLanguage') ;
326 }else{
327 $lang = $ENV{HTTP_ACCEPT_LANGUAGE};
330 $lang = substr($lang, 0, 2);
332 return $lang;