Bug 6540 - Add defaultsql command line option
[koha.git] / C4 / Templates.pm
blob436849291535c3621c199e8bd4a77a054d00799c
1 package C4::Templates;
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
8 # Copyright 2009 Chris Cormack and The Koha Dev Team
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
15 # version.
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
25 =head1 NAME
27 Koha::Templates - Object for manipulating templates for use with Koha
29 =cut
31 use base qw(Class::Accessor);
32 use Template;
33 use Template::Constants qw( :debug );
35 use C4::Context;
37 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
39 sub new {
40 my $class = shift;
41 my $interface = shift;
42 my $filename = shift;
43 my $tmplbase = shift;
44 my $htdocs;
45 if ( $interface ne "intranet" ) {
46 $htdocs = C4::Context->config('opachtdocs');
48 else {
49 $htdocs = C4::Context->config('intrahtdocs');
52 my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface );
53 my $template = Template->new(
55 EVAL_PERL => 1,
56 ABSOLUTE => 1,
57 INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
58 FILTERS => {},
61 ) or die Template->error();
62 my $self = {
63 TEMPLATE => $template,
64 VARS => {},
66 bless $self, $class;
67 $self->theme($theme);
68 $self->lang($lang);
69 $self->filename($filename);
70 $self->htdocs($htdocs);
71 $self->interface($interface);
72 $self->{VARS}->{"test"} = "value";
73 return $self;
77 sub output {
78 my $self = shift;
79 my $vars = shift;
81 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
82 my $template = $self->{TEMPLATE};
83 if ( $self->interface eq 'intranet' ) {
84 $vars->{themelang} = '/intranet-tmpl';
86 else {
87 $vars->{themelang} = '/opac-tmpl';
89 $vars->{lang} = $self->lang;
90 $vars->{themelang} .= '/' . $self->theme . '/' . $self->lang;
91 $vars->{yuipath} =
92 ( C4::Context->preference("yuipath") eq "local"
93 ? $vars->{themelang} . "/lib/yui"
94 : C4::Context->preference("yuipath") );
95 $vars->{interface} =
96 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
97 $vars->{theme} = $self->theme;
98 $vars->{opaccolorstylesheet} =
99 C4::Context->preference('opaccolorstylesheet');
100 $vars->{opacsmallimage} = C4::Context->preference('opacsmallimage');
101 $vars->{opacstylesheet} = C4::Context->preference('opacstylesheet');
103 # add variables set via param to $vars for processing
104 # and clean any utf8 mess
105 for my $k ( keys %{ $self->{VARS} } ) {
106 $vars->{$k} = $self->{VARS}->{$k};
107 if (ref($vars->{$k}) eq 'ARRAY'){
108 utf8_arrayref($vars->{$k});
110 elsif (ref($vars->{$k}) eq 'HASH'){
111 utf8_hashref($vars->{$k});
113 else {
114 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
117 my $data;
118 # binmode( STDOUT, ":utf8" );
119 $template->process( $self->filename, $vars, \$data )
120 || die "Template process failed: ", $template->error();
121 return $data;
124 sub utf8_arrayref {
125 my $arrayref = shift;
126 foreach my $element (@$arrayref){
127 if (ref($element) eq 'ARRAY'){
128 utf8_arrayref($element);
129 next;
131 if (ref($element) eq 'HASH'){
132 utf8_hashref($element);
133 next;
135 utf8::encode($element) if utf8::is_utf8($element);
139 sub utf8_hashref {
140 my $hashref = shift;
141 for my $key (keys %{$hashref}){
142 if (ref($hashref->{$key}) eq 'ARRAY'){
143 utf8_arrayref($hashref->{$key});
144 next;
146 if (ref($hashref->{$key}) eq 'HASH'){
147 utf8_hashref($hashref->{$key});
148 next;
150 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
155 # FIXME - this is a horrible hack to cache
156 # the current known-good language, temporarily
157 # put in place to resolve bug 4403. It is
158 # used only by C4::XSLT::XSLTParse4Display;
159 # the language is set via the usual call
160 # to themelanguage.
161 my $_current_language = 'en';
163 sub _current_language {
164 return $_current_language;
167 sub themelanguage {
168 my ( $htdocs, $tmpl, $interface ) = @_;
169 my $query = new CGI;
171 # Set some defaults for language and theme
172 # First, check the user's preferences
173 my $lang;
175 # But, if there's a cookie set, obey it
176 $lang = $query->cookie('KohaOpacLanguage')
177 if ( defined $query and $query->cookie('KohaOpacLanguage') );
179 # Fall back to English
180 my @languages;
181 if ( $interface eq 'intranet' ) {
182 @languages = split ",", C4::Context->preference("language");
184 else {
185 @languages = split ",", C4::Context->preference("opaclanguages");
187 if ($lang) {
188 @languages = ( $lang, @languages );
190 else {
191 $lang = $languages[0] || 'en';
193 my $theme = 'prog'; # in the event of theme failure default to 'prog' -fbcit
194 my @themes;
195 if ( $interface eq "intranet" ) {
196 @themes = split " ", C4::Context->preference("template");
198 else {
199 @themes = split " ", C4::Context->preference("opacthemes");
202 # searches through the themes and languages. First template it find it returns.
203 # Priority is for getting the theme right.
204 THEME:
205 foreach my $th (@themes) {
206 foreach my $la (@languages) {
207 if ( -e "$htdocs/$th/$la/modules/$tmpl" ) {
208 $theme = $th;
209 $lang = $la;
210 last THEME;
212 last unless $la =~ /[-_]/;
215 $_current_language = $lang; # FIXME part of bad hack to paper over bug 4403
216 return ( $theme, $lang );
219 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
220 sub param {
221 my $self = shift;
222 while (@_) {
223 my $key = shift;
224 my $val = shift;
225 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
226 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
227 $self->{VARS}->{$key} = $val;