debug enter/leave_sub balanciert
[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 my $instance;
43
44 sub new {
45   $main::lxdebug->enter_sub();
46
47   my ($type, $menufile) = @_;
48
49   if ($instance) {
50     $::lxdebug->leave_sub;
51     return $instance;
52   }
53
54   my $self    = {};
55   my $inifile = Inifile->new($menufile);
56
57   map { $self->{$_} = $inifile->{$_} } keys %{ $inifile };
58
59   bless $self, $type;
60
61   $self->set_access();
62
63   $main::lxdebug->leave_sub();
64
65   return $instance = $self;
66 }
67
68 sub menuitem_js {
69   my ($self, $myconfig, $form, $item) = @_;
70
71   my $module = $form->{script};
72   my $action = "section_menu";
73
74   #if ($self->{$item}{module}) {
75   $module = $self->{$item}{module};
76
77   #}
78   if ($self->{$item}{action}) {
79     $action = $self->{$item}{action};
80   }
81
82   my $level = $form->escape($item);
83   my $str   = qq|$module?action=$action&level=$level|;
84   my @vars  = qw(module action target href);
85
86   if ($self->{$item}{href}) {
87     $str  = qq|$self->{$item}{href}|;
88     @vars = qw(module target href);
89   }
90
91   map { delete $self->{$item}{$_} } @vars;
92
93   # add other params
94   foreach my $key (keys %{ $self->{$item} }) {
95     $str .= "&" . $form->escape($key, 1) . "=";
96     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
97     $value = $myconfig->{$value} . "/$conf" if ($conf);
98     $str .= $form->escape($value, 1);
99   }
100
101   $str .= " ";
102
103 }
104
105 sub menuitem_new {
106   $main::lxdebug->enter_sub();
107
108   my ($self, $name, $item) = @_;
109
110   my $form        =  $main::form;
111   my $myconfig    = \%main::myconfig;
112
113   my $module      = $self->{$name}->{module} || $form->{script};
114   my $action      = $self->{$name}->{action};
115
116   $item->{target} = $self->{$name}->{target} || "main_window";
117   $item->{href}   = $self->{$name}->{href}   || "${module}?action=" . $form->escape($action);
118
119   my @vars = qw(module target href);
120   push @vars, 'action' unless ($self->{$name}->{href});
121
122   map { delete $self->{$name}{$_} } @vars;
123
124   # add other params
125   foreach my $key (keys %{ $self->{$name} }) {
126     my ($value, $conf)  = split(m/=/, $self->{$name}->{$key}, 2);
127     $value              = $myconfig->{$value} . "/$conf" if ($conf);
128     $item->{href}      .= "&" . $form->escape($key) . "=" . $form->escape($value);
129   }
130
131   $main::lxdebug->leave_sub();
132 }
133
134 sub menuitem_v3 {
135   $main::lxdebug->enter_sub();
136
137   my ($self, $myconfig, $form, $item, $other) = @_;
138
139   my $module = $form->{script};
140   my $action = "section_menu";
141   my $target = "";
142
143   if ($self->{$item}{module}) {
144     $module = $self->{$item}{module};
145   }
146   if ($self->{$item}{action}) {
147     $action = $self->{$item}{action};
148   }
149   if ($self->{$item}{target}) {
150     $target = $self->{$item}{target};
151   }
152
153   my $level = $form->escape($item);
154
155   my $str = qq|<a href="$module?action=| . $form->escape($action) . qq|&level=| . $form->escape($level);
156
157   my @vars = qw(module action target href);
158
159   if ($self->{$item}{href}) {
160     $str  = qq|<a href=$self->{$item}{href}|;
161     @vars = qw(module target href);
162   }
163
164   map { delete $self->{$item}{$_} } @vars;
165
166   # add other params
167   foreach my $key (keys %{ $self->{$item} }) {
168     $str .= "&" . $form->escape($key, 1) . "=";
169     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
170     $value = $myconfig->{$value} . "/$conf" if ($conf);
171     $str .= $form->escape($value, 1);
172   }
173
174   $str .= '"';
175
176   if ($target) {
177     $str .= qq| target="| . $form->quote($target) . qq|"|;
178   }
179
180   if ($other) {
181     foreach my $key (keys(%{$other})) {
182       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
183     }
184   }
185
186   $str .= ">";
187
188   $main::lxdebug->leave_sub();
189
190   return $str;
191 }
192
193 sub menuitem_XML {
194   $main::lxdebug->enter_sub();
195
196   my ($self, $myconfig, $form, $item, $other) = @_;
197
198   my $module = $form->{script};
199   my $action = "section_menu";
200   my $target = "";
201
202   if ($self->{$item}{module}) {
203     $module = $self->{$item}{module};
204   }
205   if ($self->{$item}{action}) {
206     $action = $self->{$item}{action};
207   }
208   if ($self->{$item}{target}) {
209     $target = $self->{$item}{target};
210   }
211
212   my $level = $form->escape($item);
213
214   my $str = qq| link="$module?action=| . $form->escape($action) .
215     qq|&amp;level=| . $form->escape($level);
216
217   my @vars = qw(module action target href);
218
219   if ($self->{$item}{href}) {
220     $str  = qq| link=$self->{$item}{href}|;
221     @vars = qw(module target href);
222   }
223
224   map { delete $self->{$item}{$_} } @vars;
225
226   # add other params
227   foreach my $key (keys %{ $self->{$item} }) {
228     $str .= "&amp;" . $form->escape($key, 1) . "=";
229     my ($value, $conf) = split(/=/, $self->{$item}{$key}, 2);
230     $value = $myconfig->{$value} . "/$conf" if ($conf);
231     $str .= $form->escape($value, 1);
232   }
233
234   $str .= '"';
235
236
237
238   if ($other) {
239     foreach my $key (keys(%{$other})) {
240       $str .= qq| ${key}="| . $form->quote($other->{$key}) . qq|"|;
241     }
242   }
243
244
245   $main::lxdebug->leave_sub();
246
247   return $str;
248 }
249
250 sub access_control {
251   $main::lxdebug->enter_sub(2);
252
253   my ($self, $myconfig, $menulevel) = @_;
254
255   my @menu = ();
256
257   if ($menulevel eq "") {
258     @menu = grep { !/--/ } @{ $self->{ORDER} };
259   } else {
260     @menu = grep { /^${menulevel}--/ } @{ $self->{ORDER} };
261   }
262
263   $main::lxdebug->leave_sub(2);
264
265   return @menu;
266 }
267
268 sub parse_access_string {
269   my $self   = shift;
270   my $key    = shift;
271   my $access = shift;
272
273   my $form        =  $main::form;
274   my $auth        =  $main::auth;
275   my $myconfig    = \%main::myconfig;
276
277   my @stack;
278   my $cur_ary = [];
279
280   push @stack, $cur_ary;
281
282   while ($access =~ m/^([a-z_]+|\||\&|\(|\)|\s+)/) {
283     my $token = $1;
284     substr($access, 0, length($1)) = "";
285
286     next if ($token =~ /\s/);
287
288     if ($token eq "(") {
289       my $new_cur_ary = [];
290       push @stack, $new_cur_ary;
291       push @{$cur_ary}, $new_cur_ary;
292       $cur_ary = $new_cur_ary;
293
294     } elsif ($token eq ")") {
295       pop @stack;
296       if (!@stack) {
297         $form->error("Error in menu.ini for entry ${key}: missing '('");
298       }
299       $cur_ary = $stack[-1];
300
301     } elsif (($token eq "|") || ($token eq "&")) {
302       push @{$cur_ary}, $token;
303
304     } else {
305       push @{$cur_ary}, $auth->check_right($form->{login}, $token, 1);
306     }
307   }
308
309   if ($access) {
310     $form->error("Error in menu.ini for entry ${key}: unrecognized token at the start of '$access'\n");
311   }
312
313   if (1 < scalar @stack) {
314     $main::form->error("Error in menu.ini for entry ${key}: Missing ')'\n");
315   }
316
317   return SL::Auth::evaluate_rights_ary($stack[0]);
318 }
319
320 sub set_access {
321   my $self = shift;
322
323   my $key;
324
325   foreach $key (@{ $self->{ORDER} }) {
326     my $entry = $self->{$key};
327
328     $entry->{GRANTED}              = $entry->{ACCESS} ? $self->parse_access_string($key, $entry->{ACCESS}) : 1;
329     $entry->{IS_MENU}              = $entry->{submenu} || ($key !~ m/--/);
330     $entry->{NUM_VISIBLE_CHILDREN} = 0;
331
332     if ($key =~ m/--/) {
333       my $parent = $key;
334       substr($parent, rindex($parent, '--')) = '';
335       $entry->{GRANTED} &&= $self->{$parent}->{GRANTED};
336     }
337
338     $entry->{VISIBLE} = $entry->{GRANTED};
339   }
340
341   foreach $key (reverse @{ $self->{ORDER} }) {
342     my $entry = $self->{$key};
343
344     if ($entry->{IS_MENU}) {
345       $entry->{VISIBLE} &&= $entry->{NUM_VISIBLE_CHILDREN} > 0;
346     }
347
348     next if (($key !~ m/--/) || !$entry->{VISIBLE});
349
350     my $parent = $key;
351     substr($parent, rindex($parent, '--')) = '';
352     $self->{$parent}->{NUM_VISIBLE_CHILDREN}++;
353   }
354
355 #   $self->dump_visible();
356
357   $self->{ORDER} = [ grep { $self->{$_}->{VISIBLE} } @{ $self->{ORDER} } ];
358
359   { no strict 'refs';
360   # ToDO: fix this. nuke and pave algorithm without type checking screams for problems.
361   map { delete @{$self->{$_}}{qw(GRANTED IS_MENU NUM_VISIBLE_CHILDREN VISIBLE ACCESS)} if ($_ ne 'ORDER') } keys %{ $self };
362   }
363 }
364
365 sub dump_visible {
366   my $self = shift;
367   foreach my $key (@{ $self->{ORDER} }) {
368     my $entry = $self->{$key};
369     $main::lxdebug->message(0, "$entry->{GRANTED} $entry->{VISIBLE} $entry->{NUM_VISIBLE_CHILDREN} $key");
370   }
371 }
372
373 1;
374