1 package SL::ReportGenerator;
4 use List::Util qw(max);
5 use Scalar::Util qw(blessed);
7 #use PDF::API2; # these two eat up to .75s on startup. only load them if we actually need them
11 use SL::Helper::GlAttachments qw(append_gl_pdf_attachments);
12 use SL::Helper::CreatePDF qw(merge_pdfs);
13 use SL::JSON qw(to_json);
15 # Cause locales.pl to parse these files:
16 # parse_html_template('report_generator/html_report')
23 $self->{myconfig} = shift;
24 $self->{form} = shift;
28 'std_column_visibility' => 0,
29 'output_format' => 'HTML',
30 'controller_class ' => '',
31 'allow_pdf_export' => 1,
32 'allow_csv_export' => 1,
33 'allow_chart_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',
55 'encoding' => 'UTF-8',
59 'assignments_y' => [],
64 'variable_list' => [],
67 $self->{data_present} = 0;
71 $self->set_options(@_) if (@_);
80 $self->{columns} = \%columns;
82 foreach my $column (values %{ $self->{columns} }) {
83 $column->{visible} = $self->{options}->{std_column_visibility} unless defined $column->{visible};
86 if( $::form->{report_generator_csv_options_for_import} ) {
87 foreach my $key (keys %{ $self->{columns} }) {
88 $self->{columns}{$key}{text} = $key;
92 $self->set_column_order(sort keys %{ $self->{columns} });
95 sub set_column_order {
98 $self->{column_order} = [ grep { !$seen{$_}++ } @_, sort keys %{ $self->{columns} } ];
101 sub set_sort_indicator {
104 $self->{options}->{sort_indicator_column} = shift;
105 $self->{options}->{sort_indicator_direction} = shift;
113 while (my $arg = shift) {
116 if ('ARRAY' eq ref $arg) {
119 } elsif ('HASH' eq ref $arg) {
123 $self->{form}->error('Incorrect usage -- expecting hash or array ref');
126 my @columns_with_default_alignment = grep { defined $self->{columns}->{$_}->{align} } keys %{ $self->{columns} };
128 foreach my $row (@{ $row_set }) {
129 foreach my $column (@columns_with_default_alignment) {
130 $row->{$column} ||= { };
131 $row->{$column}->{align} = $self->{columns}->{$column}->{align} unless (defined $row->{$column}->{align});
134 foreach my $field (qw(data link link_class)) {
135 map { $row->{$_}->{$field} = [ $row->{$_}->{$field} ] if (ref $row->{$_}->{$field} ne 'ARRAY') } keys %{ $row };
139 push @{ $self->{data} }, $row_set;
140 $last_row_set = $row_set;
142 $self->{data_present} = 1;
145 return $last_row_set;
151 push @{ $self->{data} }, { 'type' => 'separator' };
158 push @{ $self->{data} }, $data;
165 $self->{data_present} = 0;
172 while (my ($key, $value) = each %options) {
173 if ($key eq 'pdf_export') {
174 $self->{options}->{pdf_export}->{$_} = $value->{$_} for keys %{ $value };
175 } elsif ($key eq 'csv_export') {
176 $self->{options}->{csv_export}->{$_} = $value->{$_} for keys %{ $value };
177 } elsif ($key eq 'chart_export') {
178 $self->{options}->{chart_export}->{$_} = $value->{$_} for keys %{ $value };
180 $self->{options}->{$key} = $value;
185 sub set_options_from_form {
188 my $form = $self->{form};
189 my $myconfig = $self->{myconfig};
191 foreach my $key (qw(output_format)) {
192 my $full_key = "report_generator_${key}";
193 $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
196 foreach my $format (qw(pdf csv chart)) {
197 my $opts = $self->{options}->{"${format}_export"};
198 foreach my $key (keys %{ $opts }) {
199 my $full_key = "report_generator_${format}_options_${key}";
200 $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
205 sub set_export_options {
210 'variable_list' => [ @_ ],
214 sub set_custom_headers {
218 $self->{custom_headers} = [ @_ ];
220 delete $self->{custom_headers};
224 sub get_attachment_basename {
226 my $filename = $self->{options}->{attachment_basename} || 'report';
228 # FIXME: this is bonkers. add a real sluggify method somewhere or import one.
229 $filename =~ s|.*\\||;
230 $filename =~ s|.*/||;
231 $filename =~ s| |_|g;
236 sub generate_with_headers {
237 my ($self, %params) = @_;
238 my $format = lc $self->{options}->{output_format};
239 my $form = $self->{form};
241 if (!$self->{columns}) {
242 $form->error('Incorrect usage -- no columns specified');
245 if ($format eq 'html') {
246 my $content = $self->generate_html_content(%params);
247 my $title = $form->{title};
248 $form->{title} = $self->{title} if ($self->{title});
249 $form->header(no_layout => $params{no_layout});
250 $form->{title} = $title;
254 } elsif ($format eq 'csv') {
255 # FIXME: don't do mini http in here
256 my $filename = $self->get_attachment_basename();
257 print qq|content-type: text/csv\n|;
258 print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
259 $::locale->with_raw_io(\*STDOUT, sub {
260 $self->generate_csv_content();
263 } elsif ($format eq 'pdf') {
264 $self->generate_pdf_content();
266 } elsif ($format eq 'chart') {
267 $self->generate_chart_content();
270 $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF, Chart)');
274 sub get_visible_columns {
278 return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /\Q${format}\E/i)) } @{ $self->{column_order} };
285 $value = $main::locale->quote_special_chars('HTML', $value);
287 $value =~ s/\n/<br>/g;
292 sub prepare_html_content {
293 my ($self, %params) = @_;
295 my ($column, $name, @column_headers);
297 my $opts = $self->{options};
298 my @visible_columns = $self->get_visible_columns('HTML');
300 foreach $name (@visible_columns) {
301 $column = $self->{columns}->{$name};
305 'align' => $column->{align},
306 'link' => $column->{link},
307 'text' => $column->{text},
308 'raw_header_data' => $column->{raw_header_data},
309 'show_sort_indicator' => $name eq $opts->{sort_indicator_column},
310 'sort_indicator_direction' => $opts->{sort_indicator_direction},
313 push @column_headers, $header;
317 if ($self->{custom_headers}) {
318 $header_rows = $self->{custom_headers};
320 $header_rows = [ \@column_headers ];
323 my ($outer_idx, $inner_idx) = (0, 0);
327 foreach my $row_set (@{ $self->{data} }) {
328 if ('HASH' eq ref $row_set) {
329 if ($row_set->{type} eq 'separator') {
330 if (! scalar @rows) {
331 $next_border_top = 1;
333 $rows[-1]->{BORDER_BOTTOM} = 1;
341 'IS_COLSPAN_DATA' => $row_set->{type} eq 'colspan_data',
342 'NUM_COLUMNS' => scalar @visible_columns,
343 'BORDER_TOP' => $next_border_top,
344 'data' => $row_set->{data},
347 push @rows, $row_data;
349 $next_border_top = 0;
356 foreach my $row (@{ $row_set }) {
359 my $output_columns = [ ];
361 foreach my $col_name (@visible_columns) {
367 my $col = $row->{$col_name} || { data => [] };
368 $col->{CELL_ROWS} = [ ];
369 foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
370 push @{ $col->{CELL_ROWS} }, {
371 'data' => '' . $self->html_format($col->{data}->[$i]),
372 'link' => $col->{link}->[$i],
373 link_class => $col->{link_class}->[$i],
377 # Force at least a to be displayed so that browsers
378 # will format the table cell (e.g. borders etc).
379 if (!scalar @{ $col->{CELL_ROWS} }) {
380 push @{ $col->{CELL_ROWS} }, { 'data' => ' ' };
381 } elsif ((1 == scalar @{ $col->{CELL_ROWS} }) && (!defined $col->{CELL_ROWS}->[0]->{data} || ($col->{CELL_ROWS}->[0]->{data} eq ''))) {
382 $col->{CELL_ROWS}->[0]->{data} = ' ';
385 push @{ $output_columns }, $col;
386 $skip_next = $col->{colspan} ? $col->{colspan} - 1 : 0;
390 'COLUMNS' => $output_columns,
391 'outer_idx' => $outer_idx,
392 'outer_idx_odd' => $outer_idx % 2,
393 'inner_idx' => $inner_idx,
394 'BORDER_TOP' => $next_border_top,
397 push @rows, $row_data;
399 $next_border_top = 0;
403 my @export_variables = $self->{form}->flatten_variables(@{ $self->{export}->{variable_list} });
405 my $allow_pdf_export = $opts->{allow_pdf_export};
408 'TITLE' => $opts->{title},
409 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
410 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
411 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
412 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
413 'ALLOW_PDF_EXPORT' => $allow_pdf_export,
414 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export},
415 'ALLOW_CHART_EXPORT' => $opts->{allow_chart_export},
416 'SHOW_EXPORT_BUTTONS' => ($allow_pdf_export || $opts->{allow_csv_export} || $opts->{allow_chart_export}) && $self->{data_present},
417 'HEADER_ROWS' => $header_rows,
418 'NUM_COLUMNS' => scalar @column_headers,
420 'EXPORT_VARIABLES' => \@export_variables,
421 'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
422 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
423 'DATA_PRESENT' => $self->{data_present},
424 'CONTROLLER_DISPATCH' => $opts->{controller_class},
425 'TABLE_CLASS' => $opts->{table_class},
426 'SKIP_BUTTONS' => !!$params{action_bar},
432 sub create_action_bar_actions {
433 my ($self, $variables, %params) = @_;
436 foreach my $type (qw(pdf csv chart)) {
437 next unless $variables->{"ALLOW_" . uc($type) . "_EXPORT"};
439 my $key = $variables->{CONTROLLER_DISPATCH} ? 'action' : 'report_generator_dispatch_to';
440 my $value = "report_generator_export_as_${type}";
441 $value = $variables->{CONTROLLER_DISPATCH} . "/${value}" if $variables->{CONTROLLER_DISPATCH};
443 push @actions, action => [
444 $type eq 'pdf' ? $::locale->text('PDF export') : $type eq 'csv' ? $::locale->text('CSV export') : $::locale->text('Chart export'),
445 submit => [ '#report_generator_form', {(
447 defined $params{action_bar_additional_submit_values}
448 ? %{$params{action_bar_additional_submit_values}}
454 if (scalar(@actions) > 1) {
457 action => [ $::locale->text('Export') ],
466 sub setup_action_bar {
467 my ($self, $variables, %params) = @_;
469 my @actions = $self->create_action_bar_actions($variables, %params);
471 if ($params{action_bar_setup_hook}) {
472 $params{action_bar_setup_hook}->(@actions);
475 my $action_bar = blessed($params{action_bar}) ? $params{action_bar} : ($::request->layout->get('actionbar'))[0];
476 $action_bar->add(@actions);
480 sub generate_html_content {
481 my ($self, %params) = @_;
483 $params{action_bar} //= 1;
485 my $variables = $self->prepare_html_content(%params);
486 $self->setup_action_bar($variables, %params) if $params{action_bar};
488 my $stuff = $self->{form}->parse_html_template($self->{options}->{html_template}, $variables);
495 return $_[0] * 72 / 2.54;
498 sub generate_pdf_content {
506 my $variables = $self->prepare_html_content();
507 my $form = $self->{form};
508 my $myconfig = $self->{myconfig};
510 my $opts = $self->{options};
511 my $pdfopts = $opts->{pdf_export};
513 my (@data, @column_props, @cell_props);
515 my ($data_row, $cell_props_row);
516 my @visible_columns = $self->get_visible_columns('PDF');
517 my $num_columns = scalar @visible_columns;
518 my $num_header_rows = 1;
520 my $font_encoding = 'UTF-8';
522 foreach my $name (@visible_columns) {
523 push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' };
526 if (!$self->{custom_headers}) {
528 $cell_props_row = [];
529 push @data, $data_row;
530 push @cell_props, $cell_props_row;
532 foreach my $name (@visible_columns) {
533 my $column = $self->{columns}->{$name};
535 push @{ $data_row }, $column->{text};
536 push @{ $cell_props_row }, {};
540 $num_header_rows = scalar @{ $self->{custom_headers} };
542 foreach my $custom_header_row (@{ $self->{custom_headers} }) {
544 $cell_props_row = [];
545 push @data, $data_row;
546 push @cell_props, $cell_props_row;
548 foreach my $custom_header_col (@{ $custom_header_row }) {
549 push @{ $data_row }, $custom_header_col->{text};
551 my $num_output = ($custom_header_col->{colspan} * 1 > 1) ? $custom_header_col->{colspan} : 1;
552 if ($num_output > 1) {
553 push @{ $data_row }, ('') x ($num_output - 1);
554 push @{ $cell_props_row }, { 'colspan' => $num_output };
555 push @{ $cell_props_row }, ({ }) x ($num_output - 1);
558 push @{ $cell_props_row }, {};
564 foreach my $row_set (@{ $self->{data} }) {
565 if ('HASH' eq ref $row_set) {
566 if ($row_set->{type} eq 'colspan_data') {
567 push @data, [ $row_set->{data} ];
569 $cell_props_row = [];
570 push @cell_props, $cell_props_row;
572 foreach (0 .. $num_columns - 1) {
573 push @{ $cell_props_row }, { 'background_color' => '#666666',
574 # BUG PDF:Table -> 0.9.12:
575 # font_color is used in next row, so dont set font_color
576 # 'font_color' => '#ffffff',
577 'colspan' => $_ == 0 ? -1 : undef, };
583 foreach my $row (@{ $row_set }) {
585 $cell_props_row = [];
587 push @data, $data_row;
588 push @cell_props, $cell_props_row;
591 foreach my $col_name (@visible_columns) {
592 my $col = $row->{$col_name};
593 push @{ $data_row }, join("\n", @{ $col->{data} || [] });
595 $column_props[$col_idx]->{justify} = 'right' if ($col->{align} eq 'right');
597 my $cell_props = { };
598 push @{ $cell_props_row }, $cell_props;
600 if ($col->{colspan} && $col->{colspan} > 1) {
601 $cell_props->{colspan} = $col->{colspan};
609 foreach my $i (0 .. scalar(@data) - 1) {
610 my $aref = $data[$i];
611 my $num_columns_here = scalar @{ $aref };
613 if ($num_columns_here < $num_columns) {
614 push @{ $aref }, ('') x ($num_columns - $num_columns_here);
615 } elsif ($num_columns_here > $num_columns) {
616 splice @{ $aref }, $num_columns;
621 'a3' => [ 842, 1190 ],
622 'a4' => [ 595, 842 ],
623 'a5' => [ 420, 595 ],
624 'letter' => [ 612, 792 ],
625 'legal' => [ 612, 1008 ],
628 my %supported_fonts = map { $_ => 1 } qw(courier georgia helvetica times verdana);
630 my $paper_size = defined $pdfopts->{paper_size} && defined $papersizes->{lc $pdfopts->{paper_size}} ? lc $pdfopts->{paper_size} : 'a4';
631 my ($paper_width, $paper_height);
633 if (lc $pdfopts->{orientation} eq 'landscape') {
634 ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[1, 0];
636 ($paper_width, $paper_height) = @{$papersizes->{$paper_size}}[0, 1];
639 my $margin_top = _cm2bp($pdfopts->{margin_top} || 1.5);
640 my $margin_bottom = _cm2bp($pdfopts->{margin_bottom} || 1.5);
641 my $margin_left = _cm2bp($pdfopts->{margin_left} || 1.5);
642 my $margin_right = _cm2bp($pdfopts->{margin_right} || 1.5);
644 my $table = PDF::Table->new();
645 my $pdf = PDF::API2->new();
646 my $page = $pdf->page();
648 $pdf->mediabox($paper_width, $paper_height);
650 my $font = $pdf->corefont(defined $pdfopts->{font_name} && $supported_fonts{lc $pdfopts->{font_name}} ? ucfirst $pdfopts->{font_name} : 'Verdana',
651 '-encoding' => $font_encoding);
652 my $font_size = $pdfopts->{font_size} || 7;
653 my $title_font_size = $font_size + 1;
655 my $font_height = $font_size + 2 * $padding;
656 my $title_font_height = $font_size + 2 * $padding;
658 my $header_height = $opts->{title} ? 2 * $title_font_height : undef;
659 my $footer_height = $pdfopts->{number} ? 2 * $font_height : undef;
661 my $top_text_height = 0;
663 if ($self->{options}->{top_info_text}) {
664 my $top_text = $self->{options}->{top_info_text};
665 $top_text =~ s/\r//g;
666 $top_text =~ s/\n+$//;
668 my @lines = split m/\n/, $top_text;
669 $top_text_height = $font_height * scalar @lines;
671 foreach my $line_no (0 .. scalar(@lines) - 1) {
672 my $y_pos = $paper_height - $margin_top - $header_height - $line_no * $font_height;
673 my $text_obj = $page->text();
675 $text_obj->font($font, $font_size);
676 $text_obj->translate($margin_left, $y_pos);
677 $text_obj->text($lines[$line_no]);
685 'w' => $paper_width - $margin_left - $margin_right,
686 'start_y' => $paper_height - $margin_top - $header_height - $top_text_height,
687 'next_y' => $paper_height - $margin_top - $header_height,
688 'start_h' => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height - $top_text_height,
689 'next_h' => $paper_height - $margin_top - $margin_bottom - $header_height - $footer_height,
691 'background_color_odd' => '#ffffff',
692 'background_color_even' => '#eeeeee',
694 'font_size' => $font_size,
695 'font_color' => '#000000',
696 'num_header_rows' => $num_header_rows,
698 'bg_color' => '#ffffff',
700 'font_color' => '#000000',
702 'column_props' => \@column_props,
703 'cell_props' => \@cell_props,
704 'max_word_length' => 60,
708 foreach my $page_num (1..$pdf->pages()) {
709 my $curpage = $pdf->openpage($page_num);
711 if ($pdfopts->{number}) {
712 my $label = $main::locale->text("Page #1/#2", $page_num, $pdf->pages());
713 my $text_obj = $curpage->text();
715 $text_obj->font($font, $font_size);
716 $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($label) / 2, $margin_bottom);
717 $text_obj->text($label);
720 if ($opts->{title}) {
721 my $title = $opts->{title};
722 my $text_obj = $curpage->text();
724 $text_obj->font($font, $title_font_size);
725 $text_obj->translate(($paper_width - $margin_left - $margin_right) / 2 + $margin_left - $text_obj->advancewidth($title) / 2,
726 $paper_height - $margin_top);
727 $text_obj->text($title, '-underline' => 1);
731 my $content = $pdf->stringify();
733 $main::lxdebug->message(LXDebug->DEBUG2(),"addattachments ?? =".$form->{report_generator_addattachments}." GL=".$form->{GL});
734 if ($form->{report_generator_addattachments} && $form->{GL}) {
735 $content = $self->append_gl_pdf_attachments($form,$content);
738 # 1. check if we return the report as binary pdf
739 if ($params{want_binary_pdf}) {
742 # 2. check if we want and can directly print the report
744 if ($pdfopts->{print} && $pdfopts->{printer_id}) {
745 $form->{printer_id} = $pdfopts->{printer_id};
746 $form->get_printer_code($myconfig);
747 $printer_command = $form->{printer_command};
749 if ($printer_command) {
750 $self->_print_content('printer_command' => $printer_command,
751 'content' => $content,
752 'copies' => $pdfopts->{copies});
753 $form->{report_generator_printed} = 1;
756 # 3. default: redirect http with file attached
757 my $filename = $self->get_attachment_basename();
759 print qq|content-type: application/pdf\n|;
760 print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
762 $::locale->with_raw_io(\*STDOUT, sub {
768 sub verify_paper_size {
770 my $requested_paper_size = lc shift;
771 my $default_paper_size = shift;
773 my %allowed_paper_sizes = map { $_ => 1 } qw(a3 a4 a5 letter legal);
775 return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
782 foreach my $i (1 .. max $params{copies}, 1) {
783 my $printer = IO::File->new("| $params{printer_command}");
784 $main::form->error($main::locale->text('Could not spawn the printer command.')) if (!$printer);
785 $printer->print($params{content});
790 sub _handle_quoting_and_encoding {
791 my ($self, $text, $do_unquote, $encoding) = @_;
793 $text = $main::locale->unquote_special_chars('HTML', $text) if $do_unquote;
794 $text = Encode::encode($encoding || 'UTF-8', $text);
799 sub generate_csv_content {
801 my $stdout = ($::dispatcher->get_standard_filehandles)[1];
803 # Text::CSV_XS seems to downgrade to bytes already (see
804 # SL/FCGIFixes.pm). Therefore don't let FCGI do that again.
805 $::locale->with_raw_io($stdout, sub { $self->_generate_csv_content($stdout) });
808 sub _generate_csv_content {
809 my ($self, $stdout) = @_;
811 my %valid_sep_chars = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
812 my %valid_escape_chars = ('"' => 1, "'" => 1);
813 my %valid_quote_chars = ('"' => 1, "'" => 1);
815 my $opts = $self->{options}->{csv_export};
816 my $eol = $opts->{eol_style} eq 'DOS' ? "\r\n" : "\n";
817 my $sep_char = $valid_sep_chars{$opts->{sep_char}} ? $valid_sep_chars{$opts->{sep_char}} : ';';
818 my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char} : '"';
819 my $quote_char = $valid_quote_chars{$opts->{quote_char}} ? $opts->{quote_char} : '"';
821 $escape_char = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
823 my $csv = Text::CSV_XS->new({ 'binary' => 1,
824 'sep_char' => $sep_char,
825 'escape_char' => $escape_char,
826 'quote_char' => $quote_char,
829 my @visible_columns = $self->get_visible_columns('CSV');
831 if ($opts->{headers}) {
832 if (!$self->{custom_headers}) {
833 $csv->print($stdout, [ map { $self->_handle_quoting_and_encoding($self->{columns}->{$_}->{text}, 1, $opts->{encoding}) } @visible_columns ]);
836 foreach my $row (@{ $self->{custom_headers} }) {
839 foreach my $col (@{ $row }) {
840 my $num_output = ($col->{colspan} && ($col->{colspan} > 1)) ? $col->{colspan} : 1;
841 push @{ $fields }, ($self->_handle_quoting_and_encoding($col->{text}, 1, $opts->{encoding})) x $num_output;
844 $csv->print($stdout, $fields);
849 foreach my $row_set (@{ $self->{data} }) {
850 next if ('ARRAY' ne ref $row_set);
851 foreach my $row (@{ $row_set }) {
854 foreach my $col (@visible_columns) {
860 my $num_output = ($row->{$col}{colspan} && ($row->{$col}->{colspan} > 1)) ? $row->{$col}->{colspan} : 1;
861 $skip_next = $num_output - 1;
863 push @data, join($eol, map { s/\r?\n/$eol/g; $self->_handle_quoting_and_encoding($_, 0, $opts->{encoding}) } @{ $row->{$col}->{data} });
864 push @data, ('') x $skip_next if ($skip_next);
867 $csv->print($stdout, \@data);
872 sub generate_chart_content {
873 my ($self, %params) = @_;
875 $params{action_bar} //= 1;
877 my $opts = $self->{options};
879 my $assignment_x = $opts->{chart_export}->{assignment_x};
880 my $assignments_y = $opts->{chart_export}->{assignments_y};
884 foreach my $row_set (@{ $self->{data} }) {
885 next if ('ARRAY' ne ref $row_set);
886 foreach my $row (@{ $row_set }) {
887 my $label = $row->{$assignment_x}->{data}->[0];
889 push @labels, $label;
892 foreach my $assignment_y (@$assignments_y) {
893 my $y = $row->{$assignment_y}->{data}->[0];
896 push @datasets, \@set;
902 'TITLE' => $opts->{title},
903 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
904 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
905 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
906 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
907 'EXPORT_VARIABLE_LIST' => join(' ', @{ $self->{export}->{variable_list} }),
908 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
909 'DATA_PRESENT' => $self->{data_present},
910 'CONTROLLER_DISPATCH' => $opts->{controller_class},
911 'TABLE_CLASS' => $opts->{table_class},
912 'SKIP_BUTTONS' => !!$params{action_bar},
915 $::request->layout->add_javascripts('chart.js', 'kivi.ChartReport.js');
918 print $::form->parse_html_template('report_generator/chart_report',
920 labels => to_json(\@labels),
921 datasets => to_json(\@datasets),
922 data_labels => to_json($assignments_y),
928 sub check_for_pdf_api {
929 return eval { require PDF::API2; 1; } ? 1 : 0;
938 SL::ReportGenerator.pm: the kivitendo way of getting data in shape
942 my $report = SL::ReportGenerator->new(\%myconfig, $form);
943 $report->set_options(%options); # optional
944 $report->set_columns(%column_defs);
945 $report->set_sort_indicator($column, $direction); # optional
946 $report->add_data($row1, $row2, @more_rows);
947 $report->generate_with_headers();
949 This creates a report object, sets a few columns, adds some data and generates a standard report.
950 Sorting of columns will be alphabetic, and options will be set to their defaults.
951 The report will be printed including table headers, html headers and http headers.
955 Imagine the following scenario:
956 There's a simple form, which loads some data from the database, and needs to print it out. You write a template for it.
957 Then there may be more than one line. You add a loop in the template.
958 Then there are some options made by the user, such as hidden columns. You add more to the template.
959 Then it lacks usability. You want it to be able to sort the data. You add code for that.
960 Then there are too many results, you need pagination, you want to print or export that data..... and so on.
962 The ReportGenerator class was designed because this exact scenario happened about half a dozen times in kivitendo.
963 It's purpose is to manage all those formating, culling, sorting, and templating.
964 Which makes it almost as complicated to use as doing the work by yourself.
970 =item new \%myconfig,$form,%options
972 Creates a new ReportGenerator object, sets all given options, and returns it.
974 =item set_columns %columns
976 Sets the columns available to this report.
978 =item set_column_order @columns
980 Sets the order of columns. Any columns not present here are appended in alphabetic order.
982 =item set_sort_indicator $column,$direction
984 Sets sorting of the table by specifying a column and a direction, where the direction will be evaluated to ascending if true.
985 Note that this is only for displaying. The data has to have already been sorted when it was added.
987 =item add_data \@data
989 =item add_data \%data
991 Adds data to the report. A given hash_ref is interpreted as a single line of
992 data, every array_ref as a collection of lines. Every line will be expected to
993 be in a key => value format. Note that the rows have to already have been
996 The ReportGenerator is only able to display pre-sorted data and to indicate by
997 which column and in which direction the data has been sorted via visual clues
998 in the column headers. It also provides links to invert the sort direction.
1002 Adds a separator line to the report.
1004 =item add_control \%data
1006 Adds a control element to the data. Control elements are an experimental feature to add functionality to a report the regular data cannot.
1007 Every control element needs to set IS_CONTROL_DATA, in order to be recognized by the template.
1008 Currently the only control element is a colspan element, which can be used as a mini header further down the report.
1012 Deletes all data added to the report, but keeps options set.
1014 =item set_options %options
1016 Sets options. For an incomplete list of options, see section configuration.
1018 =item set_options_from_form
1020 Tries to import options from the $form object given at creation
1022 =item set_export_options $next_sub,@variable_list
1024 Sets next_sub and additional variables needed for export.
1026 =item get_attachment_basename
1028 Returns the set attachment_basename option, or 'report' if nothing was set. See configuration for the option.
1030 =item generate_with_headers
1032 Parses the report, adds headers and prints it out. Headers depend on the option 'output_format',
1033 for example 'HTML' will add proper table headers, html headers and http headers. See configuration for this option.
1035 =item get_visible_columns $format
1037 Returns a list of columns that will be visible in the report after considering all options or match the given format.
1039 =item html_format $value
1041 Escapes HTML characters in $value and substitutes newlines with '<br>'. Returns the escaped $value.
1043 =item prepare_html_content $column,$name,@column_headers
1045 Parses the data, and sets internal data needed for certain output format. Must be called once before the template is invoked.
1046 Should not be called externally, since all render and generate functions invoke it anyway.
1048 =item generate_html_content
1050 The html generation function. Is invoked by generate_with_headers.
1052 =item generate_pdf_content
1054 The PDF generation function. It is invoked by generate_with_headers and renders the PDF with the PDF::API2 library.
1056 If the param want_binary_pdf is set, the binary pdf stream will be returned.
1057 If $pdfopts->{print} && $pdfopts->{printer_id} are set, the pdf will be printed (output is directed to print command).
1059 Otherwise and the default a html form with a downloadable file is returned.
1061 =item generate_csv_content
1063 The CSV generation function. Uses XS_CSV to parse the information into csv.
1067 =head1 CONFIGURATION
1069 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.
1071 =head2 General Options
1075 =item std_column_visibility
1077 Standard column visibility. Used if no visibility is set. Use this to save the trouble of enabling every column. Default is no.
1081 Output format. Used by generate_with_headers to determine the format. Supported options are HTML, CSV, and PDF. Default is HTML.
1083 =item allow_pdf_export
1085 Used to determine if a button for PDF export should be displayed. Default is yes.
1087 =item allow_csv_export
1089 Used to determine if a button for CSV export should be displayed. Default is yes.
1093 The template to be used for HTML reports. Default is 'report_generator/html_report'.
1095 =item controller_class
1097 If this is used from a C<SL::Controller::Base> based controller class, pass the
1098 class name here and make sure C<SL::Controller::Helper::ReportGenerator> is
1099 used in the controller. That way the exports stay functional.
1109 Paper size. Default is a4. Supported paper sizes are a3, a4, a5, letter and legal.
1111 =item orientation (landscape)
1113 Landscape or portrait. Default is landscape.
1117 Default is Verdana. Supported font names are Courier, Georgia, Helvetica, Times and Verdana. This option only affects the rendering with PDF::API2.
1121 Default is 7. This option only affects the rendering with PDF::API2.
1131 The paper margins in cm. They all default to 1.5.
1135 Set to a true value if the pages should be numbered. Default is 1.
1139 If set then the resulting PDF will be output to a printer. If not it will be downloaded by the user. Default is no.
1157 Character to enclose entries. Default is double quote (").
1161 Character to separate entries. Default is semicolon (;).
1165 Character to escape the quote_char. Default is double quote (").
1169 End of line style. Default is Unix.
1173 Include headers? Default is yes.
1177 Character encoding. Default is UTF-8.
1185 =head1 MODULE AUTHORS
1187 Moritz Bunkus E<lt>mbunkus@linet-services.deE<gt>
1189 L<http://linet-services.de>