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