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;
51 $self->set_options(@_) if (@_);
53 return bless $self, $type;
60 $self->{columns} = \%columns;
62 foreach my $column (values %{ $self->{columns} }) {
63 $column->{visible} = $self->{options}->{std_column_visibility} unless defined $column->{visible};
66 $self->set_column_order(sort keys %{ $self->{columns} });
69 sub set_column_order {
73 my %columns = map { $order++; ($_, $order) } @_;
75 foreach my $column (sort keys %{ $self->{columns} }) {
76 next if $columns{$column};
79 $columns{$column} = $order;
82 $self->{column_order} = [ sort { $columns{$a} <=> $columns{$b} } keys %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 foreach my $row (@{ $row_set }) {
111 foreach my $field (qw(data link)) {
112 map { $row->{$_}->{$field} = [ $row->{$_}->{$field} ] if (ref $row->{$_}->{$field} ne 'ARRAY') } keys %{ $row };
116 push @{ $self->{data} }, $row_set;
117 $last_row_set = $row_set;
119 $self->{data_present} = 1;
122 return $last_row_set;
128 push @{ $self->{data} }, { 'type' => 'separator' };
135 push @{ $self->{data} }, $data;
142 $self->{data_present} = 0;
149 map { $self->{options}->{$_} = $options{$_} } keys %options;
152 sub set_options_from_form {
155 my $form = $self->{form};
156 my $myconfig = $self->{myconfig};
158 foreach my $key (qw(output_format)) {
159 my $full_key = "report_generator_${key}";
160 $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
163 foreach my $format (qw(pdf csv)) {
164 my $opts = $self->{options}->{"${format}_export"};
165 foreach my $key (keys %{ $opts }) {
166 my $full_key = "report_generator_${format}_options_${key}";
167 $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
172 sub set_export_options {
177 'variable_list' => join(" ", @_),
181 sub get_attachment_basename {
183 my $filename = $self->{options}->{attachment_basename} || 'report';
184 $filename =~ s|.*\\||;
185 $filename =~ s|.*/||;
190 sub generate_with_headers {
192 my $format = lc $self->{options}->{output_format};
193 my $form = $self->{form};
195 if (!$self->{columns}) {
196 $form->error('Incorrect usage -- no columns specified');
199 if ($format eq 'html') {
200 my $title = $form->{title};
201 $form->{title} = $self->{title} if ($self->{title});
203 $form->{title} = $title;
205 print $self->generate_html_content();
207 } elsif ($format eq 'csv') {
208 my $filename = $self->get_attachment_basename();
209 print qq|content-type: text/csv\n|;
210 print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
211 $self->generate_csv_content();
213 } elsif ($format eq 'pdf') {
214 $self->generate_pdf_content();
217 $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
221 sub get_visible_columns {
225 return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /${format}/i)) } @{ $self->{column_order} };
232 $value = $self->{form}->quote_html($value);
234 $value =~ s/\n/<br>/g;
239 sub prepare_html_content {
242 my ($column, $name, @column_headers);
244 my $opts = $self->{options};
245 my @visible_columns = $self->get_visible_columns('HTML');
247 foreach $name (@visible_columns) {
248 $column = $self->{columns}->{$name};
252 'link' => $column->{link},
253 'text' => $column->{text},
254 'show_sort_indicator' => $name eq $opts->{sort_indicator_column},
255 'sort_indicator_direction' => $opts->{sort_indicator_direction},
258 push @column_headers, $header;
261 my ($outer_idx, $inner_idx) = (0, 0);
264 foreach my $row_set (@{ $self->{data} }) {
265 if ('HASH' eq ref $row_set) {
268 'IS_SEPARATOR' => $row_set->{type} eq 'separator',
269 'IS_COLSPAN_DATA' => $row_set->{type} eq 'colspan_data',
270 'NUM_COLUMNS' => scalar @visible_columns,
271 'data' => $row_set->{data},
274 push @rows, $row_data;
281 foreach my $row (@{ $row_set }) {
284 foreach my $col_name (@visible_columns) {
285 my $col = $row->{$col_name};
286 $col->{CELL_ROWS} = [ ];
287 foreach my $i (0 .. scalar(@{ $col->{data} }) - 1) {
288 push @{ $col->{CELL_ROWS} }, {
289 'data' => $self->html_format($col->{data}->[$i]),
290 'link' => $col->{link}->[$i],
296 'COLUMNS' => [ map { $row->{$_} } @visible_columns ],
297 'outer_idx' => $outer_idx,
298 'outer_idx_odd' => $outer_idx % 2,
299 'inner_idx' => $inner_idx,
302 push @rows, $row_data;
306 my @export_variables;
307 foreach my $key (split m/ +/, $self->{export}->{variable_list}) {
308 push @export_variables, { 'key' => $key, 'value' => $self->{form}->{$key} };
311 my $allow_pdf_export = $opts->{allow_pdf_export} && (-x $main::html2ps_bin) && (-x $main::ghostscript_bin);
314 'TITLE' => $opts->{title},
315 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
316 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
317 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
318 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
319 'ALLOW_PDF_EXPORT' => $allow_pdf_export,
320 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export},
321 'SHOW_EXPORT_BUTTONS' => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present},
322 'COLUMN_HEADERS' => \@column_headers,
323 'NUM_COLUMNS' => scalar @column_headers,
325 'EXPORT_VARIABLES' => \@export_variables,
326 'EXPORT_VARIABLE_LIST' => $self->{export}->{variable_list},
327 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
328 'DATA_PRESENT' => $self->{data_present},
334 sub generate_html_content {
336 my $variables = $self->prepare_html_content();
338 return $self->{form}->parse_html_template2('report_generator/html_report', $variables);
341 sub verify_paper_size {
343 my $requested_paper_size = lc shift;
344 my $default_paper_size = shift;
346 my %allowed_paper_sizes = map { $_ => 1 } qw(a3 a4 letter legal);
348 return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
351 sub generate_pdf_content {
353 my $variables = $self->prepare_html_content();
354 my $form = $self->{form};
355 my $myconfig = $self->{myconfig};
356 my $opt = $self->{options}->{pdf_export};
358 my $opt_number = $opt->{number} ? 'number : 1' : '';
359 my $opt_landscape = $opt->{orientation} eq 'landscape' ? 'landscape : 1' : '';
361 my $opt_paper_size = $self->verify_paper_size($opt->{paper_size}, 'a4');
363 my $html2ps_config = <<"END"
373 type: ${opt_paper_size};
379 margin-top: $opt->{margin_top}cm;
380 margin-left: $opt->{margin_left}cm;
381 margin-bottom: $opt->{margin_bottom}cm;
382 margin-right: $opt->{margin_right}cm;
386 font-family: Helvetica;
387 font-size: $opt->{font_size}pt;
394 if ($opt->{print} && $opt->{printer_id}) {
395 $form->{printer_id} = $opt->{printer_id};
396 $form->get_printer_code($myconfig);
397 $printer_command = $form->{printer_command};
400 my $cfg_file_name = Common::tmpname() . '-html2ps-config';
401 my $cfg_file = IO::File->new($cfg_file_name, 'w') || $form->error($locale->text('Could not write the html2ps config file.'));
403 $cfg_file->print($html2ps_config);
406 my $html_file_name = Common::tmpname() . '.html';
407 my $html_file = IO::File->new($html_file_name, 'w');
410 unlink $cfg_file_name;
411 $form->error($locale->text('Could not write the temporary HTML file.'));
414 $html_file->print($form->parse_html_template('report_generator/pdf_report', $variables));
418 "\"${main::html2ps_bin}\" -f \"${cfg_file_name}\" \"${html_file_name}\" | " .
419 "\"${main::ghostscript_bin}\" -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=${opt_paper_size} -sOutputFile=- -c .setpdfwrite -";
421 my $gs = IO::File->new("${cmdline} |");
425 if (!$printer_command) {
426 my $filename = $self->get_attachment_basename();
427 print qq|content-type: application/pdf\n|;
428 print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
430 while (my $line = <$gs>) {
435 while (my $line = <$gs>) {
441 unlink $cfg_file_name, $html_file_name;
443 if ($printer_command && $content) {
444 foreach my $i (1 .. max $opt->{copies}, 1) {
445 my $printer = IO::File->new("| ${printer_command}");
447 $form->error($locale->text('Could not spawn the printer command.'));
449 $printer->print($content);
453 $form->{report_generator_printed} = 1;
457 unlink $cfg_file_name, $html_file_name;
458 $form->error($locale->text('Could not spawn html2ps or GhostScript.'));
462 sub generate_csv_content {
465 my %valid_sep_chars = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
466 my %valid_escape_chars = ('"' => 1, "'" => 1);
467 my %valid_quote_chars = ('"' => 1, "'" => 1);
469 my $opts = $self->{options}->{csv_export};
470 my $eol = $opts->{eol_style} eq 'DOS' ? "\r\n" : "\n";
471 my $sep_char = $valid_sep_chars{$opts->{sep_char}} ? $valid_sep_chars{$opts->{sep_char}} : ';';
472 my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char} : '"';
473 my $quote_char = $valid_quote_chars{$opts->{quote_char}} ? $opts->{quote_char} : '"';
475 $escape_char = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
477 my $csv = Text::CSV_XS->new({ 'binary' => 1,
478 'sep_char' => $sep_char,
479 'escape_char' => $escape_char,
480 'quote_char' => $quote_char,
483 my $stdout = wraphandle(\*STDOUT);
484 my @visible_columns = $self->get_visible_columns('CSV');
486 if ($opts->{headers}) {
487 $csv->print($stdout, [ map { $self->{columns}->{$_}->{text} } @visible_columns ]);
490 foreach my $row_set (@{ $self->{data} }) {
491 next if ('ARRAY' ne ref $row_set);
492 foreach my $row (@{ $row_set }) {
494 foreach my $col (@visible_columns) {
495 push @data, join($eol, map { s/\r?\n/$eol/g; $_ } @{ $row->{$col}->{data} });
497 $csv->print($stdout, \@data);