summaryrefslogtreecommitdiff
path: root/LedgerSMB
diff options
context:
space:
mode:
Diffstat (limited to 'LedgerSMB')
-rwxr-xr-xLedgerSMB/Template.pm67
-rwxr-xr-xLedgerSMB/Template/HTML.pm31
-rwxr-xr-xLedgerSMB/Template/PDF.pm82
-rwxr-xr-xLedgerSMB/Template/PS.pm83
-rwxr-xr-xLedgerSMB/Template/TXT.pm83
5 files changed, 324 insertions, 22 deletions
diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm
index 77c7a78a..0a822f7d 100755
--- a/LedgerSMB/Template.pm
+++ b/LedgerSMB/Template.pm
@@ -3,10 +3,9 @@
LedgerSMB::Template - Template support module for LedgerSMB
-=head1 SYOPSIS
+=head1 SYNOPSIS
-This module renders templates to provide HTML interfaces. LaTeX support
-forthcoming.
+This module renders templates.
=head1 METHODS
@@ -25,6 +24,10 @@ include_path allows one to override the template directory and use this with use
This command renders the template and writes the result to standard 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,
@@ -43,7 +46,6 @@ your software.
package LedgerSMB::Template;
use Error qw(:try);
-use Template;
use LedgerSMB::Sysconfig;
sub new {
@@ -55,7 +57,13 @@ sub new {
$self->{template} = $args{template};
$self->{format} = $args{format};
$self->{language} = $args{language};
- $self->{output} = '';
+ if ($args{outputfile}) {
+ $self->{outputfile} =
+ "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
+ } else {
+ $self->{outputfile} =
+ "${LedgerSMB::Sysconfig::tempdir}/$args{template}-output";
+ }
$self->{include_path} = $args{path};
$self->{locale} = $args{locale};
@@ -89,16 +97,8 @@ sub _valid_language {
sub render {
my $self = shift;
my $vars = shift;
- my $template;
my $format = "LedgerSMB::Template::$self->{format}";
- $template = Template->new({
- INCLUDE_PATH => $self->{include_path},
- START_TAG => quotemeta('<?lsmb'),
- END_TAG => quotemeta('?>'),
- DELIMITER => ';',
- }) || throw Error::Simple Template->error();
-
eval "require $format";
if ($@) {
throw Error::Simple $@;
@@ -108,15 +108,44 @@ sub render {
if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
$cleanvars->{text} = $self->{locale}->text();
}
- if (not $template->process(
- $format->can('get_template')->($self->{template}),
- $cleanvars, \$self->{output}, binmode => ':utf8')) {
- throw Error::Simple $template->error();
+
+ $format->can('process')->($self, $cleanvars);
+ return $format->can('postprocess')->($self);
+}
+
+sub output {
+ my $self = shift;
+ my $method = shift;
+
+ if ('mail' eq lc $method) {
+ #XXX do something
+ $self->_http_output;
+ } elsif ('print' eq lc $method) {
+ #XXX do something
+ $self->_http_output;
+ } else {
+ $self->_http_output;
}
+}
- $format->can('postprocess')->($self);
+sub _http_output {
+ my $self = shift;
+ my $FH;
- return $self->{output};
+ if ($self->{mimetype} =~ /^text/) {
+ print "Content-Type: $self->{mimetype}; charset=utf-8\n\n";
+ } else {
+ print "Content-Type: $self->{mimetype}\n\n";
+ }
+ open($FH, '<', $self->{rendered}) or
+ throw Error::Simple 'Unable to open rendered file';
+ while (<$FH>) {
+ print $_;
+ }
+ close($FH);
+ unlink($self->{rendered}) or
+ throw Error::Simple 'Unable to delete output file';
+ exit;
}
1;
diff --git a/LedgerSMB/Template/HTML.pm b/LedgerSMB/Template/HTML.pm
index d052319d..d02000a9 100755
--- a/LedgerSMB/Template/HTML.pm
+++ b/LedgerSMB/Template/HTML.pm
@@ -7,7 +7,7 @@ LedgerSMB::Template::HTML Template support module for LedgerSMB
=over
-=item get_template ()
+=item get_template ($name)
Returns the appropriate template filename for this format.
@@ -16,7 +16,11 @@ Returns the appropriate template filename for this format.
This method returns a reference to a hash that contains a copy of the passed
hashref's data with HTML entities converted to escapes.
-=item postprocess ()
+=item process ($parent, $cleanvars)
+
+Processes the template for HTML.
+
+=item postprocess ($parent)
Currently does nothing.
@@ -37,6 +41,7 @@ package LedgerSMB::Template::HTML;
use Error qw(:try);
use CGI;
+use Template;
sub get_template {
my $name = shift;
@@ -65,9 +70,29 @@ sub preprocess {
return $vars;
}
+sub process {
+ my $parent = shift;
+ my $cleanvars = shift;
+ my $template;
+
+ $template = Template->new({
+ INCLUDE_PATH => $parent->{include_path},
+ START_TAG => quotemeta('<?lsmb'),
+ END_TAG => quotemeta('?>'),
+ DELIMITER => ';',
+ }) || throw Error::Simple Template->error();
+ if (not $template->process(
+ get_template($parent->{template}),
+ $cleanvars, "$parent->{outputfile}.html", binmode => ':utf8')) {
+ throw Error::Simple $template->error();
+ }
+ $parent->{mimetype} = 'text/html';
+}
+
sub postprocess {
my $parent = shift;
- return;
+ $parent->{rendered} = "$parent->{outputfile}.html";
+ return "$parent->{outputfile}.html";
}
1;
diff --git a/LedgerSMB/Template/PDF.pm b/LedgerSMB/Template/PDF.pm
new file mode 100755
index 00000000..72c080c6
--- /dev/null
+++ b/LedgerSMB/Template/PDF.pm
@@ -0,0 +1,82 @@
+
+=head1 NAME
+
+LedgerSMB::Template::PDF Template support module for LedgerSMB
+
+=head1 METHODS
+
+=over
+
+=item get_template ($name)
+
+Returns the appropriate template filename for this format.
+
+=item preprocess ($vars)
+
+Currently does nothing.
+
+=item process ($parent, $cleanvars)
+
+Processes the template for PDF.
+
+=item postprocess ($parent)
+
+Currently does nothing.
+
+=back
+
+=head1 Copyright (C) 2007, The LedgerSMB core team.
+
+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. For a full list
+including contact information of contributors, maintainers, and copyright
+holders, see the CONTRIBUTORS file.
+=cut
+
+package LedgerSMB::Template::PDF;
+
+use Error qw(:try);
+use Template::Latex;
+
+sub get_template {
+ my $name = shift;
+ return "${name}.tex";
+}
+sub preprocess {
+ my $rawvars = shift;
+ my $vars;
+ my $type = ref $rawvars;
+ return $vars;
+}
+
+sub process {
+ my $parent = shift;
+ my $cleanvars = shift;
+ my $template;
+
+ $template = Template::Latex->new({
+ LATEX_FORMAT => 'pdf',
+ INCLUDE_PATH => $parent->{include_path},
+ START_TAG => quotemeta('<?lsmb'),
+ END_TAG => quotemeta('?>'),
+ DELIMITER => ';',
+ }) || throw Error::Simple Template::Latex->error();
+
+ if (not $template->process(
+ get_template($parent->{template}),
+ $cleanvars, "$parent->{outputfile}.pdf", binmode => ':utf8')) {
+ throw Error::Simple $template->error();
+ }
+ $parent->{mimetype} = 'application/pdf';
+}
+
+sub postprocess {
+ my $parent = shift;
+ $parent->{rendered} = "$parent->{outputfile}.pdf";
+ return "$parent->{outputfile}.pdf";
+}
+
+1;
diff --git a/LedgerSMB/Template/PS.pm b/LedgerSMB/Template/PS.pm
new file mode 100755
index 00000000..55349113
--- /dev/null
+++ b/LedgerSMB/Template/PS.pm
@@ -0,0 +1,83 @@
+
+=head1 NAME
+
+LedgerSMB::Template::PS Template support module for LedgerSMB
+
+=head1 METHODS
+
+=over
+
+=item get_template ($name)
+
+Returns the appropriate template filename for this format.
+
+=item preprocess ($vars)
+
+Currently does nothing.
+
+=item process ($parent, $cleanvars)
+
+Processes the template for Postscript.
+
+=item postprocess ($parent)
+
+Currently does nothing.
+
+=back
+
+=head1 Copyright (C) 2007, The LedgerSMB core team.
+
+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. For a full list
+including contact information of contributors, maintainers, and copyright
+holders, see the CONTRIBUTORS file.
+=cut
+
+package LedgerSMB::Template::PS;
+
+use Error qw(:try);
+use Template::Latex;
+
+sub get_template {
+ my $name = shift;
+ return "${name}.tex";
+}
+
+sub preprocess {
+ my $rawvars = shift;
+ my $vars;
+ my $type = ref $rawvars;
+ return $vars;
+}
+
+sub process {
+ my $parent = shift;
+ my $cleanvars = shift;
+ my $template;
+
+ $template = Template::Latex->new({
+ LATEX_FORMAT => 'ps',
+ INCLUDE_PATH => $parent->{include_path},
+ START_TAG => quotemeta('<?lsmb'),
+ END_TAG => quotemeta('?>'),
+ DELIMITER => ';',
+ }) || throw Error::Simple Template::Latex->error();
+
+ if (not $template->process(
+ get_template($parent->{template}),
+ $cleanvars, "$parent->{outputfile}.ps", binmode => ':utf8')) {
+ throw Error::Simple $template->error();
+ }
+ $parent->{mimetype} = 'application/postscript';
+}
+
+sub postprocess {
+ my $parent = shift;
+ $parent->{rendered} = "$parent->{outputfile}.ps";
+ return "$parent->{outputfile}.ps";
+}
+
+1;
diff --git a/LedgerSMB/Template/TXT.pm b/LedgerSMB/Template/TXT.pm
new file mode 100755
index 00000000..d7a76430
--- /dev/null
+++ b/LedgerSMB/Template/TXT.pm
@@ -0,0 +1,83 @@
+
+=head1 NAME
+
+LedgerSMB::Template::TXT Template support module for LedgerSMB
+
+=head1 METHODS
+
+=over
+
+=item get_template ($name)
+
+Returns the appropriate template filename for this format.
+
+=item preprocess ($vars)
+
+Currently does nothing.
+
+=item process ($parent, $cleanvars)
+
+Processes the template for text.
+
+=item postprocess ($parent)
+
+Currently does nothing.
+
+=back
+
+=head1 Copyright (C) 2007, The LedgerSMB core team.
+
+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. For a full list
+including contact information of contributors, maintainers, and copyright
+holders, see the CONTRIBUTORS file.
+=cut
+
+package LedgerSMB::Template::TXT;
+
+use Error qw(:try);
+use Template;
+
+sub get_template {
+ my $name = shift;
+ return "${name}.txt";
+}
+
+sub preprocess {
+ my $rawvars = shift;
+ my $vars;
+ my $type = ref $rawvars;
+
+ return $vars;
+}
+
+sub process {
+ my $parent = shift;
+ my $cleanvars = shift;
+ my $template;
+
+ $template = Template->new({
+ INCLUDE_PATH => $parent->{include_path},
+ START_TAG => quotemeta('<?lsmb'),
+ END_TAG => quotemeta('?>'),
+ DELIMITER => ';',
+ }) || throw Error::Simple Template->error();
+
+ if (not $template->process(
+ get_template($parent->{template}),
+ $cleanvars, "$parent->{outputfile}.txt", binmode => ':utf8')) {
+ throw Error::Simple $template->error();
+ }
+ $parent->{mimetype} = 'text/plain';
+}
+
+sub postprocess {
+ my $parent = shift;
+ $parent->{rendered} = "$parent->{outputfile}.txt";
+ return "$parent->{outputfile}.txt";
+}
+
+1;