summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template.pm
blob: 4a69431268e411f17389538546a18d3339b2492f (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. }
  193. else {
  194. $cleanvars->{text} = sub { return shift @_ };
  195. }
  196. $format->can('process')->($self, $cleanvars);
  197. #return $format->can('postprocess')->($self);
  198. my $post = $format->can('postprocess')->($self);
  199. if (!$self->{'noauto'}) {
  200. # Clean up
  201. $self->output;
  202. if ($self->{rendered}) {
  203. unlink($self->{rendered}) or
  204. throw Error::Simple 'Unable to delete output file';
  205. }
  206. }
  207. return $post;
  208. }
  209. sub output {
  210. my $self = shift;
  211. my %args = @_;
  212. $self->{output_args} = \%args;
  213. my $method = $self->{method} || $args{method} || $args{media};
  214. if ('email' eq lc $method) {
  215. $self->_email_output;
  216. } elsif ('print' eq lc $method) {
  217. $self->_lpr_output;
  218. } elsif (defined $self->{output} or $method = 'Screen') {
  219. $self->_http_output;
  220. exit;
  221. } elsif (defined $method) {
  222. $self->_lpr_output;
  223. } else {
  224. $self->_http_output_file;
  225. }
  226. }
  227. sub _http_output {
  228. my ($self, $data) = @_;
  229. $data ||= $self->{output};
  230. if ($self->{format} !~ /^\p{IsAlnum}+$/) {
  231. throw Error::Simple "Invalid format";
  232. }
  233. if (!defined $data and defined $self->{rendered}){
  234. $data = "";
  235. open (DATA, '<', $self->{rendered});
  236. while (my $line = <DATA>){
  237. $data .= $line;
  238. }
  239. }
  240. my $format = "LedgerSMB::Template::$self->{format}";
  241. my $disposition = "";
  242. my $name = $format->can('postprocess')->($self);
  243. if ($name) {
  244. $name =~ s#^.*/##;
  245. $disposition .= qq|\nContent-Disposition: attachment; filename="$name"|;
  246. }
  247. if ($self->{mimetype} =~ /^text/) {
  248. print "Content-Type: $self->{mimetype}; charset=utf-8$disposition\n\n";
  249. binmode STDOUT, ':utf8';
  250. } else {
  251. print "Content-Type: $self->{mimetype}$disposition\n\n";
  252. binmode STDOUT, ':bytes';
  253. }
  254. print $data;
  255. binmode STDOUT, ':utf8';
  256. }
  257. sub _http_output_file {
  258. my $self = shift;
  259. my $FH;
  260. open($FH, '<:bytes', $self->{rendered}) or
  261. throw Error::Simple 'Unable to open rendered file';
  262. my $data;
  263. {
  264. local $/;
  265. $data = <$FH>;
  266. }
  267. close($FH);
  268. $self->_http_output($data);
  269. unlink($self->{rendered}) or
  270. throw Error::Simple 'Unable to delete output file';
  271. exit;
  272. }
  273. sub _email_output {
  274. my $self = shift;
  275. my $args = $self->{output_args};
  276. my @mailmime;
  277. if (!$self->{rendered} and !$args->{attach}) {
  278. $args->{message} .= $self->{output};
  279. @mailmime = ('contenttype', $self->{mimeytype});
  280. }
  281. my $mail = new LedgerSMB::Mailer(
  282. from => $args->{from} || $self->{user}->{email},
  283. to => $args->{to},
  284. cc => $args->{cc},
  285. bcc => $args->{bcc},
  286. subject => $args->{subject},
  287. notify => $args->{notify},
  288. message => $args->{message},
  289. @mailmime,
  290. );
  291. if ($args->{attach} or $self->{mimetype} !~ m#^text/# or $self->{rendered}) {
  292. my @attachment;
  293. my $name = $args->{filename};
  294. if ($self->{rendered}) {
  295. @attachment = ('file', $self->{rendered});
  296. $name ||= $self->{rendered};
  297. } else {
  298. @attachment = ('data', $self->{output});
  299. }
  300. $mail->attach(
  301. mimetype => $self->{mimetype},
  302. filename => $name,
  303. strip => $$,
  304. @attachment,
  305. );
  306. }
  307. $mail->send;
  308. }
  309. sub _lpr_output {
  310. my ($self, $in_args) = shift;
  311. my $args = $self->{output_args};
  312. if ($self->{format} ne 'LaTeX') {
  313. throw Error::Simple "Invalid Format";
  314. }
  315. my $lpr = $LedgerSMB::Sysconfig::printer{$args->{media}};
  316. open(LPR, '|-', $lpr);
  317. # Output is not defined here. In the future we should consider
  318. # changing this to use the system command and hit the file as an arg.
  319. # -- CT
  320. open (FILE, '<', "$self->{rendered}");
  321. while (my $line = <FILE>){
  322. print LPR $line;
  323. }
  324. close(LPR);
  325. }
  326. 1;