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