1 package SL::ReportGenerator;
 
   5 use List::Util qw(max);
 
   7 #use PDF::API2;    # these two eat up to .75s on startup. only load them if we actually need them
 
  12 # Cause locales.pl to parse these files:
 
  13 # parse_html_template('report_generator/html_report')
 
  20   $self->{myconfig} = shift;
 
  21   $self->{form}     = shift;
 
  25     'std_column_visibility' => 0,
 
  26     'output_format'         => 'HTML',
 
  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   $self->set_column_order(sort keys %{ $self->{columns} });
 
  79 sub set_column_order {
 
  82   $self->{column_order} = [ grep { !$seen{$_}++ } @_, sort keys %{ $self->{columns} } ];
 
  85 sub set_sort_indicator {
 
  88   $self->{options}->{sort_indicator_column}    = shift;
 
  89   $self->{options}->{sort_indicator_direction} = shift;
 
  97   while (my $arg = shift) {
 
 100     if ('ARRAY' eq ref $arg) {
 
 103     } elsif ('HASH' eq ref $arg) {
 
 107       $self->{form}->error('Incorrect usage -- expecting hash or array ref');
 
 110     my @columns_with_default_alignment = grep { defined $self->{columns}->{$_}->{align} } keys %{ $self->{columns} };
 
 112     foreach my $row (@{ $row_set }) {
 
 113       foreach my $column (@columns_with_default_alignment) {
 
 114         $row->{$column}          ||= { };
 
 115         $row->{$column}->{align}   = $self->{columns}->{$column}->{align} unless (defined $row->{$column}->{align});
 
 118       foreach my $field (qw(data link)) {
 
 119         map { $row->{$_}->{$field} = [ $row->{$_}->{$field} ] if (ref $row->{$_}->{$field} ne 'ARRAY') } keys %{ $row };
 
 123     push @{ $self->{data} }, $row_set;
 
 124     $last_row_set = $row_set;
 
 126     $self->{data_present} = 1;
 
 129   return $last_row_set;
 
 135   push @{ $self->{data} }, { 'type' => 'separator' };
 
 142   push @{ $self->{data} }, $data;
 
 149   $self->{data_present} = 0;
 
 156   while (my ($key, $value) = each %options) {
 
 157     if ($key eq 'pdf_export') {
 
 158       map { $self->{options}->{pdf_export}->{$_} = $value->{$_} } keys %{ $value };
 
 160       $self->{options}->{$key} = $value;
 
 165 sub set_options_from_form {
 
 168   my $form     = $self->{form};
 
 169   my $myconfig = $self->{myconfig};
 
 171   foreach my $key (qw(output_format)) {
 
 172     my $full_key = "report_generator_${key}";
 
 173     $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
 
 176   foreach my $format (qw(pdf csv)) {
 
 177     my $opts = $self->{options}->{"${format}_export"};
 
 178     foreach my $key (keys %{ $opts }) {
 
 179       my $full_key = "report_generator_${format}_options_${key}";
 
 180       $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
 
 185 sub set_export_options {
 
 190     'variable_list' => [ @_ ],
 
 194 sub set_custom_headers {
 
 198     $self->{custom_headers} = [ @_ ];
 
 200     delete $self->{custom_headers};
 
 204 sub get_attachment_basename {
 
 206   my $filename =  $self->{options}->{attachment_basename} || 'report';
 
 207   $filename    =~ s|.*\\||;
 
 208   $filename    =~ s|.*/||;
 
 213 sub generate_with_headers {
 
 215   my $format = lc $self->{options}->{output_format};
 
 216   my $form   = $self->{form};
 
 218   if (!$self->{columns}) {
 
 219     $form->error('Incorrect usage -- no columns specified');
 
 222   if ($format eq 'html') {
 
 223     my $title      = $form->{title};
 
 224     $form->{title} = $self->{title} if ($self->{title});
 
 226     $form->{title} = $title;
 
 228     print $self->generate_html_content();
 
 230   } elsif ($format eq 'csv') {
 
 231     my $filename = $self->get_attachment_basename();
 
 232     print qq|content-type: text/csv\n|;
 
 233     print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
 
 234     $::locale->with_raw_io(\*STDOUT, sub {
 
 235       $self->generate_csv_content();
 
 238   } elsif ($format eq 'pdf') {
 
 239     $self->generate_pdf_content();
 
 242     $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
 
 246 sub get_visible_columns {
 
 250   return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /\Q${format}\E/i)) } @{ $self->{column_order} };
 
 257   $value =  $main::locale->quote_special_chars('HTML', $value);
 
 259   $value =~ s/\n/<br>/g;
 
 264 sub prepare_html_content {
 
 267   my ($column, $name, @column_headers);
 
 269   my $opts            = $self->{options};
 
 270   my @visible_columns = $self->get_visible_columns('HTML');
 
 272   foreach $name (@visible_columns) {
 
 273     $column = $self->{columns}->{$name};
 
 277       'align'                    => $column->{align},
 
 278       'link'                     => $column->{link},
 
 279       'text'                     => $column->{text},
 
 280       'show_sort_indicator'      => $name eq $opts->{sort_indicator_column},
 
 281       'sort_indicator_direction' => $opts->{sort_indicator_direction},
 
 284     push @column_headers, $header;
 
 288   if ($self->{custom_headers}) {
 
 289     $header_rows = $self->{custom_headers};
 
 291     $header_rows = [ \@column_headers ];
 
 294   my ($outer_idx, $inner_idx) = (0, 0);
 
 298   foreach my $row_set (@{ $self->{data} }) {
 
 299     if ('HASH' eq ref $row_set) {
 
 300       if ($row_set->{type} eq 'separator') {
 
 301         if (! scalar @rows) {
 
 302           $next_border_top = 1;
 
 304           $rows[-1]->{BORDER_BOTTOM} = 1;
 
 312         'IS_COLSPAN_DATA' => $row_set->{type} eq 'colspan_data',
 
 313         'NUM_COLUMNS'     => scalar @visible_columns,
 
 314         'BORDER_TOP'      => $next_border_top,
 
 315         'data'            => $row_set->{data},
 
 318       push @rows, $row_data;
 
 320       $next_border_top = 0;
 
 327     foreach my $row (@{ $row_set }) {
 
 330       my $output_columns = [ ];
 
 332       foreach my $col_name (@visible_columns) {
 
 338         my $col = $row->{$col_name} || { data => [] };
 
 339         $col->{CELL_ROWS} = [ ];
 
 340         foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
 
 341           push @{ $col->{CELL_ROWS} }, {
 
 342             'data' => $self->html_format($col->{data}->[$i]),
 
 343             'link' => $col->{link}->[$i],
 
 347         # Force at least a   to be displayed so that browsers
 
 348         # will format the table cell (e.g. borders etc).
 
 349         if (!scalar @{ $col->{CELL_ROWS} }) {
 
 350           push @{ $col->{CELL_ROWS} }, { 'data' => ' ' };
 
 351         } elsif ((1 == scalar @{ $col->{CELL_ROWS} }) && (!defined $col->{CELL_ROWS}->[0]->{data} || ($col->{CELL_ROWS}->[0]->{data} eq ''))) {
 
 352           $col->{CELL_ROWS}->[0]->{data} = ' ';
 
 355         push @{ $output_columns }, $col;
 
 356         $skip_next = $col->{colspan} ? $col->{colspan} - 1 : 0;
 
 360         'COLUMNS'       => $output_columns,
 
 361         'outer_idx'     => $outer_idx,
 
 362         'outer_idx_odd' => $outer_idx % 2,
 
 363         'inner_idx'     => $inner_idx,
 
 364         'BORDER_TOP'    => $next_border_top,
 
 367       push @rows, $row_data;
 
 369       $next_border_top = 0;
 
 373   my @export_variables = $self->{form}->flatten_variables(@{ $self->{export}->{variable_list} });
 
 375   my $allow_pdf_export = $opts->{allow_pdf_export};
 
 378     'TITLE'                => $opts->{title},
 
 379     'TOP_INFO_TEXT'        => $self->html_format($opts->{top_info_text}),
 
 380     'RAW_TOP_INFO_TEXT'    => $opts->{raw_top_info_text},
 
 381     'BOTTOM_INFO_TEXT'     => $self->html_format($opts->{bottom_info_text}),
 
 382     'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
 
 383     'ALLOW_PDF_EXPORT'     => $allow_pdf_export,
 
 384     'ALLOW_CSV_EXPORT'     => $opts->{allow_csv_export},
 
 385     'SHOW_EXPORT_BUTTONS'  => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present},
 
 386     'HEADER_ROWS'          => $header_rows,
 
 387     'NUM_COLUMNS'          => scalar @column_headers,
 
 389     'EXPORT_VARIABLES'     => \@export_variables,
 
 390     'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
 
 391     'EXPORT_NEXTSUB'       => $self->{export}->{nextsub},
 
 392     'DATA_PRESENT'         => $self->{data_present},
 
 398 sub generate_html_content {
 
 400   my $variables = $self->prepare_html_content();
 
 402   return $self->{form}->parse_html_template($self->{options}->{html_template}, $variables);
 
 408   return $_[0] * 72 / 2.54;
 
 411 sub generate_pdf_content {
 
 418   my $variables  = $self->prepare_html_content();
 
 419   my $form       = $self->{form};
 
 420   my $myconfig   = $self->{myconfig};
 
 422   my $opts       = $self->{options};
 
 423   my $pdfopts    = $opts->{pdf_export};
 
 425   my (@data, @column_props, @cell_props);
 
 427   my ($data_row, $cell_props_row);
 
 428   my @visible_columns = $self->get_visible_columns('PDF');
 
 429   my $num_columns     = scalar @visible_columns;
 
 430   my $num_header_rows = 1;
 
 432   my $font_encoding   = $main::dbcharset || 'ISO-8859-15';
 
 434   foreach my $name (@visible_columns) {
 
 435     push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' };
 
 438   if (!$self->{custom_headers}) {
 
 440     $cell_props_row = [];
 
 441     push @data,       $data_row;
 
 442     push @cell_props, $cell_props_row;
 
 444     foreach my $name (@visible_columns) {
 
 445       my $column = $self->{columns}->{$name};
 
 447       push @{ $data_row },       $column->{text};
 
 448       push @{ $cell_props_row }, {};
 
 452     $num_header_rows = scalar @{ $self->{custom_headers} };
 
 454     foreach my $custom_header_row (@{ $self->{custom_headers} }) {
 
 456       $cell_props_row = [];
 
 457       push @data,       $data_row;
 
 458       push @cell_props, $cell_props_row;
 
 460       foreach my $custom_header_col (@{ $custom_header_row }) {
 
 461         push @{ $data_row }, $custom_header_col->{text};
 
 463         my $num_output  = ($custom_header_col->{colspan} * 1 > 1) ? $custom_header_col->{colspan} : 1;
 
 464         if ($num_output > 1) {
 
 465           push @{ $data_row },       ('') x ($num_output - 1);
 
 466           push @{ $cell_props_row }, { 'colspan' => $num_output };
 
 467           push @{ $cell_props_row }, ({ }) x ($num_output - 1);
 
 470           push @{ $cell_props_row }, {};
 
 476   foreach my $row_set (@{ $self->{data} }) {
 
 477     if ('HASH' eq ref $row_set) {
 
 478       if ($row_set->{type} eq 'colspan_data') {
 
 479         push @data, [ $row_set->{data} ];
 
 481         $cell_props_row = [];
 
 482         push @cell_props, $cell_props_row;
 
 484         foreach (0 .. $num_columns - 1) {
 
 485           push @{ $cell_props_row }, { 'background_color' => '#666666',
 
 486                                        'font_color'       => '#ffffff',
 
 487                                        'colspan'          => $_ == 0 ? -1 : undef, };
 
 493     foreach my $row (@{ $row_set }) {
 
 495       $cell_props_row = [];
 
 497       push @data,       $data_row;
 
 498       push @cell_props, $cell_props_row;
 
 501       foreach my $col_name (@visible_columns) {
 
 502         my $col = $row->{$col_name};
 
 503         push @{ $data_row }, join("\n", @{ $col->{data} || [] });
 
 505         $column_props[$col_idx]->{justify} = 'right' if ($col->{align} eq 'right');
 
 507         my $cell_props = { };
 
 508         push @{ $cell_props_row }, $cell_props;
 
 510         if ($col->{colspan} && $col->{colspan} > 1) {
 
 511           $cell_props->{colspan} = $col->{colspan};
 
 519   foreach my $i (0 .. scalar(@data) - 1) {
 
 520     my $aref             = $data[$i];
 
 521     my $num_columns_here = scalar @{ $aref };
 
 523     if ($num_columns_here < $num_columns) {
 
 524       push @{ $aref }, ('') x ($num_columns - $num_columns_here);
 
 525     } elsif ($num_columns_here > $num_columns) {
 
 526       splice @{ $aref }, $num_columns;
 
 531     'a3'         => [ 842, 1190 ],
 
 532     'a4'         => [ 595,  842 ],
 
 533     'a5'         => [ 420,  595 ],
 
 534     'letter'     => [ 612,  792 ],
 
 535     'legal'      => [ 612, 1008 ],
 
 538   my %supported_fonts = map { $_ => 1 } qw(courier georgia helvetica times verdana);
 
 540   my $paper_size  = defined $pdfopts->{paper_size} && defined $papersizes->{lc $pdfopts->{paper_size}} ? lc $pdfopts->{paper_size} : 'a4';
 
 541   my ($paper_width, $paper_height);
 
 543   if (lc $pdfopts->{orientation} eq 'landscape') {
 
 544     ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[1, 0];
 
 546     ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[0, 1];
 
 549   my $margin_top        = _cm2bp($pdfopts->{margin_top}    || 1.5);
 
 550   my $margin_bottom     = _cm2bp($pdfopts->{margin_bottom} || 1.5);
 
 551   my $margin_left       = _cm2bp($pdfopts->{margin_left}   || 1.5);
 
 552   my $margin_right      = _cm2bp($pdfopts->{margin_right}  || 1.5);
 
 554   my $table             = PDF::Table->new();
 
 555   my $pdf               = PDF::API2->new();
 
 556   my $page              = $pdf->page();
 
 558   $pdf->mediabox($paper_width, $paper_height);
 
 560   my $font              = $pdf->corefont(defined $pdfopts->{font_name} && $supported_fonts{lc $pdfopts->{font_name}} ? ucfirst $pdfopts->{font_name} : 'Verdana',
 
 561                                          '-encoding' => $font_encoding);
 
 562   my $font_size         = $pdfopts->{font_size} || 7;
 
 563   my $title_font_size   = $font_size + 1;
 
 565   my $font_height       = $font_size + 2 * $padding;
 
 566   my $title_font_height = $font_size + 2 * $padding;
 
 568   my $header_height     = 2 * $title_font_height if ($opts->{title});
 
 569   my $footer_height     = 2 * $font_height       if ($pdfopts->{number});
 
 571   my $top_text_height   = 0;
 
 573   if ($self->{options}->{top_info_text}) {
 
 574     my $top_text     =  $self->{options}->{top_info_text};
 
 575     $top_text        =~ s/\r//g;
 
 576     $top_text        =~ s/\n+$//;
 
 578     my @lines        =  split m/\n/, $top_text;
 
 579     $top_text_height =  $font_height * scalar @lines;
 
 581     foreach my $line_no (0 .. scalar(@lines) - 1) {
 
 582       my $y_pos    = $paper_height - $margin_top - $header_height - $line_no * $font_height;
 
 583       my $text_obj = $page->text();
 
 585       $text_obj->font($font, $font_size);
 
 586       $text_obj->translate($margin_left, $y_pos);
 
 587       $text_obj->text($lines[$line_no]);
 
 595                 'w'                     => $paper_width - $margin_left - $margin_right,
 
 596                 'start_y'               => $paper_height - $margin_top                  - $header_height                  - $top_text_height,
 
 597                 'next_y'                => $paper_height - $margin_top                  - $header_height,
 
 598                 'start_h'               => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height - $top_text_height,
 
 599                 'next_h'                => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height,
 
 601                 'background_color_odd'  => '#ffffff',
 
 602                 'background_color_even' => '#eeeeee',
 
 604                 'font_size'             => $font_size,
 
 605                 'font_color'            => '#000000',
 
 606                 'num_header_rows'       => $num_header_rows,
 
 608                   'bg_color'            => '#ffffff',
 
 610                   'font_color'          => '#000000',
 
 612                 'column_props'          => \@column_props,
 
 613                 'cell_props'            => \@cell_props,
 
 614                 'max_word_length'       => 60,
 
 618   foreach my $page_num (1..$pdf->pages()) {
 
 619     my $curpage  = $pdf->openpage($page_num);
 
 621     if ($pdfopts->{number}) {
 
 622       my $label    = $main::locale->text("Page #1/#2", $page_num, $pdf->pages());
 
 623       my $text_obj = $curpage->text();
 
 625       $text_obj->font($font, $font_size);
 
 626       $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($label) / 2, $margin_bottom);
 
 627       $text_obj->text($label);
 
 630     if ($opts->{title}) {
 
 631       my $title    = $opts->{title};
 
 632       my $text_obj = $curpage->text();
 
 634       $text_obj->font($font, $title_font_size);
 
 635       $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($title) / 2,
 
 636                            $paper_height - $margin_top);
 
 637       $text_obj->text($title, '-underline' => 1);
 
 641   my $content = $pdf->stringify();
 
 644   if ($pdfopts->{print} && $pdfopts->{printer_id}) {
 
 645     $form->{printer_id} = $pdfopts->{printer_id};
 
 646     $form->get_printer_code($myconfig);
 
 647     $printer_command = $form->{printer_command};
 
 650   if ($printer_command) {
 
 651     $self->_print_content('printer_command' => $printer_command,
 
 652                           'content'         => $content,
 
 653                           'copies'          => $pdfopts->{copies});
 
 654     $form->{report_generator_printed} = 1;
 
 657     my $filename = $self->get_attachment_basename();
 
 659     print qq|content-type: application/pdf\n|;
 
 660     print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
 
 662     $::locale->with_raw_io(\*STDOUT, sub {
 
 668 sub verify_paper_size {
 
 670   my $requested_paper_size = lc shift;
 
 671   my $default_paper_size   = shift;
 
 673   my %allowed_paper_sizes  = map { $_ => 1 } qw(a3 a4 a5 letter legal);
 
 675   return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
 
 682   foreach my $i (1 .. max $params{copies}, 1) {
 
 683     my $printer = IO::File->new("| $params{printer_command}");
 
 684     $main::form->error($main::locale->text('Could not spawn the printer command.')) if (!$printer);
 
 685     $printer->print($params{content});
 
 690 sub unescape_string {
 
 694   $text     = $main::locale->unquote_special_chars('HTML', $text);
 
 695   $text     = $::locale->{iconv}->convert($text);
 
 700 sub generate_csv_content {
 
 703   my %valid_sep_chars    = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
 
 704   my %valid_escape_chars = ('"' => 1, "'" => 1);
 
 705   my %valid_quote_chars  = ('"' => 1, "'" => 1);
 
 707   my $opts        = $self->{options}->{csv_export};
 
 708   my $eol         = $opts->{eol_style} eq 'DOS'               ? "\r\n"                              : "\n";
 
 709   my $sep_char    = $valid_sep_chars{$opts->{sep_char}}       ? $valid_sep_chars{$opts->{sep_char}} : ';';
 
 710   my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char}                : '"';
 
 711   my $quote_char  = $valid_quote_chars{$opts->{quote_char}}   ? $opts->{quote_char}                 : '"';
 
 713   $escape_char    = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
 
 715   my $csv = Text::CSV_XS->new({ 'binary'      => 1,
 
 716                                 'sep_char'    => $sep_char,
 
 717                                 'escape_char' => $escape_char,
 
 718                                 'quote_char'  => $quote_char,
 
 721   my $stdout          = wraphandle(\*STDOUT);
 
 722   my @visible_columns = $self->get_visible_columns('CSV');
 
 724   if ($opts->{headers}) {
 
 725     if (!$self->{custom_headers}) {
 
 726       $csv->print($stdout, [ map { $self->unescape_string($self->{columns}->{$_}->{text}) } @visible_columns ]);
 
 729       foreach my $row (@{ $self->{custom_headers} }) {
 
 732         foreach my $col (@{ $row }) {
 
 733           my $num_output = ($col->{colspan} && ($col->{colspan} > 1)) ? $col->{colspan} : 1;
 
 734           push @{ $fields }, ($self->unescape_string($col->{text})) x $num_output;
 
 737         $csv->print($stdout, $fields);
 
 742   foreach my $row_set (@{ $self->{data} }) {
 
 743     next if ('ARRAY' ne ref $row_set);
 
 744     foreach my $row (@{ $row_set }) {
 
 747       foreach my $col (@visible_columns) {
 
 753         my $num_output = ($row->{$col}{colspan} && ($row->{$col}->{colspan} > 1)) ? $row->{$col}->{colspan} : 1;
 
 754         $skip_next     = $num_output - 1;
 
 756         push @data, join($eol, map { s/\r?\n/$eol/g; $_ } @{ $row->{$col}->{data} });
 
 757         push @data, ('') x $skip_next if ($skip_next);
 
 760       $csv->print($stdout, \@data);
 
 771 SL::ReportGenerator.pm: the Lx-Office way of getting data in shape
 
 775   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 776      $report->set_options(%options);                         # optional
 
 777      $report->set_columns(%column_defs);
 
 778      $report->set_sort_indicator($column, $direction);       # optional
 
 779      $report->add_data($row1, $row2, @more_rows);
 
 780      $report->generate_with_headers();
 
 782 This creates a report object, sets a few columns, adds some data and generates a standard report.
 
 783 Sorting of columns will be alphabetic, and options will be set to their defaults.
 
 784 The report will be printed including table headers, html headers and http headers.
 
 788 Imagine the following scenario:
 
 789 There's a simple form, which loads some data from the database, and needs to print it out. You write a template for it.
 
 790 Then there may be more than one line. You add a loop in the template.
 
 791 Then there are some options made by the user, such as hidden columns. You add more to the template.
 
 792 Then it lacks usability. You want it to be able to sort the data. You add code for that.
 
 793 Then there are too many results, you need pagination, you want to print or export that data..... and so on.
 
 795 The ReportGenerator class was designed because this exact scenario happened about half a dozen times in Lx-Office.
 
 796 It's purpose is to manage all those formating, culling, sorting, and templating.
 
 797 Which makes it almost as complicated to use as doing the work for yourself.
 
 803 =item new \%myconfig,$form,%options
 
 805 Creates a new ReportGenerator object, sets all given options, and returns it.
 
 807 =item set_columns %columns
 
 809 Sets the columns available to this report.
 
 811 =item set_column_order @columns
 
 813 Sets the order of columns. Any columns not present here are appended in alphabetic order.
 
 815 =item set_sort_indicator $column,$direction
 
 817 Sets sorting ot the table by specifying a column and a direction, where the direction will be evaluated to ascending if true.
 
 818 Note that this is only for displaying. The data has to be presented already sorted.
 
 820 =item add_data \@data
 
 822 =item add_data \%data
 
 824 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.
 
 825 Every line will be expected to be in a kay => value format. Note that the rows have to be already sorted.
 
 826 ReportGenerator does only colum sorting on its own, and provides links to sorting and visual cue as to which column was sorted by.
 
 830 Adds a separator line to the report.
 
 832 =item add_control \%data
 
 834 Adds a control element to the data. Control elements are an experimental feature to add functionality to a report the regular data cannot.
 
 835 Every control element needs to set IS_CONTROL_DATA, in order to be recongnized by the template.
 
 836 Currently the only control element is a colspan element, which can be used as a mini header further down the report.
 
 840 Deletes all data filled into the report, but keeps options set.
 
 842 =item set_options %options
 
 844 Sets options. For an incomplete list of options, see section configuration.
 
 846 =item set_options_from_form
 
 848 Tries to import options from the $form object given at creation
 
 850 =item set_export_options $next_sub,@variable_list
 
 852 Sets next_sub and additional variables needed for export.
 
 854 =item get_attachment_basename
 
 856 Returns the set attachment_basename option, or 'report' if nothing was set. See configuration for the option.
 
 858 =item generate_with_headers
 
 860 Parses the report, adds headers and prints it out. Headers depend on the option 'output_format',
 
 861 for example 'HTML' will add proper table headers, html headers and http headers. See configuration for this option.
 
 863 =item get_visible_columns $format
 
 865 Returns a list of columns that will be visible in the report after considering all options or match the given format.
 
 867 =item html_format $value
 
 869 Escapes HTML characters in $value and substitutes newlines with '<br>'. Returns the escaped $value.
 
 871 =item prepare_html_content $column,$name,@column_headers
 
 873 Parses the data, and sets internal data needed for certain output format. Must be called once before the template is invoked.
 
 874 Should not be called extrenally, since all render and generate functions invoke it anyway.
 
 876 =item generate_html_content
 
 878 The html generation function. Is invoked by generate_with_headers.
 
 880 =item generate_pdf_content
 
 882 The PDF generation function. It is invoked by generate_with_headers and renders the PDF with the PDF::API2 library.
 
 884 =item generate_csv_content
 
 886 The CSV generation function. Uses XS_CSV to parse the information into csv.
 
 892 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.
 
 894 =head2 General Options
 
 898 =item std_column_visibility
 
 900 Standard column visibility. Used if no visibility is set. Use this to save the trouble of enabling every column. Default is no.
 
 904 Output format. Used by generate_with_headers to determine the format. Supported options are HTML, CSV, and PDF. Default is HTML.
 
 906 =item allow_pdf_export
 
 908 Used to determine if a button for PDF export should be displayed. Default is yes.
 
 910 =item allow_csv_export
 
 912 Used to determine if a button for CSV export should be displayed. Default is yes.
 
 916 The template to be used for HTML reports. Default is 'report_generator/html_report'.
 
 926 Paper size. Default is a4. Supported paper sizes are a3, a4, a5, letter and legal.
 
 928 =item orientation (landscape)
 
 930 Landscape or portrait. Default is landscape.
 
 934 Default is Verdana. Supported font names are Courier, Georgia, Helvetica, Times and Verdana. This option only affects the rendering with PDF::API2.
 
 938 Default is 7. This option only affects the rendering with PDF::API2.
 
 948 The paper margins in cm. They all default to 1.5.
 
 952 Set to a true value if the pages should be numbered. Default is 1.
 
 956 If set then the resulting PDF will be output to a printer. If not it will be downloaded by the user. Default is no.
 
 974 Character to enclose entries. Default is double quote (").
 
 978 Character to separate entries. Default is semicolon (;).
 
 982 Character to escape the quote_char. Default is double quote (").
 
 986 End of line style. Default is Unix.
 
 990 Include headers? Default is yes.
 
 998 =head1 MODULE AUTHORS
 
1000 Moritz Bunkus E<lt>mbunkus@linet-services.deE<gt>
 
1002 L<http://linet-services.de>