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