summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 2432b5f143c01b8a88b7a0963510b1ef8b8f8c95 (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], [no_auto_output => $bool], [method => $string] );
  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. no_auto_output disables the automatic output of rendered templates.
  14. method is the output method to use, defaults to HTTP
  15. media is a synonym for method
  16. =item render($hashref)
  17. This command renders the template and writes the result to standard output.
  18. Currently email and server-side printing are not supported.
  19. =item output
  20. This function outputs the rendered file in an appropriate manner.
  21. =item my $bool = _valid_language()
  22. This command checks for valid langages. Returns 1 if the language is valid,
  23. 0 if it is not.
  24. =back
  25. =head1 Copyright 2007, The LedgerSMB Core Team
  26. This file is licensed under the GNU General Public License version 2, or at your
  27. option any later version. A copy of the license should have been included with
  28. your software.
  29. =cut
  30. package LedgerSMB::Template;
  31. use Error qw(:try);
  32. use LedgerSMB::Sysconfig;
  33. use LedgerSMB::Mailer;
  34. sub new {
  35. my $class = shift;
  36. my $self = {};
  37. my %args = @_;
  38. $self->{myconfig} = $args{user};
  39. $self->{template} = $args{template};
  40. $self->{format} = $args{format};
  41. $self->{format} = 'PS' if lc $self->{format} eq 'postscript';
  42. $self->{language} = $args{language};
  43. if ($args{outputfile}) {
  44. $self->{outputfile} =
  45. "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
  46. } else {
  47. $self->{outputfile} =
  48. "${LedgerSMB::Sysconfig::tempdir}/$args{template}-output-$$";
  49. }
  50. $self->{include_path} = $args{path};
  51. $self->{locale} = $args{locale};
  52. $self->{noauto} = $args{noauto};
  53. $self->{method} = $args{method};
  54. $self->{method} ||= $args{media};
  55. bless $self, $class;
  56. if (!$self->{include_path}){
  57. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  58. if (defined $self->{language}){
  59. if (!$self->_valid_language){
  60. throw Error::Simple 'Invalid language';
  61. return undef;
  62. }
  63. $self->{include_path} = "$self->{'include_path'}"
  64. ."/$self->{language}"
  65. .";$self->{'include_path'}"
  66. }
  67. }
  68. return $self;
  69. }
  70. sub _valid_language {
  71. my $self = shift;
  72. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  73. return 0;
  74. }
  75. return 1;
  76. }
  77. sub render {
  78. my $self = shift;
  79. my $vars = shift;
  80. my $format = "LedgerSMB::Template::$self->{format}";
  81. eval "require $format";
  82. if ($@) {
  83. throw Error::Simple $@;
  84. }
  85. my $cleanvars = $format->can('preprocess')->($vars);
  86. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  87. $cleanvars->{text} = sub { return $self->{locale}->text(@_)};
  88. }
  89. $format->can('process')->($self, $cleanvars);
  90. #return $format->can('postprocess')->($self);
  91. my $post = $format->can('postprocess')->($self);
  92. if (!$self->{'noauto'}) {
  93. $self->output;
  94. }
  95. return $post;
  96. }
  97. sub output {
  98. my $self = shift;
  99. my %args = @_;
  100. my $method = $self->{method} || $args{method} || $args{media};
  101. if ('email' eq lc $method) {
  102. $self->_email_output;
  103. } elsif ('print' eq lc $method) {
  104. $self->_lpr_output;
  105. } else {
  106. $self->_http_output;
  107. }
  108. }
  109. sub _http_output {
  110. my $self = shift;
  111. my $FH;
  112. if ($self->{mimetype} =~ /^text/) {
  113. print "Content-Type: $self->{mimetype}; charset=utf-8\n\n";
  114. } else {
  115. print "Content-Type: $self->{mimetype}\n\n";
  116. }
  117. open($FH, '<:bytes', $self->{rendered}) or
  118. throw Error::Simple 'Unable to open rendered file';
  119. my $data;
  120. {
  121. local $/;
  122. $data = <$FH>;
  123. }
  124. close($FH);
  125. binmode STDOUT, ':bytes';
  126. print $data;
  127. binmode STDOUT, ':utf8';
  128. unlink($self->{rendered}) or
  129. throw Error::Simple 'Unable to delete output file';
  130. exit;
  131. }
  132. sub _email_output {
  133. my $self = shift;
  134. my $mail = new LedgerSMB::Mailer;
  135. #TODO stub
  136. }
  137. sub _lpr_output {
  138. my $self = shift;
  139. #TODO stub
  140. }
  141. 1;