summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/CSV.pm
blob: 42324a890b3472be80306c758f536bcf4934d0e5 (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 Error qw(:try);
  24. use Template;
  25. use LedgerSMB::Template::TTI18N;
  26. sub get_template {
  27. my $name = shift;
  28. return "${name}.csv";
  29. }
  30. sub preprocess {
  31. my $rawvars = shift;
  32. my $vars;
  33. my $type = ref $rawvars;
  34. #XXX fix escaping function
  35. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  36. if ( $type eq 'ARRAY' ) {
  37. for (@{$rawvars}) {
  38. push @{$vars}, preprocess( $_ );
  39. }
  40. } elsif ( $type eq 'HASH' ) {
  41. for ( keys %{$rawvars} ) {
  42. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  43. }
  44. } else {
  45. $vars = $rawvars;
  46. $vars =~ s/\ / /;
  47. $vars =~ s/(\t\n\r )+/ /g;
  48. $vars =~ s/(^ +| +$)//g;
  49. $vars =~ s/~/\\~/g;
  50. $vars =~ s/<.*?>//g;
  51. $vars = qq|"$vars"| if $vars !~ /^\w+$/;
  52. $vars = '' if $vars =~ /^""$/;
  53. }
  54. return $vars;
  55. }
  56. sub process {
  57. my $parent = shift;
  58. my $cleanvars = shift;
  59. my $template;
  60. $template = Template->new({
  61. INCLUDE_PATH => $parent->{include_path},
  62. START_TAG => quotemeta('<?lsmb'),
  63. END_TAG => quotemeta('?>'),
  64. DELIMITER => ';',
  65. }) || throw Error::Simple Template->error();
  66. if (not $template->process(
  67. get_template($parent->{template}),
  68. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  69. 'escape' => \&preprocess},
  70. "$parent->{outputfile}.csv", binmode => ':utf8')) {
  71. throw Error::Simple $template->error();
  72. }
  73. $parent->{mimetype} = 'text/plain';
  74. }
  75. sub postprocess {
  76. my $parent = shift;
  77. $parent->{rendered} = "$parent->{outputfile}.csv";
  78. return "$parent->{outputfile}.csv";
  79. }
  80. 1;