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