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,
46 'variable_list' => '',
49 $self->set_options(@_) if (@_);
51 return bless $self, $type;
58 $self->{columns} = \%columns;
60 foreach my $column (values %{ $self->{columns} }) {
61 $column->{visible} = $self->{options}->{std_column_visibility} unless defined $column->{visible};
64 $self->set_column_order(sort keys %{ $self->{columns} });
67 sub set_column_order {
71 my %columns = map { $order++; ($_, $order) } @_;
73 foreach my $column (sort keys %{ $self->{columns} }) {
74 next if $columns{$column};
77 $columns{$column} = $order;
80 $self->{column_order} = [ sort { $columns{$a} <=> $columns{$b} } keys %columns ];
83 sub set_sort_indicator {
86 $self->{options}->{sort_indicator_column} = shift;
87 $self->{options}->{sort_indicator_direction} = shift;
95 while (my $arg = shift) {
96 if ('ARRAY' eq ref $arg) {
97 push @{ $self->{data} }, $arg;
100 } elsif ('HASH' eq ref $arg) {
101 my $row_set = [ $arg ];
102 push @{ $self->{data} }, $row_set;
103 $last_row_set = $row_set;
106 $self->{form}->error('Incorrect usage -- expecting hash or array ref');
110 return $last_row_set;
116 push @{ $self->{data} }, { 'type' => 'separator' };
129 map { $self->{options}->{$_} = $options{$_} } keys %options;
132 sub set_options_from_form {
135 my $form = $self->{form};
136 my $myconfig = $self->{myconfig};
138 foreach my $key (qw(output_format)) {
139 my $full_key = "report_generator_${key}";
140 $self->{options}->{$key} = $form->{$full_key} if (defined $form->{$full_key});
143 foreach my $format (qw(pdf csv)) {
144 my $opts = $self->{options}->{"${format}_export"};
145 foreach my $key (keys %{ $opts }) {
146 my $full_key = "report_generator_${format}_options_${key}";
147 $opts->{$key} = $key =~ /^margin/ ? $form->parse_amount($myconfig, $form->{$full_key}) : $form->{$full_key};
152 sub set_export_options {
157 'variable_list' => join(" ", @_),
161 sub generate_with_headers {
163 my $format = lc $self->{options}->{output_format};
165 if (!$self->{columns}) {
166 $self->{form}->error('Incorrect usage -- no columns specified');
169 my $filename = $self->{options}->{attachment_basename} || 'report';
170 $filename =~ s|.*\\||;
171 $filename =~ s|.*/||;
173 if ($format eq 'html') {
174 $self->{form}->{title} = $self->{title};
175 $self->{form}->header();
176 print $self->generate_html_content();
178 } elsif ($format eq 'csv') {
179 print qq|content-type: text/csv\n|;
180 print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
181 $self->generate_csv_content();
183 } elsif ($format eq 'pdf') {
184 $self->generate_pdf_content();
187 $self->{form}->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
191 sub get_visible_columns {
195 return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /${format}/i)) } @{ $self->{column_order} };
202 $value = $self->{form}->quote_html($value);
204 $value =~ s/\n/<br>/g;
209 sub prepare_html_content {
212 my ($column, $name, @column_headers);
214 my $opts = $self->{options};
215 my @visible_columns = $self->get_visible_columns('HTML');
217 foreach $name (@visible_columns) {
218 $column = $self->{columns}->{$name};
222 'link' => $column->{link},
223 'text' => $column->{text},
224 'show_sort_indicator' => $name eq $opts->{sort_indicator_column},
225 'sort_indicator_direction' => $opts->{sort_indicator_direction},
228 push @column_headers, $header;
231 my ($outer_idx, $inner_idx) = (0, 0);
234 foreach my $row_set (@{ $self->{data} }) {
235 if ('HASH' eq ref $row_set) {
238 'IS_SEPARATOR' => $row_set->{type} eq 'separator',
239 'NUM_COLUMNS' => scalar @visible_columns,
242 push @rows, $row_data;
249 foreach my $row (@{ $row_set }) {
252 map { $row->{$_}->{data} = $self->html_format($row->{$_}->{data}) } @visible_columns;
255 'COLUMNS' => [ map { $row->{$_} } @visible_columns ],
256 'outer_idx' => $outer_idx,
257 'outer_idx_odd' => $outer_idx % 2,
258 'inner_idx' => $inner_idx,
261 push @rows, $row_data;
265 my @export_variables;
266 foreach my $key (split m/ +/, $self->{export}->{variable_list}) {
267 push @export_variables, { 'key' => $key, 'value' => $self->{form}->{$key} };
270 my $allow_pdf_export = $opts->{allow_pdf_export} && (-x $main::html2ps_bin) && (-x $main::ghostscript_bin);
273 'TITLE' => $opts->{title},
274 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
275 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
276 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
277 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
278 'ALLOW_PDF_EXPORT' => $allow_pdf_export,
279 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export},
280 'SHOW_EXPORT_BUTTONS' => $allow_pdf_export || $opts->{allow_csv_export},
281 'COLUMN_HEADERS' => \@column_headers,
282 'NUM_COLUMNS' => scalar @column_headers,
284 'EXPORT_VARIABLES' => \@export_variables,
285 'EXPORT_VARIABLE_LIST' => $self->{export}->{variable_list},
286 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
292 sub generate_html_content {
294 my $variables = $self->prepare_html_content();
296 return $self->{form}->parse_html_template('report_generator/html_report', $variables);
299 sub verify_paper_size {
301 my $requested_paper_size = lc shift;
302 my $default_paper_size = shift;
304 my %allowed_paper_sizes = map { $_ => 1 } qw(a3 a4 letter legal);
306 return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
309 sub generate_pdf_content {
311 my $variables = $self->prepare_html_content();
312 my $form = $self->{form};
313 my $myconfig = $self->{myconfig};
314 my $opt = $self->{options}->{pdf_export};
316 my $opt_number = $opt->{number} ? 'number : 1' : '';
317 my $opt_landscape = $opt->{orientation} eq 'landscape' ? 'landscape : 1' : '';
319 my $opt_paper_size = $self->verify_paper_size($opt->{paper_size}, 'a4');
321 my $html2ps_config = <<"END"
331 type: ${opt_paper_size};
337 margin-top: $opt->{margin_top}cm;
338 margin-left: $opt->{margin_left}cm;
339 margin-bottom: $opt->{margin_bottom}cm;
340 margin-right: $opt->{margin_right}cm;
344 font-family: Helvetica;
345 font-size: $opt->{font_size}pt;
352 if ($opt->{print} && $opt->{printer_id}) {
353 $form->{printer_id} = $opt->{printer_id};
354 $form->get_printer_code($myconfig);
355 $printer_command = $form->{printer_command};
358 my $cfg_file_name = Common::tmpname() . '-html2ps-config';
359 my $cfg_file = IO::File->new($cfg_file_name, 'w') || $form->error($locale->text('Could not write the html2ps config file.'));
361 $cfg_file->print($html2ps_config);
364 my $html_file_name = Common::tmpname() . '.html';
365 my $html_file = IO::File->new($html_file_name, 'w');
368 unlink $cfg_file_name;
369 $form->error($locale->text('Could not write the temporary HTML file.'));
372 $html_file->print($form->parse_html_template('report_generator/pdf_report', $variables));
376 "\"${main::html2ps_bin}\" -f \"${cfg_file_name}\" \"${html_file_name}\" | " .
377 "\"${main::ghostscript_bin}\" -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=${opt_paper_size} -sOutputFile=- -c .setpdfwrite -";
379 my $gs = IO::File->new("${cmdline} |");
383 if (!$printer_command) {
384 print qq|content-type: application/pdf\n|;
385 print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
387 while (my $line = <$gs>) {
392 while (my $line = <$gs>) {
398 unlink $cfg_file_name, $html_file_name;
400 if ($printer_command && $content) {
401 foreach my $i (1 .. max $opt->{copies}, 1) {
402 my $printer = IO::File->new("| ${printer_command}");
404 $form->error($locale->text('Could not spawn the printer command.'));
406 $printer->print($content);
410 $form->{report_generator_printed} = 1;
414 unlink $cfg_file_name, $html_file_name;
415 $form->error($locale->text('Could not spawn html2ps or GhostScript.'));
419 sub generate_csv_content {
422 my %valid_sep_chars = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
423 my %valid_escape_chars = ('"' => 1, "'" => 1);
424 my %valid_quote_chars = ('"' => 1, "'" => 1);
426 my $opts = $self->{options}->{csv_export};
427 my $eol = $opts->{eol_style} eq 'DOS' ? "\r\n" : "\n";
428 my $sep_char = $valid_sep_chars{$opts->{sep_char}} ? $valid_sep_chars{$opts->{sep_char}} : ';';
429 my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char} : '"';
430 my $quote_char = $valid_quote_chars{$opts->{quote_char}} ? $opts->{quote_char} : '"';
432 $escape_char = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
434 my $csv = Text::CSV_XS->new({ 'binary' => 1,
435 'sep_char' => $sep_char,
436 'escape_char' => $escape_char,
437 'quote_char' => $quote_char,
440 my $stdout = wraphandle(\*STDOUT);
441 my @visible_columns = $self->get_visible_columns('CSV');
443 if ($opts->{headers}) {
444 $csv->print($stdout, [ map { $self->{columns}->{$_}->{text} } @visible_columns ]);
447 foreach my $row_set (@{ $self->{data} }) {
448 next if ('ARRAY' ne ref $row_set);
449 foreach my $row (@{ $row_set }) {
450 $csv->print($stdout, [ map { $row->{$_}->{data} } @visible_columns ]);