Bug 12539: Follow up to fix fallback theme
[koha.git] / C4 / Templates.pm
blob879249a386934e71d5a1c6a09efa1a09f07e43c4
1 package C4::Templates;
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
7 use List::MoreUtils qw/any/;
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 activethemes preferredtheme 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');
56 my ($theme, $lang, $activethemes)= themelanguage( $htdocs, $tmplbase, $interface, $query);
57 my @includes;
58 foreach (@$activethemes) {
59 push @includes, "$htdocs/$_/$lang/includes";
60 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
62 my $template = Template->new(
63 { EVAL_PERL => 1,
64 ABSOLUTE => 1,
65 PLUGIN_BASE => 'Koha::Template::Plugin',
66 COMPILE_EXT => C4::Context->config('template_cache_dir')?'.ttc':'',
67 COMPILE_DIR => C4::Context->config('template_cache_dir')?C4::Context->config('template_cache_dir'):'',,
68 INCLUDE_PATH => \@includes,
69 FILTERS => {},
71 ) or die Template->error();
72 my $self = {
73 TEMPLATE => $template,
74 VARS => {},
76 bless $self, $class;
77 $self->theme($theme);
78 $self->lang($lang);
79 $self->activethemes($activethemes);
80 $self->preferredtheme($activethemes->[0]);
81 $self->filename($filename);
82 $self->htdocs($htdocs);
83 $self->interface($interface);
84 $self->{VARS}->{"test"} = "value";
85 return $self;
89 sub output {
90 my $self = shift;
91 my $vars = shift;
93 # my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
94 my $template = $self->{TEMPLATE};
95 if ( $self->interface eq 'intranet' ) {
96 $vars->{themelang} = '/intranet-tmpl';
98 else {
99 $vars->{themelang} = '/opac-tmpl';
101 $vars->{lang} = $self->lang;
102 $vars->{themelang} .= '/' . $self->preferredtheme . '/' . $self->lang;
103 $vars->{yuipath} =
104 ( C4::Context->preference("yuipath") eq "local"
105 ? ( $self->interface eq 'intranet' ? $vars->{themelang} . "/lib/yui" : "/opac-tmpl/lib/yui" )
106 : C4::Context->preference("yuipath") );
107 $vars->{interface} =
108 ( $self->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
109 $vars->{theme} = $self->theme;
110 $vars->{opaccolorstylesheet} =
111 C4::Context->preference('opaccolorstylesheet');
112 $vars->{opaclayoutstylesheet} =
113 C4::Context->preference('opaclayoutstylesheet');
115 # add variables set via param to $vars for processing
116 # and clean any utf8 mess
117 for my $k ( keys %{ $self->{VARS} } ) {
118 $vars->{$k} = $self->{VARS}->{$k};
119 if (ref($vars->{$k}) eq 'ARRAY'){
120 utf8_arrayref($vars->{$k});
122 elsif (ref($vars->{$k}) eq 'HASH'){
123 utf8_hashref($vars->{$k});
125 else {
126 utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
129 my $data;
130 # binmode( STDOUT, ":utf8" );
131 $template->process( $self->filename, $vars, \$data )
132 || die "Template process failed: ", $template->error();
133 return $data;
136 sub utf8_arrayref {
137 my $arrayref = shift;
138 foreach my $element (@$arrayref){
139 if (ref($element) eq 'ARRAY'){
140 utf8_arrayref($element);
141 next;
143 if (ref($element) eq 'HASH'){
144 utf8_hashref($element);
145 next;
147 utf8::encode($element) if utf8::is_utf8($element);
151 sub utf8_hashref {
152 my $hashref = shift;
153 for my $key (keys %{$hashref}){
154 if (ref($hashref->{$key}) eq 'ARRAY'){
155 utf8_arrayref($hashref->{$key});
156 next;
158 if (ref($hashref->{$key}) eq 'HASH'){
159 utf8_hashref($hashref->{$key});
160 next;
162 utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
166 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
167 sub param {
168 my $self = shift;
169 while (@_) {
170 my $key = shift;
171 my $val = shift;
172 if ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
173 elsif ( ref($val) eq 'HASH' && !scalar %$val ) { $val = undef; }
174 if ( $key ) {
175 $self->{VARS}->{$key} = $val;
176 } else {
177 warn "Problem = a value of $val has been passed to param without key";
183 =head1 NAME
185 C4::Templates - Functions for managing templates
187 =head1 FUNCTIONS
189 =cut
191 # FIXME: this is a quick fix to stop rc1 installing broken
192 # Still trying to figure out the correct fix.
193 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
195 #---------------------------------------------------------------------------------------------------------
196 # FIXME - POD
198 sub _get_template_file {
199 my ($tmplbase, $interface, $query) = @_;
201 my $is_intranet = $interface eq 'intranet';
202 my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
203 my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
204 my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
206 return ($htdocs, $theme, $lang, $filename);
210 sub gettemplate {
211 my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
212 ($query) or warn "no query in gettemplate";
213 my $path = C4::Context->preference('intranet_includes') || 'includes';
214 my ($htdocs, $theme, $lang, $filename)
215 = _get_template_file($tmplbase, $interface, $query);
216 $filename = $tmplbase if ( $is_plugin );
217 my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
219 # Bidirectionality, must be sent even if is the only language
220 my $current_lang = regex_lang_subtags($lang);
221 my $bidi;
222 $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
223 $template->param(
224 bidi => $bidi,
226 # Languages
227 my $languages_loop = getTranslatedLanguages($interface,$theme,$lang);
228 my $num_languages_enabled = 0;
229 foreach my $lang (@$languages_loop) {
230 foreach my $sublang (@{ $lang->{'sublanguages_loop'} }) {
231 $num_languages_enabled++ if $sublang->{enabled};
234 my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well
235 $template->param(
236 languages_loop => $languages_loop,
237 one_language_enabled => $one_language_enabled,
238 ) unless $one_language_enabled;
240 return $template;
244 #---------------------------------------------------------------------------------------------------------
245 # FIXME - POD
246 # FIXME - Rewritten to remove hardcoded theme with minimal changes, need to be rethinked
247 sub themelanguage {
248 my ($htdocs, $tmpl, $interface, $query) = @_;
249 ($query) or warn "no query in themelanguage";
251 # Select a language based on cookie, syspref available languages & browser
252 my $lang = C4::Languages::getlanguage($query);
254 # Get theme
255 my @themes = ( C4::Context->preference( ($interface eq 'intranet') ? 'template' : 'opacthemes' ) );
256 my $fallback = C4::Context->preference( ($interface eq 'intranet') ? 'template' : 'OPACFallback' );
257 push @themes, $fallback;
259 # Try to find first theme for the selected theme/lang, then for fallback/lang
260 for my $theme (@themes) {
261 if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
262 return ($theme, $lang, \@themes);
265 # Otherwise return theme/'en', last resort fallback/'en'
266 for my $theme (@themes) {
267 if ( -e "$htdocs/$theme/en/modules/$tmpl" ) {
268 return ($theme, 'en', \@themes);
274 sub setlanguagecookie {
275 my ( $query, $language, $uri ) = @_;
277 my $cookie = $query->cookie(
278 -name => 'KohaOpacLanguage',
279 -value => $language,
280 -HttpOnly => 1,
281 -expires => '+3y'
283 print $query->redirect(
284 -uri => $uri,
285 -cookie => $cookie
289 =head2 getlanguagecookie
291 my $cookie = getlanguagecookie($query,$language);
293 Returns a cookie object containing the calculated language to be used.
295 =cut
297 sub getlanguagecookie {
298 my ( $query, $language ) = @_;
299 my $cookie = $query->cookie(
300 -name => 'KohaOpacLanguage',
301 -value => $language,
302 -HttpOnly => 1,
303 -expires => '+3y'
306 return $cookie;
309 =head2 GetColumnDefs
311 my $columns = GetColumnDefs( $cgi )
313 It is passed a CGI object and returns a hash of hashes containing
314 the column names and descriptions for each table defined in the
315 columns.def file corresponding to the CGI object.
317 =cut
319 sub GetColumnDefs {
321 my $query = shift;
323 my $columns = {};
325 my $htdocs = C4::Context->config('intrahtdocs');
326 my $columns_file = 'columns.def';
328 # Get theme and language to build the path to columns.def
329 my ($theme, $lang, $availablethemes) =
330 themelanguage($htdocs, 'about.tt', 'intranet', $query);
331 # Build columns.def path
332 my $path = "$htdocs/$theme/$lang/$columns_file";
333 my $fh;
334 if ( ! open ( $fh, q{<}, $path ) ) {
335 carp "Error opening $path. Check your templates.";
336 return;
338 # Loop through the columns.def file
339 while ( my $input = <$fh> ){
340 chomp $input;
341 if ( $input =~ m|<field name="(.*)">(.*)</field>| ) {
342 my ( $table, $column ) = split( '\.', $1);
343 my $description = $2;
344 # Initialize the table array if needed.
345 @{$columns->{ $table }} = () if ! defined $columns->{ $table };
346 # Push field and description
347 push @{$columns->{ $table }},
348 { field => $column, description => $description };
351 close $fh;
353 return $columns;