From 84ba8214f6f1e68de5fa317b8239a7888a4aac03 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 23 Apr 2008 14:58:30 +0000 Subject: [PATCH] =?utf8?q?ReportGenerator:=20Unterst=C3=BCtzung=20f=C3=BCr?= =?utf8?q?=20mehrzeilige=20und=20individuell=20festgelegte=20Tabellenk?= =?utf8?q?=C3=B6pfe/Spalten=C3=BCberschriften=20implementiert.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/ReportGenerator.pm | 70 +++++++++++++++---- modules/override/PDF/Table.pm | 59 +++++++++------- .../report_generator/html_report_de.html | 4 +- .../report_generator/html_report_master.html | 4 +- 4 files changed, 99 insertions(+), 38 deletions(-) diff --git a/SL/ReportGenerator.pm b/SL/ReportGenerator.pm index 647dcab76..cd9caea1c 100644 --- a/SL/ReportGenerator.pm +++ b/SL/ReportGenerator.pm @@ -189,6 +189,16 @@ sub set_export_options { }; } +sub set_custom_headers { + my $self = shift; + + if (@_) { + $self->{custom_headers} = [ @_ ]; + } else { + delete $self->{custom_headers}; + } +} + sub get_attachment_basename { my $self = shift; my $filename = $self->{options}->{attachment_basename} || 'report'; @@ -269,6 +279,13 @@ sub prepare_html_content { push @column_headers, $header; } + my $header_rows; + if ($self->{custom_headers}) { + $header_rows = $self->{custom_headers}; + } else { + $header_rows = [ \@column_headers ]; + } + my ($outer_idx, $inner_idx) = (0, 0); my $next_border_top; my @rows; @@ -354,7 +371,7 @@ sub prepare_html_content { 'ALLOW_PDF_EXPORT' => $allow_pdf_export, 'ALLOW_CSV_EXPORT' => $opts->{allow_csv_export}, 'SHOW_EXPORT_BUTTONS' => ($allow_pdf_export || $opts->{allow_csv_export}) && $self->{data_present}, - 'COLUMN_HEADERS' => \@column_headers, + 'HEADER_ROWS' => $header_rows, 'NUM_COLUMNS' => scalar @column_headers, 'ROWS' => \@rows, 'EXPORT_VARIABLES' => \@export_variables, @@ -390,22 +407,43 @@ sub render_pdf_with_pdf_api2 { my (@data, @column_props, @cell_props); - my $data_row = []; - my $cell_props_row = []; + my ($data_row, $cell_props_row); my @visible_columns = $self->get_visible_columns('HTML'); + my $num_columns = scalar @visible_columns; + my $num_header_rows = 1; foreach $name (@visible_columns) { - $column = $self->{columns}->{$name}; - - push @{ $data_row }, $column->{text}; - push @{ $cell_props_row }, {}; - push @column_props, { 'justify' => $column->{align} eq 'right' ? 'right' : 'left' }; + push @column_props, { 'justify' => $self->{columns}->{$name}->{align} eq 'right' ? 'right' : 'left' }; } - push @data, $data_row; - push @cell_props, $cell_props_row; + if (!$self->{custom_headers}) { + $data_row = []; + $cell_props_row = []; + push @data, $data_row; + push @cell_props, $cell_props_row; + + foreach $name (@visible_columns) { + $column = $self->{columns}->{$name}; + + push @{ $data_row }, $column->{text}; + push @{ $cell_props_row }, {}; + } - my $num_columns = scalar @column_props; + } else { + $num_header_rows = scalar @{ $self->{custom_headers} }; + + foreach my $custom_header_row (@{ $self->{custom_headers} }) { + $data_row = []; + $cell_props_row = []; + push @data, $data_row; + push @cell_props, $cell_props_row; + + foreach my $custom_header_col (@{ $custom_header_row }) { + push @{ $data_row }, $custom_header_col->{text}; + push @{ $cell_props_row }, {}; + } + } + } foreach my $row_set (@{ $self->{data} }) { if ('HASH' eq ref $row_set) { @@ -534,6 +572,7 @@ sub render_pdf_with_pdf_api2 { 'font' => $font, 'font_size' => $font_size, 'font_color' => '#000000', + 'num_header_rows' => $num_header_rows, 'header_props' => { 'bg_color' => '#ffffff', 'repeat' => 1, @@ -656,7 +695,14 @@ sub generate_csv_content { my @visible_columns = $self->get_visible_columns('CSV'); if ($opts->{headers}) { - $csv->print($stdout, [ map { $self->unescape_string($self->{columns}->{$_}->{text}) } @visible_columns ]); + if (!$self->{custom_headers}) { + $csv->print($stdout, [ map { $self->unescape_string($self->{columns}->{$_}->{text}) } @visible_columns ]); + + } else { + foreach my $custom_header_row (@{ $self->{custom_headers} }) { + $csv->print($stdout, [ map { $self->unescape_string($_->{text}) } @{ $custom_header_row } ]); + } + } } foreach my $row_set (@{ $self->{data} }) { diff --git a/modules/override/PDF/Table.pm b/modules/override/PDF/Table.pm index 5efbff1d8..6101b9a01 100644 --- a/modules/override/PDF/Table.pm +++ b/modules/override/PDF/Table.pm @@ -238,7 +238,9 @@ sub table { # Table Header Section #===================================== # Disable header row into the table - my $header_props = 0; + my $header_props; + my $num_header_rows = 0; + my @header_rows; # Check if the user enabled it ? if (defined $arg{'header_props'} and ref( $arg{'header_props'}) eq 'HASH') { # Transfer the reference to local variable @@ -249,8 +251,9 @@ sub table { $header_props->{'font_color'} = $header_props->{'font_color'} || '#000066'; $header_props->{'font_size'} = $header_props->{'font_size'} || $fnt_size + 2; $header_props->{'bg_color'} = $header_props->{'bg_color'} || '#FFFFAA'; + + $num_header_rows = $arg{'num_header_rows'} || 1; } - my $header_row = undef; #===================================== # Other Parameters check #===================================== @@ -276,12 +279,14 @@ sub table { my $pg_cnt = 1; my $cur_y = $ybase; my $cell_props = $arg{cell_props} || []; # per cell properties - my $row_cnt = ( ref $header_props and $header_props->{'repeat'} ) ? 1 : 0; # current row in user data + my $row_cnt = $num_header_rows; #If there is valid data array reference use it! if (ref $data eq 'ARRAY') { # Copy the header row if header is enabled - @$header_row = $$data[0] if defined $header_props; + if (defined $header_props) { + map { push @header_rows, $$data[$_] } (0..$num_header_rows - 1); + } # Determine column widths based on content # an arrayref whose values are a hashref holding @@ -292,7 +297,7 @@ sub table { # the actual widths of the column/row intersection my $row_props = []; # An array ref with the widths of the header row - my $header_row_props = []; + my @header_row_props; # Scalars that hold sum of the maximum and minimum widths of all columns my ( $max_col_w, $min_col_w ) = ( 0,0 ); @@ -301,9 +306,10 @@ sub table { # Hash that will hold the width of every word from input text my $word_w = {}; my $rows_counter = 0; - my $first_row = 1; foreach $row ( @{$data} ) { + push(@header_row_props, []) if ($rows_counter < $num_header_rows); + my $column_widths = []; #holds the width of each column for( my $j = 0; $j < scalar(@$row) ; $j++ ) { # look for font information for this column @@ -360,9 +366,12 @@ sub table { }#End of for(my $j.... $row_props->[$rows_counter] = $column_widths; # Copy the calculated row properties of header row. - @$header_row_props = @$column_widths if (!$rows_counter and ref $header_props); + if (($rows_counter < $num_header_rows) && $header_props) { + push(@header_row_props, [ @{ $column_widths } ]); + } $rows_counter++; } + $main::lxdebug->dump(0, "hrp", \@header_row_props); # Calc real column widths and expand table width if needed. my $calc_column_widths; ($calc_column_widths, $width) = $self->CalcColumnWidths( $col_props, $width ); @@ -373,6 +382,8 @@ sub table { my ( $gfx , $gfx_bg , $background_color , $font_color, ); my ( $bot_marg, $table_top_y, $text_start , $record, $record_widths ); + my $remaining_header_rows = $header_props ? $num_header_rows : 0; + # Each iteration adds a new page as neccessary while(scalar(@{$data})) { my $page_header; @@ -392,14 +403,11 @@ sub table { $bot_marg = $table_top_y - $next_h; if ( ref $header_props and $header_props->{'repeat'}) { - # Copy Header Data - @$page_header = @$header_row; - my $hrp ; - @$hrp = @$header_row_props ; - # Then prepend it to master data array - unshift @$data ,@$page_header ; - unshift @$row_props ,$hrp ; - $first_row = 1; # Means YES + foreach my $idx (0 .. $num_header_rows - 1) { + unshift @$data, [ @{ $header_rows[$idx] } ]; + unshift @$row_props, [ @{ $header_row_props[$idx] } ]; + } + $remaining_header_rows = $num_header_rows; } } @@ -438,7 +446,7 @@ sub table { $background_color = $rows_counter % 2 ? $background_color_even : $background_color_odd; $font_color = $rows_counter % 2 ? $font_color_even : $font_color_odd; - if ($first_row and ref $header_props) { + if ($remaining_header_rows and ref $header_props) { $background_color = $header_props->{'bg_color'} } $text_start = $cur_y - $fnt_size - $pad_top; @@ -455,7 +463,7 @@ sub table { $leftovers->[$j] = undef; # Choose font color - if ( $first_row and ref $header_props ) { + if ( $remaining_header_rows and ref $header_props ) { $txt->fillcolor( $header_props->{'font_color'} ); } elsif ( $cell_props->[$row_cnt][$j]{font_color} ) { @@ -469,7 +477,7 @@ sub table { } # Choose font size - if ( $first_row and ref $header_props ) { + if ( $remaining_header_rows and ref $header_props ) { $col_fnt_size = $header_props->{'font_size'}; } elsif ( $col_props->[$j]->{'font_size'} ) { @@ -480,7 +488,7 @@ sub table { } # Choose font family - if ( $first_row and ref $header_props ) { + if ( $remaining_header_rows and ref $header_props ) { $txt->font( $header_props->{'font'}, $header_props->{'font_size'}); } elsif ( $col_props->[$j]->{'font'} ) { @@ -493,7 +501,7 @@ sub table { $col_props->[$j]->{justify} = $col_props->[$j]->{justify} || 'left'; my $this_width; - if (!$first_row && $cell_props->[$row_cnt]->[$j]->{colspan}) { + if (!$remaining_header_rows && $cell_props->[$row_cnt]->[$j]->{colspan}) { $colspan = -1 == $cell_props->[$row_cnt]->[$j]->{colspan} ? $num_cols - $j : $cell_props->[$row_cnt]->[$j]->{colspan}; my $last_idx = $j + $colspan - 1; $this_width = sum @{ $calc_column_widths }[$j..$last_idx]; @@ -550,10 +558,10 @@ sub table { $col_props->[$j]->{'background_color'} || $background_color ) { $gfx_bg->rect( $cur_x, $cur_y-$row_h, $calc_column_widths->[$j], $row_h); - if ( $cell_props->[$row_cnt][$j]->{'background_color'} && !$first_row ) { + if ( $cell_props->[$row_cnt][$j]->{'background_color'} && !$remaining_header_rows ) { $gfx_bg->fillcolor($cell_props->[$row_cnt][$j]->{'background_color'}); - } elsif ( $col_props->[$j]->{'background_color'} && !$first_row ) { + } elsif ( $col_props->[$j]->{'background_color'} && !$remaining_header_rows ) { $gfx_bg->fillcolor($col_props->[$j]->{'background_color'}); } else { @@ -576,8 +584,11 @@ sub table { $gfx->move( $xbase , $cur_y ); $gfx->hline( $xbase + $width ); $rows_counter++; - $row_cnt++ unless ( $first_row ); - $first_row = 0; + if ($remaining_header_rows) { + $remaining_header_rows--; + } else { + $row_cnt++; + } }# End of while(scalar(@{$data}) and $cur_y-$row_h > $bot_marg) # Draw vertical lines diff --git a/templates/webpages/report_generator/html_report_de.html b/templates/webpages/report_generator/html_report_de.html index 9b51544cf..79d8ddca3 100644 --- a/templates/webpages/report_generator/html_report_de.html +++ b/templates/webpages/report_generator/html_report_de.html @@ -38,11 +38,13 @@ [% IF DATA_PRESENT %]

+ [%- FOREACH row = HEADER_ROWS %] - [% FOREACH col = COLUMN_HEADERS %] + [% FOREACH col = row %] [% END %] + [%- END %] [% FOREACH row = ROWS %] [% IF row.IS_CONTROL %] diff --git a/templates/webpages/report_generator/html_report_master.html b/templates/webpages/report_generator/html_report_master.html index 3c049d541..e59d5df18 100644 --- a/templates/webpages/report_generator/html_report_master.html +++ b/templates/webpages/report_generator/html_report_master.html @@ -38,11 +38,13 @@ [% IF DATA_PRESENT %]

[% IF col.link %][% END %][% col.text %][% IF col.link %][% IF col.show_sort_indicator %][% END %][% ELSE %][% IF col.show_sort_indicator %][% END %][% END %]
+ [%- FOREACH row = HEADER_ROWS %] - [% FOREACH col = COLUMN_HEADERS %] + [% FOREACH col = row %] [% END %] + [%- END %] [% FOREACH row = ROWS %] [% IF row.IS_CONTROL %] -- 2.20.1
[% IF col.link %][% END %][% col.text %][% IF col.link %][% IF col.show_sort_indicator %][% END %][% ELSE %][% IF col.show_sort_indicator %][% END %][% END %]