8b2a201c077a7c3a0bbc21de9ad80d181d870639
[kivitendo-erp.git] / SL / Menu.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 # SQL-Ledger Accounting
9 # Copyright (C) 2001
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
17 # This program is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU General Public License as published by
19 # the Free Software Foundation; either version 2 of the License, or
20 # (at your option) any later version.
21 #
22 # This program is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #=====================================================================
30 #
31 # routines for menu items
32 #
33 #=====================================================================
34
35 package Menu;
36
37 use SL::Auth;
38 use SL::Inifile;
39
40 use strict;
41
42 sub new {
43   $main::lxdebug->enter_sub();
44
45   my ($type, $menufile) = @_;
46
47   my $self    = {};
48   my $inifile = Inifile->new($menufile);
49
50   map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
51
52   bless $self, $type;
53
54   $self->set_access();
55
56   $main::lxdebug->leave_sub();
57
58   return $self;
59 }
60
61 sub menuitem {
62   $main::lxdebug->enter_sub();
63
64   my ($self, $myconfig, $form, $item) = @_;
65
66   my $module = $self->{$item}{module} || $form->{script};
67   my $action = $self->{$item}{action} || "section_menu";
68   my $target = $self->{$item}{target} || "";
69   my $level  = $form->escape($item);
70   my $target_token = ($target) ? "target='$target'" : '';
71
72   my $href = ($self->{$item}{href})
73            ? $form->escape($self->{$item}{href})
74            : "$module?action=$action";
75
76   # add other params
77   foreach my $key (keys %{ $self->{$item} }) {
78     next if $key =~ /target|module|action/;
79     $href .= "&" . $form->escape($key, 1) . "=";
80     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
81     $value = $myconfig->{$value} . "/$conf" if ($conf);
82     $href .= $form->escape($value, 1);
83   }
84
85   my $str = "<a href='$href' $target_token>";
86
87   $main::lxdebug->leave_sub();
88
89   return $str;
90 }
91
92 sub menuitem_js {
93   my ($self, $myconfig, $form, $item) = @_;
94
95   my $module = $form->{script};
96   my $action = "section_menu";
97
98   #if ($self->{$item}{module}) {
99   $module = $self->{$item}{module};
100
101   #}
102   if ($self->{$item}{action}) {
103     $action = $self->{$item}{action};
104   }
105
106   my $level = $form->escape($item);
107   my $str   = qq|$module?action=$action&level=$level|;
108   my @vars  = qw(module action target href);
109
110   if ($self->{$item}{href}) {
111     $str  = qq|$self->{$item}{href}|;
112     @vars = qw(module target href);
113   }
114
115   map { delete $self->{$item}{$_} } @vars;
116
117   # add other params
118   foreach my $key (keys %{ $self->{$item} }) {
119     $str .= "&" . $form->escape($key, 1) . "=";
120     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
121     $value = $myconfig->{$value} . "/$conf" if ($conf);
122     $str .= $form->escape($value, 1);
123   }
124
125   $str .= " ";
126
127 }
128
129 sub menuitem_new {
130   $main::lxdebug->enter_sub();
131
132   my ($self, $name, $item) = @_;
133
134   my $form        =  $main::form;
135   my $myconfig    = \%main::myconfig;
136
137   my $module      = $self->{$name}->{module} || $form->{script};
138   my $action      = $self->{$name}->{action};
139
140   $item->{target} = $self->{$name}->{target} || "main_window";
141   $item->{href}   = $self->{$name}->{href}   || "${module}?action=" . $form->escape($action);
142
143   my @vars = qw(module target href);
144   push @vars, 'action' unless ($self->{$name}->{href});
145
146   map { delete $self->{$name}{$_} } @vars;
147
148   # add other params
149   foreach my $key (keys %{ $self->{$name} }) {
150     my ($value, $conf)  = split(m/=/, $self->{$name}->{$key}, 2);
151     $value              = $myconfig->{$value} . "/$conf" if ($conf);
152     $item->{href}      .= "&" . $form->escape($key) . "=" . $form->escape($value);
153   }
154
155   $main::lxdebug->leave_sub();
156 }
157
158 sub menuitem_v3 {
159   $main::lxdebug->enter_sub();
160
161   my ($self, $myconfig, $form, $item, $other) = @_;
162
163   my $module = $form->{script};
164   my $action = "section_menu";
165   my $target = "";
166
167   if ($self->{$item}{module}) {
168     $module = $self->{$item}{module};
169   }
170   if ($self->{$item}{action}) {
171     $action = $self->{$item}{action};
172   }
173   if ($self->{$item}{target}) {
174     $target = $self->{$item}{target};
175   }
176
177   my $level = $form->escape($item);
178
179   my $str = qq|<a href="$module?action=| . $form->escape($action) . qq|&level=| . $form->escape($level);
180
181   my @vars = qw(module action target href);
182
183   if ($self->{$item}{href}) {
184     $str  = qq|<a href=$self->{$item}{href}|;
185     @vars = qw(module target href);
186   }
187
188   map { delete $self->{$item}{$_} } @vars;
189
190   # add other params
191   foreach my $key (keys %{ $self->{$item} }) {
192     $str .= "&" . $form->escape($key, 1) . "=";
193     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
194     $value = $myconfig->{$value} . "/$conf" if ($conf);
195     $str .= $form->escape($value, 1);
196   }
197
198   $str .= '"';
199
200   if ($target) {
201     $str .= qq| target="| . $form->quote($target) . qq|"|;
202   }
203
204   if ($other) {
205     foreach my $key (keys(%{$other})) {
206       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
207     }
208   }
209
210   $str .= ">";
211
212   $main::lxdebug->leave_sub();
213
214   return $str;
215 }
216
217 sub menuitem_XML {
218   $main::lxdebug->enter_sub();
219
220   my ($self, $myconfig, $form, $item, $other) = @_;
221
222   my $module = $form->{script};
223   my $action = "section_menu";
224   my $target = "";
225
226   if ($self->{$item}{module}) {
227     $module = $self->{$item}{module};
228   }
229   if ($self->{$item}{action}) {
230     $action = $self->{$item}{action};
231   }
232   if ($self->{$item}{target}) {
233     $target = $self->{$item}{target};
234   }
235
236   my $level = $form->escape($item);
237
238   my $str = qq| link="$module?action=| . $form->escape($action) .
239     qq|&amp;level=| . $form->escape($level);
240
241   my @vars = qw(module action target href);
242
243   if ($self->{$item}{href}) {
244     $str  = qq| link=$self->{$item}{href}|;
245     @vars = qw(module target href);
246   }
247
248   map { delete $self->{$item}{$_} } @vars;
249
250   # add other params
251   foreach my $key (keys %{ $self->{$item} }) {
252     $str .= "&amp;" . $form->escape($key, 1) . "=";
253     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
254     $value = $myconfig->{$value} . "/$conf" if ($conf);
255     $str .= $form->escape($value, 1);
256   }
257
258   $str .= '"';
259
260
261
262   if ($other) {
263     foreach my $key (keys(%{$other})) {
264       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
265     }
266   }
267
268
269   $main::lxdebug->leave_sub();
270
271   return $str;
272 }
273
274 sub access_control {
275   $main::lxdebug->enter_sub(2);
276
277   my ($self, $myconfig, $menulevel) = @_;
278
279   my @menu = ();
280
281   if ($menulevel eq "") {
282     @menu = grep { !/--/ } @{ $self->{ORDER} };
283   } else {
284     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
285   }
286
287   $main::lxdebug->leave_sub(2);
288
289   return @menu;
290 }
291
292 sub parse_access_string {
293   my $self   = shift;
294   my $key    = shift;
295   my $access = shift;
296
297   my $form        =  $main::form;
298   my $auth        =  $main::auth;
299   my $myconfig    = \%main::myconfig;
300
301   my @stack;
302   my $cur_ary = [];
303
304   push @stack, $cur_ary;
305
306   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
307     my $token = $1;
308     substr($access, 0, length($1)) = "";
309
310     next if ($token =~ /\s/);
311
312     if ($token eq "(") {
313       my $new_cur_ary = [];
314       push @stack, $new_cur_ary;
315       push @{$cur_ary}, $new_cur_ary;
316       $cur_ary = $new_cur_ary;
317
318     } elsif ($token eq ")") {
319       pop @stack;
320       if (!@stack) {
321         $form->error("Error in menu.ini for entry ${key}: missing '('");
322       }
323       $cur_ary = $stack[-1];
324
325     } elsif (($token eq "|") || ($token eq "&")) {
326       push @{$cur_ary}, $token;
327
328     } else {
329       push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
330     }
331   }
332
333   if ($access) {
334     $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
335   }
336
337   if (1 < scalar @stack) {
338     $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
339   }
340
341   return SL::Auth::evaluate_rights_ary($stack[0]);
342 }
343
344 sub set_access {
345   my $self = shift;
346
347   my $key;
348
349   foreach $key (@{ $self->{ORDER} }) {
350     my $entry = $self->{$key};
351
352     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
353     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
354     $entry->{NUM_VISIBLE_CHILDREN} = 0;
355
356     if ($key =~ m/--/) {
357       my $parent = $key;
358       substr($parent, rindex($parent, '--')) = '';
359       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
360     }
361
362     $entry->{VISIBLE} = $entry->{GRANTED};
363   }
364
365   foreach $key (reverse @{ $self->{ORDER} }) {
366     my $entry = $self->{$key};
367
368     if ($entry->{IS_MENU}) {
369       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
370     }
371
372     next if (($key !~ m/--/) || !$entry->{VISIBLE});
373
374     my $parent = $key;
375     substr($parent, rindex($parent, '--')) = '';
376     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
377   }
378
379 #   $self->dump_visible();
380
381   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
382
383   { no strict 'refs';
384   # ToDO: fix this. nuke and pave algorithm without type checking screams for problems.
385   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
386   }
387 }
388
389 sub dump_visible {
390   my $self = shift;
391   foreach my $key (@{ $self->{ORDER} }) {
392     my $entry = $self->{$key};
393     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
394   }
395 }
396
397 1;
398