summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 28c1f40e224b441f4e9b73476b30b838a75d6d88 (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 warnings;
  73. use strict;
  74. use Carp;
  75. use Error qw(:try);
  76. use LedgerSMB::Sysconfig;
  77. use LedgerSMB::Mailer;
  78. sub new {
  79. my $class = shift;
  80. my $self = {};
  81. my %args = @_;
  82. $self->{myconfig} = $args{user};
  83. $self->{template} = $args{template};
  84. $self->{format} = $args{format};
  85. $self->{language} = $args{language};
  86. $self->{no_escape} = $args{no_escape};
  87. $self->{debug} = $args{debug};
  88. $self->{outputfile} =
  89. "${LedgerSMB::Sysconfig::tempdir}/$args{output_file}" if
  90. $args{output_file};
  91. $self->{include_path} = $args{path};
  92. $self->{locale} = $args{locale};
  93. $self->{noauto} = $args{no_auto_output};
  94. $self->{method} = $args{method};
  95. $self->{method} ||= $args{media};
  96. $self->{format_args} = $args{format_options};
  97. $self->{output_args} = $args{output_options};
  98. # SC: Muxing pre-format_args LaTeX format specifications. Now with
  99. # DVI support.
  100. if (lc $self->{format} eq 'dvi') {
  101. $self->{format} = 'LaTeX';
  102. $self->{format_args}{filetype} = 'dvi';
  103. } elsif (lc $self->{format} eq 'pdf') {
  104. $self->{format} = 'LaTeX';
  105. $self->{format_args}{filetype} = 'pdf';
  106. } elsif (lc $self->{format} eq 'ps' or lc $self->{format} eq 'postscript') {
  107. $self->{format} = 'LaTeX';
  108. $self->{format_args}{filetype} = 'ps';
  109. }
  110. bless $self, $class;
  111. if ($self->{format} !~ /^\p{IsAlnum}+$/) {
  112. throw Error::Simple "Invalid format";
  113. }
  114. if (!$self->{include_path}){
  115. ## SC: XXX hardcoding due to config migration, will need adjustment
  116. $self->{include_path} = $self->{'myconfig'}->{'templates'};
  117. $self->{include_path} ||= 'templates/demo';
  118. if (defined $self->{language}){
  119. if (!$self->_valid_language){
  120. throw Error::Simple 'Invalid language';
  121. return undef;
  122. }
  123. $self->{include_path} = "$self->{'include_path'}"
  124. ."/$self->{language}"
  125. .";$self->{'include_path'}"
  126. }
  127. }
  128. return $self;
  129. }
  130. sub new_UI {
  131. my $class = shift;
  132. return $class->new(@_, no_auto_ouput => 0, format => 'HTML', path => 'UI');
  133. }
  134. sub _valid_language {
  135. my $self = shift;
  136. if ($self->{language} =~ m#(/|\\|:|\.\.|^\.)#){
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. sub render {
  142. my $self = shift;
  143. my $vars = shift;
  144. if ($self->{format} !~ /^\p{IsAlnum}+$/) {
  145. throw Error::Simple "Invalid format";
  146. }
  147. my $format = "LedgerSMB::Template::$self->{format}";
  148. eval "require $format";
  149. if ($@) {
  150. throw Error::Simple $@;
  151. }
  152. my $cleanvars;
  153. if ($self->{no_escape}) {
  154. carp 'no_escape mode enabled in rendering';
  155. $cleanvars = $vars;
  156. } else {
  157. $cleanvars = $format->can('preprocess')->($vars);
  158. }
  159. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  160. $cleanvars->{text} = sub { return $self->{locale}->text(@_)};
  161. } else {
  162. $cleanvars->{text} = sub { return shift @_ };
  163. }
  164. $format->can('process')->($self, $cleanvars);
  165. #return $format->can('postprocess')->($self);
  166. my $post = $format->can('postprocess')->($self);
  167. if (!$self->{'noauto'}) {
  168. $self->output;
  169. # Clean up
  170. if ($self->{rendered}) {
  171. unlink($self->{rendered}) or
  172. throw Error::Simple 'Unable to delete output file';
  173. }
  174. }
  175. return $post;
  176. }
  177. sub output {
  178. my $self = shift;
  179. my %args = @_;
  180. my $method = $self->{method} || $args{method} || $args{media};
  181. if ('email' eq lc $method) {
  182. $self->_email_output;
  183. } elsif ('print' eq lc $method) {
  184. $self->_lpr_output;
  185. } elsif (defined $self->{output}) {
  186. $self->_http_output;
  187. exit;
  188. } else {
  189. $self->_http_output_file;
  190. }
  191. }
  192. sub _http_output {
  193. my $self = shift;
  194. my $data = shift;
  195. $data ||= $self->{output};
  196. if ($self->{format} !~ /^\p{IsAlnum}+$/) {
  197. throw Error::Simple "Invalid format";
  198. }
  199. my $format = "LedgerSMB::Template::$self->{format}";
  200. my $disposition = "";
  201. my $name = $format->can('postprocess')->($self);
  202. if ($name) {
  203. $name =~ s#^.*/##;
  204. $disposition .= qq|\nContent-Disposition: attachment; filename="$name"|;
  205. }
  206. if ($self->{mimetype} =~ /^text/) {
  207. print "Content-Type: $self->{mimetype}; charset=utf-8$disposition\n\n";
  208. binmode STDOUT, ':utf8';
  209. } else {
  210. print "Content-Type: $self->{mimetype}$disposition\n\n";
  211. binmode STDOUT, ':bytes';
  212. }
  213. print $data;
  214. binmode STDOUT, ':utf8';
  215. }
  216. sub _http_output_file {
  217. my $self = shift;
  218. my $FH;
  219. open($FH, '<:bytes', $self->{rendered}) or
  220. throw Error::Simple 'Unable to open rendered file';
  221. my $data;
  222. {
  223. local $/;
  224. $data = <$FH>;
  225. }
  226. close($FH);
  227. $self->_http_output($data);
  228. unlink($self->{rendered}) or
  229. throw Error::Simple 'Unable to delete output file';
  230. exit;
  231. }
  232. sub _email_output {
  233. my $self = shift;
  234. my $args = $self->{output_args};
  235. my @mailmime;
  236. if (!$self->{rendered} and !$args->{attach}) {
  237. $args->{message} .= $self->{output};
  238. @mailmime = ('contenttype', $self->{mimeytype});
  239. }
  240. my $mail = new LedgerSMB::Mailer(
  241. from => $args->{from} || $self->{user}->{email},
  242. to => $args->{to},
  243. cc => $args->{cc},
  244. bcc => $args->{bcc},
  245. subject => $args->{subject},
  246. notify => $args->{notify},
  247. message => $args->{message},
  248. @mailmime,
  249. );
  250. if ($args->{attach} or $self->{mimetype} !~ m#^text/# or $self->{rendered}) {
  251. my @attachment;
  252. my $name = $args->{filename};
  253. if ($self->{rendered}) {
  254. @attachment = ('file', $self->{rendered});
  255. $name ||= $self->{rendered};
  256. } else {
  257. @attachment = ('data', $self->{output});
  258. }
  259. $mail->attach(
  260. mimetype => $self->{mimetype},
  261. filename => $name,
  262. strip => $$,
  263. @attachment,
  264. );
  265. }
  266. $mail->send;
  267. }
  268. sub _lpr_output {
  269. my $self = shift;
  270. #TODO stub
  271. }
  272. 1;