Bug 11349: Remove unnecesary name translation
[koha.git] / C4 / Templates.pm
1 package C4::Templates;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use CGI;
7 use List::MoreUtils qw/any/;
8
9 # Copyright 2009 Chris Cormack and The Koha Dev Team
10 #
11 # This file is part of Koha.
12 #
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.
17 #
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.
21 #
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
25
26 =head1 NAME 
27
28     Koha::Templates - Object for manipulating templates for use with Koha
29
30 =cut
31
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 );
36
37 use C4::Context;
38
39 __PACKAGE__->mk_accessors(qw( theme activethemes preferredtheme lang filename htdocs interface vars));
40
41
42
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');
52     }
53     else {
54         $htdocs = C4::Context->config('intrahtdocs');
55     }
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';
61     }
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 => {},
70         }
71     ) or die Template->error();
72     my $self = {
73         TEMPLATE => $template,
74         VARS     => {},
75     };
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;
86
87 }
88
89 sub output {
90     my $self = shift;
91     my $vars = shift;
92
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';
97     }
98     else {
99         $vars->{themelang} = '/opac-tmpl';
100     }
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->{opacsmallimage} = C4::Context->preference('opacsmallimage');
113     $vars->{opaclayoutstylesheet} =
114         C4::Context->preference('opaclayoutstylesheet');
115
116     # add variables set via param to $vars for processing
117     # and clean any utf8 mess
118     for my $k ( keys %{ $self->{VARS} } ) {
119         $vars->{$k} = $self->{VARS}->{$k};
120         if (ref($vars->{$k}) eq 'ARRAY'){
121             utf8_arrayref($vars->{$k});
122         }
123         elsif (ref($vars->{$k}) eq 'HASH'){
124             utf8_hashref($vars->{$k});
125         }
126         else {
127             utf8::encode($vars->{$k}) if utf8::is_utf8($vars->{$k});
128         }
129     }
130     my $data;
131 #    binmode( STDOUT, ":utf8" );
132     $template->process( $self->filename, $vars, \$data )
133       || die "Template process failed: ", $template->error();
134     return $data;
135 }
136
137 sub utf8_arrayref {
138     my $arrayref = shift;
139     foreach my $element (@$arrayref){
140         if (ref($element) eq 'ARRAY'){
141             utf8_arrayref($element);
142             next;
143         }
144         if (ref($element) eq 'HASH'){
145             utf8_hashref($element);
146             next;
147         }
148         utf8::encode($element) if utf8::is_utf8($element);
149     }        
150 }         
151
152 sub utf8_hashref {
153     my $hashref = shift;
154     for my $key (keys %{$hashref}){
155         if (ref($hashref->{$key}) eq 'ARRAY'){
156             utf8_arrayref($hashref->{$key});
157             next;
158         }
159         if (ref($hashref->{$key}) eq 'HASH'){
160             utf8_hashref($hashref->{$key});
161             next;
162         }
163         utf8::encode($hashref->{$key}) if utf8::is_utf8($hashref->{$key});
164     }
165 }
166
167 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
168 sub param {
169     my $self = shift;
170     while (@_) {
171         my $key = shift;
172         my $val = shift;
173         if    ( ref($val) eq 'ARRAY' && !scalar @$val ) { $val = undef; }
174         elsif ( ref($val) eq 'HASH'  && !scalar %$val ) { $val = undef; }
175         if ( $key ) {
176             $self->{VARS}->{$key} = $val;
177         } else {
178             warn "Problem = a value of $val has been passed to param without key";
179         }
180     }
181 }
182
183
184 =head1 NAME
185
186 C4::Templates - Functions for managing templates
187
188 =head1 FUNCTIONS
189
190 =cut
191
192 # FIXME: this is a quick fix to stop rc1 installing broken
193 # Still trying to figure out the correct fix.
194 my $path = C4::Context->config('intrahtdocs') . "/prog/en/includes/";
195
196 #---------------------------------------------------------------------------------------------------------
197 # FIXME - POD
198
199 sub _get_template_file {
200     my ($tmplbase, $interface, $query) = @_;
201
202     my $is_intranet = $interface eq 'intranet';
203     my $htdocs = C4::Context->config($is_intranet ? 'intrahtdocs' : 'opachtdocs');
204     my ($theme, $lang, $availablethemes) = themelanguage($htdocs, $tmplbase, $interface, $query);
205
206     # if the template doesn't exist, load the English one as a last resort
207     my $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
208     unless (-f $filename) {
209         $lang = 'en';
210         $filename = "$htdocs/$theme/$lang/modules/$tmplbase";
211     }
212     return ($htdocs, $theme, $lang, $filename);
213 }
214
215
216 sub gettemplate {
217     my ( $tmplbase, $interface, $query, $is_plugin ) = @_;
218     ($query) or warn "no query in gettemplate";
219     my $path = C4::Context->preference('intranet_includes') || 'includes';
220     my ($htdocs, $theme, $lang, $filename)
221        =  _get_template_file($tmplbase, $interface, $query);
222     $filename = $tmplbase if ( $is_plugin );
223     my $template = C4::Templates->new($interface, $filename, $tmplbase, $query);
224
225     # Bidirectionality, must be sent even if is the only language
226     my $current_lang = regex_lang_subtags($lang);
227     my $bidi;
228     $bidi = get_bidi($current_lang->{script}) if $current_lang->{script};
229     $template->param(
230             bidi                 => $bidi,
231     );
232     # Languages
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};
238          }
239     }
240     my $one_language_enabled = ($num_languages_enabled <= 1) ? 1 : 0; # deal with zero enabled langs as well
241     $template->param(
242             languages_loop       => $languages_loop,
243             one_language_enabled => $one_language_enabled,
244     ) unless $one_language_enabled;
245
246     return $template;
247 }
248
249
250 #---------------------------------------------------------------------------------------------------------
251 # FIXME - POD
252 sub themelanguage {
253     my ($htdocs, $tmpl, $interface, $query) = @_;
254     ($query) or warn "no query in themelanguage";
255
256     # Select a language based on cookie, syspref available languages & browser
257     my $lang = C4::Languages::getlanguage($query);
258
259     # Select theme
260     my $is_intranet = $interface eq 'intranet';
261     my @themes = split(" ", C4::Context->preference(
262         $is_intranet ? "template" : "opacthemes" ));
263     push @themes, 'prog';
264
265     # Try to find first theme for the selected language
266     for my $theme (@themes) {
267         if ( -e "$htdocs/$theme/$lang/modules/$tmpl" ) {
268             return ($theme, $lang, \@themes)
269         }
270     }
271     # Otherwise, return prog theme in English 'en'
272     return ('prog', 'en', \@themes);
273 }
274
275
276 sub setlanguagecookie {
277     my ( $query, $language, $uri ) = @_;
278
279     my $cookie = $query->cookie(
280         -name    => 'KohaOpacLanguage',
281         -value   => $language,
282         -HttpOnly => 1,
283         -expires => '+3y'
284     );
285     print $query->redirect(
286         -uri    => $uri,
287         -cookie => $cookie
288     );
289 }
290
291 =head2 getlanguagecookie
292
293     my $cookie = getlanguagecookie($query,$language);
294
295 Returns a cookie object containing the calculated language to be used.
296
297 =cut
298
299 sub getlanguagecookie {
300     my ( $query, $language ) = @_;
301     my $cookie = $query->cookie(
302         -name    => 'KohaOpacLanguage',
303         -value   => $language,
304         -HttpOnly => 1,
305         -expires => '+3y'
306     );
307
308     return $cookie;
309 }
310
311 1;