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