summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 5568373c255d44664953422a8cc49765891bd2c7 (plain)
  1. =head1 NAME
  2. LedgerSMB::Template - Template support module for LedgerSMB
  3. =head1 SYNOPSIS
  4. This module renders templates.
  5. =head1 METHODS
  6. =over
  7. =item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path]);
  8. This command instantiates a new template:
  9. template is the file name of the template to be processed.
  10. format is the type of format to be used. Currently only HTML is supported
  11. language (optional) specifies the language for template selection.
  12. include_path allows one to override the template directory and use this with user interface templates.
  13. =item render($hashref)
  14. This command renders the template and writes the result to standard output.
  15. Currently email and server-side printing are not supported.
  16. =item output
  17. This function outputs the rendered file in an appropriate manner.
  18. =item my $bool = _valid_language()
  19. This command checks for valid langages. Returns 1 if the language is valid,
  20. 0 if it is not.
  21. =back
  22. =head1 Copyright 2007, The LedgerSMB Core Team
  23. This file is licensed under the GNU General Public License version 2, or at your
  24. option any later version. A copy of the license should have been included with
  25. your software.
  26. =cut
  27. package LedgerSMB::Template;
  28. use Error qw(:try);
  29. use LedgerSMB::Sysconfig;
  30. use LedgerSMB::Mailer;
  31. sub new {
  32. my $class = shift;
  33. my $self = {};
  34. my %args = @_;
  35. $self->{myconfig} = $args{user};
  36. $self->{template} = $args{template};
  37. $self->{format} = $args{format};
  38. $self->{language} = $args{language};
  39. if ($args{outputfile}) {
  40. $self->{outputfile} =
  41. "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
  42. } else {
  43. $self->{outputfile} =
  44. "${LedgerSMB::Sysconfig::tempdir}/$args{template}-output";
  45. }
  46. $self->{include_path} = $args{path};
  47. $self->{locale} = $args{locale};
  48. bless $self, $class;
  49. if (!$self->{include_path}){
  50. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  51. if (defined $self->{language}){
  52. if (!$self->_valid_language){
  53. throw Error::Simple 'Invalid language';
  54. return undef;
  55. }
  56. $self->{include_path} = "$self->{'include_path'}"
  57. ."/$self->{language}"
  58. .";$self->{'include_path'}"
  59. }
  60. }
  61. return $self;
  62. }
  63. sub _valid_language {
  64. my $self = shift;
  65. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  66. return 0;
  67. }
  68. return 1;
  69. }
  70. sub render {
  71. my $self = shift;
  72. my $vars = shift;
  73. my $format = "LedgerSMB::Template::$self->{format}";
  74. eval "require $format";
  75. if ($@) {
  76. throw Error::Simple $@;
  77. }
  78. my $cleanvars = $format->can('preprocess')->($vars);
  79. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  80. $cleanvars->{text} = sub { return $self->{locale}->text(@_)};
  81. }
  82. $format->can('process')->($self, $cleanvars);
  83. return $format->can('postprocess')->($self);
  84. }
  85. sub output {
  86. my $self = shift;
  87. my %args = @_;
  88. my $method = $args{method} || $args{media};
  89. if ('email' eq lc $method) {
  90. $self->_email_output;
  91. } elsif ('print' eq lc $method) {
  92. $self->_lpr_output;
  93. } else {
  94. $self->_http_output;
  95. }
  96. }
  97. sub _http_output {
  98. my $self = shift;
  99. my $FH;
  100. if ($self->{mimetype} =~ /^text/) {
  101. print "Content-Type: $self->{mimetype}; charset=utf-8\n\n";
  102. } else {
  103. print "Content-Type: $self->{mimetype}\n\n";
  104. }
  105. open($FH, '<', $self->{rendered}) or
  106. throw Error::Simple 'Unable to open rendered file';
  107. while (<$FH>) {
  108. print $_;
  109. }
  110. close($FH);
  111. unlink($self->{rendered}) or
  112. throw Error::Simple 'Unable to delete output file';
  113. exit;
  114. }
  115. sub _email_output {
  116. my $self = shift;
  117. my $mail = new LedgerSMB::Mailer;
  118. #TODO stub
  119. }
  120. sub _lpr_output {
  121. my $self = shift;
  122. #TODO stub
  123. }
  124. 1;