summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 0a822f7d919dcfb308d55c68103eaa04ca500af7 (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. sub new {
  31. my $class = shift;
  32. my $self = {};
  33. my %args = @_;
  34. $self->{myconfig} = $args{user};
  35. $self->{template} = $args{template};
  36. $self->{format} = $args{format};
  37. $self->{language} = $args{language};
  38. if ($args{outputfile}) {
  39. $self->{outputfile} =
  40. "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}";
  41. } else {
  42. $self->{outputfile} =
  43. "${LedgerSMB::Sysconfig::tempdir}/$args{template}-output";
  44. }
  45. $self->{include_path} = $args{path};
  46. $self->{locale} = $args{locale};
  47. bless $self, $class;
  48. if (!$self->{include_path}){
  49. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  50. if (defined $self->{language}){
  51. if (!$self->_valid_language){
  52. throw Error::Simple 'Invalid language';
  53. return undef;
  54. }
  55. $self->{include_path} = "$self->{'include_path'}"
  56. ."/$self->{language}"
  57. .";$self->{'include_path'}"
  58. }
  59. }
  60. return $self;
  61. }
  62. sub _valid_language {
  63. my $self = shift;
  64. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. sub render {
  70. my $self = shift;
  71. my $vars = shift;
  72. my $format = "LedgerSMB::Template::$self->{format}";
  73. eval "require $format";
  74. if ($@) {
  75. throw Error::Simple $@;
  76. }
  77. my $cleanvars = $format->can('preprocess')->($vars);
  78. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  79. $cleanvars->{text} = $self->{locale}->text();
  80. }
  81. $format->can('process')->($self, $cleanvars);
  82. return $format->can('postprocess')->($self);
  83. }
  84. sub output {
  85. my $self = shift;
  86. my $method = shift;
  87. if ('mail' eq lc $method) {
  88. #XXX do something
  89. $self->_http_output;
  90. } elsif ('print' eq lc $method) {
  91. #XXX do something
  92. $self->_http_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. 1;