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