Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[kivitendo-erp.git] / SL / Template / Simple.pm
1 #====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #====================================================================
8
9 package SL::Template::Simple;
10
11 use strict;
12
13 use Scalar::Util qw(blessed);
14
15 # Parameters:
16 #   1. The template's file name
17 #   2. A reference to the Form object
18 #   3. A reference to the myconfig hash
19 #
20 # Returns:
21 #   A new template object
22 sub new {
23   my $type = shift;
24   my $self = {};
25
26   bless($self, $type);
27   $self->_init(@_);
28
29   return $self;
30 }
31
32 sub _init {
33   my $self = shift;
34
35   $self->{source}    = shift;
36   $self->{form}      = shift;
37   $self->{myconfig}  = shift;
38   $self->{userspath} = shift;
39
40   $self->{error}     = undef;
41   $self->{quot_re}   = '"';
42
43   $self->set_tag_style('<%', '%>');
44 }
45
46 sub set_tag_style {
47   my $self                    = shift;
48   my $tag_start               = shift;
49   my $tag_end                 = shift;
50
51   $self->{tag_start}          = $tag_start;
52   $self->{tag_end}            = $tag_end;
53   $self->{tag_start_qm}       = quotemeta $tag_start;
54   $self->{tag_end_qm}         = quotemeta $tag_end;
55
56   $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
57 }
58
59 sub cleanup {
60   my ($self) = @_;
61 }
62
63 # Parameters:
64 #   1. A typeglob for the file handle. The output will be written
65 #      to this file handle.
66 #
67 # Returns:
68 #   1 on success and undef or 0 if there was an error. In the latter case
69 #   the calling function can retrieve the error message via $obj->get_error()
70 sub parse {
71   my $self = $_[0];
72   local *OUT = $_[1];
73
74   print(OUT "Hallo!\n");
75 }
76
77 sub get_error {
78   my $self = shift;
79
80   return $self->{"error"};
81 }
82
83 sub uses_temp_file {
84   return 0;
85 }
86
87 sub _get_loop_variable {
88   my ($self, $var, $get_array, @indices) = @_;
89   my $form      = $self->{form};
90   my ($value, @methods);
91
92   if ($var =~ m/\./) {
93     ($var, @methods) = split m/\./, $var;
94   }
95
96   if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
97     $value = $form->{TEMPLATE_ARRAYS}->{$var};
98   } else {
99     $value = $form->{$var};
100   }
101
102   for (my $i = 0; $i < scalar(@indices); $i++) {
103     last unless (ref($value) eq "ARRAY");
104     $value = $value->[$indices[$i]];
105   }
106
107   for my $part (@methods) {
108     if (ref($value) =~ m/^(?:Form|HASH)$/) {
109       $value = $value->{$part};
110     } elsif (blessed($value) && $value->can($part)) {
111       $value = $value->$part;
112     } else {
113       $value = '';
114       last;
115     }
116   }
117
118   return $value;
119 }
120
121 sub substitute_vars {
122   my ($self, $text, @indices) = @_;
123
124   my $form = $self->{"form"};
125
126   while ($text =~ /$self->{substitute_vars_re}/) {
127     my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
128     my ($var, @options)     = split(/\s+/, $1);
129
130     my $value               = $self->_get_loop_variable($var, 0, @indices);
131     $value                  = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
132
133     substr($text, $tag_pos, $tag_len, $value);
134   }
135
136   return $text;
137 }
138
139 sub _parse_block_if {
140   $main::lxdebug->enter_sub();
141
142   my $self         = shift;
143   my $contents     = shift;
144   my $new_contents = shift;
145   my $pos_if       = shift;
146   my @indices      = @_;
147
148   $$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices);
149   substr($$contents, 0, $pos_if) = "";
150
151   if ($$contents !~ m/^$self->{tag_start_qm}if
152                      \s*
153                      (not\b|\!)?           # $1 -- Eventuelle Negierung
154                      \s+
155                      (\b.+?\b)             # $2 -- Name der zu überprüfenden Variablen
156                      (                     # $3 -- Beginn des optionalen Vergleiches
157                        \s*
158                        ([!=])              # $4 -- Negierung des Vergleiches speichern
159                        ([=~])              # $5 -- Art des Vergleiches speichern
160                        \s*
161                        (                   # $6 -- Gequoteter String oder Bareword
162                          $self->{quot_re}
163                          (.*?)(?<!\\)      # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
164                          $self->{quot_re}
165                        |
166                          (\b.+?\b)         # $8 -- Bareword -- als Index für $form benutzen
167                        )
168                      )?
169                      \s*
170                      $self->{tag_end_qm}
171                     /x) {
172     $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
173     $main::lxdebug->leave_sub();
174     return undef;
175   }
176
177   my $not           = $1;
178   my $var           = $2;
179   my $comparison    = $3; # Optionaler Match um $4..$8
180   my $operator_neg  = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
181   my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
182   my $quoted_word   = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
183   my $bareword      = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
184
185   $not = !$not if ($operator_neg && $operator_neg eq '!');
186
187   substr($$contents, 0, length($&)) = "";
188
189   my $block;
190   ($block, $$contents) = $self->find_end($$contents, 0, "$var $comparison", $not);
191   if (!$block) {
192     $self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"});
193     $main::lxdebug->leave_sub();
194     return undef;
195   }
196
197   my $value = $self->_get_loop_variable($var, 0, @indices);
198   my $hit   = 0;
199
200   if ($operator_type) {
201     my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
202     if ($operator_type eq '=') {
203       $hit         = ($not && !($value eq $compare_to))     || (!$not && ($value eq $compare_to));
204     } else {
205       $hit         = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
206     }
207
208   } else {
209     $hit           = ($not && ! $value)                     || (!$not &&  $value);
210   }
211
212   if ($hit) {
213     my $new_text = $self->parse_block($block, @indices);
214     if (!defined($new_text)) {
215       $main::lxdebug->leave_sub();
216       return undef;
217     }
218     $$new_contents .= $new_text;
219   }
220
221   $main::lxdebug->leave_sub();
222
223   return 1;
224 }
225
226 1;