summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/CSV.pm
blob: 50edef1529d7e6f818fec8457a7286f8e5f6b475 (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' ) { # Scalar
  44. if ($type eq 'SCALAR' ) {
  45. $vars = $$rawvars;
  46. } else {
  47. $vars = $rawvars;
  48. }
  49. $vars =~ s/(^ +| +$)//g;
  50. $vars =~ s/"/""/g;
  51. $vars = qq|"$vars"| if $vars !~ /^\w*$/;
  52. } else { # hashes and objects
  53. for ( keys %{$rawvars} ) {
  54. $vars->{preprocess($_)} = preprocess( $rawvars->{$_} );
  55. }
  56. }
  57. return $vars;
  58. }
  59. sub process {
  60. my $parent = shift;
  61. my $cleanvars = shift;
  62. my $template;
  63. my $source;
  64. my $output;
  65. if ($parent->{outputfile}) {
  66. $output = "$parent->{outputfile}.csv";
  67. } else {
  68. $output = \$parent->{output};
  69. }
  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 = Template->new({
  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. }) || throw Error::Simple Template->error();
  85. if (not $template->process(
  86. $source,
  87. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  88. 'escape' => \&preprocess},
  89. $output, binmode => ':utf8')) {
  90. throw Error::Simple $template->error();
  91. }
  92. $parent->{mimetype} = 'text/csv';
  93. }
  94. sub postprocess {
  95. my $parent = shift;
  96. $parent->{rendered} = "$parent->{outputfile}.csv" if $parent->{outputfile};
  97. return $parent->{rendered};
  98. }
  99. 1;