Beim Versenden per Email eine anständige Überschrift anzeigen und nicht "email oe".
[kivitendo-erp.git] / SL / Form.pm
index 464405c..d1dbfb6 100644 (file)
@@ -40,6 +40,7 @@ use Data::Dumper;
 
 use Cwd;
 use HTML::Template;
+use Template;
 use SL::Template;
 use CGI::Ajax;
 use SL::DBUtils;
@@ -180,7 +181,7 @@ sub new {
   $self->{action} = lc $self->{action};
   $self->{action} =~ s/( |-|,|\#)/_/g;
 
-  $self->{version}   = "2.4.2";
+  $self->{version}   = "2.4.3";
 
   $main::lxdebug->leave_sub();
 
@@ -474,7 +475,7 @@ sub header {
   $main::lxdebug->leave_sub();
 }
 
-sub parse_html_template {
+sub _prepare_html_template {
   $main::lxdebug->enter_sub();
 
   my ($self, $file, $additional_params) = @_;
@@ -508,14 +509,6 @@ sub parse_html_template {
     die($info);
   }
 
-  my $template = HTML::Template->new("filename" => $file,
-                                     "die_on_bad_params" => 0,
-                                     "strict" => 0,
-                                     "case_sensitive" => 1,
-                                     "loop_context_vars" => 1,
-                                     "global_vars" => 1);
-
-  $additional_params = {} unless ($additional_params);
   if ($self->{"DEBUG"}) {
     $additional_params->{"DEBUG"} = $self->{"DEBUG"};
   }
@@ -539,10 +532,33 @@ sub parse_html_template {
   $additional_params->{"conf_latex_templates"}        = $main::latex;
   $additional_params->{"conf_opendocument_templates"} = $main::opendocument_templates;
 
-  my @additional_param_names = keys(%{$additional_params});
+  if (%main::debug_options) {
+    map { $additional_params->{'DEBUG_' . uc($_)} = $main::debug_options{$_} } keys %main::debug_options;
+  }
+
+  $main::lxdebug->leave_sub();
+
+  return $file;
+}
+
+sub parse_html_template {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $file, $additional_params) = @_;
+
+  $additional_params ||= { };
+
+  $file = $self->_prepare_html_template($file, $additional_params);
+
+  my $template = HTML::Template->new("filename" => $file,
+                                     "die_on_bad_params" => 0,
+                                     "strict" => 0,
+                                     "case_sensitive" => 1,
+                                     "loop_context_vars" => 1,
+                                     "global_vars" => 1);
+
   foreach my $key ($template->param()) {
-    my $param = $self->{$key};
-    $param = $additional_params->{$key} if (grep(/^${key}$/, @additional_param_names));
+    my $param = $additional_params->{$key} || $self->{$key};
     $param = [] if (($template->query("name" => $key) eq "LOOP") && (ref($param) ne "ARRAY"));
     $template->param($key => $param);
   }
@@ -556,6 +572,33 @@ sub parse_html_template {
   return $output;
 }
 
+sub parse_html_template2 {
+  $main::lxdebug->enter_sub();
+
+  my ($self, $file, $additional_params) = @_;
+
+  $additional_params ||= { };
+
+  $file = $self->_prepare_html_template($file, $additional_params);
+
+  my $template = Template->new({ 'INTERPOLATE' => 0,
+                                 'EVAL_PERL'   => 0,
+                                 'ABSOLUTE'    => 1,
+                                 'CACHE_SIZE'  => 0,
+                               }) || die;
+
+  map { $additional_params->{$_} ||= $self->{$_} } keys %{ $self };
+
+  my $output;
+  $template->process($file, $additional_params, \$output);
+
+  $output = $main::locale->{iconv}->convert($output) if ($main::locale);
+
+  $main::lxdebug->leave_sub();
+
+  return $output;
+}
+
 sub show_generic_error {
   my ($self, $error, $title, $action) = @_;
 
@@ -647,7 +690,9 @@ sub redirect {
 
   if ($self->{callback}) {
 
-    ($script, $argv) = split(/\?/, $self->{callback});
+    ($script, $argv) = split(/\?/, $self->{callback}, 2);
+    $script =~ s|.*/||;
+    $script =~ s|[^a-zA-Z_\.]||g;
     exec("perl", "$script", $argv);
 
   } else {
@@ -937,8 +982,10 @@ Content-Length: $numbytes
   $main::lxdebug->leave_sub();
 }
 
-sub generate_attachment_filename {
-  my ($self) = @_;
+sub get_formname_translation {
+  my ($self, $formname) = @_;
+
+  $formname ||= $self->{formname};
 
   my %formname_translations = (
      bin_list            => $main::locale->text('Bin List'),
@@ -955,7 +1002,13 @@ sub generate_attachment_filename {
      storno_packing_list => $main::locale->text('Storno Packing List'),
   );
 
-  my $attachment_filename = $formname_translations{$self->{"formname"}};
+  return $formname_translations{$formname}
+}
+
+sub generate_attachment_filename {
+  my ($self) = @_;
+
+  my $attachment_filename = $self->get_formname_translation();
   my $prefix = 
       (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
     : ($self->{"type"} =~ /_quotation$/)                       ? "quo"
@@ -1426,6 +1479,7 @@ sub add_shipto {
 
   my $shipto;
   my @values;
+
   foreach my $item (qw(name department_1 department_2 street zipcode city country
                        contact phone fax email)) {
     if ($self->{"shipto$item"}) {
@@ -1433,6 +1487,7 @@ sub add_shipto {
     }
     push(@values, $self->{"shipto${item}"});
   }
+
   if ($shipto) {
     if ($self->{shipto_id}) {
       my $query = qq|UPDATE shipto set
@@ -1461,8 +1516,10 @@ sub add_shipto {
                        shiptocontact = ? AND
                        shiptophone = ? AND
                        shiptofax = ? AND
-                       shiptoemail = ?|;
-      my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values);
+                       shiptoemail = ? AND
+                       module = ? AND 
+                       trans_id = ?|;
+      my $insert_check = selectfirst_hashref_query($self, $dbh, $query, @values, $module, $id);
       if(!$insert_check){
         $query =
           qq|INSERT INTO shipto (trans_id, shiptoname, shiptodepartment_1, shiptodepartment_2,
@@ -2605,8 +2662,14 @@ sub update_defaults {
   my ($var) = $sth->fetchrow_array;
   $sth->finish;
 
-  $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
-  $var ||= 1;
+  if ($var =~ m/\d+$/) {
+    my $new_var  = (substr $var, $-[0]) * 1 + 1;
+    my $len_diff = length($var) - $-[0] - length($new_var);
+    $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
+
+  } else {
+    $var = $var . '1';
+  }
 
   $query = qq|UPDATE defaults SET $fld = ?|;
   do_query($self, $dbh, $query, $var);
@@ -2637,8 +2700,15 @@ sub update_business {
        WHERE id = ? FOR UPDATE|;
   my ($var) = selectrow_query($self, $dbh, $query, $business_id);
 
-  $var =~ s/\d+$/ sprintf '%0*d', length($&), $&+1 /e;
-  
+  if ($var =~ m/\d+$/) {
+    my $new_var  = (substr $var, $-[0]) * 1 + 1;
+    my $len_diff = length($var) - $-[0] - length($new_var);
+    $var         = substr($var, 0, $-[0]) . ($len_diff > 0 ? '0' x $len_diff : '') . $new_var;
+
+  } else {
+    $var = $var . '1';
+  }
+
   $query = qq|UPDATE business
               SET customernumberinit = ?
               WHERE id = ?|;