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

SF.net SVN: ledger-smb:[6280] addons/1.3/wxPOS/WXPOS



Revision: 6280
          http://sourceforge.net/p/ledger-smb/code/6280
Author:   einhverfr
Date:     2013-11-11 09:46:14 +0000 (Mon, 11 Nov 2013)
Log Message:
-----------
Initial copy of LedgerSMB template system, stripped down

Modified Paths:
--------------
    addons/1.3/wxPOS/WXPOS/Template/LaTeX.pm
    addons/1.3/wxPOS/WXPOS/Template/TXT.pm

Added Paths:
-----------
    addons/1.3/wxPOS/WXPOS/Template/
    addons/1.3/wxPOS/WXPOS/Template.pm

Modified: addons/1.3/wxPOS/WXPOS/Template/LaTeX.pm
===================================================================
--- branches/1.3/LedgerSMB/Template/LaTeX.pm	2013-11-07 14:33:37 UTC (rev 6244)
+++ addons/1.3/wxPOS/WXPOS/Template/LaTeX.pm	2013-11-11 09:46:14 UTC (rev 6280)
@@ -1,7 +1,7 @@
 
 =head1 NAME
 
-LedgerSMB::Template::LaTeX - Template support module for LedgerSMB
+WXPOS::Template::LaTeX - Template support module for LedgerSMB's WXPOS
 
 =head1 SYNOPSIS
 
@@ -49,21 +49,19 @@
 holders, see the CONTRIBUTORS file.
 =cut
 
-package LedgerSMB::Template::LaTeX;
+package WXPOS::Template::LaTeX;
 
 use warnings;
 use strict;
 
 use Error qw(:try);
 use Template::Latex;
-use LedgerSMB::Template::TTI18N;
 
 #my $binmode = ':utf8';
 my $binmode = ':raw';
 binmode STDOUT, $binmode;
 binmode STDERR, $binmode;
 
