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};
164 my $form = $self->{form};
166 if (!$self->{columns}) {
167 $form->error('Incorrect usage -- no columns specified');
170 my $filename = $self->{options}->{attachment_basename} || 'report';
171 $filename =~ s|.*\\||;
172 $filename =~ s|.*/||;
174 if ($format eq 'html') {
175 my $title = $form->{title};
176 $form->{title} = $self->{title} if ($self->{title});
178 $form->{title} = $title;
180 print $self->generate_html_content();
182 } elsif ($format eq 'csv') {
183 print qq|content-type: text/csv\n|;
184 print qq|content-disposition: attachment; filename=${filename}.csv\n\n|;
185 $self->generate_csv_content();
187 } elsif ($format eq 'pdf') {
188 $self->generate_pdf_content();
191 $form->error('Incorrect usage -- unknown format (supported are HTML, CSV, PDF)');
195 sub get_visible_columns {
199 return grep { my $c = $self->{columns}->{$_}; $c && $c->{visible} && (($c->{visible} == 1) || ($c->{visible} =~ /${format}/i)) } @{ $self->{column_order} };
206 $value = $self->{form}->quote_html($value);
208 $value =~ s/\n/<br>/g;
213 sub prepare_html_content {
216 my ($column, $name, @column_headers);
218 my $opts = $self->{options};
219 my @visible_columns = $self->get_visible_columns('HTML');
221 foreach $name (@visible_columns) {
222 $column = $self->{columns}->{$name};
226 'link' => $column->{link},
227 'text' => $column->{text},
228 'show_sort_indicator' => $name eq $opts->{sort_indicator_column},
229 'sort_indicator_direction' => $opts->{sort_indicator_direction},
232 push @column_headers, $header;
235 my ($outer_idx, $inner_idx) = (0, 0);
238 foreach my $row_set (@{ $self->{data} }) {
239 if ('HASH' eq ref $row_set) {
242 'IS_SEPARATOR' => $row_set->{type} eq 'separator',
243 'NUM_COLUMNS' => scalar @visible_columns,
246 push @rows, $row_data;
253 foreach my $row (@{ $row_set }) {
256 map { $row->{$_}->{data} = $self->html_format($row->{$_}->{data}) } @visible_columns;
259 'COLUMNS' => [ map { $row->{$_} } @visible_columns ],
260 'outer_idx' => $outer_idx,
261 'outer_idx_odd' => $outer_idx % 2,
262 'inner_idx' => $inner_idx,
265 push @rows, $row_data;
269 my @export_variables;
270 foreach my $key (split m/ +/, $self->{export}->{variable_list}) {
271 push @export_variables, { 'key' => $key, 'value' => $self->{form}->{$key} };
274 my $allow_pdf_export = $opts->{allow_pdf_export} && (-x $main::html2ps_bin) && (-x $main::ghostscript_bin);
277 'TITLE' => $opts->{title},
278 'TOP_INFO_TEXT' => $self->html_format($opts->{top_info_text}),
279 'RAW_TOP_INFO_TEXT' => $opts->{raw_top_info_text},
280 'BOTTOM_INFO_TEXT' => $self->html_format($opts->{bottom_info_text}),
281 'RAW_BOTTOM_INFO_TEXT' => $opts->{raw_bottom_info_text},
282 'ALLOW_PDF_EXPORT' => $allow_pdf_export,
283 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export},
284 'SHOW_EXPORT_BUTTONS' => $allow_pdf_export || $opts->{allow_csv_export},
285 'COLUMN_HEADERS' => \@column_headers,
286 'NUM_COLUMNS' => scalar @column_headers,
288 'EXPORT_VARIABLES' => \@export_variables,
289 'EXPORT_VARIABLE_LIST' => $self->{export}->{variable_list},
290 'EXPORT_NEXTSUB' => $self->{export}->{nextsub},
296 sub generate_html_content {
298 my $variables = $self->prepare_html_content();
300 return $self->{form}->parse_html_template('report_generator/html_report', $variables);
303 sub verify_paper_size {
305 my $requested_paper_size = lc shift;
306 my $default_paper_size = shift;
308 my %allowed_paper_sizes = map { $_ => 1 } qw(a3 a4 letter legal);
310 return $allowed_paper_sizes{$requested_paper_size} ? $requested_paper_size : $default_paper_size;
313 sub generate_pdf_content {
315 my $variables = $self->prepare_html_content();
316 my $form = $self->{form};
317 my $myconfig = $self->{myconfig};
318 my $opt = $self->{options}->{pdf_export};
320 my $opt_number = $opt->{number} ? 'number : 1' : '';
321 my $opt_landscape = $opt->{orientation} eq 'landscape' ? 'landscape : 1' : '';
323 my $opt_paper_size = $self->verify_paper_size($opt->{paper_size}, 'a4');
325 my $html2ps_config = <<"END"
335 type: ${opt_paper_size};
341 margin-top: $opt->{margin_top}cm;
342 margin-left: $opt->{margin_left}cm;
343 margin-bottom: $opt->{margin_bottom}cm;
344 margin-right: $opt->{margin_right}cm;
348 font-family: Helvetica;
349 font-size: $opt->{font_size}pt;
356 if ($opt->{print} && $opt->{printer_id}) {
357 $form->{printer_id} = $opt->{printer_id};
358 $form->get_printer_code($myconfig);
359 $printer_command = $form->{printer_command};
362 my $cfg_file_name = Common::tmpname() . '-html2ps-config';
363 my $cfg_file = IO::File->new($cfg_file_name, 'w') || $form->error($locale->text('Could not write the html2ps config file.'));
365 $cfg_file->print($html2ps_config);
368 my $html_file_name = Common::tmpname() . '.html';
369 my $html_file = IO::File->new($html_file_name, 'w');
372 unlink $cfg_file_name;
373 $form->error($locale->text('Could not write the temporary HTML file.'));
376 $html_file->print($form->parse_html_template('report_generator/pdf_report', $variables));
380 "\"${main::html2ps_bin}\" -f \"${cfg_file_name}\" \"${html_file_name}\" | " .
381 "\"${main::ghostscript_bin}\" -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sPAPERSIZE=${opt_paper_size} -sOutputFile=- -c .setpdfwrite -";
383 my $gs = IO::File->new("${cmdline} |");
387 if (!$printer_command) {
388 print qq|content-type: application/pdf\n|;
389 print qq|content-disposition: attachment; filename=${filename}.pdf\n\n|;
391 while (my $line = <$gs>) {
396 while (my $line = <$gs>) {
402 unlink $cfg_file_name, $html_file_name;
404 if ($printer_command && $content) {
405 foreach my $i (1 .. max $opt->{copies}, 1) {
406 my $printer = IO::File->new("| ${printer_command}");
408 $form->error($locale->text('Could not spawn the printer command.'));
410 $printer->print($content);
414 $form->{report_generator_printed} = 1;
418 unlink $cfg_file_name, $html_file_name;
419 $form->error($locale->text('Could not spawn html2ps or GhostScript.'));
423 sub generate_csv_content {
426 my %valid_sep_chars = (';' => ';', ',' => ',', ':' => ':', 'TAB' => "\t");
427 my %valid_escape_chars = ('"' => 1, "'" => 1);
428 my %valid_quote_chars = ('"' => 1, "'" => 1);
430 my $opts = $self->{options}->{csv_export};
431 my $eol = $opts->{eol_style} eq 'DOS' ? "\r\n" : "\n";
432 my $sep_char = $valid_sep_chars{$opts->{sep_char}} ? $valid_sep_chars{$opts->{sep_char}} : ';';
433 my $escape_char = $valid_escape_chars{$opts->{escape_char}} ? $opts->{escape_char} : '"';
434 my $quote_char = $valid_quote_chars{$opts->{quote_char}} ? $opts->{quote_char} : '"';
436 $escape_char = $quote_char if ($opts->{escape_char} eq 'QUOTE_CHAR');
438 my $csv = Text::CSV_XS->new({ 'binary' => 1,
439 'sep_char' => $sep_char,
440 'escape_char' => $escape_char,
441 'quote_char' => $quote_char,
444 my $stdout = wraphandle(\*STDOUT);
445 my @visible_columns = $self->get_visible_columns('CSV');
447 if ($opts->{headers}) {
448 $csv->print($stdout, [ map { $self->{columns}->{$_}->{text} } @visible_columns ]);
451 foreach my $row_set (@{ $self->{data} }) {
452 next if ('ARRAY' ne ref $row_set);
453 foreach my $row (@{ $row_set }) {
454 $csv->print($stdout, [ map { $row->{$_}->{data} } @visible_columns ]);