summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-04-26 20:57:40 +0000
committereinhverfr <einhverfr@4979c152-3d1c-0410-bac9-87ea11338e46>2007-04-26 20:57:40 +0000
commit323ed79bf81fa5e9bba4c1b14cb2c48ffeda614e (patch)
tree632f230f196d649d7754f0e27b2b7182bf511a98
parent6c5c1c36fa682244c355f6c06808ec715f0a2baa (diff)
Adding localization support to templates (for UI) and porting over to parameterized arguments
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1106 4979c152-3d1c-0410-bac9-87ea11338e46
-rwxr-xr-xLedgerSMB/Template.pm188
-rw-r--r--bin/arapprn.pl8
-rw-r--r--bin/cp.pl4
-rw-r--r--bin/io.pl3
-rw-r--r--bin/pos.pl3
-rw-r--r--bin/rp.pl11
6 files changed, 120 insertions, 97 deletions
diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm
index 93254df1..ce66a0eb 100755
--- a/LedgerSMB/Template.pm
+++ b/LedgerSMB/Template.pm
@@ -1,25 +1,39 @@
-#=====================================================================
-#
-# Template support module for LedgerSMB
-# LedgerSMB::Template
-#
-# LedgerSMB
-# Small Medium Business Accounting software
-# http://www.ledgersmb.org/
-#
-#
-# Copyright (C) 2007
-# This work contains copyrighted information from a number of sources all used
-# with permission. It is released under the GNU General Public License
-# Version 2 or, at your option, any later version. See COPYRIGHT file for
-# details.
-#
-#
-#======================================================================
-# This package contains template related functions:
-#
-#
-#====================================================================
+=head1 NAME
+
+LedgerSMB::Template - Template support module for LedgerSMB
+
+=head1 SYOPSIS
+
+This module renders templates to provide HTML interfaces. LaTeX support
+forthcoming.
+
+=head1 METHODS
+
+=item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path]);
+
+ This command instantiates a new template:
+ template is the file name of the template to be processed.
+ format is the type of format to be used. Currently only HTML is supported
+ language (optional) specifies the language for template selection.
+ include_path allows one to override the template directory and use this with user interface templates.
+
+=item render($hashref)
+
+This command renders the template and writes the result to standard output.
+Currently email and server-side printing are not supported.
+
+=item my $bool = _valid_language()
+
+This command checks for valid langages. Returns 1 if the language is valid,
+0 if it is not.
+
+=head1 Copyright 2007, The LedgerSMB Core Team
+
+This file is licensed under the Gnu General Public License version 2, or at your
+option any later version. A copy of the license should have been included with
+your software.
+
+=cut
use Error qw(:try);
use Template;
use LedgerSMB::Sysconfig;
@@ -27,77 +41,75 @@ use LedgerSMB::Sysconfig;
package LedgerSMB::Template;
sub new {
- my $class = shift;
- my $self = {};
- $self->{myconfig} = shift;
- $self->{template} = shift;
- $self->{format} = shift;
- $self->{language} = shift;
- $self->{output} = '';
- bless $self, $class;
- return $self;
-}
+ my $class = shift;
+ my $self = {};
+ my %args = @_;
+
+ $self->{myconfig} = $args{user};
+ $self->{template} = $args{template};
+ $self->{format} = $args{format};
+ $self->{language} = $args{language};
+ $self->{output} = '';
+ $self->{include_path} = $args{path};
+ $self->{locale} = $args{locale};
+
+ bless $self, $class;
+
+ if (!$self->{include_path}){
+ $self->{include_path} = $self->{'myconfig'}->{'templates'};
+ if (defined $self->{language}){
+ if (!$self->_valid_language){
+ throw Error::Simple 'Invalid language';
+ return undef;
+ }
+ $self->{include_path} = "$self->{'include_path'}"
+ ."/$self->{language}"
+ .";$self->{'include_path'}"
+ }
+ }
+
-sub valid_language {
- my $self = shift;
+ return $self;
+}
- # XXX Actually perform validity checks
- return 1;
+sub _valid_language {
+ my $self = shift;
+ if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
+ return 0;
+ }
+ return 1;
}
sub render {
- my $self = shift;
- my $vars = shift;
- my $template;
-
- if ( not defined $self->{language} ) {
- $template = Template->new(
- {
- INCLUDE_PATH => $self->{'myconfig'}->{'templates'},
- START_TAG => quotemeta('<?lsmb'),
- END_TAG => quotemeta('?>'),
- DELIMITER => ';',
- }
- ) || throw Error::Simple Template->error();
- }
- elsif ( $self->valid_language() ) {
- $template = Template->new(
- {
- INCLUDE_PATH =>
-"$self->{'myconfig'}->{'templates'}/$self->{language};$self->{'myconfig'}->{'templates'}",
- START_TAG => quotemeta('<?lsmb'),
- END_TAG => quotemeta('?>'),
- DELIMITER => ';',
- }
- ) || throw Error::Simple Template->error();
- }
- else {
- throw Error::Simple 'Invalid language';
- }
-
- eval "require LedgerSMB::Template::$self->{format}";
- if ($@) {
- throw Error::Simple $@;
- }
-
- my $cleanvars =
- &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars);
- if (
- not $template->process(
- &{"LedgerSMB::Template::$self->{format}::get_template"}(
- $self->{template} ),
- $cleanvars,
- \$self->{output},
- binmode => ':utf8'
- )
- )
- {
- throw Error::Simple $template->error();
- }
-
- &{"LedgerSMB::Template::$self->{format}::postprocess"}($self);
-
- return $self->{output};
+ my $self = shift;
+ my $vars = shift;
+ my $template;
+
+ $template = Template->new({
+ INCLUDE_PATH => $self->{include_path},
+ START_TAG => quotemeta('<?lsmb'),
+ END_TAG => quotemeta('?>'),
+ DELIMITER => ';',
+ }) || throw Error::Simple Template->error();
+
+ eval "require LedgerSMB::Template::$self->{format}";
+ if ($@) {
+ throw Error::Simple $@;
+ }
+
+ my $cleanvars = &{"LedgerSMB::Template::$self->{format}::preprocess"}($vars);
+ if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
+ $cleanvars->{text} = \&$self->{locale}->text();
+ }
+ if (not $template->process(
+ &{"LedgerSMB::Template::$self->{format}::get_template"}($self->{template}),
+ $cleanvars, \$self->{output}, binmode => ':utf8')) {
+ throw Error::Simple $template->error();
+ }
+
+ &{"LedgerSMB::Template::$self->{format}::postprocess"}($self);
+
+ return $self->{output};
}
1;
diff --git a/bin/arapprn.pl b/bin/arapprn.pl
index 223518d0..0d256c28 100644
--- a/bin/arapprn.pl
+++ b/bin/arapprn.pl
@@ -288,7 +288,9 @@ sub print_check {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new(
+ user => \%myconfig, template => $form->{'formname'},
+ format => 'HTML' );
try {
$template->render($form);
$form->header;
@@ -561,7 +563,9 @@ sub print_transaction {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new(
+ user => \%myconfig, template => $form->{'formname'},
+ format => 'HTML' );
try {
$template->render($form);
$form->header;
diff --git a/bin/cp.pl b/bin/cp.pl
index 6a9fc309..d21fa18b 100644
--- a/bin/cp.pl
+++ b/bin/cp.pl
@@ -49,6 +49,7 @@ use LedgerSMB::CP;
use LedgerSMB::OP;
use LedgerSMB::IS;
use LedgerSMB::IR;
+use LedgerSMB::Template;
require "bin/arap.pl";
@@ -1475,7 +1476,8 @@ sub print_form {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new( user => \%myconfig,
+ template => $form->{'formname'}, format => 'HTML' );
try {
$template->render($form);
$form->header;
diff --git a/bin/io.pl b/bin/io.pl
index 4c41462d..d1a00af9 100644
--- a/bin/io.pl
+++ b/bin/io.pl
@@ -1770,7 +1770,8 @@ sub print_form {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new( user => \%myconfig,
+ template => $form->{'formname'}, format => 'HTML' );
try {
$template->render($form);
$form->header;
diff --git a/bin/pos.pl b/bin/pos.pl
index f4192cb6..8cc8d73c 100644
--- a/bin/pos.pl
+++ b/bin/pos.pl
@@ -1010,7 +1010,8 @@ sub print_form {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new(user => \%myconfig,
+ template => $form->{'formname'}, format => 'HTML' );
try {
$template->render($form);
$form->header;
diff --git a/bin/rp.pl b/bin/rp.pl
index a962ada1..a5c906d3 100644
--- a/bin/rp.pl
+++ b/bin/rp.pl
@@ -1083,7 +1083,8 @@ sub generate_income_statement {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new( user => \%myconfig,
+ template => $form->{'formname'}, format => 'HTML' );
try {
$template->render($form);
$form->header;
@@ -1139,7 +1140,8 @@ sub generate_balance_sheet {
if ( ( $form->{'media'} eq 'screen' ) and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'}, 'HTML' );
+ LedgerSMB::Template->new( user => \%myconfig,
+ template => $form->{'formname'}, format => 'HTML' );
try {
$template->render($form);
$form->header;
@@ -2293,8 +2295,9 @@ sub print_form {
and ( $form->{'format'} eq 'html' ) )
{
my $template =
- LedgerSMB::Template->new( \%myconfig, $form->{'formname'},
- 'HTML' );
+ LedgerSMB::Template->new( user => \%myconfig,
+ template => $form->{'formname'},
+ format => 'HTML' );
try {
$template->render($form);
$form->header;