1 package SL::ReportGenerator;
 
   4 use List::Util qw(max);
 
   6 #use PDF::API2;    # these two eat up to .75s on startup. only load them if we actually need them
 
  11 # Cause locales.pl to parse these files:
 
  12 # parse_html_template('report_generator/html_report')
 
  19   $self->{myconfig} = shift;
 
  20   $self->{form}     = shift;
 
  24     'std_column_visibility' => 0,
 
  25     'output_format'         => 'HTML',
 
  26     'controller_class   '   => '',
 
  27     'allow_pdf_export'      => 1,
 
  28     'allow_csv_export'      => 1,
 
  29     'html_template'         => 'report_generator/html_report',
 
  32       'orientation'         => 'landscape',
 
  33       'font_name'           => 'Verdana',
 
  37       'margin_bottom'       => 1.5,
 
  38       'margin_right'        => 1.5,
 
  48       'eol_style'           => 'Unix',
 
  54     'variable_list' => [],
 
  57   $self->{data_present} = 0;
 
  61   $self->set_options(@_) if (@_);
 
  70   $self->{columns} = \%columns;
 
  72   foreach my $column (values %{ $self->{columns} }) {
 
  73     $column->{visible} = $self->{options}->{std_column_visibility} unless defined $column->{visible};
 
  76   if( $::form->{report_generator_csv_options_for_import} ) {
 
  77     foreach my $key (keys %{ $self->{columns} }) {
 
  78       $self->{columns}{$key}{text} = $key;
 
  82   $self->set_column_order(sort keys %{ $self->{columns} });
 
  85 sub set_column_order {
 
  88   $self->{column_order} = [ grep { !$seen{$_}++ } @_, sort keys %{ $self->{columns} } ];
 
  91 sub set_sort_indicator {
 
  94   $self->{options}->{sort_indicator_column}    = shift;
 
  95   $self->{options}->{sort_indicator_direction} = shift;
 
 103   while (my $arg = shift) {
 
 106     if ('ARRAY' eq ref $arg) {
 
 109     } elsif ('HASH' eq ref $arg) {
 
 113       $self->{form}->error('Incorrect usage -- expecting hash or array ref');
 
 116     my @columns_with_default_alignment = grep { defined $self->{columns}->{$_}->{align} } keys %{ $self->{columns} };
 
 118     foreach my $row (@{ $row_set }) {
 
 119       foreach my $column (@columns_with_default_alignment) {
 
 120         $row->{$column}          ||= { };
 
 121         $row->{$column}->{align}   = $self->{columns}->{$column}->{align} unless (defined $row->{$column}->{align});
 
 124       foreach my $field (qw(data link link_class)) {
 
 125         map { $row->{$_}->{$field} = [ $row->{$_}->{$field} ] if (ref $row->{$_}->{$field} ne 'ARRAY') } keys %{ $row };
 
 129     push @{ $self->{data} }, $row_set;
 
 130     $last_row_set = $row_set;
 
 132     $self->{data_present} = 1;
 
 135   return $last_row_set;
 
 141   push @{ $self->{data} }, { 'type' => 'separator' };
 
 148   push @{ $self->{data} }, $data;
 
 155   $self->{data_present} = 0;
 
 162   while (my ($key, $value) = each %options) {
 
 163     if ($key eq 'pdf_export') {
 
 164       map { $self->{options}->{pdf_export}->{$_} = $value->{$_} } keys %{ $value };
 
 166       $self->{options}->{$key} = $value;
 
 171 sub set_options_from_form {
 
 174   my $form     = $self->{form};
 
 175   my $myconfig = $self->{myconfig};
 
 177   foreach my $key (qw(output_format)) {
 
 178     my $full_key = "report_generator_${key}";
 
 179     $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
 
 182   foreach my $format (qw(pdf csv)) {
 
 183     my $opts = $self->{options}->{"${format}_export"};
 
 184     foreach my $key (keys %{ $opts }) {
 
 185       my $full_key = "report_generator_${format}_options_${key}";
 
 186       $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
 
 191 sub set_export_options {
 
 196     'variable_list' => [ @_ ],
 
 200 sub set_custom_headers {
 
 204     $self->{custom_headers} = [ @_ ];
 
 206     delete $self->{custom_headers};
 
 210 sub get_attachment_basename {
 
 212   my $filename =  $self->{options}->{attachment_basename} || 'report';
 
 214   # FIXME: this is bonkers. add a real sluggify method somewhere or import one.
 
 215   $filename    =~ s|.*\\||;
 
 216   $filename    =~ s|.*/||;
 
 217   $filename    =~ s| |_|g;
 
 222 sub generate_with_headers {
 
 223   my ($self, %params) = @_;
 
 224   my $format = lc $self->{options}->{output_format};
 
 225   my $form   = $self->{form};
 
 227   if (!$self->{columns}) {
 
 228     $form->error('Incorrect usage -- no columns specified');
 
 231   if ($format eq 'html') {
 
 232     my $title      = $form->{title};
 
 233     $form->{title} = $self->{title} if ($self->{title});
 
 234     $form->header(no_layout => $params{no_layout});
 
 235     $form->{title} = $title;
 
 237     print $self->generate_html_content();
 
 239   } elsif ($format eq 'csv') {
 
 240     # FIXME: don't do mini http in here
 
 241     my $filename = $self->get_attachment_basename();
 
 242     print qq|content-type: text/csv\n|;
 
 243     print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
 
 244     $::locale->with_raw_io(\*STDOUT, sub {
 
 245       $self->generate_csv_content();
 
 248   } elsif ($format eq 'pdf') {
 
 249     $self->generate_pdf_content();
 
 252     $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
 
 256 sub get_visible_columns {
 
 260   return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /\Q${format}\E/i)) } @{ $self->{column_order} };
 
 267   $value =  $main::locale->quote_special_chars('HTML', $value);
 
 269   $value =~ s/\n/<br>/g;
 
 274 sub prepare_html_content {
 
 277   my ($column, $name, @column_headers);
 
 279   my $opts            = $self->{options};
 
 280   my @visible_columns = $self->get_visible_columns('HTML');
 
 282   foreach $name (@visible_columns) {
 
 283     $column = $self->{columns}->{$name};
 
 287       'align'                    => $column->{align},
 
 288       'link'                     => $column->{link},
 
 289       'text'                     => $column->{text},
 
 290       'show_sort_indicator'      => $name eq $opts->{sort_indicator_column},
 
 291       'sort_indicator_direction' => $opts->{sort_indicator_direction},
 
 294     push @column_headers, $header;
 
 298   if ($self->{custom_headers}) {
 
 299     $header_rows = $self->{custom_headers};
 
 301     $header_rows = [ \@column_headers ];
 
 304   my ($outer_idx, $inner_idx) = (0, 0);
 
 308   foreach my $row_set (@{ $self->{data} }) {
 
 309     if ('HASH' eq ref $row_set) {
 
 310       if ($row_set->{type} eq 'separator') {
 
 311         if (! scalar @rows) {
 
 312           $next_border_top = 1;
 
 314           $rows[-1]->{BORDER_BOTTOM} = 1;
 
 322         'IS_COLSPAN_DATA' => $row_set->{type} eq 'colspan_data',
 
 323         'NUM_COLUMNS'     => scalar @visible_columns,
 
 324         'BORDER_TOP'      => $next_border_top,
 
 325         'data'            => $row_set->{data},
 
 328       push @rows, $row_data;
 
 330       $next_border_top = 0;
 
 337     foreach my $row (@{ $row_set }) {
 
 340       my $output_columns = [ ];
 
 342       foreach my $col_name (@visible_columns) {
 
 348         my $col = $row->{$col_name} || { data => [] };
 
 349         $col->{CELL_ROWS} = [ ];
 
 350         foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
 
 351           push @{ $col->{CELL_ROWS} }, {
 
 352             'data' => '' . $self->html_format($col->{data}->[$i]),
 
 353             'link' => $col->{link}->[$i],
 
 354             link_class => $col->{link_class}->[$i],
 
 358         # Force at least a   to be displayed so that browsers
 
 359         # will format the table cell (e.g. borders etc).
 
 360         if (!scalar @{ $col->{CELL_ROWS} }) {
 
 361           push @{ $col->{CELL_ROWS} }, { 'data' => ' ' };
 
 362         } elsif ((1 == scalar @{ $col->{CELL_ROWS} }) && (!defined $col->{CELL_ROWS}->[0]->{data} || ($col->{CELL_ROWS}->[0]->{data} eq ''))) {
 
 363           $col->{CELL_ROWS}->[0]->{data} = ' ';
 
 366         push @{ $output_columns }, $col;
 
 367         $skip_next = $col->{colspan} ? $col->{colspan} - 1 : 0;
 
 371         'COLUMNS'       => $output_columns,
 
 372         'outer_idx'     => $outer_idx,
 
 373         'outer_idx_odd' => $outer_idx % 2,
 
 374         'inner_idx'     => $inner_idx,
 
 375         'BORDER_TOP'    => $next_border_top,
 
 378       push @rows, $row_data;
 
 380       $next_border_top = 0;
 
 384   my @export_variables = $self->{form}->flatten_variables(@{ $self->{export}->{variable_list} });
 
 386   my $allow_pdf_export = $opts->{allow_pdf_export};
 
 389     'TITLE'                => $opts->{title},
 
 390     'TOP_INFO_TEXT'        => $self->html_format($opts->{top_info_text}),
 
 391     'RAW_TOP_INFO_TEXT'    => $opts->{raw_top_info_text},
 
 392     'BOTTOM_INFO_TEXT'     => $self->html_format($opts->{bottom_info_text}),
 
 393     'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
 
 394     'ALLOW_PDF_EXPORT'     => $allow_pdf_export,
 
 395     'ALLOW_CSV_EXPORT'     => $opts->{allow_csv_export},
 
 396     'SHOW_EXPORT_BUTTONS'  => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present},
 
 397     'HEADER_ROWS'          => $header_rows,
 
 398     'NUM_COLUMNS'          => scalar @column_headers,
 
 400     'EXPORT_VARIABLES'     => \@export_variables,
 
 401     'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
 
 402     'EXPORT_NEXTSUB'       => $self->{export}->{nextsub},
 
 403     'DATA_PRESENT'         => $self->{data_present},
 
 404     'CONTROLLER_DISPATCH'  => $opts->{controller_class},
 
 405     'TABLE_CLASS'          => $opts->{table_class},
 
 411 sub generate_html_content {
 
 413   my $variables = $self->prepare_html_content();
 
 415   my $stuff  = $self->{form}->parse_html_template($self->{options}->{html_template}, $variables);
 
 422   return $_[0] * 72 / 2.54;
 
 425 sub generate_pdf_content {
 
 432   my $variables  = $self->prepare_html_content();
 
 433   my $form       = $self->{form};
 
 434   my $myconfig   = $self->{myconfig};
 
 436   my $opts       = $self->{options};
 
 437   my $pdfopts    = $opts->{pdf_export};
 
 439   my (@data, @column_props, @cell_props);
 
 441   my ($data_row, $cell_props_row);
 
 442   my @visible_columns = $self->get_visible_columns('PDF');
 
 443   my $num_columns     = scalar @visible_columns;
 
 444   my $num_header_rows = 1;
 
 446   my $font_encoding   = 'UTF-8';
 
 448   foreach my $name (@visible_columns) {
 
 449     push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' };
 
 452   if (!$self->{custom_headers}) {
 
 454     $cell_props_row = [];
 
 455     push @data,       $data_row;
 
 456     push @cell_props, $cell_props_row;
 
 458     foreach my $name (@visible_columns) {
 
 459       my $column = $self->{columns}->{$name};
 
 461       push @{ $data_row },       $column->{text};
 
 462       push @{ $cell_props_row }, {};
 
 466     $num_header_rows = scalar @{ $self->{custom_headers} };
 
 468     foreach my $custom_header_row (@{ $self->{custom_headers} }) {
 
 470       $cell_props_row = [];
 
 471       push @data,       $data_row;
 
 472       push @cell_props, $cell_props_row;
 
 474       foreach my $custom_header_col (@{ $custom_header_row }) {
 
 475         push @{ $data_row }, $custom_header_col->{text};
 
 477         my $num_output  = ($custom_header_col->{colspan} * 1 > 1) ? $custom_header_col->{colspan} : 1;
 
 478         if ($num_output > 1) {
 
 479           push @{ $data_row },       ('') x ($num_output - 1);
 
 480           push @{ $cell_props_row }, { 'colspan' => $num_output };
 
 481           push @{ $cell_props_row }, ({ }) x ($num_output - 1);
 
 484           push @{ $cell_props_row }, {};
 
 490   foreach my $row_set (@{ $self->{data} }) {
 
 491     if ('HASH' eq ref $row_set) {
 
 492       if ($row_set->{type} eq 'colspan_data') {
 
 493         push @data, [ $row_set->{data} ];
 
 495         $cell_props_row = [];
 
 496         push @cell_props, $cell_props_row;
 
 498         foreach (0 .. $num_columns - 1) {
 
 499           push @{ $cell_props_row }, { 'background_color' => '#666666',
 
 500                                        'font_color'       => '#ffffff',
 
 501                                        'colspan'          => $_ == 0 ? -1 : undef, };
 
 507     foreach my $row (@{ $row_set }) {
 
 509       $cell_props_row = [];
 
 511       push @data,       $data_row;
 
 512       push @cell_props, $cell_props_row;
 
 515       foreach my $col_name (@visible_columns) {
 
 516         my $col = $row->{$col_name};
 
 517         push @{ $data_row }, join("\n", @{ $col->{data} || [] });
 
 519         $column_props[$col_idx]->{justify} = 'right' if ($col->{align} eq 'right');
 
 521         my $cell_props = { };
 
 522         push @{ $cell_props_row }, $cell_props;
 
 524         if ($col->{colspan} && $col->{colspan} > 1) {
 
 525           $cell_props->{colspan} = $col->{colspan};
 
 533   foreach my $i (0 .. scalar(@data) - 1) {
 
 534     my $aref             = $data[$i];
 
 535     my $num_columns_here = scalar @{ $aref };
 
 537     if ($num_columns_here < $num_columns) {
 
 538       push @{ $aref }, ('') x ($num_columns - $num_columns_here);
 
 539     } elsif ($num_columns_here > $num_columns) {
 
 540       splice @{ $aref }, $num_columns;
 
 545     'a3'         => [ 842, 1190 ],
 
 546     'a4'         => [ 595,  842 ],
 
 547     'a5'         => [ 420,  595 ],
 
 548     'letter'     => [ 612,  792 ],
 
 549     'legal'      => [ 612, 1008 ],
 
 552   my %supported_fonts = map { $_ => 1 } qw(courier georgia helvetica times verdana);
 
 554   my $paper_size  = defined $pdfopts->{paper_size} && defined $papersizes->{lc $pdfopts->{paper_size}} ? lc $pdfopts->{paper_size} : 'a4';
 
 555   my ($paper_width, $paper_height);
 
 557   if (lc $pdfopts->{orientation} eq 'landscape') {
 
 558     ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[1, 0];
 
 560     ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[0, 1];
 
 563   my $margin_top        = _cm2bp($pdfopts->{margin_top}    || 1.5);
 
 564   my $margin_bottom     = _cm2bp($pdfopts->{margin_bottom} || 1.5);
 
 565   my $margin_left       = _cm2bp($pdfopts->{margin_left}   || 1.5);
 
 566   my $margin_right      = _cm2bp($pdfopts->{margin_right}  || 1.5);
 
 568   my $table             = PDF::Table->new();
 
 569   my $pdf               = PDF::API2->new();
 
 570   my $page              = $pdf->page();
 
 572   $pdf->mediabox($paper_width, $paper_height);
 
 574   my $font              = $pdf->corefont(defined $pdfopts->{font_name} && $supported_fonts{lc $pdfopts->{font_name}} ? ucfirst $pdfopts->{font_name} : 'Verdana',
 
 575                                          '-encoding' => $font_encoding);
 
 576   my $font_size         = $pdfopts->{font_size} || 7;
 
 577   my $title_font_size   = $font_size + 1;
 
 579   my $font_height       = $font_size + 2 * $padding;
 
 580   my $title_font_height = $font_size + 2 * $padding;
 
 582   my $header_height     = $opts->{title}     ? 2 * $title_font_height : undef;
 
 583   my $footer_height     = $pdfopts->{number} ? 2 * $font_height       : undef;
 
 585   my $top_text_height   = 0;
 
 587   if ($self->{options}->{top_info_text}) {
 
 588     my $top_text     =  $self->{options}->{top_info_text};
 
 589     $top_text        =~ s/\r//g;
 
 590     $top_text        =~ s/\n+$//;
 
 592     my @lines        =  split m/\n/, $top_text;
 
 593     $top_text_height =  $font_height * scalar @lines;
 
 595     foreach my $line_no (0 .. scalar(@lines) - 1) {
 
 596       my $y_pos    = $paper_height - $margin_top - $header_height - $line_no * $font_height;
 
 597       my $text_obj = $page->text();
 
 599       $text_obj->font($font, $font_size);
 
 600       $text_obj->translate($margin_left, $y_pos);
 
 601       $text_obj->text($lines[$line_no]);
 
 609                 'w'                     => $paper_width - $margin_left - $margin_right,
 
 610                 'start_y'               => $paper_height - $margin_top                  - $header_height                  - $top_text_height,
 
 611                 'next_y'                => $paper_height - $margin_top                  - $header_height,
 
 612                 'start_h'               => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height - $top_text_height,
 
 613                 'next_h'                => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height,
 
 615                 'background_color_odd'  => '#ffffff',
 
 616                 'background_color_even' => '#eeeeee',
 
 618                 'font_size'             => $font_size,
 
 619                 'font_color'            => '#000000',
 
 620                 'num_header_rows'       => $num_header_rows,
 
 622                   'bg_color'            => '#ffffff',
 
 624                   'font_color'          => '#000000',
 
 626                 'column_props'          => \@column_props,
 
 627                 'cell_props'            => \@cell_props,
 
 628                 'max_word_length'       => 60,
 
 632   foreach my $page_num (1..$pdf->pages()) {
 
 633     my $curpage  = $pdf->openpage($page_num);
 
 635     if ($pdfopts->{number}) {
 
 636       my $label    = $main::locale->text("Page #1/#2", $page_num, $pdf->pages());
 
 637       my $text_obj = $curpage->text();
 
 639       $text_obj->font($font, $font_size);
 
 640       $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($label) / 2, $margin_bottom);
 
 641       $text_obj->text($label);
 
 644     if ($opts->{title}) {
 
 645       my $title    = $opts->{title};
 
 646       my $text_obj = $curpage->text();
 
 648       $text_obj->font($font, $title_font_size);
 
 649       $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($title) / 2,
 
 650                            $paper_height - $margin_top);
 
 651       $text_obj->text($title, '-underline' => 1);
 
 655   my $content = $pdf->stringify();
 
 658   if ($pdfopts->{print} && $pdfopts->{printer_id}) {
 
 659     $form->{printer_id} = $pdfopts->{printer_id};
 
 660     $form->get_printer_code($myconfig);
 
 661     $printer_command = $form->{printer_command};
 
 664   if ($printer_command) {
 
 665     $self->_print_content('printer_command' => $printer_command,
 
 666                           'content'         => $content,
 
 667                           'copies'          => $pdfopts->{copies});
 
 668     $form->{report_generator_printed} = 1;
 
 671     my $filename = $self->get_attachment_basename();
 
 673     print qq|content-type: application/pdf\n|;
 
 674     print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
 
 676     $::locale->with_raw_io(\*STDOUT, sub {
 
 682 sub verify_paper_size {
 
 684   my $requested_paper_size = lc shift;
 
 685   my $default_paper_size   = shift;
 
 687   my %allowed_paper_sizes  = map { $_ => 1 } qw(a3 a4 a5 letter legal);
 
 689   return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
 
 696   foreach my $i (1 .. max $params{copies}, 1) {
 
 697     my $printer = IO::File->new("| $params{printer_command}");
 
 698     $main::form->error($main::locale->text('Could not spawn the printer command.')) if (!$printer);
 
 699     $printer->print($params{content});
 
 704 sub _handle_quoting_and_encoding {
 
 705   my ($self, $text, $do_unquote) = @_;
 
 707   $text = $main::locale->unquote_special_chars('HTML', $text) if $do_unquote;
 
 708   $text = Encode::encode('UTF-8', $text);
 
 713 sub generate_csv_content {
 
 715   my $stdout = ($::dispatcher->get_standard_filehandles)[1];
 
 717   # Text::CSV_XS seems to downgrade to bytes already (see
 
 718   # SL/FCGIFixes.pm). Therefore don't let FCGI do that again.
 
 719   $::locale->with_raw_io($stdout, sub { $self->_generate_csv_content($stdout) });
 
 722 sub _generate_csv_content {
 
 723   my ($self, $stdout) = @_;
 
 725   my %valid_sep_chars    = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
 
 726   my %valid_escape_chars = ('"' => 1, "'" => 1);
 
 727   my %valid_quote_chars  = ('"' => 1, "'" => 1);
 
 729   my $opts        = $self->{options}->{csv_export};
 
 730   my $eol         = $opts->{eol_style} eq 'DOS'               ? "\r\n"                              : "\n";
 
 731   my $sep_char    = $valid_sep_chars{$opts->{sep_char}}       ? $valid_sep_chars{$opts->{sep_char}} : ';';
 
 732   my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char}                : '"';
 
 733   my $quote_char  = $valid_quote_chars{$opts->{quote_char}}   ? $opts->{quote_char}                 : '"';
 
 735   $escape_char    = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
 
 737   my $csv = Text::CSV_XS->new({ 'binary'      => 1,
 
 738                                 'sep_char'    => $sep_char,
 
 739                                 'escape_char' => $escape_char,
 
 740                                 'quote_char'  => $quote_char,
 
 743   my @visible_columns = $self->get_visible_columns('CSV');
 
 745   if ($opts->{headers}) {
 
 746     if (!$self->{custom_headers}) {
 
 747       $csv->print($stdout, [ map { $self->_handle_quoting_and_encoding($self->{columns}->{$_}->{text}, 1) } @visible_columns ]);
 
 750       foreach my $row (@{ $self->{custom_headers} }) {
 
 753         foreach my $col (@{ $row }) {
 
 754           my $num_output = ($col->{colspan} && ($col->{colspan} > 1)) ? $col->{colspan} : 1;
 
 755           push @{ $fields }, ($self->_handle_quoting_and_encoding($col->{text}, 1)) x $num_output;
 
 758         $csv->print($stdout, $fields);
 
 763   foreach my $row_set (@{ $self->{data} }) {
 
 764     next if ('ARRAY' ne ref $row_set);
 
 765     foreach my $row (@{ $row_set }) {
 
 768       foreach my $col (@visible_columns) {
 
 774         my $num_output = ($row->{$col}{colspan} && ($row->{$col}->{colspan} > 1)) ? $row->{$col}->{colspan} : 1;
 
 775         $skip_next     = $num_output - 1;
 
 777         push @data, join($eol, map { s/\r?\n/$eol/g; $self->_handle_quoting_and_encoding($_, 0) } @{ $row->{$col}->{data} });
 
 778         push @data, ('') x $skip_next if ($skip_next);
 
 781       $csv->print($stdout, \@data);
 
 786 sub check_for_pdf_api {
 
 787   return eval { require PDF::API2; 1; } ? 1 : 0;
 
 796 SL::ReportGenerator.pm: the kivitendo way of getting data in shape
 
 800   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 801      $report->set_options(%options);                         # optional
 
 802      $report->set_columns(%column_defs);
 
 803      $report->set_sort_indicator($column, $direction);       # optional
 
 804      $report->add_data($row1, $row2, @more_rows);
 
 805      $report->generate_with_headers();
 
 807 This creates a report object, sets a few columns, adds some data and generates a standard report.
 
 808 Sorting of columns will be alphabetic, and options will be set to their defaults.
 
 809 The report will be printed including table headers, html headers and http headers.
 
 813 Imagine the following scenario:
 
 814 There's a simple form, which loads some data from the database, and needs to print it out. You write a template for it.
 
 815 Then there may be more than one line. You add a loop in the template.
 
 816 Then there are some options made by the user, such as hidden columns. You add more to the template.
 
 817 Then it lacks usability. You want it to be able to sort the data. You add code for that.
 
 818 Then there are too many results, you need pagination, you want to print or export that data..... and so on.
 
 820 The ReportGenerator class was designed because this exact scenario happened about half a dozen times in kivitendo.
 
 821 It's purpose is to manage all those formating, culling, sorting, and templating.
 
 822 Which makes it almost as complicated to use as doing the work for yourself.
 
 828 =item new \%myconfig,$form,%options
 
 830 Creates a new ReportGenerator object, sets all given options, and returns it.
 
 832 =item set_columns %columns
 
 834 Sets the columns available to this report.
 
 836 =item set_column_order @columns
 
 838 Sets the order of columns. Any columns not present here are appended in alphabetic order.
 
 840 =item set_sort_indicator $column,$direction
 
 842 Sets sorting ot the table by specifying a column and a direction, where the direction will be evaluated to ascending if true.
 
 843 Note that this is only for displaying. The data has to be presented already sorted.
 
 845 =item add_data \@data
 
 847 =item add_data \%data
 
 849 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.
 
 850 Every line will be expected to be in a kay => value format. Note that the rows have to be already sorted.
 
 851 ReportGenerator does only colum sorting on its own, and provides links to sorting and visual cue as to which column was sorted by.
 
 855 Adds a separator line to the report.
 
 857 =item add_control \%data
 
 859 Adds a control element to the data. Control elements are an experimental feature to add functionality to a report the regular data cannot.
 
 860 Every control element needs to set IS_CONTROL_DATA, in order to be recongnized by the template.
 
 861 Currently the only control element is a colspan element, which can be used as a mini header further down the report.
 
 865 Deletes all data filled into the report, but keeps options set.
 
 867 =item set_options %options
 
 869 Sets options. For an incomplete list of options, see section configuration.
 
 871 =item set_options_from_form
 
 873 Tries to import options from the $form object given at creation
 
 875 =item set_export_options $next_sub,@variable_list
 
 877 Sets next_sub and additional variables needed for export.
 
 879 =item get_attachment_basename
 
 881 Returns the set attachment_basename option, or 'report' if nothing was set. See configuration for the option.
 
 883 =item generate_with_headers
 
 885 Parses the report, adds headers and prints it out. Headers depend on the option 'output_format',
 
 886 for example 'HTML' will add proper table headers, html headers and http headers. See configuration for this option.
 
 888 =item get_visible_columns $format
 
 890 Returns a list of columns that will be visible in the report after considering all options or match the given format.
 
 892 =item html_format $value
 
 894 Escapes HTML characters in $value and substitutes newlines with '<br>'. Returns the escaped $value.
 
 896 =item prepare_html_content $column,$name,@column_headers
 
 898 Parses the data, and sets internal data needed for certain output format. Must be called once before the template is invoked.
 
 899 Should not be called extrenally, since all render and generate functions invoke it anyway.
 
 901 =item generate_html_content
 
 903 The html generation function. Is invoked by generate_with_headers.
 
 905 =item generate_pdf_content
 
 907 The PDF generation function. It is invoked by generate_with_headers and renders the PDF with the PDF::API2 library.
 
 909 =item generate_csv_content
 
 911 The CSV generation function. Uses XS_CSV to parse the information into csv.
 
 917 These are known options and their defaults. Options for pdf export and csv export need to be set as a hashref inside the export option.
 
 919 =head2 General Options
 
 923 =item std_column_visibility
 
 925 Standard column visibility. Used if no visibility is set. Use this to save the trouble of enabling every column. Default is no.
 
 929 Output format. Used by generate_with_headers to determine the format. Supported options are HTML, CSV, and PDF. Default is HTML.
 
 931 =item allow_pdf_export
 
 933 Used to determine if a button for PDF export should be displayed. Default is yes.
 
 935 =item allow_csv_export
 
 937 Used to determine if a button for CSV export should be displayed. Default is yes.
 
 941 The template to be used for HTML reports. Default is 'report_generator/html_report'.
 
 943 =item controller_class
 
 945 If this is used from a C<SL::Controller::Base> based controller class, pass the
 
 946 class name here and make sure C<SL::Controller::Helper::ReportGenerator> is
 
 947 used in the controller. That way the exports stay functional.
 
 957 Paper size. Default is a4. Supported paper sizes are a3, a4, a5, letter and legal.
 
 959 =item orientation (landscape)
 
 961 Landscape or portrait. Default is landscape.
 
 965 Default is Verdana. Supported font names are Courier, Georgia, Helvetica, Times and Verdana. This option only affects the rendering with PDF::API2.
 
 969 Default is 7. This option only affects the rendering with PDF::API2.
 
 979 The paper margins in cm. They all default to 1.5.
 
 983 Set to a true value if the pages should be numbered. Default is 1.
 
 987 If set then the resulting PDF will be output to a printer. If not it will be downloaded by the user. Default is no.
 
1005 Character to enclose entries. Default is double quote (").
 
1009 Character to separate entries. Default is semicolon (;).
 
1013 Character to escape the quote_char. Default is double quote (").
 
1017 End of line style. Default is Unix.
 
1021 Include headers? Default is yes.
 
1029 =head1 MODULE AUTHORS
 
1031 Moritz Bunkus E<lt>mbunkus@linet-services.deE<gt>
 
1033 L<http://linet-services.de>