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 get_attachment_basename {
163 my $filename = $self->{options}->{attachment_basename} || 'report';
164 $filename =~ s|.*\\||;
165 $filename =~ s|.*/||;
170 sub generate_with_headers {
172 my $format = lc $self->{options}->{output_format};
173 my $form = $self->{form};
175 if (!$self->{columns}) {
176 $form->error('Incorrect usage -- no columns specified');
179 if ($format eq 'html') {
180 my $title = $form->{title};
181 $form->{title} = $self->{title} if ($self->{title});
183 $form->{title} = $title;
185 print $self->generate_html_content();
187 } elsif ($format eq 'csv') {
188 my $filename = $self->get_attachment_basename();
189 print qq|content-type: text/csv\n|;
190 print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
191 $self->generate_csv_content();
193 } elsif ($format eq 'pdf') {
194 $self->generate_pdf_content();
197 $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
201 sub get_visible_columns {
205 return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /${format}/i)) } @{ $self->{column_order} };
212 $value = $self->{form}->quote_html($value);
214 $value =~ s/\n/<br>/g;
219 sub prepare_html_content {
222 my ($column, $name, @column_headers);
224 my $opts = $self->{options};
225 my @visible_columns = $self->get_visible_columns('HTML');
227 foreach $name (@visible_columns) {
228 $column = $self->{columns}->{$name};
232 'link' => $column->{link},
233 'text' => $column->{text},
234 'show_sort_indicator' => $name eq $opts->{sort_indicator_column},
235 'sort_indicator_direction' => $opts->{sort_indicator_direction},
238 push @column_headers, $header;
241 my ($outer_idx, $inner_idx) = (0, 0);
244 foreach my $row_set (@{ $self->{data} }) {
245 if ('HASH' eq ref $row_set) {
248 'IS_SEPARATOR' => $row_set->{type} eq 'separator',
249 'NUM_COLUMNS' => scalar @visible_columns,
252 push @rows, $row_data;
259 foreach my $row (@{ $row_set }) {
262 map { $row->{$_}->{data} = $self->html_format($row->{$_}->{data}) } @visible_columns;
265 'COLUMNS' => [ map { $row->{$_} } @visible_columns ],
266 'outer_idx' => $outer_idx,
267 'outer_idx_odd' => $outer_idx % 2,
268 'inner_idx' => $inner_idx,
271 push @rows, $row_data;
275 my @export_variables;
276 foreach my $key (split m/ +/, $self->{export}->{variable_list}) {
277 push @export_variables, { 'key' => $key, 'value' => $self->{form}->{$key} };
280 my $allow_pdf_export = $opts->{allow_pdf_export} && (-x $main::html2ps_bin) && (-x $main::ghostscript_bin);
283 'TITLE' => $opts->{title},
284 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
285 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
286 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
287 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
288 'ALLOW_PDF_EXPORT' => $allow_pdf_export,
289 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export},
290 'SHOW_EXPORT_BUTTONS' => $allow_pdf_export || $opts->{allow_csv_export},
291 'COLUMN_HEADERS' => \@column_headers,
292 'NUM_COLUMNS' => scalar @column_headers,
294 'EXPORT_VARIABLES' => \@export_variables,
295 'EXPORT_VARIABLE_LIST' => $self->{export}->{variable_list},
296 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
302 sub generate_html_content {
304 my $variables = $self->prepare_html_content();
306 return $self->{form}->parse_html_template('report_generator/html_report', $variables);
309 sub verify_paper_size {
311 my $requested_paper_size = lc shift;
312 my $default_paper_size = shift;
314 my %allowed_paper_sizes = map { $_ => 1 } qw(a3 a4 letter legal);
316 return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
319 sub generate_pdf_content {
321 my $variables = $self->prepare_html_content();
322 my $form = $self->{form};
323 my $myconfig = $self->{myconfig};
324 my $opt = $self->{options}->{pdf_export};
326 my $opt_number = $opt->{number} ? 'number : 1' : '';
327 my $opt_landscape = $opt->{orientation} eq 'landscape' ? 'landscape : 1' : '';
329 my $opt_paper_size = $self->verify_paper_size($opt->{paper_size}, 'a4');
331 my $html2ps_config = <<"END"
341 type: ${opt_paper_size};
347 margin-top: $opt->{margin_top}cm;
348 margin-left: $opt->{margin_left}cm;
349 margin-bottom: $opt->{margin_bottom}cm;
350 margin-right: $opt->{margin_right}cm;
354 font-family: Helvetica;
355 font-size: $opt->{font_size}pt;
362 if ($opt->{print} && $opt->{printer_id}) {
363 $form->{printer_id} = $opt->{printer_id};
364 $form->get_printer_code($myconfig);
365 $printer_command = $form->{printer_command};
368 my $cfg_file_name = Common::tmpname() . '-html2ps-config';
369 my $cfg_file = IO::File->new($cfg_file_name, 'w') || $form->error($locale->text('Could not write the html2ps config file.'));
371 $cfg_file->print($html2ps_config);
374 my $html_file_name = Common::tmpname() . '.html';
375 my $html_file = IO::File->new($html_file_name, 'w');
378 unlink $cfg_file_name;
379 $form->error($locale->text('Could not write the temporary HTML file.'));
382 $html_file->print($form->parse_html_template('report_generator/pdf_report', $variables));
386 "\"${main::html2ps_bin}\" -f \"${cfg_file_name}\" \"${html_file_name}\" | " .
387 "\"${main::ghostscript_bin}\" -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=${opt_paper_size} -sOutputFile=- -c .setpdfwrite -";
389 my $gs = IO::File->new("${cmdline} |");
393 if (!$printer_command) {
394 my $filename = $self->get_attachment_basename();
395 print qq|content-type: application/pdf\n|;
396 print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
398 while (my $line = <$gs>) {
403 while (my $line = <$gs>) {
409 unlink $cfg_file_name, $html_file_name;
411 if ($printer_command && $content) {
412 foreach my $i (1 .. max $opt->{copies}, 1) {
413 my $printer = IO::File->new("| ${printer_command}");
415 $form->error($locale->text('Could not spawn the printer command.'));
417 $printer->print($content);
421 $form->{report_generator_printed} = 1;
425 unlink $cfg_file_name, $html_file_name;
426 $form->error($locale->text('Could not spawn html2ps or GhostScript.'));
430 sub generate_csv_content {
433 my %valid_sep_chars = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
434 my %valid_escape_chars = ('"' => 1, "'" => 1);
435 my %valid_quote_chars = ('"' => 1, "'" => 1);
437 my $opts = $self->{options}->{csv_export};
438 my $eol = $opts->{eol_style} eq 'DOS' ? "\r\n" : "\n";
439 my $sep_char = $valid_sep_chars{$opts->{sep_char}} ? $valid_sep_chars{$opts->{sep_char}} : ';';
440 my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char} : '"';
441 my $quote_char = $valid_quote_chars{$opts->{quote_char}} ? $opts->{quote_char} : '"';
443 $escape_char = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
445 my $csv = Text::CSV_XS->new({ 'binary' => 1,
446 'sep_char' => $sep_char,
447 'escape_char' => $escape_char,
448 'quote_char' => $quote_char,
451 my $stdout = wraphandle(\*STDOUT);
452 my @visible_columns = $self->get_visible_columns('CSV');
454 if ($opts->{headers}) {
455 $csv->print($stdout, [ map { $self->{columns}->{$_}->{text} } @visible_columns ]);
458 foreach my $row_set (@{ $self->{data} }) {
459 next if ('ARRAY' ne ref $row_set);
460 foreach my $row (@{ $row_set }) {
461 $csv->print($stdout, [ map { $row->{$_}->{data} } @visible_columns ]);