advance_payment_transfer_charts: Ergänzung Abhängigkeit und chart.link setzen
[kivitendo-erp.git] / modules / override / PDF / Table.pm
index 8f5d031..602c887 100755 (executable)
@@ -134,8 +134,8 @@ sub text_block
     # Check if any text to display
     unless( defined( $text) and length($text) > 0 )
     {
-        carp "Warning: No input text found. Trying to add dummy '-' and not to break everything.\n";
-        $text = '-';
+        carp "Warning: No input text found. Trying to add dummy '-' and not to break everything.\n";
+        $text = ' ';
     }
 
     # Strip any <CR> and Split the text into paragraphs
@@ -211,8 +211,12 @@ sub text_block
         }
 
         # Lets take from paragraph as many words as we can put into $width - $indent;
-        while ( @paragraph and $text_object->advancewidth( join("\x20", @line)."\x20" . $paragraph[0]) +
-                                $line_width < $width )
+        # Always take at least one word; otherwise we'd end up in an infinite loop.
+        while ( !scalar(@line) || (
+          @paragraph && (
+            $text_object->advancewidth( join("\x20", @line)."\x20" . $paragraph[0]) + $line_width < $width
+          )
+        ))
         {
             push(@line, shift(@paragraph));
         }
@@ -328,6 +332,7 @@ sub table
         max_word_length       => 1,
         cell_render_hook      => 1,
         default_text          => 1,
+        num_header_rows       => 1,
     );
     foreach my $key (keys %arg)
     {
@@ -373,7 +378,7 @@ sub table
     #=====================================
     # Disable header row into the table
     my $header_props = undef;
-
+    my (@header_rows, @header_row_cell_props);
     # Check if the user enabled it ?
     if(defined $arg{'header_props'} and ref( $arg{'header_props'}) eq 'HASH')
     {
@@ -388,9 +393,8 @@ sub table
         $header_props->{'font_underline'} = $header_props->{'font_underline'} || $fnt_underline;
         $header_props->{'bg_color'      } = $header_props->{'bg_color'      } || '#FFFFAA';
         $header_props->{'justify'       } = $header_props->{'justify'       };
+        $header_props->{num_header_rows } = $arg{num_header_rows } || 1;
     }
-
-    my $header_row  = undef;
     #=====================================
     # Other Parameters check
     #=====================================
@@ -433,7 +437,10 @@ sub table
     }
 
     # 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..$header_props->{num_header_rows} - 1);
+      map { push @header_row_cell_props, $$cell_props[$_] } (0..$header_props->{num_header_rows} - 1);
+    }
     # Determine column widths based on content
 
     #  an arrayref whose values are a hashref holding
@@ -444,7 +451,7 @@ sub table
     #  the actual widths of the column/row intersection
     my $row_col_widths = [];
     # An array ref with the widths of the header row
-    my $header_row_props = [];
+    my @header_row_widths;
 
     # Scalars that hold sum of the maximum and minimum widths of all columns
     my ( $max_col_w  , $min_col_w   ) = ( 0,0 );
@@ -456,6 +463,8 @@ sub table
 
     for( my $row_idx = 0; $row_idx < scalar(@$data) ; $row_idx++ )
     {
+        #push @header_row_widths, [] if $row_idx < $header_props->{num_header_rows};
+
         my $column_widths = []; #holds the width of each column
         # Init the height for this row
         $rows_height->[$row_idx] = 0;
@@ -496,8 +505,12 @@ sub table
                 $rows_height->[$row_idx] = $cell_font_size;
             }
 
+            if (!defined $data->[$row_idx][$column_idx]) {
+              $data->[$row_idx][$column_idx] = ' ';
+            }
+
             # This should fix a bug with very long words like serial numbers etc.
-            if( $max_word_len > 0 )
+            if( $max_word_len > 0 && $data->[$row_idx][$column_idx])
             {
                 $data->[$row_idx][$column_idx] =~ s#(\S{$max_word_len})(?=\S)#$1 #g;
             }
@@ -544,7 +557,9 @@ sub table
         $row_col_widths->[$row_idx] = $column_widths;
 
         # Copy the calculated row properties of header row.
-        @$header_row_props = @$column_widths if(!$row_idx and ref $header_props);
+        if (ref $header_props && $row_idx < $header_props->{num_header_rows}) {
+          push @header_row_widths, [ @{ $column_widths } ];
+        }
     }
 
     # Calc real column widths and expand table width if needed.
@@ -555,10 +570,12 @@ sub table
     # Lets draw what we have!
     my $row_index    = 0;
     # Store header row height for later use if headers have to be repeated
-    my $header_row_height = $rows_height->[0];
+    my @header_row_heights = @$rows_height[0 .. $header_props->{num_header_rows}-1];
 
     my ( $gfx, $gfx_bg, $background_color, $font_color, $bot_marg, $table_top_y, $text_start);
 
+    my $remaining_header_rows = $header_props ? $header_props->{num_header_rows} : 0;
+
     # Each iteration adds a new page as neccessary
     while(scalar(@{$data}))
     {
@@ -601,17 +618,11 @@ sub table
 
             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_col_widths, $hrp;
-                unshift @$rows_height, $header_row_height;
-
-                $first_row = 1; # Means YES
-                $row_index--; # Rollback the row_index because a new header row has been added
+                unshift @$data,           @header_rows;
+                unshift @$row_col_widths, @header_row_widths;
+                unshift @$rows_height,    @header_row_heights;
+                $remaining_header_rows = $header_props->{num_header_rows};
+                $first_row = 1;
             }
         }
 
@@ -662,10 +673,15 @@ sub table
 
             # Row cell props - TODO in another commit
 
