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