1 package SL::ReportGenerator;
 
   7 use List::Util qw(max);
 
  10 #use PDF::API2;    # these two eat up to .75s on startup. only load them if we actually need them
 
  17 # Cause locales.pl to parse these files:
 
  18 # parse_html_template('report_generator/html_report')
 
  25   $self->{myconfig} = shift;
 
  26   $self->{form}     = shift;
 
  30     'std_column_visibility' => 0,
 
  31     'output_format'         => 'HTML',
 
  32     'allow_pdf_export'      => 1,
 
  33     'allow_csv_export'      => 1,
 
  34     'html_template'         => 'report_generator/html_report',
 
  37       'orientation'         => 'landscape',
 
  38       'font_name'           => 'Verdana',
 
  42       'margin_bottom'       => 1.5,
 
  43       'margin_right'        => 1.5,
 
  53       'eol_style'           => 'Unix',
 
  59     'variable_list' => [],
 
  62   $self->{data_present} = 0;
 
  66   $self->set_options(@_) if (@_);
 
  75   $self->{columns} = \%columns;
 
  77   foreach my $column (values %{ $self->{columns} }) {
 
  78     $column->{visible} = $self->{options}->{std_column_visibility} unless defined $column->{visible};
 
  81   $self->set_column_order(sort keys %{ $self->{columns} });
 
  84 sub set_column_order {
 
  87   $self->{column_order} = [ grep { !$seen{$_}++ } @_, sort keys %{ $self->{columns} } ];
 
  90 sub set_sort_indicator {
 
  93   $self->{options}->{sort_indicator_column}    = shift;
 
  94   $self->{options}->{sort_indicator_direction} = shift;
 
 102   while (my $arg = shift) {
 
 105     if ('ARRAY' eq ref $arg) {
 
 108     } elsif ('HASH' eq ref $arg) {
 
 112       $self->{form}->error('Incorrect usage -- expecting hash or array ref');
 
 115     my @columns_with_default_alignment = grep { defined $self->{columns}->{$_}->{align} } keys %{ $self->{columns} };
 
 117     foreach my $row (@{ $row_set }) {
 
 118       foreach my $column (@columns_with_default_alignment) {
 
 119         $row->{$column}          ||= { };
 
 120         $row->{$column}->{align}   = $self->{columns}->{$column}->{align} unless (defined $row->{$column}->{align});
 
 123       foreach my $field (qw(data link)) {
 
 124         map { $row->{$_}->{$field} = [ $row->{$_}->{$field} ] if (ref $row->{$_}->{$field} ne 'ARRAY') } keys %{ $row };
 
 128     push @{ $self->{data} }, $row_set;
 
 129     $last_row_set = $row_set;
 
 131     $self->{data_present} = 1;
 
 134   return $last_row_set;
 
 140   push @{ $self->{data} }, { 'type' => 'separator' };
 
 147   push @{ $self->{data} }, $data;
 
 154   $self->{data_present} = 0;
 
 161   while (my ($key, $value) = each %options) {
 
 162     if ($key eq 'pdf_export') {
 
 163       map { $self->{options}->{pdf_export}->{$_} = $value->{$_} } keys %{ $value };
 
 165       $self->{options}->{$key} = $value;
 
 170 sub set_options_from_form {
 
 173   my $form     = $self->{form};
 
 174   my $myconfig = $self->{myconfig};
 
 176   foreach my $key (qw(output_format)) {
 
 177     my $full_key = "report_generator_${key}";
 
 178     $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
 
 181   foreach my $format (qw(pdf csv)) {
 
 182     my $opts = $self->{options}->{"${format}_export"};
 
 183     foreach my $key (keys %{ $opts }) {
 
 184       my $full_key = "report_generator_${format}_options_${key}";
 
 185       $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
 
 190 sub set_export_options {
 
 195     'variable_list' => [ @_ ],
 
 199 sub set_custom_headers {
 
 203     $self->{custom_headers} = [ @_ ];
 
 205     delete $self->{custom_headers};
 
 209 sub get_attachment_basename {
 
 211   my $filename =  $self->{options}->{attachment_basename} || 'report';
 
 212   $filename    =~ s|.*\\||;
 
 213   $filename    =~ s|.*/||;
 
 218 sub generate_with_headers {
 
 220   my $format = lc $self->{options}->{output_format};
 
 221   my $form   = $self->{form};
 
 223   if (!$self->{columns}) {
 
 224     $form->error('Incorrect usage -- no columns specified');
 
 227   if ($format eq 'html') {
 
 228     my $title      = $form->{title};
 
 229     $form->{title} = $self->{title} if ($self->{title});
 
 231     $form->{title} = $title;
 
 233     print $self->generate_html_content();
 
 235   } elsif ($format eq 'csv') {
 
 236     my $filename = $self->get_attachment_basename();
 
 237     print qq|content-type: text/csv\n|;
 
 238     print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
 
 239     $self->generate_csv_content();
 
 241   } elsif ($format eq 'pdf') {
 
 242     $self->generate_pdf_content();
 
 245     $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
 
 249 sub get_visible_columns {
 
 253   return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /\Q${format}\E/i)) } @{ $self->{column_order} };
 
 260   $value =  $main::locale->quote_special_chars('HTML', $value);
 
 262   $value =~ s/\n/<br>/g;
 
 267 sub prepare_html_content {
 
 270   my ($column, $name, @column_headers);
 
 272   my $opts            = $self->{options};
 
 273   my @visible_columns = $self->get_visible_columns('HTML');
 
 275   foreach $name (@visible_columns) {
 
 276     $column = $self->{columns}->{$name};
 
 280       'align'                    => $column->{align},
 
 281       'link'                     => $column->{link},
 
 282       'text'                     => $column->{text},
 
 283       'show_sort_indicator'      => $name eq $opts->{sort_indicator_column},
 
 284       'sort_indicator_direction' => $opts->{sort_indicator_direction},
 
 287     push @column_headers, $header;
 
 291   if ($self->{custom_headers}) {
 
 292     $header_rows = $self->{custom_headers};
 
 294     $header_rows = [ \@column_headers ];
 
 297   my ($outer_idx, $inner_idx) = (0, 0);
 
 301   foreach my $row_set (@{ $self->{data} }) {
 
 302     if ('HASH' eq ref $row_set) {
 
 303       if ($row_set->{type} eq 'separator') {
 
 304         if (! scalar @rows) {
 
 305           $next_border_top = 1;
 
 307           $rows[-1]->{BORDER_BOTTOM} = 1;
 
 315         'IS_COLSPAN_DATA' => $row_set->{type} eq 'colspan_data',
 
 316         'NUM_COLUMNS'     => scalar @visible_columns,
 
 317         'BORDER_TOP'      => $next_border_top,
 
 318         'data'            => $row_set->{data},
 
 321       push @rows, $row_data;
 
 323       $next_border_top = 0;
 
 330     foreach my $row (@{ $row_set }) {
 
 333       my $output_columns = [ ];
 
 335       foreach my $col_name (@visible_columns) {
 
 341         my $col = $row->{$col_name};
 
 342         $col->{CELL_ROWS} = [ ];
 
 343         foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
 
 344           push @{ $col->{CELL_ROWS} }, {
 
 345             'data' => $self->html_format($col->{data}->[$i]),
 
 346             'link' => $col->{link}->[$i],
 
 350         # Force at least a   to be displayed so that browsers
 
 351         # will format the table cell (e.g. borders etc).
 
 352         if (!scalar @{ $col->{CELL_ROWS} }) {
 
 353           push @{ $col->{CELL_ROWS} }, { 'data' => ' ' };
 
 354         } elsif ((1 == scalar @{ $col->{CELL_ROWS} }) && (!defined $col->{CELL_ROWS}->[0]->{data} || ($col->{CELL_ROWS}->[0]->{data} eq ''))) {
 
 355           $col->{CELL_ROWS}->[0]->{data} = ' ';
 
 358         push @{ $output_columns }, $col;
 
 359         $skip_next = $col->{colspan} ? $col->{colspan} - 1 : 0;
 
 363         'COLUMNS'       => $output_columns,
 
 364         'outer_idx'     => $outer_idx,
 
 365         'outer_idx_odd' => $outer_idx % 2,
 
 366         'inner_idx'     => $inner_idx,
 
 367         'BORDER_TOP'    => $next_border_top,
 
 370       push @rows, $row_data;
 
 372       $next_border_top = 0;
 
 376   my @export_variables = $self->{form}->flatten_variables(@{ $self->{export}->{variable_list} });
 
 378   my $allow_pdf_export = $opts->{allow_pdf_export};
 
 381     'TITLE'                => $opts->{title},
 
 382     'TOP_INFO_TEXT'        => $self->html_format($opts->{top_info_text}),
 
 383     'RAW_TOP_INFO_TEXT'    => $opts->{raw_top_info_text},
 
 384     'BOTTOM_INFO_TEXT'     => $self->html_format($opts->{bottom_info_text}),
 
 385     'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
 
 386     'ALLOW_PDF_EXPORT'     => $allow_pdf_export,
 
 387     'ALLOW_CSV_EXPORT'     => $opts->{allow_csv_export},
 
 388     'SHOW_EXPORT_BUTTONS'  => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present},
 
 389     'HEADER_ROWS'          => $header_rows,
 
 390     'NUM_COLUMNS'          => scalar @column_headers,
 
 392     'EXPORT_VARIABLES'     => \@export_variables,
 
 393     'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
 
 394     'EXPORT_NEXTSUB'       => $self->{export}->{nextsub},
 
 395     'DATA_PRESENT'         => $self->{data_present},
 
 401 sub generate_html_content {
 
 403   my $variables = $self->prepare_html_content();
 
 405   return $self->{form}->parse_html_template($self->{options}->{html_template}, $variables);
 
 411   return $_[0] * 72 / 2.54;
 
 418   $text    = decode('UTF-8', $text) if ($self->{text_is_utf8});
 
 423 sub generate_pdf_content {
 
 430   my $variables  = $self->prepare_html_content();
 
 431   my $form       = $self->{form};
 
 432   my $myconfig   = $self->{myconfig};
 
 434   my $opts       = $self->{options};
 
 435   my $pdfopts    = $opts->{pdf_export};
 
 437   my (@data, @column_props, @cell_props);
 
 439   my ($data_row, $cell_props_row);
 
 440   my @visible_columns = $self->get_visible_columns('HTML');
 
 441   my $num_columns     = scalar @visible_columns;
 
 442   my $num_header_rows = 1;
 
 444   my $font_encoding     = $main::dbcharset || 'ISO-8859-15';
 
 445   $self->{text_is_utf8} = $font_encoding =~ m/^utf-?8$/i;
 
 447   foreach my $name (@visible_columns) {
 
 448     push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' };
 
 451   if (!$self->{custom_headers}) {
 
 453     $cell_props_row = [];
 
 454     push @data,       $data_row;
 
 455     push @cell_props, $cell_props_row;
 
 457     foreach my $name (@visible_columns) {
 
 458       my $column = $self->{columns}->{$name};
 
 460       push @{ $data_row },       $self->_decode_text($column->{text});
 
 461       push @{ $cell_props_row }, {};
 
 465     $num_header_rows = scalar @{ $self->{custom_headers} };
 
 467     foreach my $custom_header_row (@{ $self->{custom_headers} }) {
 
 469       $cell_props_row = [];
 
 470       push @data,       $data_row;
 
 471       push @cell_props, $cell_props_row;
 
 473       foreach my $custom_header_col (@{ $custom_header_row }) {
 
 474         push @{ $data_row }, $self->_decode_text($custom_header_col->{text});
 
 476         my $num_output  = ($custom_header_col->{colspan} * 1 > 1) ? $custom_header_col->{colspan} : 1;
 
 477         if ($num_output > 1) {
 
 478           push @{ $data_row },       ('') x ($num_output - 1);
 
 479           push @{ $cell_props_row }, { 'colspan' => $num_output };
 
 480           push @{ $cell_props_row }, ({ }) x ($num_output - 1);
 
 483           push @{ $cell_props_row }, {};
 
 489   foreach my $row_set (@{ $self->{data} }) {
 
 490     if ('HASH' eq ref $row_set) {
 
 491       if ($row_set->{type} eq 'colspan_data') {
 
 492         push @data, [ $self->_decode_text($row_set->{data}) ];
 
 494         $cell_props_row = [];
 
 495         push @cell_props, $cell_props_row;
 
 497         foreach (0 .. $num_columns - 1) {
 
 498           push @{ $cell_props_row }, { 'background_color' => '#666666',
 
 499                                        'font_color'       => '#ffffff',
 
 500                                        'colspan'          => $_ == 0 ? -1 : undef, };
 
 506     foreach my $row (@{ $row_set }) {
 
 508       $cell_props_row = [];
 
 510       push @data,       $data_row;
 
 511       push @cell_props, $cell_props_row;
 
 514       foreach my $col_name (@visible_columns) {
 
 515         my $col = $row->{$col_name};
 
 516         push @{ $data_row }, $self->_decode_text(join("\n", @{ $col->{data} }));
 
 518         $column_props[$col_idx]->{justify} = 'right' if ($col->{align} eq 'right');
 
 520         my $cell_props = { };
 
 521         push @{ $cell_props_row }, $cell_props;
 
 523         if ($col->{colspan} && $col->{colspan} > 1) {
 
 524           $cell_props->{colspan} = $col->{colspan};
 
 532   foreach my $i (0 .. scalar(@data) - 1) {
 
 533     my $aref             = $data[$i];
 
 534     my $num_columns_here = scalar @{ $aref };
 
 536     if ($num_columns_here < $num_columns) {
 
 537       push @{ $aref }, ('') x ($num_columns - $num_columns_here);
 
 538     } elsif ($num_columns_here > $num_columns) {
 
 539       splice @{ $aref }, $num_columns;
 
 544     'a3'         => [ 842, 1190 ],
 
 545     'a4'         => [ 595,  842 ],
 
 546     'a5'         => [ 420,  595 ],
 
 547     'letter'     => [ 612,  792 ],
 
 548     'legal'      => [ 612, 1008 ],
 
 551   my %supported_fonts = map { $_ => 1 } qw(courier georgia helvetica times verdana);
 
 553   my $paper_size  = defined $pdfopts->{paper_size} && defined $papersizes->{lc $pdfopts->{paper_size}} ? lc $pdfopts->{paper_size} : 'a4';
 
 554   my ($paper_width, $paper_height);
 
 556   if (lc $pdfopts->{orientation} eq 'landscape') {
 
 557     ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[1, 0];
 
 559     ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[0, 1];
 
 562   my $margin_top        = _cm2bp($pdfopts->{margin_top}    || 1.5);
 
 563   my $margin_bottom     = _cm2bp($pdfopts->{margin_bottom} || 1.5);
 
 564   my $margin_left       = _cm2bp($pdfopts->{margin_left}   || 1.5);
 
 565   my $margin_right      = _cm2bp($pdfopts->{margin_right}  || 1.5);
 
 567   my $table             = PDF::Table->new();
 
 568   my $pdf               = PDF::API2->new();
 
 569   my $page              = $pdf->page();
 
 571   $pdf->mediabox($paper_width, $paper_height);
 
 573   my $font              = $pdf->corefont(defined $pdfopts->{font_name} && $supported_fonts{lc $pdfopts->{font_name}} ? ucfirst $pdfopts->{font_name} : 'Verdana',
 
 574                                          '-encoding' => $font_encoding);
 
 575   my $font_size         = $pdfopts->{font_size} || 7;
 
 576   my $title_font_size   = $font_size + 1;
 
 578   my $font_height       = $font_size + 2 * $padding;
 
 579   my $title_font_height = $font_size + 2 * $padding;
 
 581   my $header_height     = 2 * $title_font_height if ($opts->{title});
 
 582   my $footer_height     = 2 * $font_height       if ($pdfopts->{number});
 
 584   my $top_text_height   = 0;
 
 586   if ($self->{options}->{top_info_text}) {
 
 587     my $top_text     =  $self->_decode_text($self->{options}->{top_info_text});
 
 588     $top_text        =~ s/\r//g;
 
 589     $top_text        =~ s/\n+$//;
 
 591     my @lines        =  split m/\n/, $top_text;
 
 592     $top_text_height =  $font_height * scalar @lines;
 
 594     foreach my $line_no (0 .. scalar(@lines) - 1) {
 
 595       my $y_pos    = $paper_height - $margin_top - $header_height - $line_no * $font_height;
 
 596       my $text_obj = $page->text();
 
 598       $text_obj->font($font, $font_size);
 
 599       $text_obj->translate($margin_left, $y_pos);
 
 600       $text_obj->text($lines[$line_no]);
 
 608                 'w'                     => $paper_width - $margin_left - $margin_right,
 
 609                 'start_y'               => $paper_height - $margin_top                  - $header_height                  - $top_text_height,
 
 610                 'next_y'                => $paper_height - $margin_top                  - $header_height,
 
 611                 'start_h'               => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height - $top_text_height,
 
 612                 'next_h'                => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height,
 
 614                 'background_color_odd'  => '#ffffff',
 
 615                 'background_color_even' => '#eeeeee',
 
 617                 'font_size'             => $font_size,
 
 618                 'font_color'            => '#000000',
 
 619                 'num_header_rows'       => $num_header_rows,
 
 621                   'bg_color'            => '#ffffff',
 
 623                   'font_color'          => '#000000',
 
 625                 'column_props'          => \@column_props,
 
 626                 'cell_props'            => \@cell_props,
 
 627                 'max_word_length'       => 60,
 
 631   foreach my $page_num (1..$pdf->pages()) {
 
 632     my $curpage  = $pdf->openpage($page_num);
 
 634     if ($pdfopts->{number}) {
 
 635       my $label    = $self->_decode_text($main::locale->text("Page #1/#2", $page_num, $pdf->pages()));
 
 636       my $text_obj = $curpage->text();
 
 638       $text_obj->font($font, $font_size);
 
 639       $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($label) / 2, $margin_bottom);
 
 640       $text_obj->text($label);
 
 643     if ($opts->{title}) {
 
 644       my $title    = $self->_decode_text($opts->{title});
 
 645       my $text_obj = $curpage->text();
 
 647       $text_obj->font($font, $title_font_size);
 
 648       $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($title) / 2,
 
 649                            $paper_height - $margin_top);
 
 650       $text_obj->text($title, '-underline' => 1);
 
 654   my $content = $pdf->stringify();
 
 657   if ($pdfopts->{print} && $pdfopts->{printer_id}) {
 
 658     $form->{printer_id} = $pdfopts->{printer_id};
 
 659     $form->get_printer_code($myconfig);
 
 660     $printer_command = $form->{printer_command};
 
 663   if ($printer_command) {
 
 664     $self->_print_content('printer_command' => $printer_command,
 
 665                           'content'         => $content,
 
 666                           'copies'          => $pdfopts->{copies});
 
 667     $form->{report_generator_printed} = 1;
 
 670     my $filename = $self->get_attachment_basename();
 
 672     print qq|content-type: application/pdf\n|;
 
 673     print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
 
 679 sub verify_paper_size {
 
 681   my $requested_paper_size = lc shift;
 
 682   my $default_paper_size   = shift;
 
 684   my %allowed_paper_sizes  = map { $_ => 1 } qw(a3 a4 a5 letter legal);
 
 686   return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
 
 693   foreach my $i (1 .. max $params{copies}, 1) {
 
 694     my $printer = IO::File->new("| $params{printer_command}");
 
 695     $main::form->error($main::locale->text('Could not spawn the printer command.')) if (!$printer);
 
 696     $printer->print($params{content});
 
 701 sub unescape_string {
 
 704   my $iconv = $main::locale->{iconv};
 
 706   $text     = $main::locale->unquote_special_chars('HTML', $text);
 
 707   $text     = $main::locale->{iconv}->convert($text) if ($main::locale->{iconv});
 
 712 sub generate_csv_content {
 
 715   my %valid_sep_chars    = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
 
 716   my %valid_escape_chars = ('"' => 1, "'" => 1);
 
 717   my %valid_quote_chars  = ('"' => 1, "'" => 1);
 
 719   my $opts        = $self->{options}->{csv_export};
 
 720   my $eol         = $opts->{eol_style} eq 'DOS'               ? "\r\n"                              : "\n";
 
 721   my $sep_char    = $valid_sep_chars{$opts->{sep_char}}       ? $valid_sep_chars{$opts->{sep_char}} : ';';
 
 722   my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char}                : '"';
 
 723   my $quote_char  = $valid_quote_chars{$opts->{quote_char}}   ? $opts->{quote_char}                 : '"';
 
 725   $escape_char    = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
 
 727   my $csv = Text::CSV_XS->new({ 'binary'      => 1,
 
 728                                 'sep_char'    => $sep_char,
 
 729                                 'escape_char' => $escape_char,
 
 730                                 'quote_char'  => $quote_char,
 
 733   my $stdout          = wraphandle(\*STDOUT);
 
 734   my @visible_columns = $self->get_visible_columns('CSV');
 
 736   if ($opts->{headers}) {
 
 737     if (!$self->{custom_headers}) {
 
 738       $csv->print($stdout, [ map { $self->unescape_string($self->{columns}->{$_}->{text}) } @visible_columns ]);
 
 741       foreach my $row (@{ $self->{custom_headers} }) {
 
 744         foreach my $col (@{ $row }) {
 
 745           my $num_output = ($col->{colspan} && ($col->{colspan} > 1)) ? $col->{colspan} : 1;
 
 746           push @{ $fields }, ($self->unescape_string($col->{text})) x $num_output;
 
 749         $csv->print($stdout, $fields);
 
 754   foreach my $row_set (@{ $self->{data} }) {
 
 755     next if ('ARRAY' ne ref $row_set);
 
 756     foreach my $row (@{ $row_set }) {
 
 759       foreach my $col (@visible_columns) {
 
 765         my $num_output = ($col->{colspan} && ($col->{colspan} > 1)) ? $col->{colspan} : 1;
 
 766         $skip_next     = $num_output - 1;
 
 768         push @data, join($eol, map { s/\r?\n/$eol/g; $_ } @{ $row->{$col}->{data} });
 
 769         push @data, ('') x $skip_next if ($skip_next);
 
 772       $csv->print($stdout, \@data);
 
 783 SL::ReportGenerator.pm: the Lx-Office way of getting data in shape
 
 787   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 788      $report->set_options(%options);                         # optional
 
 789      $report->set_columns(%column_defs);
 
 790      $report->set_sort_indicator($column, $direction);       # optional
 
 791      $report->add_data($row1, $row2, @more_rows);
 
 792      $report->generate_with_headers();
 
 794 This creates a report object, sets a few columns, adds some data and generates a standard report.
 
 795 Sorting of columns will be alphabetic, and options will be set to their defaults.
 
 796 The report will be printed including table headers, html headers and http headers.
 
 800 Imagine the following scenario:
 
 801 There's a simple form, which loads some data from the database, and needs to print it out. You write a template for it.
 
 802 Then there may be more than one line. You add a loop in the template.
 
 803 Then there are some options made by the user, such as hidden columns. You add more to the template.
 
 804 Then it lacks usability. You want it to be able to sort the data. You add code for that.
 
 805 Then there are too many results, you need pagination, you want to print or export that data..... and so on.
 
 807 The ReportGenerator class was designed because this exact scenario happened about half a dozen times in Lx-Office.
 
 808 It's purpose is to manage all those formating, culling, sorting, and templating.
 
 809 Which makes it almost as complicated to use as doing the work for yourself.
 
 815 =item new \%myconfig,$form,%options
 
 817 Creates a new ReportGenerator object, sets all given options, and returns it.
 
 819 =item set_columns %columns
 
 821 Sets the columns available to this report.
 
 823 =item set_column_order @columns
 
 825 Sets the order of columns. Any columns not present here are appended in alphabetic order.
 
 827 =item set_sort_indicator $column,$direction
 
 829 Sets sorting ot the table by specifying a column and a direction, where the direction will be evaluated to ascending if true.
 
 830 Note that this is only for displaying. The data has to be presented already sorted.
 
 832 =item add_data \@data
 
 834 =item add_data \%data
 
 836 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.
 
 837 Every line will be expected to be in a kay => value format. Note that the rows have to be already sorted.
 
 838 ReportGenerator does only colum sorting on its own, and provides links to sorting and visual cue as to which column was sorted by.
 
 842 Adds a separator line to the report.
 
 844 =item add_control \%data
 
 846 Adds a control element to the data. Control elements are an experimental feature to add functionality to a report the regular data cannot.
 
 847 Every control element needs to set IS_CONTROL_DATA, in order to be recongnized by the template.
 
 848 Currently the only control element is a colspan element, which can be used as a mini header further down the report.
 
 852 Deletes all data filled into the report, but keeps options set.
 
 854 =item set_options %options
 
 856 Sets options. For an incomplete list of options, see section configuration.
 
 858 =item set_options_from_form
 
 860 Tries to import options from the $form object given at creation
 
 862 =item set_export_options $next_sub,@variable_list
 
 864 Sets next_sub and additional variables needed for export.
 
 866 =item get_attachment_basename
 
 868 Returns the set attachment_basename option, or 'report' if nothing was set. See configuration for the option.
 
 870 =item generate_with_headers
 
 872 Parses the report, adds headers and prints it out. Headers depend on the option 'output_format',
 
 873 for example 'HTML' will add proper table headers, html headers and http headers. See configuration for this option.
 
 875 =item get_visible_columns $format
 
 877 Returns a list of columns that will be visible in the report after considering all options or match the given format.
 
 879 =item html_format $value
 
 881 Escapes HTML characters in $value and substitutes newlines with '<br>'. Returns the escaped $value.
 
 883 =item prepare_html_content $column,$name,@column_headers
 
 885 Parses the data, and sets internal data needed for certain output format. Must be called once before the template is invoked.
 
 886 Should not be called extrenally, since all render and generate functions invoke it anyway.
 
 888 =item generate_html_content
 
 890 The html generation function. Is invoked by generate_with_headers.
 
 892 =item generate_pdf_content
 
 894 The PDF generation function. It is invoked by generate_with_headers and renders the PDF with the PDF::API2 library.
 
 896 =item generate_csv_content
 
 898 The CSV generation function. Uses XS_CSV to parse the information into csv.
 
 904 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.
 
 906 =head2 General Options
 
 910 =item std_column_visibility
 
 912 Standard column visibility. Used if no visibility is set. Use this to save the trouble of enabling every column. Default is no.
 
 916 Output format. Used by generate_with_headers to determine the format. Supported options are HTML, CSV, and PDF. Default is HTML.
 
 918 =item allow_pdf_export
 
 920 Used to determine if a button for PDF export should be displayed. Default is yes.
 
 922 =item allow_csv_export
 
 924 Used to determine if a button for CSV export should be displayed. Default is yes.
 
 928 The template to be used for HTML reports. Default is 'report_generator/html_report'.
 
 938 Paper size. Default is a4. Supported paper sizes are a3, a4, a5, letter and legal.
 
 940 =item orientation (landscape)
 
 942 Landscape or portrait. Default is landscape.
 
 946 Default is Verdana. Supported font names are Courier, Georgia, Helvetica, Times and Verdana. This option only affects the rendering with PDF::API2.
 
 950 Default is 7. This option only affects the rendering with PDF::API2.
 
 960 The paper margins in cm. They all default to 1.5.
 
 964 Set to a true value if the pages should be numbered. Default is 1.
 
 968 If set then the resulting PDF will be output to a printer. If not it will be downloaded by the user. Default is no.
 
 986 Character to enclose entries. Default is double quote (").
 
 990 Character to separate entries. Default is semicolon (;).
 
 994 Character to escape the quote_char. Default is double quote (").
 
 998 End of line style. Default is Unix.
 
1002 Include headers? Default is yes.
 
1010 =head1 MODULE AUTHORS
 
1012 Moritz Bunkus E<lt>mbunkus@linet-services.deE<gt>
 
1014 L<http://linet-services.de>