[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1106] trunk



Revision: 1106
          http://svn.sourceforge.net/ledger-smb/?rev=1106&view=rev
Author:   einhverfr
Date:     2007-04-26 13:57:40 -0700 (Thu, 26 Apr 2007)

Log Message:
-----------
Adding localization support to templates (for UI) and porting over to parameterized arguments

Modified Paths:
--------------
    trunk/LedgerSMB/Template.pm
    trunk/bin/arapprn.pl
    trunk/bin/cp.pl
    trunk/bin/io.pl
    trunk/bin/pos.pl
    trunk/bin/rp.pl

Modified: trunk/LedgerSMB/Template.pm
===================================================================
--- trunk/LedgerSMB/Template.pm	2007-04-26 20:28:38 UTC (rev 1105)
+++ trunk/LedgerSMB/Template.pm	2007-04-26 20:57:40 UTC (rev 1106)
@@ -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 @@
 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 = @_;
 
-sub valid_language {
-    my $self = shift;
+	$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};
 
-    # XXX Actually perform validity checks
-    return 1;
+	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'}"
+		}
+	}
+
+
+	return $self;
 }
 
+sub _valid_language {
+	my $self = shift;
+	if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
+		return 0;
+	}
+	return 1;
+}
+
 sub render {
-    my $self = shift;
-    my $vars = shift;
-    my $template;
+	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';
-    }
+	$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 $@;
-    }
+	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();
-    }
+	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);
+	&{"LedgerSMB::Template::$self->{format}::postprocess"}($self);
 
-    return $self->{output};
+	return $self->{output};
 }
 
 1;

Modified: trunk/bin/arapprn.pl
===================================================================
--- trunk/bin/arapprn.pl	2007-04-26 20:28:38 UTC (rev 1105)
+++ trunk/bin/arapprn.pl	2007-04-26 20:57:40 UTC (rev 1106)
@@ -288,7 +288,9 @@
     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 @@
     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;

Modified: trunk/bin/cp.pl
===================================================================
--- trunk/bin/cp.pl	2007-04-26 20:28:38 UTC (rev 1105)
+++ trunk/bin/cp.pl	2007-04-26 20:57:40 UTC (rev 1106)
@@ -49,6 +49,7 @@
 use LedgerSMB::OP;
 use LedgerSMB::IS;
 use LedgerSMB::IR;
+use LedgerSMB::Template;
 
 require "bin/arap.pl";
 
@@ -1475,7 +1476,8 @@
     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;

Modified: trunk/bin/io.pl
===================================================================
--- trunk/bin/io.pl	2007-04-26 20:28:38 UTC (rev 1105)
+++ trunk/bin/io.pl	2007-04-26 20:57:40 UTC (rev 1106)
@@ -1770,7 +1770,8 @@
     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;

Modified: trunk/bin/pos.pl
===================================================================
--- trunk/bin/pos.pl	2007-04-26 20:28:38 UTC (rev 1105)
+++ trunk/bin/pos.pl	2007-04-26 20:57:40 UTC (rev 1106)
@@ -1010,7 +1010,8 @@
     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;

Modified: trunk/bin/rp.pl
===================================================================
--- trunk/bin/rp.pl	2007-04-26 20:28:38 UTC (rev 1105)
+++ trunk/bin/rp.pl	2007-04-26 20:57:40 UTC (rev 1106)
@@ -1083,7 +1083,8 @@
     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 @@
     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 @@
                     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;


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.