summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/XLS.pm
blob: acb8196e3fefeeac01ffe1765af771bd431a0cb5 (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::XLS Template support module for LedgerSMB
  3. =head1 SYNOPSIS
  4. Excel spreadsheet output. For details about the XML template document
  5. elements, see Excel::Template. For details about various parameters used, see
  6. Spreadsheet::WriteExcel. As this module uses Excel::Template::Plus, flow
  7. control and variable substitution are handled with TT with the usual for LSMB
  8. tag formatting of <?lsmb foo ?> instead of the more HTML::Template-like forms
  9. of Excel::Template.
  10. =head1 METHODS
  11. =over
  12. =item get_template ($name)
  13. Returns the appropriate template filename for this format. '.xlst' is the
  14. extension that was chosen for the templates.
  15. =item preprocess ($vars)
  16. Returns $vars.
  17. =item process ($parent, $cleanvars)
  18. Processes the template for text.
  19. =item postprocess ($parent)
  20. Returns the output filename.
  21. =back
  22. =head1 Copyright (C) 2007, The LedgerSMB core team.
  23. This work contains copyrighted information from a number of sources all used
  24. with permission.
  25. It is released under the GNU General Public License Version 2 or, at your
  26. option, any later version. See COPYRIGHT file for details. For a full list
  27. including contact information of contributors, maintainers, and copyright
  28. holders, see the CONTRIBUTORS file.
  29. =cut
  30. package LedgerSMB::Template::XLS;
  31. use warnings;
  32. use strict;
  33. use Error qw(:try);
  34. use CGI::Simple::Standard qw(:html);
  35. use Excel::Template::Plus;
  36. use LedgerSMB::Template::TTI18N;
  37. sub get_template {
  38. my $name = shift;
  39. return "${name}.xlst";
  40. }
  41. sub preprocess {
  42. my $rawvars = shift;
  43. my $vars;
  44. my $type = ref $rawvars;
  45. #XXX fix escaping function
  46. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  47. return $rawvars unless defined $rawvars;
  48. if ( $type eq 'ARRAY' ) {
  49. for (@{$rawvars}) {
  50. push @{$vars}, preprocess( $_ );
  51. }
  52. } elsif (!$type) {
  53. return escapeHTML($rawvars);
  54. } elsif ($type eq 'SCALAR') {
  55. return escapeHTML($$rawvars);
  56. } else { # Hashes and objects
  57. for ( keys %{$rawvars} ) {
  58. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  59. }
  60. }
  61. return $vars;
  62. }
  63. sub process {
  64. my $parent = shift;
  65. my $cleanvars = shift;
  66. my $template;
  67. my $source;
  68. my $tempdir = ${LedgerSMB::Sysconfig::tempdir};
  69. $parent->{outputfile} ||= "$tempdir/$parent->{template}-output-$$";
  70. if (ref $parent->{template} eq 'SCALAR') {
  71. $source = $parent->{template};
  72. } elsif (ref $parent->{template} eq 'ARRAY') {
  73. $source = join "\n", @{$parent->{template}};
  74. } else {
  75. $source = get_template($parent->{template});
  76. }
  77. $template = Excel::Template::Plus->new(
  78. engine => 'TT',
  79. template => $source,
  80. params => {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  81. 'escape' => \&preprocess},
  82. config => {
  83. INCLUDE_PATH => $parent->{include_path},
  84. START_TAG => quotemeta('<?lsmb'),
  85. END_TAG => quotemeta('?>'),
  86. DELIMITER => ';',
  87. DEBUG => ($parent->{debug})? 'dirs': undef,
  88. DEBUG_FORMAT => '',},
  89. );
  90. $template->write_file("$parent->{outputfile}.xls");
  91. $parent->{mimetype} = 'application/vnd.ms-excel';
  92. }
  93. sub postprocess {
  94. my $parent = shift;
  95. $parent->{rendered} = "$parent->{outputfile}.xls" if $parent->{outputfile};
  96. return $parent->{rendered};
  97. }
  98. 1;