+            # Added to resolve infite loop bug with returned undef values
+            for(my $d = 0; $d < scalar(@{$record}) ; $d++)
+            {
+                $record->[$d] = ' ' unless( defined $record->[$d]);
+            }
 
             # Choose colors for this row
-            $background_color = $row_index % 2 ? $background_color_even  : $background_color_odd;
-            $font_color       = $row_index % 2 ? $font_color_even        : $font_color_odd;
+            $background_color = ($row_index - $header_props->{num_header_rows}) % 2 ? $background_color_even  : $background_color_odd;
+            $font_color       = ($row_index - $header_props->{num_header_rows}) % 2 ? $font_color_even        : $font_color_odd;
 
             #Determine current row height
             my $current_row_height = $pad_top + $pre_calculated_row_height + $pad_bot;
@@ -695,7 +711,7 @@ sub table
                 # look for font information for this cell
                 my ($cell_font, $cell_font_size, $cell_font_color, $cell_font_underline, $justify);
 
-                if( $first_row and ref $header_props)
+                if( $remaining_header_rows and ref $header_props)
                 {
                     $cell_font           = $header_props->{'font'};
                     $cell_font_size      = $header_props->{'font_size'};
@@ -737,13 +753,16 @@ sub table
                                        //  $default_text;
 
                 my $this_width;
-                if (!$first_row && $cell_props->[$row_index][$column_idx]->{colspan}) {
-                    $colspan     = -1 == $cell_props->[$row_index][$column_idx]->{colspan}
-                                 ? $num_cols - $column_idx
-                                 : $cell_props->[$row_index][$column_idx]->{colspan};
+                if (!$remaining_header_rows && $cell_props->[$row_index + $header_props->{num_header_rows}][$column_idx]->{colspan}) {
+                    $colspan = $cell_props->[$row_index + $header_props->{num_header_rows}][$column_idx]->{colspan};
+                } elsif ($remaining_header_rows && ($header_row_cell_props[$header_props->{num_header_rows} - $remaining_header_rows][$column_idx]->{colspan})) {
+                    $colspan = $header_row_cell_props[$header_props->{num_header_rows} - $remaining_header_rows][$column_idx]->{colspan};
+                }
+
+                if ($colspan) {
+                    $colspan     = $num_cols - $column_idx if (-1 == $colspan);
                     my $last_idx = $column_idx + $colspan - 1;
                     $this_width  = sum @{ $calc_column_widths }[$column_idx..$last_idx];
-
                 } else {
                     $this_width = $calc_column_widths->[$column_idx];
                 }
@@ -828,13 +847,13 @@ sub table
             {
                 my $cell_bg_color;
 
-                if( $first_row and ref $header_props)
+                if( $remaining_header_rows and ref $header_props)
                 {                                  #Compatibility                 Consistency with other props
                     $cell_bg_color = $header_props->{'bg_color'} || $header_props->{'background_color'};
                 }
 
                 # Get the most specific value if none was already set from header_props
-                $cell_bg_color ||= $cell_props->[$row_index][$column_idx]->{'background_color'}
+                $cell_bg_color ||= $cell_props->[$row_index + $header_props->{num_header_rows}][$column_idx]->{'background_color'}
                                ||  $col_props->[$column_idx]->{'background_color'}
                                ||  $background_color;
 
@@ -848,7 +867,7 @@ sub table
 
                 if ($line_w && $vertical_lines[$column_idx] && ($column_idx != (scalar(@{ $record }) - 1))) {
                     $gfx->move($cur_x, $cur_y);
-                    $gfx->vline($cur_y - $row_h);
+                    $gfx->vline($cur_y - $current_row_height);
                     $gfx->fillcolor($border_color);
                 }
             }#End of for(my $column_idx....
@@ -860,8 +879,12 @@ sub table
                 $gfx->hline( $xbase + $width );
             }
 
-            $row_index++ unless ( $do_leftovers );
             $first_row = 0;
+            if ($remaining_header_rows) {
+              $remaining_header_rows--;
+            } else {
+              $row_index++ unless $do_leftovers;
+            }
         }# End of Row_Loop
 
         if ($gfx)
@@ -923,32 +946,11 @@ sub CalcColumnWidths
         $calc_widths->[$j] = $col_props->[$j]->{min_w} || 0;;
     }
 
-    # Allow columns to expand to max_w before applying extra space equally.
-    my $is_last_iter;
-    for (;;)
-    {
-        my $span = ($avail_width - $min_width) / scalar( @$col_props);
-        last if $span <= 0;
-
-        $min_width = 0;
-        my $next_will_be_last_iter = 1;
-        for(my $j = 0; $j < scalar(@$col_props); $j++ )
-        {
-            my $new_w = $calc_widths->[$j] + $span;
-
-            if (!$is_last_iter && $new_w > $col_props->[$j]->{max_w})
-            {
-                $new_w = $col_props->[$j]->{max_w}
-            }
-            if ($calc_widths->[$j] != $new_w )
-            {
-                $calc_widths->[$j] = $new_w;
-                $next_will_be_last_iter = 0;
-            }
-            $min_width += $new_w;
-        }
-        last if $is_last_iter;
-        $is_last_iter = $next_will_be_last_iter;
+    my $span = 0;
+    # Calculate how much can be added to every column to fit the available width
+    $span = ($avail_width - $min_width) / scalar( @$col_props);
+    for (my $j = 0; $j < scalar(@$col_props); $j++ ) {
+      $calc_widths->[$j] = $col_props->[$j]->{min_w} + $span;
     }
 
     return ($calc_widths,$avail_width);