summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: ba2cd22128a082e05312a814b209c0825f17adcd (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->{language} = $args{language};
  42. if ($args{outputfile}) {
  43. $self->{outputfile} =
  44. "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
  45. } else {
  46. $self->{outputfile} =
  47. "${LedgerSMB::Sysconfig::tempdir}/$args{template}-output-$$";
  48. }
  49. $self->{include_path} = $args{path};
  50. $self->{locale} = $args{locale};
  51. $self->{noauto} = $args{noauto};
  52. $self->{method} = $args{method};
  53. $self->{method} ||= $args{media};
  54. bless $self, $class;
  55. if (!$self->{include_path}){
  56. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  57. if (defined $self->{language}){
  58. if (!$self->_valid_language){
  59. throw Error::Simple 'Invalid language';
  60. return undef;
  61. }
  62. $self->{include_path} = "$self->{'include_path'}"
  63. ."/$self->{language}"
  64. .";$self->{'include_path'}"
  65. }
  66. }
  67. return $self;
  68. }
  69. sub _valid_language {
  70. my $self = shift;
  71. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. sub render {
  77. my $self = shift;
  78. my $vars = shift;
  79. my $format = "LedgerSMB::Template::$self->{format}";
  80. eval "require $format";
  81. if ($@) {
  82. throw Error::Simple $@;
  83. }
  84. my $cleanvars = $format->can('preprocess')->($vars);
  85. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  86. $cleanvars->{text} = sub { return $self->{locale}->text(@_)};
  87. }
  88. $format->can('process')->($self, $cleanvars);
  89. #return $format->can('postprocess')->($self);
  90. my $post = $format->can('postprocess')->($self);
  91. if (!$self->{'noauto'}) {
  92. $self->output;
  93. }
  94. return $post;
  95. }
  96. sub output {
  97. my $self = shift;
  98. my %args = @_;
  99. my $method = $self->{method} || $args{method} || $args{media};
  100. if ('email' eq lc $method) {
  101. $self->_email_output;
  102. } elsif ('print' eq lc $method) {
  103. $self->_lpr_output;
  104. } else {
  105. $self->_http_output;
  106. }
  107. }
  108. sub _http_output {
  109. my $self = shift;
  110. my $FH;
  111. print STDERR "Content-Type: $self->{mimetype}; charset=utf-8\n\n";
  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, '<', $self->{rendered}) or
  118. throw Error::Simple 'Unable to open rendered file';
  119. while (<$FH>) {
  120. print $_;
  121. }
  122. close($FH);
  123. unlink($self->{rendered}) or
  124. throw Error::Simple 'Unable to delete output file';
  125. exit;
  126. }
  127. sub _email_output {
  128. my $self = shift;
  129. my $mail = new LedgerSMB::Mailer;
  130. #TODO stub
  131. }
  132. sub _lpr_output {
  133. my $self = shift;
  134. #TODO stub
  135. }
  136. 1;