1 package SL::ReportGenerator;
4 use List::Util qw(max);
14 $self->{myconfig} = shift;
15 $self->{form} = shift;
19 'std_column_visibility' => 0,
20 'output_format' => 'HTML',
21 'allow_pdf_export' => 1,
22 'allow_csv_export' => 1,
25 'orientation' => 'landscape',
29 'margin_bottom' => 1.5,
30 'margin_right' => 1.5,
40 'eol_style' => 'Unix',
46 'variable_list' => '',
49 $self->{data_present} = 0;
53 $self->set_options(@_) if (@_);
62 $self->{columns} = \%columns;
64 foreach my $column (values %{ $self->{columns} }) {
65 $column->{visible} = $self->{options}->{std_column_visibility} unless defined $column->{visible};
68 $self->set_column_order(sort keys %{ $self->{columns} });
71 sub set_column_order {
75 my %columns = map { $order++; ($_, $order) } @_;
77 foreach my $column (sort keys %{ $self->{columns} }) {
78 next if $columns{$column};
81 $columns{$column} = $order;
84 $self->{column_order} = [ sort { $columns{$a} <=> $columns{$b} } keys %columns ];
87 sub set_sort_indicator {
90 $self->{options}->{sort_indicator_column} = shift;
91 $self->{options}->{sort_indicator_direction} = shift;
99 while (my $arg = shift) {
102 if ('ARRAY' eq ref $arg) {
105 } elsif ('HASH' eq ref $arg) {
109 $self->{form}->error('Incorrect usage -- expecting hash or array ref');
112 my @columns_with_default_alignment = grep { defined $self->{columns}->{$_}->{align} } keys %{ $self->{columns} };
114 foreach my $row (@{ $row_set }) {
115 foreach my $column (@columns_with_default_alignment) {
116 $row->{$column} ||= { };
117 $row->{$column}->{align} = $self->{columns}->{$column}->{align} unless (defined $row->{$column}->{align});
120 foreach my $field (qw(data link)) {
121 map { $row->{$_}->{$field} = [ $row->{$_}->{$field} ] if (ref $row->{$_}->{$field} ne 'ARRAY') } keys %{ $row };
125 push @{ $self->{data} }, $row_set;
126 $last_row_set = $row_set;
128 $self->{data_present} = 1;
131 return $last_row_set;
137 push @{ $self->{data} }, { 'type' => 'separator' };
144 push @{ $self->{data} }, $data;
151 $self->{data_present} = 0;
158 map { $self->{options}->{$_} = $options{$_} } keys %options;
161 sub set_options_from_form {
164 my $form = $self->{form};
165 my $myconfig = $self->{myconfig};
167 foreach my $key (qw(output_format)) {
168 my $full_key = "report_generator_${key}";
169 $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
172 foreach my $format (qw(pdf csv)) {
173 my $opts = $self->{options}->{"${format}_export"};
174 foreach my $key (keys %{ $opts }) {
175 my $full_key = "report_generator_${format}_options_${key}";
176 $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
181 sub set_export_options {
186 'variable_list' => join(" ", @_),
190 sub get_attachment_basename {
192 my $filename = $self->{options}->{attachment_basename} || 'report';
193 $filename =~ s|.*\\||;
194 $filename =~ s|.*/||;
199 sub generate_with_headers {
201 my $format = lc $self->{options}->{output_format};
202 my $form = $self->{form};
204 if (!$self->{columns}) {
205 $form->error('Incorrect usage -- no columns specified');
208 if ($format eq 'html') {
209 my $title = $form->{title};
210 $form->{title} = $self->{title} if ($self->{title});
212 $form->{title} = $title;
214 print $self->generate_html_content();
216 } elsif ($format eq 'csv') {
217 my $filename = $self->get_attachment_basename();
218 print qq|content-type: text/csv\n|;
219 print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
220 $self->generate_csv_content();
222 } elsif ($format eq 'pdf') {
223 $self->generate_pdf_content();
226 $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
230 sub get_visible_columns {
234 return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /\Q${format}\E/i)) } @{ $self->{column_order} };
241 $value = $self->{form}->quote_html($value);
243 $value =~ s/\n/<br>/g;
248 sub prepare_html_content {
251 my ($column, $name, @column_headers);
253 my $opts = $self->{options};
254 my @visible_columns = $self->get_visible_columns('HTML');
256 foreach $name (@visible_columns) {
257 $column = $self->{columns}->{$name};
261 'link' => $column->{link},
262 'text' => $column->{text},
263 'show_sort_indicator' => $name eq $opts->{sort_indicator_column},
264 'sort_indicator_direction' => $opts->{sort_indicator_direction},
267 push @column_headers, $header;
270 my ($outer_idx, $inner_idx) = (0, 0);
273 foreach my $row_set (@{ $self->{data} }) {
274 if ('HASH' eq ref $row_set) {
277 'IS_SEPARATOR' => $row_set->{type} eq 'separator',
278 'IS_COLSPAN_DATA' => $row_set->{type} eq 'colspan_data',
279 'NUM_COLUMNS' => scalar @visible_columns,
280 'data' => $row_set->{data},
283 push @rows, $row_data;
290 foreach my $row (@{ $row_set }) {
293 foreach my $col_name (@visible_columns) {
294 my $col = $row->{$col_name};
295 $col->{CELL_ROWS} = [ ];
296 foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
297 push @{ $col->{CELL_ROWS} }, {
298 'data' => $self->html_format($col->{data}->[$i]),
299 'link' => $col->{link}->[$i],
305 'COLUMNS' => [ map { $row->{$_} } @visible_columns ],
306 'outer_idx' => $outer_idx,
307 'outer_idx_odd' => $outer_idx % 2,
308 'inner_idx' => $inner_idx,
311 push @rows, $row_data;
315 my @export_variables;
316 foreach my $key (split m/ +/, $self->{export}->{variable_list}) {
317 push @export_variables, { 'key' => $key, 'value' => $self->{form}->{$key} };
320 my $allow_pdf_export = $opts->{allow_pdf_export} && (-x $main::html2ps_bin) && (-x $main::ghostscript_bin);
323 'TITLE' => $opts->{title},
324 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
325 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
326 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
327 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
328 'ALLOW_PDF_EXPORT' => $allow_pdf_export,
329 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export},
330 'SHOW_EXPORT_BUTTONS' => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present},
331 'COLUMN_HEADERS' => \@column_headers,
332 'NUM_COLUMNS' => scalar @column_headers,
334 'EXPORT_VARIABLES' => \@export_variables,
335 'EXPORT_VARIABLE_LIST' => $self->{export}->{variable_list},
336 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
337 'DATA_PRESENT' => $self->{data_present},
343 sub generate_html_content {
345 my $variables = $self->prepare_html_content();
347 return $self->{form}->parse_html_template2('report_generator/html_report', $variables);
350 sub verify_paper_size {
352 my $requested_paper_size = lc shift;
353 my $default_paper_size = shift;
355 my %allowed_paper_sizes = map { $_ => 1 } qw(a3 a4 letter legal);
357 return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
360 sub generate_pdf_content {
362 my $variables = $self->prepare_html_content();
363 my $form = $self->{form};
364 my $myconfig = $self->{myconfig};
365 my $opt = $self->{options}->{pdf_export};
367 my $opt_number = $opt->{number} ? 'number : 1' : '';
368 my $opt_landscape = $opt->{orientation} eq 'landscape' ? 'landscape : 1' : '';
370 my $opt_paper_size = $self->verify_paper_size($opt->{paper_size}, 'a4');
372 my $html2ps_config = <<"END"
382 type: ${opt_paper_size};
388 margin-top: $opt->{margin_top}cm;
389 margin-left: $opt->{margin_left}cm;
390 margin-bottom: $opt->{margin_bottom}cm;
391 margin-right: $opt->{margin_right}cm;
395 font-family: Helvetica;
396 font-size: $opt->{font_size}pt;
403 if ($opt->{print} && $opt->{printer_id}) {
404 $form->{printer_id} = $opt->{printer_id};
405 $form->get_printer_code($myconfig);
406 $printer_command = $form->{printer_command};
409 my $cfg_file_name = Common::tmpname() . '-html2ps-config';
410 my $cfg_file = IO::File->new($cfg_file_name, 'w') || $form->error($locale->text('Could not write the html2ps config file.'));
412 $cfg_file->print($html2ps_config);
415 my $html_file_name = Common::tmpname() . '.html';
416 my $html_file = IO::File->new($html_file_name, 'w');
419 unlink $cfg_file_name;
420 $form->error($locale->text('Could not write the temporary HTML file.'));
423 $html_file->print($form->parse_html_template('report_generator/pdf_report', $variables));
427 "\"${main::html2ps_bin}\" -f \"${cfg_file_name}\" \"${html_file_name}\" | " .
428 "\"${main::ghostscript_bin}\" -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=${opt_paper_size} -sOutputFile=- -c .setpdfwrite -";
430 my $gs = IO::File->new("${cmdline} |");
434 if (!$printer_command) {
435 my $filename = $self->get_attachment_basename();
436 print qq|content-type: application/pdf\n|;
437 print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
439 while (my $line = <$gs>) {
444 while (my $line = <$gs>) {
450 unlink $cfg_file_name, $html_file_name;
452 if ($printer_command && $content) {
453 foreach my $i (1 .. max $opt->{copies}, 1) {
454 my $printer = IO::File->new("| ${printer_command}");
456 $form->error($locale->text('Could not spawn the printer command.'));
458 $printer->print($content);
462 $form->{report_generator_printed} = 1;
466 unlink $cfg_file_name, $html_file_name;
467 $form->error($locale->text('Could not spawn html2ps or GhostScript.'));
471 sub generate_csv_content {
474 my %valid_sep_chars = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
475 my %valid_escape_chars = ('"' => 1, "'" => 1);
476 my %valid_quote_chars = ('"' => 1, "'" => 1);
478 my $opts = $self->{options}->{csv_export};
479 my $eol = $opts->{eol_style} eq 'DOS' ? "\r\n" : "\n";
480 my $sep_char = $valid_sep_chars{$opts->{sep_char}} ? $valid_sep_chars{$opts->{sep_char}} : ';';
481 my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char} : '"';
482 my $quote_char = $valid_quote_chars{$opts->{quote_char}} ? $opts->{quote_char} : '"';
484 $escape_char = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
486 my $csv = Text::CSV_XS->new({ 'binary' => 1,
487 'sep_char' => $sep_char,
488 'escape_char' => $escape_char,
489 'quote_char' => $quote_char,
492 my $stdout = wraphandle(\*STDOUT);
493 my @visible_columns = $self->get_visible_columns('CSV');
495 if ($opts->{headers}) {
496 $csv->print($stdout, [ map { $self->{columns}->{$_}->{text} } @visible_columns ]);
499 foreach my $row_set (@{ $self->{data} }) {
500 next if ('ARRAY' ne ref $row_set);
501 foreach my $row (@{ $row_set }) {
503 foreach my $col (@visible_columns) {
504 push @data, join($eol, map { s/\r?\n/$eol/g; $_ } @{ $row->{$col}->{data} });
506 $csv->print($stdout, \@data);