CSV-Export mit UTF-8 als Encoding gefixt
[kivitendo-erp.git] / SL / ReportGenerator.pm
index 0b74f83..2b37e78 100644 (file)
@@ -1,11 +1,12 @@
 package SL::ReportGenerator;
 
-use IO::Wrap;
+use Data::Dumper;
 use List::Util qw(max);
 use Text::CSV_XS;
-use Text::Iconv;
+#use PDF::API2;    # these two eat up to .75s on startup. only load them if we actually need them
+#use PDF::Table;
 
-use SL::Form;
+use strict;
 
 # Cause locales.pl to parse these files:
 # parse_html_template('report_generator/html_report')
@@ -229,7 +230,9 @@ sub generate_with_headers {
     my $filename = $self->get_attachment_basename();
     print qq|content-type: text/csv\n|;
     print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
-    $self->generate_csv_content();
+    $::locale->with_raw_io(\*STDOUT, sub {
+      $self->generate_csv_content();
+    });
 
   } elsif ($format eq 'pdf') {
     $self->generate_pdf_content();
@@ -270,6 +273,7 @@ sub prepare_html_content {
 
     my $header = {
       'name'                     => $name,
+      'align'                    => $column->{align},
       'link'                     => $column->{link},
       'text'                     => $column->{text},
       'show_sort_indicator'      => $name eq $opts->{sort_indicator_column},
@@ -330,7 +334,7 @@ sub prepare_html_content {
           next;
         }
 
-        my $col = $row->{$col_name};
+        my $col = $row->{$col_name} || { data => [] };
         $col->{CELL_ROWS} = [ ];
         foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
           push @{ $col->{CELL_ROWS} }, {
@@ -369,9 +373,6 @@ sub prepare_html_content {
 
   my $allow_pdf_export = $opts->{allow_pdf_export};
 
-  eval { require PDF::API2; require PDF::Table; };
-  $allow_pdf_export |= 1 if (! $@);
-
   my $variables = {
     'TITLE'                => $opts->{title},
     'TOP_INFO_TEXT'        => $self->html_format($opts->{top_info_text}),
@@ -423,11 +424,13 @@ sub generate_pdf_content {
   my (@data, @column_props, @cell_props);
 
   my ($data_row, $cell_props_row);
-  my @visible_columns = $self->get_visible_columns('HTML');
+  my @visible_columns = $self->get_visible_columns('PDF');
   my $num_columns     = scalar @visible_columns;
   my $num_header_rows = 1;
 
-  foreach $name (@visible_columns) {
+  my $font_encoding   = $main::dbcharset || 'ISO-8859-15';
+
+  foreach my $name (@visible_columns) {
     push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' };
   }
 
@@ -437,8 +440,8 @@ sub generate_pdf_content {
     push @data,       $data_row;
     push @cell_props, $cell_props_row;
 
-    foreach $name (@visible_columns) {
-      $column = $self->{columns}->{$name};
+    foreach my $name (@visible_columns) {
+      my $column = $self->{columns}->{$name};
 
       push @{ $data_row },       $column->{text};
       push @{ $cell_props_row }, {};
@@ -496,7 +499,7 @@ sub generate_pdf_content {
       my $col_idx = 0;
       foreach my $col_name (@visible_columns) {
         my $col = $row->{$col_name};
-        push @{ $data_row }, join("\n", @{ $col->{data} });
+        push @{ $data_row }, join("\n", @{ $col->{data} || [] });
 
         $column_props[$col_idx]->{justify} = 'right' if ($col->{align} eq 'right');
 
@@ -554,7 +557,7 @@ sub generate_pdf_content {
   $pdf->mediabox($paper_width, $paper_height);
 
   my $font              = $pdf->corefont(defined $pdfopts->{font_name} && $supported_fonts{lc $pdfopts->{font_name}} ? ucfirst $pdfopts->{font_name} : 'Verdana',
-                                         '-encoding' => $main::dbcharset || 'ISO-8859-15');
+                                         '-encoding' => $font_encoding);
   my $font_size         = $pdfopts->{font_size} || 7;
   my $title_font_size   = $font_size + 1;
   my $padding           = 1;
@@ -607,6 +610,8 @@ sub generate_pdf_content {
                 },
                 'column_props'          => \@column_props,
                 'cell_props'            => \@cell_props,
+                'max_word_length'       => 60,
+                'border'                => 0.5,
     );
 
   foreach my $page_num (1..$pdf->pages()) {
@@ -622,12 +627,13 @@ sub generate_pdf_content {
     }
 
     if ($opts->{title}) {
+      my $title    = $opts->{title};
       my $text_obj = $curpage->text();
 
       $text_obj->font($font, $title_font_size);
-      $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($opts->{title}) / 2,
+      $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($title) / 2,
                            $paper_height - $margin_top);
-      $text_obj->text($opts->{title}, '-underline' => 1);
+      $text_obj->text($title, '-underline' => 1);
     }
   }
 
@@ -652,7 +658,9 @@ sub generate_pdf_content {
     print qq|content-type: application/pdf\n|;
     print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
 
-    print $content;
+    $::locale->with_raw_io(\*STDOUT, sub {
+      print $content;
+    });
   }
 }
 
@@ -679,12 +687,10 @@ sub _print_content {
 }
 
 sub unescape_string {
-  my $self  = shift;
-  my $text  = shift;
-  my $iconv = $main::locale->{iconv};
+  my ($self, $text, $do_iconv) = @_;
 
-  $text     = $main::locale->unquote_special_chars('HTML', $text);
-  $text     = $main::locale->{iconv}->convert($text) if ($main::locale->{iconv});
+  $text = $main::locale->unquote_special_chars('HTML', $text);
+  $text = $::locale->{iconv}->convert($text) if $do_iconv;
 
   return $text;
 }
@@ -710,12 +716,15 @@ sub generate_csv_content {
                                 'quote_char'  => $quote_char,
                                 'eol'         => $eol, });
 
-  my $stdout          = wraphandle(\*STDOUT);
   my @visible_columns = $self->get_visible_columns('CSV');
 
+  my $stdout;
+  open $stdout, '>-';
+  binmode $stdout, ':encoding(utf8)' if $::locale->is_utf8;
+
   if ($opts->{headers}) {
     if (!$self->{custom_headers}) {
-      $csv->print($stdout, [ map { $self->unescape_string($self->{columns}->{$_}->{text}) } @visible_columns ]);
+      $csv->print($stdout, [ map { $self->unescape_string($self->{columns}->{$_}->{text}, 1) } @visible_columns ]);
 
     } else {
       foreach my $row (@{ $self->{custom_headers} }) {
@@ -742,7 +751,7 @@ sub generate_csv_content {
           next;
         }
 
-        my $num_output = ($col->{colspan} && ($col->{colspan} > 1)) ? $col->{colspan} : 1;
+        my $num_output = ($row->{$col}{colspan} && ($row->{$col}->{colspan} > 1)) ? $row->{$col}->{colspan} : 1;
         $skip_next     = $num_output - 1;
 
         push @data, join($eol, map { s/\r?\n/$eol/g; $_ } @{ $row->{$col}->{data} });
@@ -771,7 +780,7 @@ SL::ReportGenerator.pm: the Lx-Office way of getting data in shape
      $report->add_data($row1, $row2, @more_rows);
      $report->generate_with_headers();
 
-This creates a report object, sets a few columns, adds some data and generates a standard report. 
+This creates a report object, sets a few columns, adds some data and generates a standard report.
 Sorting of columns will be alphabetic, and options will be set to their defaults.
 The report will be printed including table headers, html headers and http headers.
 
@@ -784,8 +793,8 @@ Then there are some options made by the user, such as hidden columns. You add mo
 Then it lacks usability. You want it to be able to sort the data. You add code for that.
 Then there are too many results, you need pagination, you want to print or export that data..... and so on.
 
-The ReportGenerator class was designed because this exact scenario happened about half a dozen times in Lx-Office. 
-It's purpose is to manage all those formating, culling, sorting, and templating. 
+The ReportGenerator class was designed because this exact scenario happened about half a dozen times in Lx-Office.
+It's purpose is to manage all those formating, culling, sorting, and templating.
 Which makes it almost as complicated to use as doing the work for yourself.
 
 =head1 FUNCTIONS
@@ -813,8 +822,8 @@ Note that this is only for displaying. The data has to be presented already sort
 
 =item add_data \%data
 
-Adds data to the report. A given hash_ref is interpreted as a single line of data, every array_ref as a collection of lines. 
-Every line will be expected to be in a kay => value format. Note that the rows have to be already sorted. 
+Adds data to the report. A given hash_ref is interpreted as a single line of data, every array_ref as a collection of lines.
+Every line will be expected to be in a kay => value format. Note that the rows have to be already sorted.
 ReportGenerator does only colum sorting on its own, and provides links to sorting and visual cue as to which column was sorted by.
 
 =item add_separator
@@ -824,7 +833,7 @@ Adds a separator line to the report.
 =item add_control \%data
 
 Adds a control element to the data. Control elements are an experimental feature to add functionality to a report the regular data cannot.
-Every control element needs to set IS_CONTROL_DATA, in order to be recongnized by the template. 
+Every control element needs to set IS_CONTROL_DATA, in order to be recongnized by the template.
 Currently the only control element is a colspan element, which can be used as a mini header further down the report.
 
 =item clear_data
@@ -849,7 +858,7 @@ Returns the set attachment_basename option, or 'report' if nothing was set. See
 
 =item generate_with_headers
 
-Parses the report, adds headers and prints it out. Headers depend on the option 'output_format', 
+Parses the report, adds headers and prints it out. Headers depend on the option 'output_format',
 for example 'HTML' will add proper table headers, html headers and http headers. See configuration for this option.
 
 =item get_visible_columns $format
@@ -862,29 +871,21 @@ Escapes HTML characters in $value and substitutes newlines with '<br>'. Returns
 
 =item prepare_html_content $column,$name,@column_headers
 
-Parses the data, and sets internal data needed for certain output format. Must be called once before the template is invoked. 
+Parses the data, and sets internal data needed for certain output format. Must be called once before the template is invoked.
 Should not be called extrenally, since all render and generate functions invoke it anyway.
+
 =item generate_html_content
 
 The html generation function. Is invoked by generate_with_headers.
 
 =item generate_pdf_content
 
-The PDF generation function. It is invoked by generate_with_headers, tests whether or not the Perl module PDF::API2 is installed and calls render_pdf_with_pdf_api2 if it is and render_pdf_with_html2ps otherwise.
+The PDF generation function. It is invoked by generate_with_headers and renders the PDF with the PDF::API2 library.
 
 =item generate_csv_content
 
 The CSV generation function. Uses XS_CSV to parse the information into csv.
 
-=item render_pdf_with_pdf_api2
-
-PDF render function using the Perl module PDF::API2.
-
-=item render_pdf_with_html2ps
-
-PDF render function using the external application html2ps.
-
 =back
 
 =head1 CONFIGURATION
@@ -905,7 +906,7 @@ Output format. Used by generate_with_headers to determine the format. Supported
 
 =item allow_pdf_export
 
-Used to determine if a button for PDF export should be displayed. Default is yes. The PDF button is hidden if neither the Perl module PDF::API2 nor the external applications html2ps and Ghostscript are available regardless of this parameter's value.
+Used to determine if a button for PDF export should be displayed. Default is yes.
 
 =item allow_csv_export
 
@@ -929,7 +930,7 @@ Paper size. Default is a4. Supported paper sizes are a3, a4, a5, letter and lega
 
 Landscape or portrait. Default is landscape.
 
-=item font_name 
+=item font_name
 
 Default is Verdana. Supported font names are Courier, Georgia, Helvetica, Times and Verdana. This option only affects the rendering with PDF::API2.