SL/Template.pm in eine Datei pro Package aufgeteilt.
[kivitendo-erp.git] / SL / Template / Excel.pm
1 package SL::Template::Excel;
2
3 use SL::Template::Simple;
4
5 use vars qw(@ISA);
6
7 @ISA = qw(SL::Template::Simple);
8
9 sub new {
10   my $type = shift;
11
12   my $self = $type->SUPER::new(@_);
13
14   return $self;
15 }
16
17 sub _init {
18   my $self = shift;
19
20   $self->{source}    = shift;
21   $self->{form}      = shift;
22   $self->{myconfig}  = shift;
23   $self->{userspath} = shift;
24
25   $self->{error}     = undef;
26
27   $self->set_tag_style('<<', '>>');
28 }
29
30 sub get_mime_type() {
31   my ($self) = @_;
32
33   return "application/msexcel";
34 }
35
36 sub uses_temp_file {
37   return 1;
38 }
39
40 sub parse {
41   $main::lxdebug->enter_sub();
42
43   my $self   = shift;
44   local *OUT = shift;
45   my $form   = $self->{"form"};
46
47   open(IN, "$form->{templates}/$form->{IN}") or do { $self->{"error"} = "$!"; return 0; };
48   my @lines = <IN>;
49   close IN;
50
51   my $contents = join("", @lines);
52   my @indices;
53   $contents =~ s{
54     $self->{tag_start} [<]* (\s?) [<>\s]* ([\w\s]+) [<>\s]* $self->{tag_end}
55   }{
56     $self->format_vars(align_right => $1 ne '', varstring => $2, length => length($&), indices =>  \@indices)
57   }egx;
58
59   if (!defined($contents)) {
60     $main::lxdebug->leave_sub();
61     return 0;
62   }
63
64   print OUT $contents;
65
66   $main::lxdebug->leave_sub();
67   return 1;
68 }
69
70 sub format_vars {
71   my ($self, %params) = @_;
72   my $form            = $self->{"form"};
73   my @indices         = @{ $params{indices} };
74   my $align_right     = $params{align_right};
75   my $varstring       = $params{varstring};
76   my $length          = $params{length};
77
78   $varstring =~ s/(\w+)/ $self->_get_loop_variable($1, 0, @indices) /eg;
79   my $old_string=$varstring;
80   my $new_string = sprintf "%*s", ($align_right ? 1 : -1 ) * $length, $varstring;
81   if (!defined($new_string) || $new_string eq ''){
82     $main::lxdebug->message(0, 'varstring' . $varstring . "old" . $old_string);
83     #  return substr $varstring, ($align_right ? (0, $length) : -$length);
84   }
85   return substr $new_string, ($align_right ? (0, $length) : -$length);
86 }
87
88 1;