Merge remote branch 'kc/master' into new/enh/bug_5917
[koha.git] / C4 / Templates.pm
1 package C4::Templates;
2
3 use strict;
4 use warnings;
5 use Carp;
6
7 # Copyright 2009 Chris Cormack and The Koha Dev Team
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 =head1 NAME 
25
26     Koha::Templates - Object for manipulating templates for use with Koha
27
28 =cut
29
30 use base qw(Class::Accessor);
31 use Template;
32 use Template::Constants qw( :debug );
33
34 use C4::Context;
35
36 __PACKAGE__->mk_accessors(qw( theme lang filename htdocs interface vars));
37
38 sub new {
39     my $class     = shift;
40     my $interface = shift;
41     my $filename  = shift;
42     my $htdocs;
43     if ( $interface ne "intranet" ) {
44         $htdocs = C4::Context->config('opachtdocs');
45     }
46     else {
47         $htdocs = C4::Context->config('intrahtdocs');
48     }
49
50 #    my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query );
51     my $theme = 'prog';
52     my $lang = 'en';
53     my $template = Template->new(
54         {
55             EVAL_PERL    => 1,
56             ABSOLUTE     => 1,
57             INCLUDE_PATH => "$htdocs/$theme/$lang/includes",
58             FILTERS      => {},
59
60         }
61     ) or die Template->error();
62     my $self = {
63         TEMPLATE => $template,
64         VARS => {},
65     };
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;
74
75 }
76
77 sub output {
78     my $self = shift;
79     my $vars = shift;
80 #    my $file = $self->htdocs . '/' . $self->theme .'/'.$self->lang.'/'.$self->filename;
81     my $template = $self->{TEMPLATE};
82     if ($self->interface eq 'intranet'){
83         $vars->{themelang} = '/intranet-tmpl';
84     }
85     else {
86         $vars->{themelang} = '/opac-tmpl';
87     }
88     $vars->{lang} = $self->lang;
89     $vars->{themelang}          .= '/' . $self->theme . '/' . $self->lang;
90     $vars->{yuipath}             = (C4::Context->preference("yuipath") eq "local"?$self->{themelang}."/lib/yui":C4::Context->preference("yuipath"));
91     $vars->{interface}           = ( $vars->{interface} ne 'intranet' ? '/opac-tmpl' : '/intranet-tmpl' );
92     $vars->{theme}               = $self->theme;
93     $vars->{opaccolorstylesheet} = C4::Context->preference('opaccolorstylesheet');
94     $vars->{opacsmallimage}      = C4::Context->preference('opacsmallimage');
95     $vars->{opacstylesheet}      = C4::Context->preference('opacstylesheet');
96     #add variables set via param to $vars for processing
97     for my $k(keys %{$self->{VARS}}){
98         $vars->{$k} = $self->{VARS}->{$k};
99     }
100     my $data;
101     $template->process( $self->filename, $vars, \$data) || die "Template process failed: ", $template->error();; 
102     return $data;
103 }
104
105 # wrapper method to allow easier transition from HTML template pro to Template Toolkit
106 sub param{
107     my $self = shift;
108     while(@_){
109         my $key = shift;
110         my $val = shift;
111         utf8::encode($val) if utf8::is_utf8($val);
112         if( ref($val) eq 'ARRAY' && ! scalar @$val ){ $val = undef; }
113         elsif( ref($val) eq 'HASH' && ! scalar %$val ){ $val = undef; }
114         $self->{VARS}->{$key} = $val;
115     }
116 }
117
118 1;