summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: c982a94d77ceb17fd54ab2f834d5e11b14c1631e (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 => $string, [locale => $locale] [language => $string], [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool], [debug => $bool], [output_file => $string] );
  8. This command instantiates a new template:
  9. =over
  10. =item template
  11. The name of the template file to be processed.
  12. =item format
  13. The format to be used. Currently HTML, PS, PDF, TXT and CSV are supported.
  14. =item locale (optional)
  15. The locale object to use for regular gettext lookups. Having this option adds
  16. the text function to the usable list for the templates. Has no effect on the
  17. gettext function.
  18. =item language (optional)
  19. The language for template selection.
  20. =item include_path (optional)
  21. Overrides the template directory. Used with user interface templates.
  22. =item no_auto_output (optional)
  23. Disables the automatic output of rendered templates.
  24. =item no_escape (optional)
  25. Disables escaping on the template variables.
  26. =item debug (optional)
  27. Enables template debugging.
  28. With the TT-based renderers, HTML, PS, PDF, TXT, and CSV, the portion of the
  29. template to get debugging messages is to be surrounded by
  30. <?lsmb DEBUG format 'foo' ?> statements. Example:
  31. <tr><td colspan="<?lsmb columns.size ?>"></td></tr>
  32. <tr class="listheading">
  33. <?lsmb FOREACH column IN columns ?>
  34. <?lsmb DEBUG format '$file line $line : [% $text %]' ?>
  35. <th class="listtop"><?lsmb heading.$column ?></th>
  36. <?lsmb DEBUG format '' ?>
  37. <?lsmb END ?>
  38. </tr>
  39. =item method/media (optional)
  40. The output method to use, defaults to HTTP. Media is a synonym for method
  41. =item output_file (optional)
  42. The base name of the file for output.
  43. =back
  44. =item render($hashref)
  45. This command renders the template. If no_auto_output was not specified during
  46. instantiation, this also writes the result to standard output and exits.
  47. Otherwise it returns the name of the output file if a file was created. When
  48. no output file is created, the output is held in $self->{output}.
  49. Currently email and server-side printing are not supported.
  50. =item output
  51. This function outputs the rendered file in an appropriate manner.
  52. =item my $bool = _valid_language()
  53. This command checks for valid langages. Returns 1 if the language is valid,
  54. 0 if it is not.
  55. =back
  56. =head1 Copyright 2007, The LedgerSMB Core Team
  57. This file is licensed under the GNU General Public License version 2, or at your
  58. option any later version. A copy of the license should have been included with
  59. your software.
  60. =cut
  61. package LedgerSMB::Template;
  62. use Error qw(:try);
  63. use LedgerSMB::Sysconfig;
  64. use LedgerSMB::Mailer;
  65. sub new {
  66. my $class = shift;
  67. my $self = {};
  68. my %args = @_;
  69. $self->{myconfig} = $args{user};
  70. $self->{template} = $args{template};
  71. $self->{format} = $args{format};
  72. $self->{format} = 'PS' if lc $self->{format} eq 'postscript';
  73. $self->{language} = $args{language};
  74. $self->{no_escape} = $args{no_escape};
  75. $self->{debug} = $args{debug};
  76. $self->{outputfile} =
  77. "${LedgerSMB::Sysconfig::tempdir}/$args{output_file}" if
  78. $args{output_file};
  79. $self->{include_path} = $args{path};
  80. $self->{locale} = $args{locale};
  81. $self->{noauto} = $args{no_auto_output};
  82. $self->{method} = $args{method};
  83. $self->{method} ||= $args{media};
  84. bless $self, $class;
  85. if (!$self->{include_path}){
  86. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  87. if (defined $self->{language}){
  88. if (!$self->_valid_language){
  89. throw Error::Simple 'Invalid language';
  90. return undef;
  91. }
  92. $self->{include_path} = "$self->{'include_path'}"
  93. ."/$self->{language}"
  94. .";$self->{'include_path'}"
  95. }
  96. }
  97. return $self;
  98. }
  99. sub _valid_language {
  100. my $self = shift;
  101. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. sub render {
  107. my $self = shift;
  108. my $vars = shift;
  109. my $format = "LedgerSMB::Template::$self->{format}";
  110. eval "require $format";
  111. if ($@) {
  112. throw Error::Simple $@;
  113. }
  114. my $cleanvars;
  115. if ($self->{no_escape}) {
  116. $cleanvars = $vars;
  117. } else {
  118. $cleanvars = $format->can('preprocess')->($vars);
  119. }
  120. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  121. $cleanvars->{text} = sub { return $self->{locale}->text(@_)};
  122. }
  123. $format->can('process')->($self, $cleanvars);
  124. #return $format->can('postprocess')->($self);
  125. my $post = $format->can('postprocess')->($self);
  126. if (!$self->{'noauto'}) {
  127. $self->output;
  128. }
  129. return $post;
  130. }
  131. sub output {
  132. my $self = shift;
  133. my %args = @_;
  134. my $method = $self->{method} || $args{method} || $args{media};
  135. if ('email' eq lc $method) {
  136. $self->_email_output;
  137. } elsif ('print' eq lc $method) {
  138. $self->_lpr_output;
  139. } elsif (defined $self->{output}) {
  140. $self->_http_output;
  141. exit;
  142. } else {
  143. $self->_http_output_file;
  144. }
  145. }
  146. sub _http_output {
  147. my $self = shift;
  148. my $data = shift;
  149. $data ||= $self->{output};
  150. my $FH;
  151. if ($self->{mimetype} =~ /^text/) {
  152. print "Content-Type: $self->{mimetype}; charset=utf-8\n\n";
  153. } else {
  154. print "Content-Type: $self->{mimetype}\n\n";
  155. }
  156. binmode STDOUT, ':bytes';
  157. print $data;
  158. binmode STDOUT, ':utf8';
  159. }
  160. sub _http_output_file {
  161. my $self = shift;
  162. my $FH;
  163. open($FH, '<:bytes', $self->{rendered}) or
  164. throw Error::Simple 'Unable to open rendered file';
  165. my $data;
  166. {
  167. local $/;
  168. $data = <$FH>;
  169. }
  170. close($FH);
  171. $self->_http_output($data);
  172. unlink($self->{rendered}) or
  173. throw Error::Simple 'Unable to delete output file';
  174. exit;
  175. }
  176. sub _email_output {
  177. my $self = shift;
  178. my $mail = new LedgerSMB::Mailer;
  179. #TODO stub
  180. }
  181. sub _lpr_output {
  182. my $self = shift;
  183. #TODO stub
  184. }
  185. 1;