summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 0c396ac6f4df430dea4cc3965509b07073d3e1f9 (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 _preprocess {
  142. my ($self, $vars) = @_;
  143. return unless $self->{myconfig};
  144. use LedgerSMB;
  145. if (UNIVERSAL::isa($vars, 'Math::BigFloat')){
  146. $vars =
  147. LedgerSMB::format_amount('LedgerSMB',
  148. amount => $vars,
  149. user => $self->{myconfig},
  150. precision => 2);
  151. }
  152. my $type = ref($vars);
  153. if ($type eq 'SCALAR' || !$type){
  154. return;
  155. }
  156. if ($type eq 'ARRAY'){
  157. for (@$vars){
  158. if (ref($_)){
  159. $self->_preprocess($_);
  160. }
  161. }
  162. }
  163. else {
  164. for my $key (keys %$vars){
  165. $self->_preprocess($vars->{$key});
  166. }
  167. }
  168. }
  169. sub render {
  170. my $self = shift;
  171. my $vars = shift;
  172. if ($self->{format} !~ /^\p{IsAlnum}+$/) {
  173. throw Error::Simple "Invalid format";
  174. }
  175. my $format = "LedgerSMB::Template::$self->{format}";
  176. # if ($self->{myconfig}){
  177. # $self->_preprocess($vars);
  178. # }
  179. eval "require $format";
  180. if ($@) {
  181. throw Error::Simple $@;
  182. }
  183. my $cleanvars;
  184. if ($self->{no_escape}) {
  185. carp 'no_escape mode enabled in rendering';
  186. $cleanvars = $vars;
  187. } else {
  188. $cleanvars = $format->can('preprocess')->($vars);
  189. }
  190. if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){
  191. $cleanvars->{text} = sub { return $self->{locale}->text(@_)};
  192. } else {
  193. $cleanvars->{text} = sub { return shift @_ };
  194. }
  195. $format->can('process')->($self, $cleanvars);
  196. #return $format->can('postprocess')->($self);
  197. my $post = $format->can('postprocess')->($self);
  198. if (!$self->{'noauto'}) {
  199. $self->output;
  200. # Clean up
  201. if ($self->{rendered}) {
  202. unlink($self->{rendered}) or
  203. throw Error::Simple 'Unable to delete output file';
  204. }
  205. }
  206. return $post;
  207. }
  208. sub output {
  209. my $self = shift;
  210. my %args = @_;
  211. my $method = $self->{method} || $args{method} || $args{media};
  212. if ('email' eq lc $method) {
  213. $self->_email_output;
  214. } elsif ('print' eq lc $method) {
  215. $self->_lpr_output;
  216. } elsif (defined $self->{output}) {
  217. $self->_http_output;
  218. exit;
  219. } else {
  220. $self->_http_output_file;
  221. }
  222. }
  223. sub _http_output {
  224. my $self = shift;
  225. my $data = shift;
  226. $data ||= $self->{output};
  227. if ($self->{format} !~ /^\p{IsAlnum}+$/) {
  228. throw Error::Simple "Invalid format";
  229. }
  230. my $format = "LedgerSMB::Template::$self->{format}";
  231. my $disposition = "";
  232. my $name = $format->can('postprocess')->($self);
  233. if ($name) {
  234. $name =~ s#^.*/##;
  235. $disposition .= qq|\nContent-Disposition: attachment; filename="$name"|;
  236. }
  237. if ($self->{mimetype} =~ /^text/) {
  238. print "Content-Type: $self->{mimetype}; charset=utf-8$disposition\n\n";
  239. binmode STDOUT, ':utf8';
  240. } else {
  241. print "Content-Type: $self->{mimetype}$disposition\n\n";
  242. binmode STDOUT, ':bytes';
  243. }
  244. print $data;
  245. binmode STDOUT, ':utf8';
  246. }
  247. sub _http_output_file {
  248. my $self = shift;
  249. my $FH;
  250. open($FH, '<:bytes', $self->{rendered}) or
  251. throw Error::Simple 'Unable to open rendered file';
  252. my $data;
  253. {
  254. local $/;
  255. $data = <$FH>;
  256. }
  257. close($FH);
  258. $self->_http_output($data);
  259. unlink($self->{rendered}) or
  260. throw Error::Simple 'Unable to delete output file';
  261. exit;
  262. }
  263. sub _email_output {
  264. my $self = shift;
  265. my $args = $self->{output_args};
  266. my @mailmime;
  267. if (!$self->{rendered} and !$args->{attach}) {
  268. $args->{message} .= $self->{output};
  269. @mailmime = ('contenttype', $self->{mimeytype});
  270. }
  271. my $mail = new LedgerSMB::Mailer(
  272. from => $args->{from} || $self->{user}->{email},
  273. to => $args->{to},
  274. cc => $args->{cc},
  275. bcc => $args->{bcc},
  276. subject => $args->{subject},
  277. notify => $args->{notify},
  278. message => $args->{message},
  279. @mailmime,
  280. );
  281. if ($args->{attach} or $self->{mimetype} !~ m#^text/# or $self->{rendered}) {
  282. my @attachment;
  283. my $name = $args->{filename};
  284. if ($self->{rendered}) {
  285. @attachment = ('file', $self->{rendered});
  286. $name ||= $self->{rendered};
  287. } else {
  288. @attachment = ('data', $self->{output});
  289. }
  290. $mail->attach(
  291. mimetype => $self->{mimetype},
  292. filename => $name,
  293. strip => $$,
  294. @attachment,
  295. );
  296. }
  297. $mail->send;
  298. }
  299. sub _lpr_output {
  300. my $self = shift;
  301. #TODO stub
  302. }
  303. 1;