summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/XLS.pm
blob: 5e03c7c9d261b06652013c3ad827640750e18cd4 (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 Error qw(:try);
  32. use CGI::Simple::Standard qw(:html);
  33. use Excel::Template::Plus;
  34. use LedgerSMB::Template::TTI18N;
  35. sub get_template {
  36. my $name = shift;
  37. return "${name}.xlst";
  38. }
  39. sub preprocess {
  40. my $rawvars = shift;
  41. my $vars;
  42. my $type = ref $rawvars;
  43. #XXX fix escaping function
  44. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  45. if ( $type eq 'ARRAY' ) {
  46. for (@{$rawvars}) {
  47. push @{$vars}, preprocess( $_ );
  48. }
  49. } elsif (!$type) {
  50. return escapeHTML($rawvars);
  51. } else { # Hashes and objects
  52. for ( keys %{$rawvars} ) {
  53. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  54. }
  55. }
  56. return $vars;
  57. }
  58. sub process {
  59. my $parent = shift;
  60. my $cleanvars = shift;
  61. my $template;
  62. my $source;
  63. my $tempdir = ${LedgerSMB::Sysconfig::tempdir};
  64. $parent->{outputfile} ||= "$tempdir/$parent->{template}-output-$$";
  65. if (ref $parent->{template} eq 'SCALAR') {
  66. $source = $parent->{template};
  67. } elsif (ref $parent->{template} eq 'ARRAY') {
  68. $source = join "\n", @{$parent->{template}};
  69. } else {
  70. $source = get_template($parent->{template});
  71. }
  72. $template = Excel::Template::Plus->new(
  73. engine => 'TT',
  74. template => $source,
  75. params => {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  76. 'escape' => \&preprocess},
  77. config => {
  78. INCLUDE_PATH => $parent->{include_path},
  79. START_TAG => quotemeta('<?lsmb'),
  80. END_TAG => quotemeta('?>'),
  81. DELIMITER => ';',
  82. DEBUG => ($parent->{debug})? 'dirs': undef,
  83. DEBUG_FORMAT => '',},
  84. );
  85. $template->write_file("$parent->{outputfile}.xls");
  86. parent->{mimetype} = 'application/vnd.ms-excel';
  87. }
  88. sub postprocess {
  89. my $parent = shift;
  90. $parent->{rendered} = "$parent->{outputfile}.xls" if $parent->{outputfile};
  91. return $parent->{rendered};
  92. }
  93. 1;