Erweiterung SL::Iconv um convert-Methode, die auch UTF8-Flag setzt & Objekt-Interface
[kivitendo-erp.git] / SL / Template.pm
index b112b2c..f767967 100644 (file)
@@ -275,22 +275,25 @@ sub parse_foreach {
 
   $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
 
+  # forech block hasn't given us an array. ignore
+  return $new_contents unless ref $ary eq 'ARRAY';
+
   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
+    # do magic markers
     $form->{"__first__"}   = $i == 1;
     $form->{"__last__"}    = ($i + 1) == scalar(@{$ary});
     $form->{"__odd__"}     = (($i + 1) % 2) == 1;
     $form->{"__counter__"} = $i + 1;
 
-    if (scalar @{$description_array} == scalar @{$ary} && $self->{"chars_per_line"} != 0) {
+    if (   ref $description_array       eq 'ARRAY'
+        && scalar @{$description_array} == scalar @{$ary}
+        && $self->{"chars_per_line"}    != 0)
+    {
       my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
       my $lpp;
 
       $description_array->[$i] =~ s/(\\newline\s?)*$//;
-      my $_description = $description_array->[$i];
-      while ($_description =~ /\\newline/) {
-        $lines++;
-        $_description =~ s/\\newline//;
-      }
+      $lines++ while ($description_array->[$i] =~ m/\\newline/g);
       $lines++;
 
       if ($current_page == 1) {
@@ -300,7 +303,10 @@ sub parse_foreach {
       }
 
       # Yes we need a manual page break -- or the user has forced one
-      if ((($current_line + $lines) > $lpp) || ($description_array->[$i] =~ /<pagebreak>/) || ($longdescription_array->[$i] =~ /<pagebreak>/)) {
+      if (   (($current_line + $lines) > $lpp)
+          || ($description_array->[$i]     =~ /<pagebreak>/)
+          || (   ref $longdescription_array eq 'ARRAY'
+              && $longdescription_array->[$i] =~ /<pagebreak>/)) {
         my $pb = $self->{"pagebreak_block"};
 
         # replace the special variables <%sumcarriedforward%>
@@ -320,7 +326,8 @@ sub parse_foreach {
       $current_line += $lines;
     }
 
-    if ($i < scalar(@{$linetotal_array})) {
+    if (   ref $linetotal_array eq 'ARRAY'
+        && $i < scalar(@{$linetotal_array})) {
       $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
     }
 
@@ -831,9 +838,12 @@ sub parse {
 
 package OpenDocumentTemplate;
 
+use Archive::Zip;
 use POSIX 'setsid';
 use vars qw(@ISA);
 
+use SL::Iconv;
+
 use Cwd;
 # use File::Copy;
 # use File::Spec;
@@ -849,19 +859,8 @@ sub new {
 
   my $self = $type->SUPER::new(@_);
 
-  foreach my $module (qw(Archive::Zip Text::Iconv)) {
-    eval("use ${module};");
-    if ($@) {
-      $self->{"form"}->error("The Perl module '${module}' could not be " .
-                             "loaded. Support for OpenDocument templates " .
-                             "does not work without it. Please install your " .
-                             "distribution's package or get the module from " .
-                             "CPAN ( http://www.cpan.org ).");
-    }
-  }
-
   $self->{"rnd"}   = int(rand(1000000));
-  $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
+  $self->{"iconv"} = SL::Iconv->new($main::dbcharset, "UTF-8");
 
   $self->set_tag_style('&lt;%', '%&gt;');
   $self->{quot_re} = '&quot;';
@@ -1470,4 +1469,95 @@ sub uses_temp_file {
   return 1;
 }
 
+
+##########################################################
+####
+#### ExcelTemplate
+####
+##########################################################
+
+package ExcelTemplate;
+
+use vars qw(@ISA);
+
+@ISA = qw(SimpleTemplate);
+
+sub new {
+  my $type = shift;
+
+  my $self = $type->SUPER::new(@_);
+
+  return $self;
+}
+sub _init {
+  my $self = shift;
+
+  $self->{source}    = shift;
+  $self->{form}      = shift;
+  $self->{myconfig}  = shift;
+  $self->{userspath} = shift;
+
+  $self->{error}     = undef;
+
+  $self->set_tag_style('<<', '>>');
+}
+
+sub get_mime_type() {
+  my ($self) = @_;
+
+  return "application/msexcel";
+}
+
+sub uses_temp_file {
+  return 1;
+}
+
+sub parse {
+  $main::lxdebug->enter_sub();
+
+  my $self   = shift;
+  local *OUT = shift;
+  my $form   = $self->{"form"};
+
+  open(IN, "$form->{templates}/$form->{IN}") or do { $self->{"error"} = "$!"; return 0; };
+  my @lines = <IN>;
+  close IN;
+
+  my $contents = join("", @lines);
+  my @indices;
+  $contents =~ s{
+    $self->{tag_start} [<]* (\s?) [<>\s]* ([\w\s]+) [<>\s]* $self->{tag_end}
+  }{
+    $self->format_vars(align_right => $1 ne '', varstring => $2, length => length($&), indices =>  \@indices)
+  }egx;
+
+  if (!defined($contents)) {
+    $main::lxdebug->leave_sub();
+    return 0;
+  }
+
+  print OUT $contents;
+
+  $main::lxdebug->leave_sub();
+  return 1;
+}
+
+sub format_vars {
+  my ($self, %params) = @_;
+  my $form            = $self->{"form"};
+  my @indices         = @{ $params{indices} };
+  my $align_right     = $params{align_right};
+  my $varstring       = $params{varstring};
+  my $length          = $params{length};
+
+  $varstring =~ s/(\w+)/ $self->_get_loop_variable($1, 0, @indices) /eg;
+  my $old_string=$varstring;
+  my $new_string = sprintf "%*s", ($align_right ? 1 : -1 ) * $length, $varstring;
+  if (!defined($new_string) || $new_string eq ''){
+    $main::lxdebug->message(0, 'varstring' . $varstring . "old" . $old_string); 
+    #  return substr $varstring, ($align_right ? (0, $length) : -$length);
+  }
+  return substr $new_string, ($align_right ? (0, $length) : -$length);
+}
+
 1;