-my $logger = Log::Log4perl->get_logger('LedgerSMB::Template::LaTeX');
 
 sub get_template {
 	my $name = shift;
@@ -75,7 +73,6 @@
 	my $vars;
 	my $type = ref $rawvars;
 
-	return $rawvars if $type =~ /^LedgerSMB::Locale/;
 	return unless defined $type;
 	if ($type eq 'ARRAY') {
 		for (@{$rawvars}) {
@@ -154,7 +151,7 @@
 	my $template;
 	my $source;
 	$parent->{outputfile} ||=
-		"${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
+		"${WXPOS::Sysconfig::tempdir}/$parent->{template}-output-$$";
 
         $parent->{binmode} = $binmode;
 	if (ref $parent->{template} eq 'SCALAR') {
@@ -183,7 +180,7 @@
 		}) || throw Error::Simple Template::Latex->error(); 
 	if (not $template->process(
 		$source, 
-		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
+		{%$cleanvars,
 			'escape' => \&preprocess},
 		"$parent->{outputfile}.$format", {binmode => 1})) {
 		throw Error::Simple $template->error();

Modified: addons/1.3/wxPOS/WXPOS/Template/TXT.pm
===================================================================
--- branches/1.3/LedgerSMB/Template/TXT.pm	2013-11-07 14:33:37 UTC (rev 6244)
+++ addons/1.3/wxPOS/WXPOS/Template/TXT.pm	2013-11-11 09:46:14 UTC (rev 6280)
@@ -1,7 +1,7 @@
 
 =head1 NAME
 
-LedgerSMB::Template::TXT - Template support module for LedgerSMB
+WXPOS::Template::TXT - Template support module for LedgerSMB's WXPOS
 
 =head1 METHODS
 
@@ -39,14 +39,13 @@
 holders, see the CONTRIBUTORS file.
 =cut
 
-package LedgerSMB::Template::TXT;
+package WXPOS::Template::TXT;
 
 use warnings;
 use strict;
 
 use Error qw(:try);
 use Template;
-use LedgerSMB::Template::TTI18N;
 
 my $binmode = ':utf8';
 binmode STDOUT, $binmode;
@@ -102,7 +101,7 @@
 
 	if (not $template->process(
 		$source, 
-		{%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
+		{%$cleanvars, 
 			'escape' => \&preprocess},
 		$output, binmode => ':utf8')) {
 		throw Error::Simple $template->error();

Copied: addons/1.3/wxPOS/WXPOS/Template.pm (from rev 6244, branches/1.3/LedgerSMB/Template.pm)
===================================================================
--- addons/1.3/wxPOS/WXPOS/Template.pm	                        (rev 0)
+++ addons/1.3/wxPOS/WXPOS/Template.pm	2013-11-11 09:46:14 UTC (rev 6280)
@@ -0,0 +1,420 @@
+
+=head1 NAME
+
+WXPOS::Template - Template support module for LedgerSMB  
+
+=head1 SYNOPSIS
+
+This module renders templates.  A copy of LedgerSMB::Template for now.
+
+=head1 METHODS
+
+=over
+
+=item new(user => \%myconfig, template => $string, format => $string, [locale => $locale] [language => $string], [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool], [debug => $bool], [output_file => $string] );
+
+This command instantiates a new template:
+
+=over
+
+=item template
+
+The template to be processed.  This can either be a reference to the template
+in string form or the name of the file that is the template to be processed.
+
+=item format
+
+The format to be used.  Currently HTML, PS, PDF, TXT and CSV are supported.
+
+=item format_options (optional)
+
+A hash of format-specific options.  See the appropriate LSMB::T::foo for
+details.
+
+=item output_options (optional)
+
+A hash of output-specific options.  See the appropriate output method for
+details.
+
+=item locale (optional)
+
+The locale object to use for regular gettext lookups.  Having this option adds
+the text function to the usable list for the templates.  Has no effect on the
+gettext function.
+
+=item language (optional)
+
+The language for template selection.
+
+=item include_path (optional)
+
+Overrides the template directory.  Used with user interface templates.
+
+=item no_auto_output (optional)
+
+Disables the automatic output of rendered templates.
+
+=item no_escape (optional)
+
+Disables escaping on the template variables.
+
+=item debug (optional)
+
+Enables template debugging.
+
+With the TT-based renderers, HTML, PS, PDF, TXT, and CSV, the portion of the
+template to get debugging messages is to be surrounded by
+<?lsmb DEBUG format 'foo' ?> statements.  Example:
+
+    <tr><td colspan="<?lsmb columns.size ?>"></td></tr>
+    <tr class="listheading">
+  <?lsmb FOREACH column IN columns ?>
+  <?lsmb DEBUG format '$file line $line : [% $text %]' ?>
+      <th class="listtop"><?lsmb heading.$column ?></th>
+  <?lsmb DEBUG format '' ?>
+  <?lsmb END ?>
+    </tr>
+
+=item method/media (optional)
+
+The output method to use, defaults to HTTP.  Media is a synonym for method
+
+=item output_file (optional)
+
+The base name of the file for output.
+
+=back
+
+=item available_formats()
+
+Returns a list of format names, any of the following (in order) as applicable:
+
+=over
+
+=item TXT (includes CSV, always available))
+
+=item PDF
+
+=item PS
+
+=back
+
+=item new_UI(user => \%myconfig, locale => $locale, template => $file, ...)
+
+Wrapper around the constructor that sets the path to 'UI', format to 'HTML',
+and leaves auto-output enabled.
+
+=item render($hashref)
+
+This command renders the template.  If no_auto_output was not specified during
+instantiation, this also writes the result to standard output and exits.
+Otherwise it returns the name of the output file if a file was created.  When
+no output file is created, the output is held in $self->{output}.
+
+Currently email and server-side printing are not supported.
+
+=item output
+
+This function outputs the rendered file in an appropriate manner.
+
+=item my $bool = _valid_language()
+
+This command checks for valid langages.  Returns 1 if the language is valid, 
+0 if it is not.
+
+=item escape($string)
+
+Escapes a scalar string if the format supports such escaping and returns the
+sanitized version.
+
+=back
+
+=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
+
+package WXPOS::Template;
+
+use warnings;
+use strict;
+use Carp;
+
+use Error qw(:try);
+use File::Copy "cp";
+
+sub available_formats {
+    my @retval = ( 'TXT');
+    if (eval {require WXPOS::Template::LaTeX}){
+        push @retval, 'PDF', 'PS';
+    }
+    return ..hidden..;
+}
+
+sub new {
+	my $class = shift;
+	my $self = {};
+	my %args = @_;
+
+	$self->{myconfig} = $args{user};
+	$self->{template} = $args{template};
+	$self->{format} = $args{format};
+	$self->{language} = $args{language};
+	$self->{no_escape} = $args{no_escape};
+	$self->{debug} = $args{debug};
+        $self->{binmode} = undef;
+	$self->{outputfile} =
+		"${WXPOS::Sysconfig::tempdir}/$args{output_file}" if
+		$args{output_file};
+	$self->{include_path} = $args{path};
+	$self->{locale} = $args{locale};
+	$self->{noauto} = $args{no_auto_output};
+	$self->{method} = $args{method};
+	$self->{method} ||= $args{media};
+	$self->{format_args} = $args{format_options};
+	$self->{output_args} = $args{output_options};
+
+	# SC: Muxing pre-format_args LaTeX format specifications.  Now with
+	#     DVI support.
+	if (lc $self->{format} eq 'dvi') {
+		$self->{format} = 'LaTeX';
+		$self->{format_args}{filetype} = 'dvi';
+	} elsif (lc $self->{format} eq 'pdf') {
+		$self->{format} = 'LaTeX';
+		$self->{format_args}{filetype} = 'pdf';
+	} elsif (lc $self->{format} eq 'ps' or lc $self->{format} eq 'postscript') {
+		$self->{format} = 'LaTeX';
+		$self->{format_args}{filetype} = 'ps';
+	} elsif (lc $self->{format} eq 'xlsx'){
+                $self->{format} = 'XML';
+                $self->{format_args}{filetype} = 'xlsx';
+        } elsif (lc $self->{format} eq 'XML'){
+                $self->{format} = 'XML';
+                $self->{format_args}{filetype} = 'xml';
+        }
+	bless $self, $class;
+
+	if ($self->{format} !~ /^\p{IsAlnum}+$/) {
+		throw Error::Simple "Invalid format";
+	}
+	if (!$self->{include_path}){
+		$self->{include_path} = $self->{'myconfig'}->{'templates'};
+		$self->{include_path} ||= 'templates/demo';
+	}
+
+	return $self;
+}
+
+sub new_UI {
+	my $class = shift;
+	return $class->new(@_, no_auto_ouput => 0, format => 'HTML', path => 'UI');
+}
+
+sub _valid_language {
+	my $self = shift;
+	if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
+		return 0;
+	}
+	return 1;
+}
+
+sub _preprocess {
+	my ($self, $vars) = @_;
+	return unless $self->{myconfig};
+	my $type = ref($vars);
+
+	if ($type eq 'SCALAR' || !$type){
+		return;
+	}
+	if ($type eq 'ARRAY'){
+		for (@$vars){
+			if (ref($_)){
+				$self->_preprocess($_);
+			}
+		}
+	}
+	else {
+		for my $key (keys %$vars){
+			$self->_preprocess($vars->{$key});
+		}
+	}
+}
+
+sub render {
+	my $self = shift;
+	my $vars = shift;
+        $vars->{LIST_FORMATS} = sub { return $self->available_formats} ;
+	if ($self->{format} !~ /^\p{IsAlnum}+$/) {
+		throw Error::Simple "Invalid format";
+	}
+	my $format = "WXPOS::Template::$self->{format}";
+
+#	if ($self->{myconfig}){
+#	        $self->_preprocess($vars);
+#	}
+	eval "require $format";
+	if ($@) {
+		die $@;
+	}
+
+	my $cleanvars;
+	if ($self->{no_escape}) {
+		carp 'no_escape mode enabled in rendering';
+		$cleanvars = $vars;
+	} else {
+		$cleanvars = $format->can('preprocess')->($vars);
+	}
+        $cleanvars->{escape} = sub { return $format->escape(@_)};
+	if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
+		$cleanvars->{text} = sub { return $self->escape($self->{locale}->text(@_))};
+	} 
+	else {
+            $cleanvars->{text} = sub { return $self->escape(shift @_) };
+	
+        }
+        $cleanvars->{tt_url} = sub {
+               my $str  = shift @_;
+
+               my $regex = qr/([^a-zA-Z0-9_.-])/;
+               $str =~ s/$regex/sprintf("%%%02x", ord($1))/ge;
+               return $str;
+        };
+
+	$format->can('process')->($self, $cleanvars);
+	#return $format->can('postprocess')->($self);
+	my $post = $format->can('postprocess')->($self);
+        #$logger->debug("\$format=$format \$self->{'noauto'}=$self->{'noauto'} \$self->{rendered}=$self->{rendered}");
+	if (!$self->{'noauto'}) {
+		# Clean up
+                $logger->debug("before self output");
+		$self->output(%$vars);
+                $logger->debug("after self output,but does not seem to return here!");
+		if ($self->{rendered}) {
+			unlink($self->{rendered}) or
+				throw Error::Simple 'Unable to delete output file';
+		}
+	}
+	return $post;
+}
+
+sub escape {
+    my ($self, $vars) = @_;
+    my $format = "WXPOS::Template::$self->{format}";
+    if ($format->can('escape')){
+         return $format->can('escape')->($vars);
+    } else {
+         return $vars;
+    }
+} 
+
+sub output {
+	my $self = shift;
+	my %args = @_;
+
+        for ( keys %args ) { $self->{output_args}->{$_} = $args{$_}; };
+
+	my $method = $self->{method} || $args{method} || $args{media};
+        $method = '' if !defined $method;
+	if ('email' eq lc $method) {
+		$self->_email_output;
+        } elsif (defined $args{OUT} and $args{printmode} eq '>'){ # To file
+                cp($self->{rendered}, $args{OUT}); 
+	} elsif ('print' eq lc $method) {
+		$self->_lpr_output;
+	} elsif (defined $self->{output} or lc $method eq 'screen') {
+		$self->_http_output;
+		throw CancelFurtherProcessing();
+	} elsif (defined $method and $method ne '' ) {
+		$self->_lpr_output;
+	} else {
+		$self->_http_output_file;
+	}
+        binmode (STDOUT, ':utf8'); # Reset binmode *after* sending file to
+                                   # email, printer, or screen.  For screen
+                                   # this should have no effect.  For printer
+                                   # or email, this should fix bug 884. --CT 
+}
+
+sub _http_output {
+	my ($self, $data) = @_;
+	$data ||= $self->{output};
+	if ($self->{format} !~ /^\p{IsAlnum}+$/) {
+		throw Error::Simple "Invalid format";
+	}
+
+	if (!defined $data and defined $self->{rendered}){
+		$data = "";
+                $logger->trace("begin DATA < self->{rendered}=$self->{rendered} \$self->{format}=$self->{format}");
+		open (DATA, '<', $self->{rendered});
+                binmode DATA, $self->{binmode};
+		while (my $line = <DATA>){
+			$data .= $line;
+		}
+                $logger->trace("end DATA < self->{rendered}");
+	        unlink($self->{rendered}) or throw Error::Simple 'Unable to delete output file';
+	}
+
+	my $format = "WXPOS::Template::$self->{format}";
+	my $disposition = "";
+	my $name = $format->can('postprocess')->($self) || $self->{rendered};
+	if ($name) {
+		$name =~ s#^.*/##;
+		$disposition .= qq|\nContent-Disposition: attachment; filename="$name"|;
+	}
+        if (!$ENV{LSMB_NOHEAD}){
+ 	    if ($self->{mimetype} =~ /^text/) {
+		print "Content-Type: $self->{mimetype}; charset=utf-8$disposition\n\n";
+	    } else {
+		print "Content-Type: $self->{mimetype}$disposition\n\n";
+	    }
+        }
+	binmode STDOUT, $self->{binmode};
+	print $data;
+        $logger->trace("end print to STDOUT");
+}
+
+sub _http_output_file {
+	my $self = shift;
+	my $FH;
+
+	open($FH, '<:bytes', $self->{rendered}) or
+		die 'Unable to open rendered file';
+	my $data;
+	{
+		local $/;
+		$data = <$FH>;
+	}
+	close($FH);
+	
+	$self->_http_output($data);
+	
+	unlink($self->{rendered}) or
+		throw Error::Simple 'Unable to delete output file';
+	throw CancelFurtherProcessing();
+}
+
+sub _lpr_output {
+	my ($self, $in_args) = shift;
+	my $args = $self->{output_args};
+	if ($self->{format} ne 'LaTeX') {
+		throw Error::Simple "Invalid Format";
+	}
+	my $lpr = $WXPOS::Sysconfig::printer{$args->{media}};
+
+	open (LPR, '|-', $lpr);
+
+	# Output is not defined here.  In the future we should consider
+	# changing this to use the system command and hit the file as an arg.
+	#  -- CT
+	open (FILE, '<', "$self->{rendered}");
+	while (my $line = <FILE>){
+		print LPR $line;
+	}
+	close(LPR);
+}
+
+
+1;

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


------------------------------------------------------------------------------
November Webinars for C, C++, Fortran Developers
Accelerate application performance with scalable programming models. Explore
techniques for threading, error checking, porting, and tuning. Get the most 
from the latest Intel processors and coprocessors. See abstracts and register
http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
_______________________________________________
Ledger-smb-commits mailing list
..hidden..
https://lists.sourceforge.net/lists/listinfo/ledger-smb-commits