summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/CSV.pm
blob: a1f92a1907bfd1264bd40da19c412ff73efe7c14 (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::CSV Template support module for LedgerSMB
  3. =head1 METHODS
  4. =over
  5. =item get_template ($name)
  6. Returns the appropriate template filename for this format.
  7. =item preprocess ($vars)
  8. Returns $vars.
  9. =item process ($parent, $cleanvars)
  10. Processes the template for text.
  11. =item postprocess ($parent)
  12. Returns the output filename.
  13. =back
  14. =head1 Copyright (C) 2007, The LedgerSMB core team.
  15. This work contains copyrighted information from a number of sources all used
  16. with permission.
  17. It is released under the GNU General Public License Version 2 or, at your
  18. option, any later version. See COPYRIGHT file for details. For a full list
  19. including contact information of contributors, maintainers, and copyright
  20. holders, see the CONTRIBUTORS file.
  21. =cut
  22. package LedgerSMB::Template::CSV;
  23. use warnings;
  24. use strict;
  25. use Error qw(:try);
  26. use Template;
  27. use LedgerSMB::Template::TTI18N;
  28. sub get_template {
  29. my $name = shift;
  30. return "${name}.csv";
  31. }
  32. sub preprocess {
  33. my $rawvars = shift;
  34. my $vars;
  35. my $type = ref $rawvars;
  36. #XXX fix escaping function
  37. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  38. return unless defined $rawvars;
  39. if ( $type eq 'ARRAY' ) {
  40. for (@{$rawvars}) {
  41. push @{$vars}, preprocess( $_ );
  42. }
  43. } elsif ( !$type or $type eq 'SCALAR' or $type eq 'Math::BigInt::GMP') {
  44. # Scalars or GMP objects (which are SCALAR refs) --CT
  45. if ($type eq 'SCALAR' or $type eq 'Math::BigInt::GMP') {
  46. $vars = $$rawvars;
  47. } else {
  48. $vars = $rawvars;
  49. }
  50. $vars =~ s/(^ +| +$)//g;
  51. $vars =~ s/"/""/g;
  52. $vars = qq|"$vars"| if $vars !~ /^\w*$/;
  53. } else { # hashes and objects
  54. for ( keys %{$rawvars} ) {
  55. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  56. }
  57. }
  58. return $vars;
  59. }
  60. sub process {
  61. my $parent = shift;
  62. my $cleanvars = shift;
  63. my $template;
  64. my $source;
  65. my $output;
  66. if ($parent->{outputfile}) {
  67. $output = "$parent->{outputfile}.csv";
  68. } else {
  69. $output = \$parent->{output};
  70. }
  71. if (ref $parent->{template} eq 'SCALAR') {
  72. $source = $parent->{template};
  73. } elsif (ref $parent->{template} eq 'ARRAY') {
  74. $source = join "\n", @{$parent->{template}};
  75. } else {
  76. $source = get_template($parent->{template});
  77. }
  78. $template = Template->new({
  79. INCLUDE_PATH => $parent->{include_path},
  80. START_TAG => quotemeta('<?lsmb'),
  81. END_TAG => quotemeta('?>'),
  82. DELIMITER => ';',
  83. DEBUG => ($parent->{debug})? 'dirs': undef,
  84. DEBUG_FORMAT => '',
  85. }) || throw Error::Simple Template->error();
  86. if (not $template->process(
  87. $source,
  88. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  89. 'escape' => \&preprocess},
  90. $output, binmode => ':utf8')) {
  91. throw Error::Simple $template->error();
  92. }
  93. $parent->{mimetype} = 'text/csv';
  94. }
  95. sub postprocess {
  96. my $parent = shift;
  97. $parent->{rendered} = "$parent->{outputfile}.csv" if $parent->{outputfile};
  98. return $parent->{rendered};
  99. }
  100. 1;