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